12-12-2014 05:15 AM
Hi Guys
I am using UART RS232 protocol and i need to calculate the check sum. My data came in decimal and in the end it must be doubles to manipulate it. So my problem is, for example, the sum of all elemants is in decimal 1742 and in hex is 6CE and i wont only the last two elemants, CE that corresponds to 206 that is the result of my checksum. Can anyone help me?
Thanks
Solved! Go to Solution.
12-12-2014 05:45 AM
12-12-2014 05:51 AM - edited 12-12-2014 05:52 AM
split number
edit: too late 😄
12-12-2014 05:58 AM
Hello Jim and thanks for answer
My data came from an HMI device and in decimal. I'll show you an example:
123 | 123 | 160 | 54 | 111 | 103 | 110 | 108 | 107 | 109 | 108 | 108 | 100 | 100 | 100 | 100 | 0 | 0 | 1 | 4 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | 96 | 1 | 0 | 0 | 0 | 0 | 0 | 10 | 51 | 1 | 0 | 0 | 51 | 1 | 1 | 23 | 12 | 0 | 206 | 125 | 125 |
The last third elemant is the Checksum. If i sum all the elemants from 160 (cmd) to the last cell before 206 (CKS) the result is 1742 (decimal) that correspond to 6CE has i told before, but is truncated to the 2 less significant digits CE that is 206. I will try the examples from you guys.
Thanks a lot
12-12-2014 07:28 AM - edited 12-12-2014 07:29 AM
I think this is what you are going for. Your main issue is that you want to use String To Byte Array. Each of those numbers are bytes. To treat them as bytes instead of ASCII characters.
12-12-2014 07:52 AM
i dont know if it fits to your req. because my input is array just sum them up and convert to string...
crossrulz i like your seminal coding
12-12-2014 08:39 AM
Thanks Syrpimp
My data came from HMI in decimal like the example that i show above. Each cell is a value. You used an array, but i think that i can't use it because it must be done automatically and the data field have variable length.
The UART protocol frame has a start frame field that corresponds to two ‘{‘ characters and an end frame field that corresponds to two ‘}’. Additionally, besides the data field, that has variable length, it has a byte that corresponds to the command type and another that is the checksum. The following table illustrates the protocol.
|
Bytes considered in checksum |
|
|
||||||||
|
|
Command Data |
|
|
|||||||
Start Frame |
Cmd |
Data0 |
Data1 |
Data2 |
Data3 |
Data4 |
Data5 |
Data6 |
Data… |
CHK SUM |
End Frame |
{{ |
|
|
|
|
|
|
|
|
|
1 Byte CHK SUM (sum of all data bytes truncated to 1 byte) |
}} |
Fig. 1 UART communication protocol template
I receive data like this:
123 | 123 | 160 | 54 | 111 | 103 | 110 | 108 | 107 | 109 | 108 | 108 | 100 | 100 | 100 | 100 | 0 | 0 | 1 | 4 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | 96 | 1 | 0 | 0 | 0 | 0 | 0 | 10 | 51 | 1 | 0 | 0 | 51 | 1 | 1 | 23 | 12 | 0 | 206 | 125 |
125 |
I'm trying explain my doubt the best way i can and i thank you all.
12-12-2014 08:55 AM
I highly recommend viewing your string in Hex display and change your numbers here into hexidecimal. Things will make a lot more sense. Then go look at my post above. It does exactly what you are asking for.
12-12-2014 09:39 AM
Hello again
I tried exactly has you told and the result isn't ok. See the attachment.
12-12-2014 09:52 AM
Your input data isn't correct. I can tell because it should be starting with 7B7B and end with 7D7D.