Hello, I am communicating with a sht4 sensor which, when I send a value in hexadecimal, returns 28bits (temperature)+8-bits(CRC)+28Bits (Humidity)+8-bits(CRC) which arrive in hexadecimal for example "0x65 0xEE 0x7D 0x82 0x08 0xC2"
my question is: what is the best way to receive these data to be able to work with them, the one I currently use separates them but if, for example, I get 0x65 0x03 when I put them together, I have 0x653 left, which causes errors in the readings, I appreciate your comments and an apology for my poor English. Thanks a lot!
I suspect you are just VIEWING them as hexadecimal. Your initial description says nothing but BITS are being sent. So, you have to process them as bits. When you convert your HEX display to bits and mark off the first 28 bits, do you get the correct value?
Yes, currently I only want to show it on the serial monitor, once the values shown are correct I will apply a formula to calculate the temperature and humidity and for this I will convert each hexadecimal part to decimal, the sensor sends the correct values, check it with a usb UART, but when passing it to arduino I have a problem with the second word, for example, from the first 16 bits, 0x5E 0x0F arrives with my current code, it prints "Temperatura=5ef" in the serial terminal when the correct thing would be "Temperatura=5e0f" and the same goes for humidity
I think I didn't explain what I'm trying to do, I have this information that is sent by serial communication [2 * 8-bit Temp-data; 8-bit CRC; 2 * 8-bit RH data; 8-bit CRC] are sent in packets of 1-8bits in Hexadecimal, I want to receive them, group them and convert to decimal
in the group I mean this, they come to me in parts
0x5D
0xC0
0x61
0xA1
0xDF
0xA0
the first two form a 16-bit number 0x5DC0 that corresponds to the temperature
the following is the CRC
4 and 5 are a 16-bit number 0xA1DF that corresponds to the relative humidity RH
and the last one the CRC
currently I try to show them in the serial monitor to verify that they are being ordered and grouped correctly
I hope to complement the information with this comment, thanks for your suggestions.
Sorry, 28-bit is wrong, this is correct: [2 * 8-bit Temp-data; 8-bit CRC; 2 * 8-bit RH data; 8-bitCRC]
The first value is the temperature signal (2 * 8-bit data + 8-bit CRC), the second is the humidity signal (2 * 8-bit data + 8-bit CRC).
Ok, then the temp and humidity are 16 bit values which conveniently match the Arduino integer for processing. Are you familiar with structures and unions. If not study up on them. Very convenient way to work with what you have.
Yes, I am thinking of working everything in decimal, and I am going to save it in an unsigned char and multiply it by 256 + the other value to be able to do the calculations for example
int Temp=(Data[0]*256)+Data[1];
where Data[0] are the first 8 bits of the temp and Data[1] the other 8 bits and so I would have the value to be able to calculate the temperature, what I am checking right now is how to save unsigned long values in an unsigned char.
Do you think it's a good option?
Thank you.