diff --git a/MIGRATION b/MIGRATION index d25b543..8c285bb 100644 --- a/MIGRATION +++ b/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: diff --git a/src/modbus.c b/src/modbus.c index 43e0f02..4211f99 100644 --- a/src/modbus.c +++ b/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 diff --git a/src/modbus.h b/src/modbus.h index 78f5441..733c9d5 100644 --- a/src/modbus.h +++ b/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 diff --git a/tests/unit-test-master.c b/tests/unit-test-master.c index 95dfa53..7ac2f05 100644 --- a/tests/unit-test-master.c +++ b/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,