简体   繁体   中英

c# read serial in hex

I am using the SDS011 laser dust sensor and want to read the data in a Windows forms Application. I get Hex Data. Example: AA C0 D4 04 3A 0A A1 60 1D AB now the fourth and the third pair represent the PM2.5 value, in this example: 04D4 -> 1236 -> 123,6 ug\/m^3 Could you please help me write a code for reading in the hex data and calculating the PM2.5 value? Thanks.

"

If the data is coming as a string then it's simple as:

string inputData = "AA C0 D4 04 3A 0A A1 60 1D AB";
var inputDataSplit = inputData.Split(' ');
// Concatenate the fourth and the third string, and convert it to integer with base 16 (hex)
int pmValue = Convert.ToInt32(inputDataSplit[3] + inputDataSplit[2], 16);
        
// 1236
Console.Write(pmValue);

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