Browse Source

Added slave ID check for response messages

Alex Stapleton 14 years ago
parent
commit
7ecfd4110a
2 changed files with 9 additions and 0 deletions
  1. 8 0
      src/modbus.c
  2. 1 0
      src/modbus.h

+ 8 - 0
src/modbus.c

@@ -80,6 +80,8 @@ const char *modbus_strerror(int errnum) {
         return "Invalid exception code";
     case EMBMDATA:
         return "Too many data";
+    case EMBBADSLAVE:
+        return "Response not from requested slave";
     default:
         return strerror(errnum);
     }
@@ -492,6 +494,12 @@ static int check_confirmation(modbus_t *ctx, uint8_t *req,
         }
     }
 
+    /* Check responding slave is the slave we requested */
+    if(req[0] != 0 && rsp[0] != req[0]) {
+        errno = EMBBADSLAVE;
+        return -1;
+    }
+
     rsp_length_computed = compute_response_length_from_request(ctx, req);
 
     /* Check length */

+ 1 - 0
src/modbus.h

@@ -117,6 +117,7 @@ enum {
 #define EMBBADEXC  (EMBXGTAR + 3)
 #define EMBUNKEXC  (EMBXGTAR + 4)
 #define EMBMDATA   (EMBXGTAR + 5)
+#define EMBBADSLAVE (EMBXGTAR + 6)
 
 extern const unsigned int libmodbus_version_major;
 extern const unsigned int libmodbus_version_minor;