
COnverting from bytes to a float

amankjain
I am trying to convert a portion of the response from a serial port that is 4 of the bytes into a single precision floating point.
I am reading the response into a string as
string returnStr
VDTRead2 returnStr
//Creating a wave of unsigned bytes as follows
Make/B/U/N=4 waveToRead
//Assigning wave from the response
waveToRead[0] = char2Num(returnStr[3])
waveToRead[1] = char2Num(returnStr[4])
waveToRead[2] = char2Num(returnStr[5])
waveToRead[3] = char2Num(returnStr[6])
//cast wave to single point float
Redimension/S waveToRead
Am I on the right path?
Thanks
AJ
If that doesn't work try reversing the order of the bytes.
However it seems that you should be using VDTReadBinary2 to read the 4 bytes directly into a variable. Since you reading 6 bytes and only the last four are of interest you can use VDTReadBinary2 to discard two bytes. Putting it altogether you would get something like this:
If your data is little-endian then you need to add /B to flip the byte order.
March 20, 2013 at 05:52 am - Permalink
March 20, 2013 at 06:12 am - Permalink