2
votes

I am new to c#. I have a serial (COM port) device which is streaming an array of bytes continuously. The array contains values of different sensors which are read by micro-controller. Basically the value of each sensor is shown by 8 bytes such that each byte represents an ascii character.There is no delimiter between these bytes. After 32 bytes, we have two bytes of a carriage (D) and a new line (A).

I use serialPort1.ReadExisting() to read data from serial port and then display readings in a textbox. However, I need the numeric values of these sensors for plotting and calculations. How can I convert these values to float numbers? Or how should I change the way I am reading serial port.

I really appreciate your help!

1
if you read "00011236" then you can use Int32.Parse() to convert to integer, otherwise it depends of the encoding of the valueHubertL
Thanks for reply. Let's assume the decimal value of my sensor is 12.96; then I get something like this in the middle of my stream: 32324950465754user2038056
you can try to play with bits v[0]+v[1]*256... if the evalue is integer, but if it is floating point then check what is implementation on your device manualHubertL

1 Answers

0
votes

To convert bytes to floating point numbers you could use the BitConverter object. Then call the ToSingle method.

see HERE for reference.

It sounds like you need to read the datasheet of the device sending in the bytes to know big endian or little endian.