01-14-2016 04:39 PM
I can correctly convert to the HEX value of the SGL input.
However it doesn't always work when converting it back to a SGL.
I tried with 12.961126 and it works fine on both sides.
But 0.2556 doesn't work on the way back. It gives 0.
I confirmed that the convert to bytes works according to http://www.h-schmidt.net/FloatConverter/IEEE754.html
If I change the least significant byte from 01 to 10, it does give a return of 0.2556000. However, I prefer not to manipulate data because this is for transport.
A workaround would be to use a C application for the conversion...
Image below is the code. It is also attached as LabVIEW 2011.
The Typecast is SGL constant (not a double). The input is a U32, but that doesn't matter. Makes no difference as U8. I will never have more than U8 data per element for this project.
Thanks
Solved! Go to Solution.
01-14-2016 05:57 PM
Try this. YOu need to change the width to 2.
Chhers,
mcduff
01-14-2016 06:50 PM
I should have attached a snippet, sorry.
Cheers,
mcduff
01-14-2016 10:20 PM
01-14-2016 11:08 PM - edited 01-14-2016 11:11 PM
Your snippet works just fine for me.
PS: You don't need the SGL bullet in the upper picture. Just wire a constant to the type input of the conversion and set its representation to single float. Also the %s on the upper typecast is unnecessary as typecast by default sets things to strings.
01-14-2016 11:12 PM - edited 01-14-2016 11:46 PM
I agree with the others that you need to clean up your code, because it is just way too convoluted. Your "bytes Input" should probably be U8.
To demostrate the concept, you don't even use formatted strings, because (especially with SGL, you might lose significant information detouring over a decimally formatted intermediary representation of the binary values.)
Your use of a string containing %s for the upper typecast input is just silly. Since only the type is important and the value is irrelevant, you should probably wire an empty string (or leave it unwired, since string is the default). Typing a formatting statement in there does not change things, but might really confuse somebody looking at the code.
You can also avoide your "toSGL" if you wire a SGL constant to the default input.
All you probably need is a hexadecimally formatted string contro/indicator a SBL contr/indicator and a typecase operation. eliminate the middleman!
(Of course you can easiy go from SGL to string and back. Same difference)
01-15-2016 11:47 AM - edited 01-15-2016 12:15 PM
Thanks for the help. It works with the first solution (also shown as solution for future visitors). I also applied the other solutions in order to optimize the code. Lots of valuable information in this thread.
Added note:
The code shown is a snippet of 2 different VIs that I merged into 1 vi for ease of forum discussion.
The strings are data from serial communication of the RAM content of a controller. It has various formats and sizes from U8 to float. I had issues with the float. The rest is largely done with bitwise operations.
Thanks again.