The Fletcher checksum is a pretty good way to ensure data integrity….
John Fletcher on Computing at Livermore
Simple checkup calculations have drawbacks, but the fletcher checksum algorithm does a pretty good job. Wikipedia has a good discussion of the benefits and drawbacks.
The following runs on an Arduino. Add the following function to your code, call it with a string, and it will output the fletcher-16 checksum.
* * generates a Fletcher 16 bit checksum, * see https://en.wikipedia.org/wiki/Fletcher%27s_checksum * this function contains an example to extract ascii values * from a String object * * Returns a string of the form: "SUM1,SUM2," that is * meant to be prepended * to a data string before writing the sting to the com port * * The receiving system is expected to decode the string and verify the * checksum is correct for the data string (see the function checkSumTest(string) * */ //*********************************** generateCheckSum() String generateCheckSum(String dataStr) { //Serial.println(dataStr); int dataStrLen = dataStr.length()+1; char strArray[dataStrLen]; dataStr.toCharArray(strArray,dataStrLen); byte sum1 = 0; byte sum2 =0; for (int i=0; i<dataStrLen-1; i++) { int asciiVal = strArray[i]; sum1 = (sum1 + asciiVal) % 255; sum2 = (sum1 + sum2) % 255; if (showCheckSum) { Serial.print("SUBD"); Serial.print(" "); Serial.print(strArray[i]); Serial.print(" "); Serial.print(asciiVal); Serial.print(" "); Serial.print(sum1); Serial.print(" "); Serial.println(sum2); } }