Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TAIST_modbus by
Diff: modbus.cpp
- Revision:
- 1:c5e6595a03db
- Parent:
- 0:f306cb0263a6
- Child:
- 2:6d1f053a6033
--- a/modbus.cpp	Wed Mar 14 06:17:48 2018 +0000
+++ b/modbus.cpp	Wed Mar 14 14:34:01 2018 +0000
@@ -1,7 +1,7 @@
 #include "mbed.h"
 #include "modbus.h"
 
-uint8_t nodeId = 0;
+uint8_t nodeId = 170;
 uint16_t regValue[ADDR_RANGE];
 
 enum state {IDLE, RECEPTION, END} protState = IDLE;
@@ -12,23 +12,23 @@
     uint8_t sum = 0;
     uint8_t lrc, i;
     char tmpbuf[] = {0, 0, 0};
-    
     if (strlen(buf) == 14) {
         for (i = 0; i < 6; i++) {
             tmpbuf[0] = buf[i*2];
             tmpbuf[1] = buf[i*2 + 1]; 
             frame[i] = strtoul(tmpbuf, NULL, 16);
+            //printf("FRAME %d \n", frame[i]);
         }
         tmpbuf[0] = buf[12]; tmpbuf[1] = buf[13]; 
         lrc = strtoul(tmpbuf, NULL, 16);
         for (i = 0; i < 6; i++) {
             sum += frame[i];
         }
-        if ((sum + lrc) == 0) {
+        //printf(" sum %d, lrc %d \n", sum, lrc);
+        if ((sum + lrc) == 256) {
             status = 1;
         }
     }
-    
     return status;    
 }
 
@@ -59,6 +59,7 @@
 
 int modbus_parser(char ch, uint8_t *frame)
 {
+    //printf("char %c \n", ch);
     static char buf[514];
     static int idx = 0;
     static int status = 0;
@@ -70,8 +71,11 @@
                 idx = 0;
                 status = 0;
             }
+            //printf("IDLE \n");
             break;
+            
         case RECEPTION:
+            //printf("RECEPTION \n");
             if ((ch >= '0') && (ch <= '9')) {
                 buf[idx++] = ch;
             } else if ((ch >= 'a') && (ch <= 'f')) {
@@ -85,17 +89,23 @@
                 protState = IDLE;
             }
             break;
+            
         case END:
             if (ch == '\n') {
+                //printf(" IN THE END\n");
                 if (process_buffer(buf, frame)) {
                     if ((frame[0] == nodeId) && (frame[1] == FUNC_CODE)) {
                         status = 1;
+                        //printf("WOW \n");
                     }
                 }
+            //status = 6;
             } 
+            //printf("ENDDDDDDDDDDDDDDDDDD\n");
             protState = IDLE;
-            break;
+            break;   
         default:
+            //printf("DEFAULT \n");
             protState = IDLE;
     }
     
    