SimpleModbusSlave allows you to communicate to any slave using the Modbus RTU protocol. The crc calculation is based on https://os.mbed.com/users/jpelletier/code/CRC/ The functions implemented are functions 3 and 16. read holding registers and preset multiple registers of the Modbus RTU Protocol. This implementation DOES NOT fully comply with the Modbus specifications.

Revision:
1:85e226914fb7
Parent:
0:7c5265097fd2
diff -r 7c5265097fd2 -r 85e226914fb7 SimpleModbusSlave.cpp
--- a/SimpleModbusSlave.cpp	Tue Oct 10 12:22:31 2017 +0000
+++ b/SimpleModbusSlave.cpp	Wed Oct 25 10:38:11 2017 +0000
@@ -18,6 +18,8 @@
 unsigned int T1_5; // inter character time out
 unsigned int T3_5; // frame delay
 unsigned char buffer = 0;
+unsigned char datarec = 0;
+unsigned char datasent = 0;
 
 Timer intercharacter_timer;
 Timeout interframe_timeout;
@@ -33,10 +35,27 @@
 void sendPacket(unsigned char bufferSize);
 unsigned int reverse_CRC(unsigned int calculated_CRC);
 
+unsigned int newdatarec(){
+    if (datarec){
+        datarec = 0;
+        return 1;
+    }
+    else return 0;
+}
+
+unsigned int newdatasent(){
+    if (datasent){
+        datasent = 0;
+        return 1;
+    }
+    else return 0;
+}
+
 void end_of_transmission()
 {
     if (TxEnablePin > 1)
         dir = 0;
+    datasent = 1;
 }
 
 unsigned int modbus_update(unsigned int *holdingRegs)
@@ -85,6 +104,7 @@
                 unsigned int crc = ((frame[buffer - 2] << 8) | frame[buffer - 1]); // combine the crc Low & High bytes                
                 if (reverse_CRC(calculate_crc16_Modbus(frame, buffer - 2)) == crc) // if the calculated crc matches the recieved crc continue
                 {                    
+                    datarec = 1;
                     function = frame[1];
                     unsigned int startingAddress = ((frame[2] << 8) | frame[3]); // combine the starting address bytes
                     unsigned int no_of_registers = ((frame[4] << 8) | frame[5]); // combine the number of register bytes