In this article, I want to give you an example of how it can be easy to implement a simple checksum check when transfering data from Arduino 101 from and to any other device.
First of all, let's think about the importance of checksum check. When data is transfered from a source to a destination device, the data might be slightly corrupted. This might happen due to noise or many other reasons. If the data is corrupted, and the receiver uses this data, some failure might happen. Therefore, it is important that the receiver understands if the data received is reliable or not. This can be done using a checksum check. Simply speaking, additional information (checksum value) are added to the payload (useful data). By comparing the data and the checksum value, the receiver is able to understand if, during the transfer, any corruption happened.
1 2 3 4 5 6 7 8 9 10 11 12 |
uint8_t CK_A; uint8_t CK_B; // CALCULATEDS THE UBX CHECKSUM OF A CHAR ARRAY (using algorithm defined in GPS manual) void UBX_calculate_checksum(uint8_t* array, unsigned int array_start, unsigned int array_length) { CK_A = 0; CK_B = 0; unsigned int i; for (i = 0; i < array_length; i++) { CK_A = CK_A + array[array_start + i]; CK_B = CK_B + CK_A; } } |
A simple checksum check algorithm is visible above. The checksum is composed by two bytes. The first one (CK_A) is the simple sum of every bytes composing the payload. The second one (CK_B) is the sum of CK_A values calculated at each step. These 2 bytes can be appended to the payload (array), and the complete packet (payload + checksum bytes) can be sent to the receiver. The receiver then implements the same calculation, obtaining the checksum values of the payload received. If the checksum calculated (2 bytes) and the checksum received (2 bytes) are exactly the same, the data can be considered reliable. If there is any difference, the data is considered not reliable. In this second case, the receiver might request to re-send the data.
In my opinion, this algorithm is very simple, therefore I really recommend you to implement it every time you think that, due to some reason, your data might become corrupted. Here below, I give some examples:
- Data sent, via any communication protocol (for example: Serial) from sender to receiver. Especially in the case of serial communication, in case you don't use any parity bit check, the checksum is very useful. And it is even better than the usual parity check. In fact, when using parity check, if 2 bits are flipped, the data will be still considered to be OK. But when using the above checksum algorithm on the complete payload, this does not happen.
- Data stored on Arduino SD card, or EEPROM. Arduino lets you store data on SD cards, or EEPROM memory. There might be any corruption when writing to EEPROM memory, due to power supply noise for example. Also, in case of SD card, the data transfer is performed using SPI communication protocol. During the transfer, some corruption might happen. A possible way to solve this issue is using both checksum check, and data mirroring (having multiple copies): in case data block #1 is corrupted, data block #2 is used (if not corrupted), and the content of data block #2 is copied to data block #1.
The following is an example of Arduino sketch which uses the checksum check. Download here: IMU_send_20160625. The program, which runs on Arduino 101, acquires the raw data from gyroscope and acceleration sensors unit. This is performed once every millisecond, by using an interrupt call. The, the PC can poll this raw data by sending a command "s" via Serial communication. The complete packet size includes a header (packet number, 2 bytes), 12 bytes of data (3-axis acceleration, 3-axis gyro, 2 bytes each), plus 2 bytes of checksum. The payload are the first 14 bytes (header plus sensors raw data).
The use of checksum check is a good idea to increase data reliability. Nice article by the author. enjoyed it. Thank you !!! Keep posting !!!
Thank you. Especially for critical data, I always use checksum, because it is easy to implement, it does not require much calculation power (especially if the byte size is small).