简体   繁体   中英

Reading stream of bytes from serial port in C#

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM