فهرست منبع

New function modbus_set_slave()

Stéphane Raimbault 15 سال پیش
والد
کامیت
237df10263
4فایلهای تغییر یافته به همراه16 افزوده شده و 5 حذف شده
  1. 3 2
      MIGRATION
  2. 7 0
      src/modbus.c
  3. 4 0
      src/modbus.h
  4. 2 3
      tests/unit-test-master.c

+ 3 - 2
MIGRATION

@@ -5,8 +5,9 @@ Migration notes from the 2.0 series (for 2.2)
 1 - modbus_init_rtu/tcp takes a new argument: the slave and it is only required
     in that function (eg. read_coil_status doesn't receive the slave ID in
     argument anymore). If you need to use different slaves with the same
-    connection (eg. RS485), you can copy modbus_param_t or set
-    modbus_param_t.slave to a different value when required.
+    connection (eg. RS485), you can copy modbus_param_t, use modbus_set_slave()
+    function or directly set modbus_param_t.slave to a different value when
+    required.
 
 2 - modbus_init_listen_tcp() has been renamed to modbus_slave_listen_tcp() and
     requires a new argument, the maximal number of connections:

+ 7 - 0
src/modbus.c

@@ -1407,6 +1407,13 @@ void modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port, int sla
         mb_param->slave = slave;
 }
 
+/* Define the slave number.
+   The special value MODBUS_BROADCAST_ADDRESS can be used. */
+void modbus_set_slave(modbus_param_t *mb_param, int slave)
+{
+        mb_param->slave = slave;
+}
+
 /* By default, the error handling mode used is FLUSH_OR_CONNECT_ON_ERROR.
 
    With FLUSH_OR_CONNECT_ON_ERROR, the library will attempt an immediate

+ 4 - 0
src/modbus.h

@@ -251,6 +251,10 @@ void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
 void modbus_init_tcp(modbus_param_t *mb_param, const char *ip_address, int port,
                      int slave);
 
+/* Define the slave number.
+   The special value MODBUS_BROADCAST_ADDRESS can be used. */
+void modbus_set_slave(modbus_param_t *mb_param, int slave);
+
 /* By default, the error handling mode used is CONNECT_ON_ERROR.
 
    With FLUSH_OR_CONNECT_ON_ERROR, the library will attempt an immediate

+ 2 - 3
tests/unit-test-master.c

@@ -420,8 +420,7 @@ int main(void)
 
         /** SLAVE REPLY **/
         printf("\nTEST SLAVE REPLY:\n");
-
-        mb_param.slave = 0x12;
+        modbus_set_slave(&mb_param, 18);
         ret = read_holding_registers(&mb_param,
                                      UT_HOLDING_REGISTERS_ADDRESS+1,
                                      UT_HOLDING_REGISTERS_NB_POINTS,
@@ -434,7 +433,7 @@ int main(void)
                 goto close;
         }
 
-        mb_param.slave = MODBUS_BROADCAST_ADDRESS;
+        modbus_set_slave(&mb_param, MODBUS_BROADCAST_ADDRESS);
         ret = read_holding_registers(&mb_param,
                                      UT_HOLDING_REGISTERS_ADDRESS,
                                      UT_HOLDING_REGISTERS_NB_POINTS,