How to send float through serial port in c#? -
this question has answer here:
- how convert float binary in c#? 1 answer
i have 2 float now,say f1 , f2. want send them micro controller through computer serial port, how should it? understanding need convert f1 , f2 binary first,then send micro, after cnvert float again. confused how convert float binary before send them out? code
b1 = system.bitconverter.getbytes(f1); b2 = system.bitconverter.getbytes(f2);
it depends on endianness of microcontroller's architecture. speaking need this:
- convert float 4-byte array. you're on right track, although c# rusty.
- send them 1 one microcontroller
if microcontroller supports floating point representation cast four-byte array float:
byte array[4]; // pre-allocate array getbytesfromserial(array); // fill array values serial port float converted = 0; memcpy(&converted, array, 4); /** "converted" holds float value */
now work if endianness of uc consistent order in sending data. if doesn't work try sending byte array in inverted order. , please mind error checking!
Comments
Post a Comment