2021-08-29 10:11 PM
Hello ,
I am working on the stm32f070c6 controller , i am reading the adc values from controller and passing to the serial monitor of USB virtual COM port, but i want to checksum the values before transmitting the adc values to the serial monitor .how can i do it, if somebody have any idea please give me suggestion to how to checksum the adc values (theory wise I am sure).
Best regards
raj
Solved! Go to Solution.
2021-08-30 01:33 AM
If your value is uint16_t adc_result, then a checksum could be:
uint16_t adc_result = ...;
uint8_t checksum = adc_result >> 8 + adc_result;
2021-08-30 12:35 AM
Are you asking how to calculate a checksum? Just add the bytes values together, transmit the result along with the original data, and verify it matches on the receiving end. Could also use a CRC to do this which is more complicated but has other advantages.
2021-08-30 01:29 AM
Thanks for your reply @TDK , theory wise I am sure, but in coding how can add the adc values while transmitting, the adc directly giving an adc in decimal form how can write code for checksum while transmitting I am little bit confused can you please clarify me in the coding part.
Thanks in Advance
raj
2021-08-30 01:33 AM
If your value is uint16_t adc_result, then a checksum could be:
uint16_t adc_result = ...;
uint8_t checksum = adc_result >> 8 + adc_result;
2021-09-04 02:50 AM
Thanks for your reply @TDK , when transmitting we will send adding of both values(rightshifted value along with adc value) ,at receiver side how can I check ? whether the data is proper or not.
2021-09-04 12:35 PM
How can you check? By writing code that does the check, of course.
if (checksum != (uint8_t) (adc_result >> 8 + adc_result)) {
...
}
2021-09-25 06:31 AM
The checksum code is wrong because the addition operator has a higher precedence than a shift operator.
https://en.cppreference.com/w/c/language/operator_precedence