Эх сурвалжийг харах

Remove the slave ID argument of modbus_new_rtu()

modbus_set_slave must be used to set the slave ID of the remote
device to talk in master mode and to set the internal slave ID in
slave mode.

If you talk to several devices, you need to call modbus_set_slave
each time the following requests must be sent to another device.
Stéphane Raimbault 14 жил өмнө
parent
commit
525aadb23a

+ 1 - 0
src/modbus-data.c

@@ -56,6 +56,7 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address,
     uint8_t value = 0;
 
     if (nb_bits > 8) {
+        /* Assert is ignored if NDEBUG is set */
         assert(nb_bits < 8);
         nb_bits = 8;
     }

+ 15 - 6
src/modbus-rtu.c

@@ -21,6 +21,7 @@
 #include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
+#include <assert.h>
 
 #include "modbus-private.h"
 
@@ -87,6 +88,8 @@ static const uint8_t table_crc_lo[] = {
     0x43, 0x83, 0x41, 0x81, 0x80, 0x40
 };
 
+/* Define the slave ID of the remote device to talk in master mode or set the
+ * internal slave ID in slave mode */
 static int _modbus_set_slave(modbus_t *ctx, int slave)
 {
     if (slave >= 1 && slave <= 247) {
@@ -104,6 +107,7 @@ static int _modbus_rtu_build_request_basis(modbus_t *ctx, int function,
                                            int addr, int nb,
                                            uint8_t *req)
 {
+    assert(ctx->slave != -1);
     req[0] = ctx->slave;
     req[1] = function;
     req[2] = addr >> 8;
@@ -117,6 +121,8 @@ static int _modbus_rtu_build_request_basis(modbus_t *ctx, int function,
 /* Builds a RTU response header */
 static int _modbus_rtu_build_response_basis(sft_t *sft, uint8_t *rsp)
 {
+    /* In this case, the slave is certainly valid because a check is already
+     * done in _modbus_rtu_listen */
     rsp[0] = sft->slave;
     rsp[1] = sft->function;
 
@@ -712,6 +718,14 @@ int _modbus_rtu_listen(modbus_t *ctx, int nb_connection)
         fprintf(stderr, "Not implemented");
     }
 
+    if (ctx->slave == -1) {
+        if (ctx->debug) {
+            fprintf(stderr, "The slave ID is not set (you must call modbus_set_slave() first)\n");
+        }
+        errno = EINVAL;
+        return -1;
+    }
+
     errno = EINVAL;
     return -1;
 }
@@ -832,23 +846,18 @@ const modbus_backend_t _modbus_rtu_backend = {
    - parity: 'N' stands for None, 'E' for Even and 'O' for odd
    - data_bits: 5, 6, 7, 8
    - stop_bits: 1, 2
-   - slave: slave number of the caller
 */
 modbus_t* modbus_new_rtu(const char *device,
                          int baud, char parity, int data_bit,
-                         int stop_bit, int slave)
+                         int stop_bit)
 {
     modbus_t *ctx;
     modbus_rtu_t *ctx_rtu;
 
     ctx = (modbus_t *) malloc(sizeof(modbus_t));
     _modbus_init_common(ctx);
-    if (_modbus_set_slave(ctx, slave) == -1) {
-        return NULL;
-    }
 
     ctx->backend = &_modbus_rtu_backend;
-
     ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t));
     ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
 #if defined(OpenBSD)

+ 1 - 1
src/modbus-rtu.h

@@ -26,6 +26,6 @@
 #define MODBUS_RTU_MAX_ADU_LENGTH  256
 
 modbus_t* modbus_new_rtu(const char *device, int baud, char parity,
-                         int data_bit, int stop_bit, int slave);
+                         int data_bit, int stop_bit);
 
 #endif /* _MODBUS_RTU_H_ */

+ 6 - 3
src/modbus.c

@@ -1225,14 +1225,17 @@ int modbus_report_slave_id(modbus_t *ctx, uint8_t *data_dest)
 
 void _modbus_init_common(modbus_t *ctx)
 {
+    /* Slave is initialized to -1 */
+    ctx->slave = -1;
+
+    ctx->debug = FALSE;
+    ctx->error_recovery = FALSE;
+
     ctx->timeout_begin.tv_sec = 0;
     ctx->timeout_begin.tv_usec = _TIME_OUT_BEGIN_OF_TRAME;
 
     ctx->timeout_end.tv_sec = 0;
     ctx->timeout_end.tv_usec = _TIME_OUT_END_OF_TRAME;
-
-    ctx->error_recovery = FALSE;
-    ctx->debug = FALSE;
 }
 
 /* Define the slave number */

+ 5 - 4
tests/random-test-client.c

@@ -37,7 +37,6 @@
    range defined by the following defines.
 */
 #define LOOP             1
-#define MY_ID            1
 #define SERVER_ID       17
 #define ADDRESS_START    0
 #define ADDRESS_END     99
@@ -59,14 +58,16 @@ int main(void)
     uint16_t *tab_rw_rq_registers;
     uint16_t *tab_rp_registers;
 
-    /*
-    ctx = modbus_new_rtu("/dev/ttyS0", 19200, 'N', 8, 1, MY_ID);
+    /* RTU */
+/*
+    ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
     modbus_set_slave(ctx, SERVER_ID);
-    */
+*/
 
     /* TCP */
     ctx = modbus_new_tcp("127.0.0.1", 1502);
     modbus_set_debug(ctx, TRUE);
+
     if (modbus_connect(ctx) == -1) {
         fprintf(stderr, "Connection failed: %s\n",
                 modbus_strerror(errno));

+ 1 - 1
tests/unit-test-client.c

@@ -41,7 +41,7 @@ int main(void)
     struct timeval timeout_begin_new;
 
     /*
-      ctx = modbus_new_rtu("/dev/ttyS0", 19200, 'N', 8, 1, CLIENT_ID);
+      ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
       modbus_set_slave(ctx, SERVER_ID);
       is_mode_rtu = TRUE;
     */

+ 0 - 1
tests/unit-test.h

@@ -25,7 +25,6 @@
 #include <stdint.h>
 #endif
 
-#define CLIENT_ID 15
 #define SERVER_ID 17
 
 const uint16_t UT_BITS_ADDRESS = 0x13;