Sukkpranhachai Gatesichapakorn / Mbed 2 deprecated TAIST_modbus

Dependencies:   mbed

Fork of TAIST_modbus by Supachai Vorapojpisut

Revision:
13:f176b5139407
Parent:
6:5bb5238f2f6e
Child:
14:e4c217949ce3
diff -r 774c2b305d2d -r f176b5139407 modbus.cpp
--- a/modbus.cpp	Wed Mar 14 09:06:54 2018 +0000
+++ b/modbus.cpp	Wed Mar 14 13:46:47 2018 +0000
@@ -4,7 +4,7 @@
 uint8_t nodeId = 0;
 uint16_t regValue[ADDR_RANGE];
 
-enum state {IDLE, RECEPTION, END} protState = IDLE;
+enum state {IDLE, RECEPTION, END} protState = IDLE;  //set three state, now is IDLE
 
 int process_buffer(char *buf, uint8_t *frame)
 {
@@ -13,15 +13,15 @@
     uint8_t lrc, i;
     char tmpbuf[] = {0, 0, 0};
     
-    if (strlen(buf) == 14) {
-        for (i = 0; i < 6; i++) {
+    if (strlen(buf) == 14) {   //comfirm the lenght of command
+        for (i = 0; i < 6; i++) {   //from hexadecimal to decimal
             tmpbuf[0] = buf[i*2];
             tmpbuf[1] = buf[i*2 + 1]; 
             frame[i] = strtoul(tmpbuf, NULL, 16);
         }
         tmpbuf[0] = buf[12]; tmpbuf[1] = buf[13]; 
         lrc = strtoul(tmpbuf, NULL, 16);
-        for (i = 0; i < 6; i++) {
+        for (i = 0; i < 6; i++) {   //confirm command
             sum += frame[i];
         }
         if ((sum + lrc) == 0) {
@@ -37,7 +37,7 @@
     nodeId = id;
 }
 
-uint16_t modbus_read(uint16_t offset)
+uint16_t modbus_read(uint16_t offset)   //return value
 {
     if (offset < ADDR_RANGE) {
         return regValue[offset];
@@ -66,19 +66,19 @@
     switch(protState) {
         case IDLE:
             if (ch == ':') {
-                protState = RECEPTION;
+                protState = RECEPTION;   //catch ':' and change the states
                 idx = 0;
                 status = 0;
             }
             break;
         case RECEPTION:
-            if ((ch >= '0') && (ch <= '9')) {
+            if ((ch >= '0') && (ch <= '9')) {   //select the value
                 buf[idx++] = ch;
             } else if ((ch >= 'a') && (ch <= 'f')) {
                 buf[idx++] = ch;
             } else if ((ch >= 'A') && (ch <= 'F')) {
                 buf[idx++] = ch;
-            } else if (ch == '\r') {
+            } else if (ch == '\r') {    //end command
                 buf[idx] = 0;
                 protState = END;
             } else {