How to send float through serial port in c#? -


this question has answer here:

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:

  1. convert float 4-byte array. you're on right track, although c# rusty.
  2. send them 1 one microcontroller
  3. 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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -