Hi all,
I am reading a load cell with a RS485 communication back to my arduino mega UART (Serial 1 using an RS485-TTL logic converter). It looks like everything is working properly; everytime I sent out a particular string ({0x20, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC2, 0xBA}), I get another string back that contains the weight I wanna read. Within this string, 4 bytes particularly tells you the value of the load cell weight.
I managed to read the string back and isolating the 4 bytes that contain the weight info. Unfortunately I am not able to convert this HEX weight into a decimal value.
I am using strtol function for the conversion; however I am getting a float value that is 0.
Any tips on what I am doing wrong?
Hi @Diego .
in this line " Serial.print(hexCar); " do the printed values exactly match the individual digits of the measured weight?
Can you show how they appear on the serial monitor?
As you can see I am getting 9 bytes back and particularly byte 6,7,4,5 correspond to the weight.
In this case info about weight is 7D3 (2003 in decimal) since 20 grams were placed on the load cell. Output is with 2 decimal digits without taking into account the dot; therefore it was 20.03 grams.
When I try to convert the char array 000007D3 into a decimal I don't get any output
Post a description of the protocol, as requested earlier.
The bytes in question could be a 32 bit floating point number, and there are several ways of converting that. However, the byte order could be reversed from Arduino.
I think the problem is that the message is NOT a character array. It is four bytes of binary: 0x07D30000. You are re-arranging the binary bytes into 0x000007D3 and adding a null terminator but now your 'string' starts with a null character and therefor has length 0: char Weight[] = {0x00, 0x00, 0x07, 0xD3, 0x00};
I think you want to take the two words of the value and put them in a 32-bit integer.
uint32_t weight = *(uint16_t *) &receivedChars_LoadCell[5];
weight <<= 16;
weight += *(uint16_t *) &receivedChars_LoadCell[3];
Serial.println(weight); // Raw binary value
Serial.println(weight / 100.0); // Weight in grams
Hi @johnwasser , it works, thank you!!. you were right, message was not a character array.
Anyway, now my only problem is that I am getting the wrong integer value.
As you can see, I am receiving correctly the bytes back. On the weight scale i have 1020.15 g. Therefore the HEX code to convert in decimal should be 18E7F (my order should be byte 5--> byte 6- --> byte 3 --> byte 4).
However, result I am getting is 16809614 that is ( byte 6 --> byte 5 --> byte 4 --> byte 3). How can I modify it to get the right value?
It's not really clear to me the logic you are applying, can you please explain it to me? sorry about it. Are you creating an integer of 32 bit right? how are you creating the integer using a pointer and a bit mask?