Commit 237df10263ef733e6d8d78dbf627b264dd3d3b5d

Authored by Stéphane Raimbault
1 parent 62d8e2f6

New function modbus_set_slave()

MIGRATION
@@ -5,8 +5,9 @@ Migration notes from the 2.0 series (for 2.2) @@ -5,8 +5,9 @@ Migration notes from the 2.0 series (for 2.2)
5 1 - modbus_init_rtu/tcp takes a new argument: the slave and it is only required 5 1 - modbus_init_rtu/tcp takes a new argument: the slave and it is only required
6 in that function (eg. read_coil_status doesn't receive the slave ID in 6 in that function (eg. read_coil_status doesn't receive the slave ID in
7 argument anymore). If you need to use different slaves with the same 7 argument anymore). If you need to use different slaves with the same
8 - connection (eg. RS485), you can copy modbus_param_t or set  
9 - modbus_param_t.slave to a different value when required. 8 + connection (eg. RS485), you can copy modbus_param_t, use modbus_set_slave()
  9 + function or directly set modbus_param_t.slave to a different value when
  10 + required.
10 11
11 2 - modbus_init_listen_tcp() has been renamed to modbus_slave_listen_tcp() and 12 2 - modbus_init_listen_tcp() has been renamed to modbus_slave_listen_tcp() and
12 requires a new argument, the maximal number of connections: 13 requires a new argument, the maximal number of connections:
src/modbus.c
@@ -1407,6 +1407,13 @@ void modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port, int sla @@ -1407,6 +1407,13 @@ void modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port, int sla
1407 mb_param->slave = slave; 1407 mb_param->slave = slave;
1408 } 1408 }
1409 1409
  1410 +/* Define the slave number.
  1411 + The special value MODBUS_BROADCAST_ADDRESS can be used. */
  1412 +void modbus_set_slave(modbus_param_t *mb_param, int slave)
  1413 +{
  1414 + mb_param->slave = slave;
  1415 +}
  1416 +
1410 /* By default, the error handling mode used is FLUSH_OR_CONNECT_ON_ERROR. 1417 /* By default, the error handling mode used is FLUSH_OR_CONNECT_ON_ERROR.
1411 1418
1412 With FLUSH_OR_CONNECT_ON_ERROR, the library will attempt an immediate 1419 With FLUSH_OR_CONNECT_ON_ERROR, the library will attempt an immediate
src/modbus.h
@@ -251,6 +251,10 @@ void modbus_init_rtu(modbus_param_t *mb_param, const char *device, @@ -251,6 +251,10 @@ void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
251 void modbus_init_tcp(modbus_param_t *mb_param, const char *ip_address, int port, 251 void modbus_init_tcp(modbus_param_t *mb_param, const char *ip_address, int port,
252 int slave); 252 int slave);
253 253
  254 +/* Define the slave number.
  255 + The special value MODBUS_BROADCAST_ADDRESS can be used. */
  256 +void modbus_set_slave(modbus_param_t *mb_param, int slave);
  257 +
254 /* By default, the error handling mode used is CONNECT_ON_ERROR. 258 /* By default, the error handling mode used is CONNECT_ON_ERROR.
255 259
256 With FLUSH_OR_CONNECT_ON_ERROR, the library will attempt an immediate 260 With FLUSH_OR_CONNECT_ON_ERROR, the library will attempt an immediate
tests/unit-test-master.c
@@ -420,8 +420,7 @@ int main(void) @@ -420,8 +420,7 @@ int main(void)
420 420
421 /** SLAVE REPLY **/ 421 /** SLAVE REPLY **/
422 printf("\nTEST SLAVE REPLY:\n"); 422 printf("\nTEST SLAVE REPLY:\n");
423 -  
424 - mb_param.slave = 0x12; 423 + modbus_set_slave(&mb_param, 18);
425 ret = read_holding_registers(&mb_param, 424 ret = read_holding_registers(&mb_param,
426 UT_HOLDING_REGISTERS_ADDRESS+1, 425 UT_HOLDING_REGISTERS_ADDRESS+1,
427 UT_HOLDING_REGISTERS_NB_POINTS, 426 UT_HOLDING_REGISTERS_NB_POINTS,
@@ -434,7 +433,7 @@ int main(void) @@ -434,7 +433,7 @@ int main(void)
434 goto close; 433 goto close;
435 } 434 }
436 435
437 - mb_param.slave = MODBUS_BROADCAST_ADDRESS; 436 + modbus_set_slave(&mb_param, MODBUS_BROADCAST_ADDRESS);
438 ret = read_holding_registers(&mb_param, 437 ret = read_holding_registers(&mb_param,
439 UT_HOLDING_REGISTERS_ADDRESS, 438 UT_HOLDING_REGISTERS_ADDRESS,
440 UT_HOLDING_REGISTERS_NB_POINTS, 439 UT_HOLDING_REGISTERS_NB_POINTS,