Calculate checksum(hex) of string with ASCII(unsigned int). Use to detected communication error.

Dependencies:   StrLib

Committer:
AkinoriHashimoto
Date:
Wed Oct 21 02:10:40 2015 +0000
Revision:
0:b3a521e42246
1st publish.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:b3a521e42246 1 /** Calculate checksum(hex) of string with ASCII(unsigned int).
AkinoriHashimoto 0:b3a521e42246 2 * Use to detected communication error.
AkinoriHashimoto 0:b3a521e42246 3 */
AkinoriHashimoto 0:b3a521e42246 4
AkinoriHashimoto 0:b3a521e42246 5 /** @code
AkinoriHashimoto 0:b3a521e42246 6 #include "mbed.h"
AkinoriHashimoto 0:b3a521e42246 7 #include "CheckSum.h"
AkinoriHashimoto 0:b3a521e42246 8
AkinoriHashimoto 0:b3a521e42246 9 DigitalOut led[]= {LED1, LED2, LED3, LED4};
AkinoriHashimoto 0:b3a521e42246 10 Serial pc(USBTX, USBRX);
AkinoriHashimoto 0:b3a521e42246 11 int main()
AkinoriHashimoto 0:b3a521e42246 12 {
AkinoriHashimoto 0:b3a521e42246 13 string str, tmp, checksum;
AkinoriHashimoto 0:b3a521e42246 14 str= "";
AkinoriHashimoto 0:b3a521e42246 15 int idx;
AkinoriHashimoto 0:b3a521e42246 16 bool ans;
AkinoriHashimoto 0:b3a521e42246 17 while(true) {
AkinoriHashimoto 0:b3a521e42246 18 led[0]= !led[0];
AkinoriHashimoto 0:b3a521e42246 19 if(pc.readable())
AkinoriHashimoto 0:b3a521e42246 20 str += pc.getLine();
AkinoriHashimoto 0:b3a521e42246 21 idx= str.find("\r\n");
AkinoriHashimoto 0:b3a521e42246 22 if(idx != string::npos) {
AkinoriHashimoto 0:b3a521e42246 23 led[1]= !led[1];
AkinoriHashimoto 0:b3a521e42246 24 tmp= str.substr(0, idx);
AkinoriHashimoto 0:b3a521e42246 25 str= str.substr(idx+ 2);
AkinoriHashimoto 0:b3a521e42246 26
AkinoriHashimoto 0:b3a521e42246 27 checksum= CheckSum::calc(tmp);
AkinoriHashimoto 0:b3a521e42246 28 ans= CheckSum::compare(tmp, checksum);
AkinoriHashimoto 0:b3a521e42246 29 pc.printf("%s\t%s.\r\n", tmp.c_str(), checksum.c_str());
AkinoriHashimoto 0:b3a521e42246 30 if(ans)
AkinoriHashimoto 0:b3a521e42246 31 pc.printf("TRUE");
AkinoriHashimoto 0:b3a521e42246 32 for(int i= 1; i < 5; i++) {
AkinoriHashimoto 0:b3a521e42246 33 checksum= CheckSum::calc(tmp, i);
AkinoriHashimoto 0:b3a521e42246 34 ans= CheckSum::compare(tmp, checksum);
AkinoriHashimoto 0:b3a521e42246 35 pc.printf("%s\t%s.\r\n", tmp.c_str(), checksum.c_str());
AkinoriHashimoto 0:b3a521e42246 36 if(ans)
AkinoriHashimoto 0:b3a521e42246 37 pc.printf("TRUE");
AkinoriHashimoto 0:b3a521e42246 38 }
AkinoriHashimoto 0:b3a521e42246 39 }
AkinoriHashimoto 0:b3a521e42246 40 }
AkinoriHashimoto 0:b3a521e42246 41 }
AkinoriHashimoto 0:b3a521e42246 42 @endcode
AkinoriHashimoto 0:b3a521e42246 43 */
AkinoriHashimoto 0:b3a521e42246 44
AkinoriHashimoto 0:b3a521e42246 45 #pragma once
AkinoriHashimoto 0:b3a521e42246 46 #include "mbed.h"
AkinoriHashimoto 0:b3a521e42246 47
AkinoriHashimoto 0:b3a521e42246 48 #include "StrLib.h"
AkinoriHashimoto 0:b3a521e42246 49
AkinoriHashimoto 0:b3a521e42246 50 class CheckSum
AkinoriHashimoto 0:b3a521e42246 51 {
AkinoriHashimoto 0:b3a521e42246 52 public:
AkinoriHashimoto 0:b3a521e42246 53 /** Create checksum (hex).
AkinoriHashimoto 0:b3a521e42246 54 * @param; str: target
AkinoriHashimoto 0:b3a521e42246 55 * Byte: returnend string size[B].
AkinoriHashimoto 0:b3a521e42246 56 * @return; The end of hex checksum.
AkinoriHashimoto 0:b3a521e42246 57 */
AkinoriHashimoto 0:b3a521e42246 58 static string calc(string str, int Byte= 1);
AkinoriHashimoto 0:b3a521e42246 59
AkinoriHashimoto 0:b3a521e42246 60 /** Compare Check.
AkinoriHashimoto 0:b3a521e42246 61 * @param; str: target.
AkinoriHashimoto 0:b3a521e42246 62 * sum: checksum of target.
AkinoriHashimoto 0:b3a521e42246 63 * @return; true or false.
AkinoriHashimoto 0:b3a521e42246 64 * if target equals to sum, returned true.
AkinoriHashimoto 0:b3a521e42246 65 * else, returned false.
AkinoriHashimoto 0:b3a521e42246 66 */
AkinoriHashimoto 0:b3a521e42246 67 static bool compare(string str, string sum);
AkinoriHashimoto 0:b3a521e42246 68 };
AkinoriHashimoto 0:b3a521e42246 69
AkinoriHashimoto 0:b3a521e42246 70 // EOF