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

Dependencies:   StrLib

Embed: (wiki syntax)

« Back to documentation index

CheckSum Class Reference

CheckSum Class Reference

Calculate checksum(hex) of string with ASCII(unsigned int). More...

#include <CheckSum.h>

Static Public Member Functions

static string calc (string str, int Byte=1)
 Create checksum (hex).
static bool compare (string str, string sum)
 Compare Check.

Detailed Description

Calculate checksum(hex) of string with ASCII(unsigned int).

Use to detected communication error.

 #include "mbed.h"
#include "CheckSum.h"

DigitalOut led[]= {LED1, LED2, LED3, LED4};
Serial pc(USBTX, USBRX);
int main()
{
    string str, tmp, checksum;
    str= "";
    int idx;
    bool ans;
    while(true) {
        led[0]= !led[0];
        if(pc.readable())
            str += pc.getLine();
        idx= str.find("\r\n");
        if(idx != string::npos) {
            led[1]= !led[1];
            tmp= str.substr(0, idx);
            str= str.substr(idx+ 2);

            checksum= CheckSum::calc(tmp);
            ans= CheckSum::compare(tmp, checksum);
            pc.printf("%s\t%s.\r\n", tmp.c_str(), checksum.c_str());
            if(ans)
                pc.printf("TRUE");
            for(int i= 1; i < 5; i++) {
                checksum= CheckSum::calc(tmp, i);
                ans= CheckSum::compare(tmp, checksum);
                pc.printf("%s\t%s.\r\n", tmp.c_str(), checksum.c_str());
                if(ans)
                    pc.printf("TRUE");
            }
        }
    }
}

Definition at line 50 of file CheckSum.h.


Member Function Documentation

string calc ( string  str,
int  Byte = 1 
) [static]

Create checksum (hex).

Parameters:
str: target Byte: returnend string size[B].
Returns:
; The end of hex checksum.

Definition at line 4 of file CheckSum.cpp.

bool compare ( string  str,
string  sum 
) [static]

Compare Check.

Parameters:
str: target. sum: checksum of target.
Returns:
; true or false. if target equals to sum, returned true. else, returned false.

Definition at line 28 of file CheckSum.cpp.