Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
authorMario de Sousa <msousa@fe.up.pt>
Mon, 21 Dec 2020 22:26:36 +0000
changeset 2714 0b636dc947a2
parent 2713 680ea4684209
child 2715 1215a6b741d5
Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
modbus/mb_runtime.c
modbus/mb_runtime.h
modbus/modbus.py
--- a/modbus/mb_runtime.c	Mon Dec 21 21:15:47 2020 +0000
+++ b/modbus/mb_runtime.c	Mon Dec 21 22:26:36 2020 +0000
@@ -410,11 +410,12 @@
 			  }
 			}
 
-			/* Set the flag_exec_status that is mapped onto a located WORD variable, so the user program
+			/* Set the flag_tn_error_code and flag_mb_error_code that are mapped onto
+             * located BYTE variables, so the user program
              * knows how the communication is going.
-             * This flag is an ammalgamation of the data in mb_error_code and tn_error_code
              */
-            client_requests[req].flag_exec_status = client_requests[req].tn_error_code * 256 + client_requests[req].mb_error_code;
+            client_requests[req].flag_mb_error_code = client_requests[req].mb_error_code;
+            client_requests[req].flag_tn_error_code = client_requests[req].tn_error_code;
             
             /* We have just finished excuting a client transcation request.
              * If the current cycle was activated by user request we reset the flag used to ask to run it
--- a/modbus/mb_runtime.h	Mon Dec 21 21:15:47 2020 +0000
+++ b/modbus/mb_runtime.h	Mon Dec 21 22:26:36 2020 +0000
@@ -171,20 +171,30 @@
            *    -> will be reset once the MB transaction has completed
            */
         u16     flag_exec_started;  
-          /* flag that will be mapped onto a (WORD) located variable 
-           * (u16 because the flag is a word! )
-           *    -> MSByte will store the result of the last executed MB transaction
+          /* flag that will be mapped onto a (BYTE) located variable 
+           * (u8 because the flag is a BYTE! )
+           *    -> will store the result of the last executed MB transaction
            *         1 -> error accessing IP network, or serial interface
            *         2 -> reply received from server was an invalid frame
            *         3 -> server did not reply before timeout expired
            *         4 -> server returned a valid Modbus error frame
-           *    -> if the MSByte is 4, the LSByte will store the MB error code returned by the server
            *    -> will be reset (set to 0) once this MB transaction has completed sucesfully
            * 
-           * In other words, this variable will be set from the current status of the
-           * mb_error_code and tn_error_code flags after each request.
-           */
-        u16     flag_exec_status;  
+           * In other words, this variable is a copy of tn_error_code, reset after each request attempt completes.
+           * We map this copy (instead of tn_error_code) onto a located variable in case the user program decides
+           * to overwrite its value and mess up the plugin logic.
+           */
+        u8      flag_tn_error_code;  
+          /* flag that will be mapped onto a (BYTE) located variable 
+           * (u8 because the flag is a BYTE! )
+           *    -> if flag_tn_error_code is 4, this flag will store the MB error code returned by the MB server in a MB error frame
+           *    -> will be reset (set to 0) once this MB transaction has completed succesfully
+           * 
+           * In other words, this variable is a copy of mb_error_code, reset after each request attempt completes.
+           * We map this copy (instead of mb_error_code) onto a located variable in case the user program decides
+           * to overwrite its value and mess up the plugin logic.
+           */
+        u8      flag_mb_error_code;  
 	} client_request_t;
 
 
--- a/modbus/modbus.py	Mon Dec 21 21:15:47 2020 +0000
+++ b/modbus/modbus.py	Mon Dec 21 22:26:36 2020 +0000
@@ -140,14 +140,23 @@
             "description": "Modbus request execution control flag",
             "children": []})        
         entries.append({
-            "name": "Request Status flag",
+            "name": "Modbus Request Status flag",
             "type": LOCATION_VAR_MEMORY,
-            "size": 16,          # WORD flag
-            "IEC_type": "WORD",  # WORD flag
+            "size": 8,           # BYTE flag
+            "IEC_type": "BYTE",  # BYTE flag
             "var_name": "var_name",
-            "location": "W" + ".".join([str(i) for i in current_location]) + ".0.1",
+            "location": "B" + ".".join([str(i) for i in current_location]) + ".0.1",
             "description": "Modbus request status flag",
             "children": []})        
+        entries.append({
+            "name": "Modbus Error Code",
+            "type": LOCATION_VAR_MEMORY,
+            "size": 8,           # BYTE flag
+            "IEC_type": "BYTE",  # BYTE flag
+            "var_name": "var_name",
+            "location": "B" + ".".join([str(i) for i in current_location]) + ".0.2",
+            "description": "Modbus Error Code received in Modbus error frame",
+            "children": []})        
         for offset in range(address, address + count):
             entries.append({
                 "name": dataname + " " + str(offset),
@@ -897,9 +906,13 @@
                                 if iecvar["LOC"][4] == 0:
                                     loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
                                     loc_vars_list.append(str(iecvar["NAME"]))
-                                # Add if it is a Communication Status Flag (mapped onto %QWa.b.c.0.1), so last number is a '1'
+                                # Add if it is a "Modbus Request Status flag" (mapped onto %QWa.b.c.0.1), so last number is a '1'
                                 if iecvar["LOC"][4] == 1:
-                                    loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_status;" % (client_requestid))
+                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_tn_error_code;" % (client_requestid))
+                                    loc_vars_list.append(str(iecvar["NAME"]))
+                                # Add if it is a "Modbus Error code" (mapped onto %QWa.b.c.0.2), so last number is a '2'
+                                if iecvar["LOC"][4] == 2:
+                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_mb_error_code;" % (client_requestid))
                                     loc_vars_list.append(str(iecvar["NAME"]))
                     client_requestid += 1
                 tcpclient_node_count += 1
@@ -947,9 +960,13 @@
                                 if iecvar["LOC"][4] == 0:
                                     loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
                                     loc_vars_list.append(str(iecvar["NAME"]))
-                                # Add if it is a Communication Status Flag (mapped onto %QWa.b.c.0.1), so last number is a '1'
+                                # Add if it is a "Modbus Request Status flag" (mapped onto %QWa.b.c.0.1), so last number is a '1'
                                 if iecvar["LOC"][4] == 1:
-                                    loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_status;" % (client_requestid))
+                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_tn_error_code;" % (client_requestid))
+                                    loc_vars_list.append(str(iecvar["NAME"]))
+                                # Add if it is a "Modbus Error code" (mapped onto %QWa.b.c.0.2), so last number is a '2'
+                                if iecvar["LOC"][4] == 2:
+                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_mb_error_code;" % (client_requestid))
                                     loc_vars_list.append(str(iecvar["NAME"]))
                     client_requestid += 1
                 rtuclient_node_count += 1