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

Dependencies:   StrLib

Files at this revision

API Documentation at this revision

Comitter:
AkinoriHashimoto
Date:
Wed Oct 21 02:10:40 2015 +0000
Commit message:
1st publish.

Changed in this revision

CheckSum.cpp Show annotated file Show diff for this revision Revisions of this file
CheckSum.h Show annotated file Show diff for this revision Revisions of this file
StrLib.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b3a521e42246 CheckSum.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CheckSum.cpp	Wed Oct 21 02:10:40 2015 +0000
@@ -0,0 +1,34 @@
+#include "CheckSum.h"
+
+
+string CheckSum::calc(string str, int Byte)
+{
+    // calculate SUM.
+    int sum= 0;
+    int size= str.size();
+
+    for(int idx= 0; idx < size; idx++)
+        sum += str[idx];
+
+    // convert HEX. REWRITE/REUSE.
+    str= I2A(sum, 16, (Byte* 2)); // Hex, over 2chars(ex. 0x0f).
+
+    // N[B] with the end of the hex checksum.
+    size= str.size();
+    if(size > (Byte* 2))        // cut tail from str.
+        return str.substr(size- (Byte* 2));
+
+    if(size == (Byte* 2))    // return 
+        return str;
+        
+    // size < Byte
+    return "ERR";
+}
+
+bool CheckSum::compare(string str, string sum)
+{
+    int Byte= sum.size()/ 2;
+    return strCompareComplete(CheckSum::calc(str, Byte), sum);
+}
+
+// EOF
\ No newline at end of file
diff -r 000000000000 -r b3a521e42246 CheckSum.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CheckSum.h	Wed Oct 21 02:10:40 2015 +0000
@@ -0,0 +1,70 @@
+/** Calculate checksum(hex) of string with ASCII(unsigned int).
+ *  Use to detected communication error.
+ */
+
+/** @code
+ #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");
+            }
+        }
+    }
+}
+ @endcode
+ */
+
+#pragma once
+#include "mbed.h"
+
+#include "StrLib.h"
+
+class CheckSum
+{
+public:
+    /** Create checksum (hex).
+     *  @param; str:  target
+     *          Byte: returnend string size[B].
+     *  @return; The end of hex checksum.
+     */
+    static string calc(string str, int Byte= 1);
+    
+    /** Compare Check.
+     *  @param; str: target.
+     *          sum: checksum of target.
+     *  @return; true or false.
+     *          if target equals to sum, returned true.
+     *          else, returned false.
+     */
+    static bool compare(string str, string sum);
+};
+
+// EOF
\ No newline at end of file
diff -r 000000000000 -r b3a521e42246 StrLib.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StrLib.lib	Wed Oct 21 02:10:40 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AkinoriHashimoto/code/StrLib/#7bc89a64bfbd