พัทธนันทน์ ศรีสุขนันทน์ / Mbed 2 deprecated TAIST_modbus

Dependencies:   mbed

Fork of TAIST_modbus by Supachai Vorapojpisut

Files at this revision

API Documentation at this revision

Comitter:
KRiTiRK
Date:
Wed Mar 21 09:09:40 2018 +0000
Parent:
6:b6d3e73ad56f
Commit message:
to kokokokokkokokkokokokookokoookoookokokokookori

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
modbus.cpp Show annotated file Show diff for this revision Revisions of this file
modbus.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 19 07:02:47 2018 +0000
+++ b/main.cpp	Wed Mar 21 09:09:40 2018 +0000
@@ -2,9 +2,10 @@
 #include "modbus.h"
 #include "RawSerial.h"
 
-DigitalOut myled(LED1);
-RawSerial pc(USBTX, USBRX);
+//DigitalOut myled(LED1);
+RawSerial pc(PC_6,PC_7);
 InterruptIn button(USER_BUTTON);
+DigitalOut sel(PA_5);
 Ticker timebase;
 
 uint32_t Tick = 0;
@@ -19,10 +20,12 @@
     uint8_t frame[6];
     uint16_t offset, count;
     uint8_t reg_map;
+    char tmpPrint[20];
     
     char ch = pc.getc();
     if (modbus_parser(ch, frame)) {
 //        printf("TRUE\n") ;
+        sel = 1;
         offset = (frame[2] << 8) | (frame[3]);
         count = (frame[4] << 8) | (frame[5]);
         
@@ -30,17 +33,17 @@
         modbus_update(0x0001, skipCount);
         
         reg_map = modbus_check(offset, count);
-        modbus_response(reg_map);
+        modbus_response(reg_map, tmpPrint);
+        
+        pc.printf(tmpPrint);
+        printf(tmpPrint);
         
         modbus_update(0x0000, 0);
         modbus_update(0x0001, 0);
         
         skipCount = 0 ;
         widthCount = 0 ;
-        
-        
-        
-        
+        sel = 0;
         
     }
 }
@@ -93,7 +96,7 @@
 //    printf("Test: %02X\r\n", modbus_check(0x0002, 0x0002));    
     while(1) {
         // blinking LED
-        myled = !myled; 
+        //myled = !myled; 
         wait(2.5);
 //        printf("Detection: %d - %d\r\n", pressedTick, releasedTick);
 //        modbus_response(0x01);
--- a/modbus.cpp	Mon Mar 19 07:02:47 2018 +0000
+++ b/modbus.cpp	Wed Mar 21 09:09:40 2018 +0000
@@ -1,7 +1,8 @@
 #include "mbed.h"
 #include "modbus.h"
 
-uint8_t nodeId = 2;
+//RawSerial pc2(PC_6,PC_7);
+uint8_t nodeId = 4;
 uint16_t regValue[ADDR_RANGE];
 
 enum state {IDLE, RECEPTION, END} protState = IDLE;
@@ -136,7 +137,7 @@
     return -lrc;
 }
 
-void modbus_response(uint8_t reg_map) 
+void modbus_response(uint8_t reg_map, char *tmpPrint) 
 {
     char fmt00[] = ":%02X%02X%02X\r\n";
     char fmt01[] = ":%02X%02X%02X%02X%02X\r\n";
@@ -156,8 +157,10 @@
             frame[3] = tmp & 0xFF;
             frame[4] = calc_lrc(frame, 4);
             fmtstr = fmt01;
-            sprintf(str, fmtstr, frame[0], frame[1], frame[2], frame[3], frame[4]);
-            printf("%s", str);  
+            //sprintf(str, fmtstr, frame[0], frame[1], frame[2], frame[3], frame[4]);
+            sprintf(tmpPrint, fmtstr, frame[0], frame[1], frame[2], frame[3], frame[4]);
+            printf("%s", str);
+            //pc2.printf("%s", str);  
             break;
         case 2:
             tmp = modbus_read(1);
@@ -165,8 +168,10 @@
             frame[3] = tmp & 0xFF;
             frame[4] = calc_lrc(frame, 4);
             fmtstr = fmt02;
-            sprintf(str, fmtstr, frame[0], frame[1], frame[2], frame[3], frame[4]);
-            printf("%s", str);          
+            //sprintf(str, fmtstr, frame[0], frame[1], frame[2], frame[3], frame[4]);
+            sprintf(tmpPrint, fmtstr, frame[0], frame[1], frame[2], frame[3], frame[4]);
+            printf("%s", str);
+            //pc2.printf("%s", str);          
             break;
         case 3:
             tmp = modbus_read(0);
@@ -177,9 +182,11 @@
             frame[5] = tmp & 0xFF;                                
             frame[6] = calc_lrc(frame, 6);
             fmtstr = fmt03;
-            sprintf(str, fmtstr, frame[0], frame[1], frame[2], frame[3], frame[4], frame[5], frame[6]);
+            //sprintf(str, fmtstr, frame[0], frame[1], frame[2], frame[3], frame[4], frame[5], frame[6]);
+            sprintf(tmpPrint, fmtstr, frame[0], frame[1], frame[2], frame[3], frame[4], frame[5], frame[6]);
 //            printf("Response: %s", str);  
-            printf("%s", str);             
+            printf("%s", str);   
+            //pc2.printf("%s", str);             
             break;
         default:
             fmtstr = fmt00;
--- a/modbus.h	Mon Mar 19 07:02:47 2018 +0000
+++ b/modbus.h	Wed Mar 21 09:09:40 2018 +0000
@@ -11,6 +11,6 @@
 uint16_t modbus_update(uint8_t offset, uint16_t val);
 int modbus_parser(char ch, uint8_t *frame);
 uint8_t modbus_check(uint16_t offset, uint16_t count);
-void modbus_response(uint8_t reg_map);
+void modbus_response(uint8_t reg_map, char *tmpPrint);
 
 #endif // MODBUS_H