Example of how to handle the Modbus Slave RTU protocol using the mbed RTOS. This example can also be used as reference to implement other slow serial protocols.

Dependencies:   MessageQueue ModbusSlaveRTU SerialPortHandler mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
gabrielrivas
Date:
Wed Jan 21 03:00:29 2015 +0000
Parent:
0:220ae68e7dd4
Commit message:
Added timer based synchronization to the receiving side tasks.

Changed in this revision

MessageQueue.lib Show annotated file Show diff for this revision Revisions of this file
ModbusSlaveRTU.lib Show annotated file Show diff for this revision Revisions of this file
SerialPortHandler.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/MessageQueue.lib	Mon Jan 19 04:07:15 2015 +0000
+++ b/MessageQueue.lib	Wed Jan 21 03:00:29 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/gabrielrivas/code/MessageQueue/#faed68cc9fff
+http://developer.mbed.org/users/gabrielrivas/code/MessageQueue/#5e151e3834db
--- a/ModbusSlaveRTU.lib	Mon Jan 19 04:07:15 2015 +0000
+++ b/ModbusSlaveRTU.lib	Wed Jan 21 03:00:29 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/gabrielrivas/code/ModbusSlaveRTU/#81fee41d95f1
+http://developer.mbed.org/users/gabrielrivas/code/ModbusSlaveRTU/#0143a57e0c8e
--- a/SerialPortHandler.lib	Mon Jan 19 04:07:15 2015 +0000
+++ b/SerialPortHandler.lib	Wed Jan 21 03:00:29 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/gabrielrivas/code/SerialPortHandler/#f66dd1c93477
+http://developer.mbed.org/users/gabrielrivas/code/SerialPortHandler/#aa72cb66762f
--- a/main.cpp	Mon Jan 19 04:07:15 2015 +0000
+++ b/main.cpp	Wed Jan 21 03:00:29 2015 +0000
@@ -9,9 +9,12 @@
 MessageQueue<uint8_t> txQueue(64);
 MessageQueue<uint8_t> rxQueue(64);
 SerialPortHandler sph(&pc, &txQueue, &rxQueue);
+Timer receiveTimer;
+bool isReceiveTimerInitialized = false;
 ModbusSlaveRTU* mbus;
 Thread* modbus_process_thread;
 Thread* data_send_thread;
+Thread* receive_timer_check_thread;
 
 uint8_t cRegs[]  = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};
 uint8_t inRegs[] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};
@@ -43,6 +46,20 @@
     }    
 }
 
+void timer_check(void const *args)
+{
+    while(true) {
+        if (receiveTimer.read_ms() >= 1) {
+            mbus->trigger();
+            isReceiveTimerInitialized = false;
+            receiveTimer.reset();
+            receiveTimer.stop();
+        }
+        /*Waits for 5 ms*/
+        Thread::wait(5);
+    }
+}
+
 int main (void) {
     pc.attach(&rx_interrupt);
     
@@ -58,16 +75,24 @@
     mbus = new ModbusSlaveRTU(0x01, &txQueue, &rxQueue, &cR, &iR, &hR);    
     modbus_process_thread = new Thread(modbus_process);
     data_send_thread= new Thread(data_send_process);
-    
+    receive_timer_check_thread = new Thread(timer_check);
     while(true)
     {
         Thread::wait(1000);     
         led = !led;
-        mbus->trigger();
     }
 }
 
 void rx_interrupt(void) {
+    if (!isReceiveTimerInitialized)
+    {
+        isReceiveTimerInitialized = true;
+        receiveTimer.start();
+    }    
+    else
+    {
+        receiveTimer.reset();
+    }            
     sph.receivePacket();
     return;
 }