Commit 192f17312bfa22644b09514bd08198e49bfd2156

Authored by Stéphane Raimbault
1 parent 93ce1365

Restore slave ID (server ID) argument in functions

The ID used at init time will be the device ID of the caller and
the server ID in request functions is the target to reach.
MIGRATION
... ... @@ -2,12 +2,8 @@
2 2 Migration notes from the 2.0 series (for 2.2)
3 3 =============================================
4 4  
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
7   - argument anymore). If you need to use different slaves with the same
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.
  5 +1 - modbus_init_rtu/tcp takes a new argument, the slave number of the device
  6 + which established the connection.
11 7  
12 8 2 - modbus_init_listen_tcp() has been renamed to modbus_slave_listen_tcp() and
13 9 requires a new argument, the maximal number of connections:
... ... @@ -65,23 +61,3 @@ The holding and input registers are now stored in an array of type
65 61 uint16_t.
66 62  
67 63 These changes reduce the memory consumption.
68   -
69   -
70   -New functions
71   -=============
72   -
73   -report_slave_id
74   -
75   -modbus_set_error_handling
76   -modbus_mapping_new
77   -modbus_mapping_free
78   -modbus_init_listen_tcp
79   -modbus_listen
80   -modbus_manage_query
81   -
82   -get_slave_query_tcp
83   -set_bits_from_byte
84   -set_bits_from_bytes
85   -get_byte_from_bits
86   -
87   -Read modbus.h for more informations.
... ...
configure.ac
... ... @@ -19,7 +19,8 @@ m4_define([libmodbus_release_status],
19 19 [m4_if(m4_eval(libmodbus_version_minor % 2), [1], [snapshot],
20 20 [release])])
21 21  
22   -m4_define([libmodbus_version], [libmodbus_version_major.libmodbus_version_minor.libmodbus_version_micro])
  22 +m4_define([libmodbus_version],
  23 + [libmodbus_version_major.libmodbus_version_minor.libmodbus_version_micro])
23 24  
24 25 AC_PREREQ(2.63)
25 26 AC_INIT([libmodbus],[libmodbus_version],[stephane.raimbault@gmail.com])
... ... @@ -39,7 +40,7 @@ AC_SUBST(LIBMODBUS_VERSION_MICRO)
39 40 AC_SUBST(LIBMODBUS_VERSION)
40 41  
41 42 # ABI version
42   -LIBMODBUS_LD_CURRENT=3
  43 +LIBMODBUS_LD_CURRENT=4
43 44 LIBMODBUS_LD_REVISION=0
44 45 LIBMODBUS_LD_AGE=0
45 46 LIBMODBUS_LT_LDFLAGS="-version-info $LIBMODBUS_LD_CURRENT:$LIBMODBUS_LD_REVISION:$LIBMODBUS_LD_AGE"
... ...
src/modbus.c
... ... @@ -324,15 +324,15 @@ static int build_query_basis_tcp(int slave, int function,
324 324 return PRESET_QUERY_LENGTH_TCP;
325 325 }
326 326  
327   -static int build_query_basis(modbus_param_t *mb_param,
  327 +static int build_query_basis(modbus_param_t *mb_param, int slave,
328 328 int function, int start_addr,
329 329 int nb, uint8_t *query)
330 330 {
331 331 if (mb_param->type_com == RTU)
332   - return build_query_basis_rtu(mb_param->slave, function,
  332 + return build_query_basis_rtu(slave, function,
333 333 start_addr, nb, query);
334 334 else
335   - return build_query_basis_tcp(mb_param->slave, function,
  335 + return build_query_basis_tcp(slave, function,
336 336 start_addr, nb, query);
337 337 }
338 338  
... ... @@ -1114,7 +1114,7 @@ int modbus_slave_manage(modbus_param_t *mb_param, const uint8_t *query,
1114 1114 }
1115 1115  
1116 1116 /* Reads IO status */
1117   -static int read_io_status(modbus_param_t *mb_param, int function,
  1117 +static int read_io_status(modbus_param_t *mb_param, int slave, int function,
1118 1118 int start_addr, int nb, uint8_t *data_dest)
1119 1119 {
1120 1120 int rc;
... ... @@ -1123,7 +1123,7 @@ static int read_io_status(modbus_param_t *mb_param, int function,
1123 1123 uint8_t query[MIN_QUERY_LENGTH];
1124 1124 uint8_t response[MAX_MESSAGE_LENGTH];
1125 1125  
1126   - query_length = build_query_basis(mb_param, function,
  1126 + query_length = build_query_basis(mb_param, slave, function,
1127 1127 start_addr, nb, query);
1128 1128  
1129 1129 rc = modbus_send(mb_param, query, query_length);
... ... @@ -1156,7 +1156,7 @@ static int read_io_status(modbus_param_t *mb_param, int function,
1156 1156  
1157 1157 /* Reads the boolean status of coils and sets the array elements
1158 1158 in the destination to TRUE or FALSE. */
1159   -int read_coil_status(modbus_param_t *mb_param, int start_addr,
  1159 +int read_coil_status(modbus_param_t *mb_param, int slave, int start_addr,
1160 1160 int nb, uint8_t *data_dest)
1161 1161 {
1162 1162 int rc;
... ... @@ -1171,8 +1171,8 @@ int read_coil_status(modbus_param_t *mb_param, int start_addr,
1171 1171 return -1;
1172 1172 }
1173 1173  
1174   - rc = read_io_status(mb_param, FC_READ_COIL_STATUS, start_addr, nb,
1175   - data_dest);
  1174 + rc = read_io_status(mb_param, slave, FC_READ_COIL_STATUS, start_addr,
  1175 + nb, data_dest);
1176 1176  
1177 1177 if (rc == -1)
1178 1178 return -1;
... ... @@ -1182,7 +1182,7 @@ int read_coil_status(modbus_param_t *mb_param, int start_addr,
1182 1182  
1183 1183  
1184 1184 /* Same as read_coil_status but reads the slaves input table */
1185   -int read_input_status(modbus_param_t *mb_param, int start_addr,
  1185 +int read_input_status(modbus_param_t *mb_param, int slave, int start_addr,
1186 1186 int nb, uint8_t *data_dest)
1187 1187 {
1188 1188 int rc;
... ... @@ -1197,7 +1197,7 @@ int read_input_status(modbus_param_t *mb_param, int start_addr,
1197 1197 return -1;
1198 1198 }
1199 1199  
1200   - rc = read_io_status(mb_param, FC_READ_INPUT_STATUS, start_addr,
  1200 + rc = read_io_status(mb_param, slave, FC_READ_INPUT_STATUS, start_addr,
1201 1201 nb, data_dest);
1202 1202  
1203 1203 if (rc == -1)
... ... @@ -1207,7 +1207,7 @@ int read_input_status(modbus_param_t *mb_param, int start_addr,
1207 1207 }
1208 1208  
1209 1209 /* Reads the data from a modbus slave and put that data into an array */
1210   -static int read_registers(modbus_param_t *mb_param, int function,
  1210 +static int read_registers(modbus_param_t *mb_param, int slave, int function,
1211 1211 int start_addr, int nb, uint16_t *data_dest)
1212 1212 {
1213 1213 int rc;
... ... @@ -1225,7 +1225,7 @@ static int read_registers(modbus_param_t *mb_param, int function,
1225 1225 return -1;
1226 1226 }
1227 1227  
1228   - query_length = build_query_basis(mb_param, function,
  1228 + query_length = build_query_basis(mb_param, slave, function,
1229 1229 start_addr, nb, query);
1230 1230  
1231 1231 rc = modbus_send(mb_param, query, query_length);
... ... @@ -1250,7 +1250,7 @@ static int read_registers(modbus_param_t *mb_param, int function,
1250 1250  
1251 1251 /* Reads the holding registers in a slave and put the data into an
1252 1252 array */
1253   -int read_holding_registers(modbus_param_t *mb_param,
  1253 +int read_holding_registers(modbus_param_t *mb_param, int slave,
1254 1254 int start_addr, int nb, uint16_t *data_dest)
1255 1255 {
1256 1256 int status;
... ... @@ -1265,15 +1265,15 @@ int read_holding_registers(modbus_param_t *mb_param,
1265 1265 return -1;
1266 1266 }
1267 1267  
1268   - status = read_registers(mb_param, FC_READ_HOLDING_REGISTERS,
  1268 + status = read_registers(mb_param, slave, FC_READ_HOLDING_REGISTERS,
1269 1269 start_addr, nb, data_dest);
1270 1270 return status;
1271 1271 }
1272 1272  
1273 1273 /* Reads the input registers in a slave and put the data into
1274 1274 an array */
1275   -int read_input_registers(modbus_param_t *mb_param, int start_addr, int nb,
1276   - uint16_t *data_dest)
  1275 +int read_input_registers(modbus_param_t *mb_param, int slave, int start_addr,
  1276 + int nb, uint16_t *data_dest)
1277 1277 {
1278 1278 int status;
1279 1279  
... ... @@ -1285,7 +1285,7 @@ int read_input_registers(modbus_param_t *mb_param, int start_addr, int nb,
1285 1285 return -1;
1286 1286 }
1287 1287  
1288   - status = read_registers(mb_param, FC_READ_INPUT_REGISTERS,
  1288 + status = read_registers(mb_param, slave, FC_READ_INPUT_REGISTERS,
1289 1289 start_addr, nb, data_dest);
1290 1290  
1291 1291 return status;
... ... @@ -1293,14 +1293,14 @@ int read_input_registers(modbus_param_t *mb_param, int start_addr, int nb,
1293 1293  
1294 1294 /* Sends a value to a register in a slave.
1295 1295 Used by force_single_coil and preset_single_register */
1296   -static int set_single(modbus_param_t *mb_param, int function,
  1296 +static int set_single(modbus_param_t *mb_param, int slave, int function,
1297 1297 int addr, int value)
1298 1298 {
1299 1299 int rc;
1300 1300 int query_length;
1301 1301 uint8_t query[MIN_QUERY_LENGTH];
1302 1302  
1303   - query_length = build_query_basis(mb_param, function,
  1303 + query_length = build_query_basis(mb_param, slave, function,
1304 1304 addr, value, query);
1305 1305  
1306 1306 rc = modbus_send(mb_param, query, query_length);
... ... @@ -1315,33 +1315,34 @@ static int set_single(modbus_param_t *mb_param, int function,
1315 1315 }
1316 1316  
1317 1317 /* Turns ON or OFF a single coil in the slave device */
1318   -int force_single_coil(modbus_param_t *mb_param, int coil_addr, int state)
  1318 +int force_single_coil(modbus_param_t *mb_param, int slave, int coil_addr, int state)
1319 1319 {
1320 1320 int status;
1321 1321  
1322 1322 if (state)
1323 1323 state = 0xFF00;
1324 1324  
1325   - status = set_single(mb_param, FC_FORCE_SINGLE_COIL,
  1325 + status = set_single(mb_param, slave, FC_FORCE_SINGLE_COIL,
1326 1326 coil_addr, state);
1327 1327  
1328 1328 return status;
1329 1329 }
1330 1330  
1331 1331 /* Sets a value in one holding register in the slave device */
1332   -int preset_single_register(modbus_param_t *mb_param, int reg_addr, int value)
  1332 +int preset_single_register(modbus_param_t *mb_param, int slave, int reg_addr,
  1333 + int value)
1333 1334 {
1334 1335 int status;
1335 1336  
1336   - status = set_single(mb_param, FC_PRESET_SINGLE_REGISTER,
  1337 + status = set_single(mb_param, slave, FC_PRESET_SINGLE_REGISTER,
1337 1338 reg_addr, value);
1338 1339  
1339 1340 return status;
1340 1341 }
1341 1342  
1342 1343 /* Sets/resets the coils in the slave from an array in argument */
1343   -int force_multiple_coils(modbus_param_t *mb_param, int start_addr, int nb,
1344   - const uint8_t *data_src)
  1344 +int force_multiple_coils(modbus_param_t *mb_param, int slave, int start_addr,
  1345 + int nb, const uint8_t *data_src)
1345 1346 {
1346 1347 int rc;
1347 1348 int i;
... ... @@ -1361,7 +1362,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int start_addr, int nb,
1361 1362 return -1;
1362 1363 }
1363 1364  
1364   - query_length = build_query_basis(mb_param, FC_FORCE_MULTIPLE_COILS,
  1365 + query_length = build_query_basis(mb_param, slave, FC_FORCE_MULTIPLE_COILS,
1365 1366 start_addr, nb, query);
1366 1367 byte_count = (nb / 8) + ((nb % 8) ? 1 : 0);
1367 1368 query[query_length++] = byte_count;
... ... @@ -1394,8 +1395,8 @@ int force_multiple_coils(modbus_param_t *mb_param, int start_addr, int nb,
1394 1395 }
1395 1396  
1396 1397 /* Copies the values in the slave from the array given in argument */
1397   -int preset_multiple_registers(modbus_param_t *mb_param, int start_addr, int nb,
1398   - const uint16_t *data_src)
  1398 +int preset_multiple_registers(modbus_param_t *mb_param, int slave, int start_addr,
  1399 + int nb, const uint16_t *data_src)
1399 1400 {
1400 1401 int rc;
1401 1402 int i;
... ... @@ -1414,7 +1415,7 @@ int preset_multiple_registers(modbus_param_t *mb_param, int start_addr, int nb,
1414 1415 return -1;
1415 1416 }
1416 1417  
1417   - query_length = build_query_basis(mb_param, FC_PRESET_MULTIPLE_REGISTERS,
  1418 + query_length = build_query_basis(mb_param, slave, FC_PRESET_MULTIPLE_REGISTERS,
1418 1419 start_addr, nb, query);
1419 1420 byte_count = nb * 2;
1420 1421 query[query_length++] = byte_count;
... ... @@ -1433,14 +1434,15 @@ int preset_multiple_registers(modbus_param_t *mb_param, int start_addr, int nb,
1433 1434 return rc;
1434 1435 }
1435 1436  
1436   -/* Returns the slave id! */
1437   -int report_slave_id(modbus_param_t *mb_param, uint8_t *data_dest)
  1437 +/* Request the slave ID */
  1438 +int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *data_dest)
1438 1439 {
1439 1440 int rc;
1440 1441 int query_length;
1441 1442 uint8_t query[MIN_QUERY_LENGTH];
1442 1443  
1443   - query_length = build_query_basis(mb_param, FC_REPORT_SLAVE_ID, 0, 0, query);
  1444 + query_length = build_query_basis(mb_param, slave,
  1445 + FC_REPORT_SLAVE_ID, 0, 0, query);
1444 1446  
1445 1447 /* HACKISH, start_addr and count are not used */
1446 1448 query_length -= 4;
... ... @@ -1474,6 +1476,7 @@ int report_slave_id(modbus_param_t *mb_param, uint8_t *data_dest)
1474 1476 - parity: "even", "odd" or "none"
1475 1477 - data_bits: 5, 6, 7, 8
1476 1478 - stop_bits: 1, 2
  1479 + - slave: slave number
1477 1480 */
1478 1481 void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
1479 1482 int baud, const char *parity, int data_bit,
... ... @@ -1510,8 +1513,7 @@ void modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port, int sla
1510 1513 mb_param->slave = slave;
1511 1514 }
1512 1515  
1513   -/* Define the slave number.
1514   - The special value MODBUS_BROADCAST_ADDRESS can be used. */
  1516 +/* Define the slave number */
1515 1517 void modbus_set_slave(modbus_param_t *mb_param, int slave)
1516 1518 {
1517 1519 mb_param->slave = slave;
... ... @@ -1519,7 +1521,7 @@ void modbus_set_slave(modbus_param_t *mb_param, int slave)
1519 1521  
1520 1522 /*
1521 1523 When disabled (default), it is expected that the application will check for error
1522   - returns and deal with them as necessary.
  1524 + returns and deal with them as necessary.
1523 1525  
1524 1526 It's not recommanded to enable error recovery for slave/server.
1525 1527  
... ...
src/modbus.h.in
... ... @@ -263,22 +263,23 @@ void modbus_set_debug(modbus_param_t *mb_param, int boolean);
263 263  
264 264 const char *modbus_strerror(int errnum);
265 265  
266   -int read_coil_status(modbus_param_t *mb_param, int start_addr, int nb,
267   - uint8_t *dest);
268   -int read_input_status(modbus_param_t *mb_param, int start_addr, int nb,
269   - uint8_t *dest);
270   -int read_holding_registers(modbus_param_t *mb_param, int start_addr, int nb,
271   - uint16_t *dest);
272   -int read_input_registers(modbus_param_t *mb_param, int start_addr, int nb,
273   - uint16_t *dest);
274   -int force_single_coil(modbus_param_t *mb_param, int coil_addr, int state);
275   -
276   -int preset_single_register(modbus_param_t *mb_param, int reg_addr, int value);
277   -int force_multiple_coils(modbus_param_t *mb_param, int start_addr, int nb,
278   - const uint8_t *data);
279   -int preset_multiple_registers(modbus_param_t *mb_param, int start_addr, int nb,
280   - const uint16_t *data);
281   -int report_slave_id(modbus_param_t *mb_param, uint8_t *dest);
  266 +int read_coil_status(modbus_param_t *mb_param, int slave, int start_addr,
  267 + int nb, uint8_t *dest);
  268 +int read_input_status(modbus_param_t *mb_param, int slave, int start_addr,
  269 + int nb, uint8_t *dest);
  270 +int read_holding_registers(modbus_param_t *mb_param, int slave, int start_addr,
  271 + int nb, uint16_t *dest);
  272 +int read_input_registers(modbus_param_t *mb_param, int slave, int start_addr,
  273 + int nb, uint16_t *dest);
  274 +int force_single_coil(modbus_param_t *mb_param, int slave, int coil_addr,
  275 + int state);
  276 +int preset_single_register(modbus_param_t *mb_param, int slave, int reg_addr,
  277 + int value);
  278 +int force_multiple_coils(modbus_param_t *mb_param, int slave, int start_addr,
  279 + int nb, const uint8_t *data);
  280 +int preset_multiple_registers(modbus_param_t *mb_param, int slave, int start_addr,
  281 + int nb, const uint16_t *data);
  282 +int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
282 283  
283 284 int modbus_mapping_new(modbus_mapping_t *mb_mapping,
284 285 int nb_coil_status, int nb_input_status,
... ...
tests/bandwidth-master.c
... ... @@ -26,7 +26,8 @@
26 26 #include <modbus.h>
27 27  
28 28 /* Tests based on PI-MBUS-300 documentation */
29   -#define SLAVE 0x11
  29 +#define MY_ID 1
  30 +#define SERVER_ID 17
30 31 #define NB_LOOPS 100000
31 32  
32 33 #define G_MSEC_PER_SEC 1000
... ... @@ -54,7 +55,7 @@ int main(void)
54 55 int rc;
55 56  
56 57 /* TCP */
57   - modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  58 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502, MY_ID);
58 59 rc = modbus_connect(&mb_param);
59 60 if (rc == -1) {
60 61 fprintf(stderr, "Connexion failed: %s\n",
... ... @@ -75,9 +76,9 @@ int main(void)
75 76 nb_points = MAX_STATUS;
76 77 start = gettime_ms();
77 78 for (i=0; i<NB_LOOPS; i++) {
78   - rc = read_coil_status(&mb_param, 0, nb_points, tab_rp_status);
  79 + rc = read_coil_status(&mb_param, SERVER_ID, 0, nb_points, tab_rp_status);
79 80 if (rc == -1) {
80   - fprintf(stderr, modbus_strerror(errno));
  81 + fprintf(stderr, "%s\n", modbus_strerror(errno));
81 82 return -1;
82 83 }
83 84 }
... ... @@ -112,9 +113,9 @@ int main(void)
112 113 nb_points = MAX_REGISTERS;
113 114 start = gettime_ms();
114 115 for (i=0; i<NB_LOOPS; i++) {
115   - rc = read_holding_registers(&mb_param, 0, nb_points, tab_rp_registers);
  116 + rc = read_holding_registers(&mb_param, SERVER_ID, 0, nb_points, tab_rp_registers);
116 117 if (rc == -1) {
117   - fprintf(stderr, modbus_strerror(errno));
  118 + fprintf(stderr, "%s\n", modbus_strerror(errno));
118 119 return -1;
119 120 }
120 121 }
... ...
tests/bandwidth-slave-many-up.c
... ... @@ -24,7 +24,6 @@
24 24  
25 25 #include <modbus.h>
26 26  
27   -#define SLAVE 0x11
28 27 #define NB_CONNECTION 5
29 28  
30 29 int slave_socket;
... ... @@ -49,9 +48,9 @@ int main(void)
49 48 /* Maximum file descriptor number */
50 49 int fdmax;
51 50  
52   - modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  51 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502, 17);
53 52  
54   - rc = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
  53 + rc = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
55 54 if (rc == -1) {
56 55 fprintf(stderr, "Failed to allocate the mapping: %s\n",
57 56 modbus_strerror(errno));
... ...
tests/bandwidth-slave-one.c
... ... @@ -23,8 +23,6 @@
23 23  
24 24 #include <modbus.h>
25 25  
26   -#define SLAVE 0x11
27   -
28 26 int main(void)
29 27 {
30 28 int socket;
... ... @@ -32,7 +30,7 @@ int main(void)
32 30 modbus_mapping_t mb_mapping;
33 31 int rc;
34 32  
35   - modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  33 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502, 17);
36 34  
37 35 rc = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
38 36 if (rc == -1) {
... ...
tests/random-test-master.c
... ... @@ -37,9 +37,10 @@
37 37 range defined by the following defines.
38 38 */
39 39 #define LOOP 1
40   -#define SLAVE 0x11
  40 +#define MY_ID 1
  41 +#define SERVER_ID 17
41 42 #define ADDRESS_START 0
42   -#define ADDRESS_END 99
  43 +#define ADDRESS_END 99
43 44  
44 45 /* At each loop, the program works in the range ADDRESS_START to
45 46 * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
... ... @@ -58,10 +59,12 @@ int main(void)
58 59 modbus_param_t mb_param;
59 60  
60 61 /* RTU parity : none, even, odd */
61   - /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
  62 + /*
  63 + modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1, MY_ID);
  64 + */
62 65  
63 66 /* TCP */
64   - modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  67 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502, MY_ID);
65 68 modbus_set_debug(&mb_param, TRUE);
66 69 if (modbus_connect(&mb_param) == -1) {
67 70 fprintf(stderr, "Connection failed: %s\n",
... ... @@ -97,42 +100,42 @@ int main(void)
97 100 nb = ADDRESS_END - addr;
98 101  
99 102 /* SINGLE COIL */
100   - rc = force_single_coil(&mb_param, addr, tab_rq_status[0]);
  103 + rc = force_single_coil(&mb_param, SERVER_ID, addr, tab_rq_status[0]);
101 104 if (rc != 1) {
102 105 printf("ERROR force_single_coil (%d)\n", rc);
103 106 printf("Slave = %d, address = %d, value = %d\n",
104   - SLAVE, addr, tab_rq_status[0]);
  107 + SERVER_ID, addr, tab_rq_status[0]);
105 108 nb_fail++;
106 109 } else {
107   - rc = read_coil_status(&mb_param, addr, 1, tab_rp_status);
  110 + rc = read_coil_status(&mb_param, SERVER_ID, addr, 1, tab_rp_status);
108 111 if (rc != 1 || tab_rq_status[0] != tab_rp_status[0]) {
109 112 printf("ERROR read_coil_status single (%d)\n", rc);
110 113 printf("Slave = %d, address = %d\n",
111   - SLAVE, addr);
  114 + SERVER_ID, addr);
112 115 nb_fail++;
113 116 }
114 117 }
115 118  
116 119 /* MULTIPLE COILS */
117   - rc = force_multiple_coils(&mb_param, addr, nb, tab_rq_status);
  120 + rc = force_multiple_coils(&mb_param, SERVER_ID, addr, nb, tab_rq_status);
118 121 if (rc != nb) {
119 122 printf("ERROR force_multiple_coils (%d)\n", rc);
120 123 printf("Slave = %d, address = %d, nb = %d\n",
121   - SLAVE, addr, nb);
  124 + SERVER_ID, addr, nb);
122 125 nb_fail++;
123 126 } else {
124   - rc = read_coil_status(&mb_param, addr, nb, tab_rp_status);
  127 + rc = read_coil_status(&mb_param, SERVER_ID, addr, nb, tab_rp_status);
125 128 if (rc != nb) {
126 129 printf("ERROR read_coil_status\n");
127 130 printf("Slave = %d, address = %d, nb = %d\n",
128   - SLAVE, addr, nb);
  131 + SERVER_ID, addr, nb);
129 132 nb_fail++;
130 133 } else {
131 134 for (i=0; i<nb; i++) {
132 135 if (tab_rp_status[i] != tab_rq_status[i]) {
133 136 printf("ERROR read_coil_status\n");
134 137 printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
135   - SLAVE, addr,
  138 + SERVER_ID, addr,
136 139 tab_rq_status[i], tab_rq_status[i],
137 140 tab_rp_status[i], tab_rp_status[i]);
138 141 nb_fail++;
... ... @@ -142,24 +145,24 @@ int main(void)
142 145 }
143 146  
144 147 /* SINGLE REGISTER */
145   - rc = preset_single_register(&mb_param, addr, tab_rq_registers[0]);
  148 + rc = preset_single_register(&mb_param, SERVER_ID, addr, tab_rq_registers[0]);
146 149 if (rc != 1) {
147 150 printf("ERROR preset_single_register (%d)\n", rc);
148 151 printf("Slave = %d, address = %d, value = %d (0x%X)\n",
149   - SLAVE, addr, tab_rq_registers[0], tab_rq_registers[0]);
  152 + SERVER_ID, addr, tab_rq_registers[0], tab_rq_registers[0]);
150 153 nb_fail++;
151 154 } else {
152   - rc = read_holding_registers(&mb_param, addr, 1, tab_rp_registers);
  155 + rc = read_holding_registers(&mb_param, SERVER_ID, addr, 1, tab_rp_registers);
153 156 if (rc != 1) {
154 157 printf("ERROR read_holding_registers single (%d)\n", rc);
155 158 printf("Slave = %d, address = %d\n",
156   - SLAVE, addr);
  159 + SERVER_ID, addr);
157 160 nb_fail++;
158 161 } else {
159 162 if (tab_rq_registers[0] != tab_rp_registers[0]) {
160 163 printf("ERROR read_holding_registers single\n");
161 164 printf("Slave = %d, address = %d, value = %d (0x%X) != %d (0x%X)\n",
162   - SLAVE, addr,
  165 + SERVER_ID, addr,
163 166 tab_rq_registers[0], tab_rq_registers[0],
164 167 tab_rp_registers[0], tab_rp_registers[0]);
165 168 nb_fail++;
... ... @@ -168,27 +171,27 @@ int main(void)
168 171 }
169 172  
170 173 /* MULTIPLE REGISTERS */
171   - rc = preset_multiple_registers(&mb_param, addr, nb,
  174 + rc = preset_multiple_registers(&mb_param, SERVER_ID, addr, nb,
172 175 tab_rq_registers);
173 176 if (rc != nb) {
174 177 printf("ERROR preset_multiple_registers (%d)\n", rc);
175 178 printf("Slave = %d, address = %d, nb = %d\n",
176   - SLAVE, addr, nb);
  179 + SERVER_ID, addr, nb);
177 180 nb_fail++;
178 181 } else {
179   - rc = read_holding_registers(&mb_param, addr, nb,
  182 + rc = read_holding_registers(&mb_param, SERVER_ID, addr, nb,
180 183 tab_rp_registers);
181 184 if (rc != nb) {
182 185 printf("ERROR read_holding_registers (%d)\n", rc);
183 186 printf("Slave = %d, address = %d, nb = %d\n",
184   - SLAVE, addr, nb);
  187 + SERVER_ID, addr, nb);
185 188 nb_fail++;
186 189 } else {
187 190 for (i=0; i<nb; i++) {
188 191 if (tab_rq_registers[i] != tab_rp_registers[i]) {
189 192 printf("ERROR read_holding_registers\n");
190 193 printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
191   - SLAVE, addr,
  194 + SERVER_ID, addr,
192 195 tab_rq_registers[i], tab_rq_registers[i],
193 196 tab_rp_registers[i], tab_rp_registers[i]);
194 197 nb_fail++;
... ...
tests/random-test-slave.c
... ... @@ -22,8 +22,6 @@
22 22  
23 23 #include <modbus.h>
24 24  
25   -#define SLAVE 0x11
26   -
27 25 int main(void)
28 26 {
29 27 int socket;
... ... @@ -31,7 +29,7 @@ int main(void)
31 29 modbus_mapping_t mb_mapping;
32 30 int rc;
33 31  
34   - modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  32 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502, 17);
35 33 /* modbus_set_debug(&mb_param, TRUE); */
36 34  
37 35 rc = modbus_mapping_new(&mb_mapping, 500, 500, 500, 500);
... ...
tests/unit-test-master.c
... ... @@ -20,12 +20,9 @@
20 20 #include <string.h>
21 21 #include <stdlib.h>
22 22 #include <errno.h>
23   -
24 23 #include <modbus.h>
25   -#include "unit-test.h"
26 24  
27   -/* Tests based on PI-MBUS-300 documentation */
28   -#define SLAVE 0x11
  25 +#include "unit-test.h"
29 26  
30 27 int main(void)
31 28 {
... ... @@ -41,10 +38,13 @@ int main(void)
41 38 float real;
42 39  
43 40 /* RTU parity : none, even, odd */
44   -/* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1, SLAVE); */
  41 + /*
  42 + modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1,
  43 + CLIENT_ID);
  44 + */
45 45  
46 46 /* TCP */
47   - modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  47 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502, CLIENT_ID);
48 48 modbus_set_debug(&mb_param, TRUE);
49 49  
50 50 if (modbus_connect(&mb_param) == -1) {
... ... @@ -73,7 +73,7 @@ int main(void)
73 73 /** COIL STATUS **/
74 74  
75 75 /* Single */
76   - rc = force_single_coil(&mb_param, UT_COIL_STATUS_ADDRESS, ON);
  76 + rc = force_single_coil(&mb_param, SERVER_ID, UT_COIL_STATUS_ADDRESS, ON);
77 77 printf("1/2 force_single_coil: ");
78 78 if (rc == 1) {
79 79 printf("OK\n");
... ... @@ -82,7 +82,7 @@ int main(void)
82 82 goto close;
83 83 }
84 84  
85   - rc = read_coil_status(&mb_param, UT_COIL_STATUS_ADDRESS, 1,
  85 + rc = read_coil_status(&mb_param, SERVER_ID, UT_COIL_STATUS_ADDRESS, 1,
86 86 tab_rp_status);
87 87 printf("2/2 read_coil_status: ");
88 88 if (rc != 1) {
... ... @@ -103,7 +103,7 @@ int main(void)
103 103  
104 104 set_bits_from_bytes(tab_value, 0, UT_COIL_STATUS_NB_POINTS,
105 105 UT_COIL_STATUS_TAB);
106   - rc = force_multiple_coils(&mb_param,
  106 + rc = force_multiple_coils(&mb_param, SERVER_ID,
107 107 UT_COIL_STATUS_ADDRESS,
108 108 UT_COIL_STATUS_NB_POINTS,
109 109 tab_value);
... ... @@ -116,7 +116,7 @@ int main(void)
116 116 }
117 117 }
118 118  
119   - rc = read_coil_status(&mb_param, UT_COIL_STATUS_ADDRESS,
  119 + rc = read_coil_status(&mb_param, SERVER_ID, UT_COIL_STATUS_ADDRESS,
120 120 UT_COIL_STATUS_NB_POINTS, tab_rp_status);
121 121 printf("2/2 read_coil_status: ");
122 122 if (rc != UT_COIL_STATUS_NB_POINTS) {
... ... @@ -144,7 +144,7 @@ int main(void)
144 144 /* End of multiple coils */
145 145  
146 146 /** INPUT STATUS **/
147   - rc = read_input_status(&mb_param, UT_INPUT_STATUS_ADDRESS,
  147 + rc = read_input_status(&mb_param, SERVER_ID, UT_INPUT_STATUS_ADDRESS,
148 148 UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
149 149 printf("1/1 read_input_status: ");
150 150  
... ... @@ -174,7 +174,7 @@ int main(void)
174 174 /** HOLDING REGISTERS **/
175 175  
176 176 /* Single register */
177   - rc = preset_single_register(&mb_param,
  177 + rc = preset_single_register(&mb_param, SERVER_ID,
178 178 UT_HOLDING_REGISTERS_ADDRESS, 0x1234);
179 179 printf("1/2 preset_single_register: ");
180 180 if (rc == 1) {
... ... @@ -184,7 +184,7 @@ int main(void)
184 184 goto close;
185 185 }
186 186  
187   - rc = read_holding_registers(&mb_param,
  187 + rc = read_holding_registers(&mb_param, SERVER_ID,
188 188 UT_HOLDING_REGISTERS_ADDRESS,
189 189 1, tab_rp_registers);
190 190 printf("2/2 read_holding_registers: ");
... ... @@ -202,7 +202,7 @@ int main(void)
202 202 /* End of single register */
203 203  
204 204 /* Many registers */
205   - rc = preset_multiple_registers(&mb_param,
  205 + rc = preset_multiple_registers(&mb_param, SERVER_ID,
206 206 UT_HOLDING_REGISTERS_ADDRESS,
207 207 UT_HOLDING_REGISTERS_NB_POINTS,
208 208 UT_HOLDING_REGISTERS_TAB);
... ... @@ -214,7 +214,7 @@ int main(void)
214 214 goto close;
215 215 }
216 216  
217   - rc = read_holding_registers(&mb_param,
  217 + rc = read_holding_registers(&mb_param, SERVER_ID,
218 218 UT_HOLDING_REGISTERS_ADDRESS,
219 219 UT_HOLDING_REGISTERS_NB_POINTS,
220 220 tab_rp_registers);
... ... @@ -237,7 +237,7 @@ int main(void)
237 237  
238 238  
239 239 /** INPUT REGISTERS **/
240   - rc = read_input_registers(&mb_param,
  240 + rc = read_input_registers(&mb_param, SERVER_ID,
241 241 UT_INPUT_REGISTERS_ADDRESS,
242 242 UT_INPUT_REGISTERS_NB_POINTS,
243 243 tab_rp_registers);
... ... @@ -287,7 +287,7 @@ int main(void)
287 287 /* The mapping begins at 0 and ending at address + nb_points so
288 288 * the addresses below are not valid. */
289 289  
290   - rc = read_coil_status(&mb_param,
  290 + rc = read_coil_status(&mb_param, SERVER_ID,
291 291 UT_COIL_STATUS_ADDRESS,
292 292 UT_COIL_STATUS_NB_POINTS + 1,
293 293 tab_rp_status);
... ... @@ -299,7 +299,7 @@ int main(void)
299 299 goto close;
300 300 }
301 301  
302   - rc = read_input_status(&mb_param,
  302 + rc = read_input_status(&mb_param, SERVER_ID,
303 303 UT_INPUT_STATUS_ADDRESS,
304 304 UT_INPUT_STATUS_NB_POINTS + 1,
305 305 tab_rp_status);
... ... @@ -311,7 +311,7 @@ int main(void)
311 311 goto close;
312 312 }
313 313  
314   - rc = read_holding_registers(&mb_param,
  314 + rc = read_holding_registers(&mb_param, SERVER_ID,
315 315 UT_HOLDING_REGISTERS_ADDRESS,
316 316 UT_HOLDING_REGISTERS_NB_POINTS + 1,
317 317 tab_rp_registers);
... ... @@ -323,7 +323,7 @@ int main(void)
323 323 goto close;
324 324 }
325 325  
326   - rc = read_input_registers(&mb_param,
  326 + rc = read_input_registers(&mb_param, SERVER_ID,
327 327 UT_INPUT_REGISTERS_ADDRESS,
328 328 UT_INPUT_REGISTERS_NB_POINTS + 1,
329 329 tab_rp_registers);
... ... @@ -335,7 +335,7 @@ int main(void)
335 335 goto close;
336 336 }
337 337  
338   - rc = force_single_coil(&mb_param,
  338 + rc = force_single_coil(&mb_param, SERVER_ID,
339 339 UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
340 340 ON);
341 341 printf("* force_single_coil: ");
... ... @@ -346,7 +346,7 @@ int main(void)
346 346 goto close;
347 347 }
348 348  
349   - rc = force_multiple_coils(&mb_param,
  349 + rc = force_multiple_coils(&mb_param, SERVER_ID,
350 350 UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
351 351 UT_COIL_STATUS_NB_POINTS,
352 352 tab_rp_status);
... ... @@ -358,7 +358,7 @@ int main(void)
358 358 goto close;
359 359 }
360 360  
361   - rc = preset_multiple_registers(&mb_param,
  361 + rc = preset_multiple_registers(&mb_param, SERVER_ID,
362 362 UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
363 363 UT_HOLDING_REGISTERS_NB_POINTS,
364 364 tab_rp_registers);
... ... @@ -374,7 +374,7 @@ int main(void)
374 374 /** TOO MANY DATA **/
375 375 printf("\nTEST TOO MANY DATA ERROR:\n");
376 376  
377   - rc = read_coil_status(&mb_param,
  377 + rc = read_coil_status(&mb_param, SERVER_ID,
378 378 UT_COIL_STATUS_ADDRESS,
379 379 MAX_STATUS + 1,
380 380 tab_rp_status);
... ... @@ -386,7 +386,7 @@ int main(void)
386 386 goto close;
387 387 }
388 388  
389   - rc = read_input_status(&mb_param,
  389 + rc = read_input_status(&mb_param, SERVER_ID,
390 390 UT_INPUT_STATUS_ADDRESS,
391 391 MAX_STATUS + 1,
392 392 tab_rp_status);
... ... @@ -398,7 +398,7 @@ int main(void)
398 398 goto close;
399 399 }
400 400  
401   - rc = read_holding_registers(&mb_param,
  401 + rc = read_holding_registers(&mb_param, SERVER_ID,
402 402 UT_HOLDING_REGISTERS_ADDRESS,
403 403 MAX_REGISTERS + 1,
404 404 tab_rp_registers);
... ... @@ -410,7 +410,7 @@ int main(void)
410 410 goto close;
411 411 }
412 412  
413   - rc = read_input_registers(&mb_param,
  413 + rc = read_input_registers(&mb_param, SERVER_ID,
414 414 UT_INPUT_REGISTERS_ADDRESS,
415 415 MAX_REGISTERS + 1,
416 416 tab_rp_registers);
... ... @@ -422,7 +422,7 @@ int main(void)
422 422 goto close;
423 423 }
424 424  
425   - rc = force_multiple_coils(&mb_param,
  425 + rc = force_multiple_coils(&mb_param, SERVER_ID,
426 426 UT_COIL_STATUS_ADDRESS,
427 427 MAX_STATUS + 1,
428 428 tab_rp_status);
... ... @@ -434,7 +434,7 @@ int main(void)
434 434 printf("FAILED\n");
435 435 }
436 436  
437   - rc = preset_multiple_registers(&mb_param,
  437 + rc = preset_multiple_registers(&mb_param, SERVER_ID,
438 438 UT_HOLDING_REGISTERS_ADDRESS,
439 439 MAX_REGISTERS + 1,
440 440 tab_rp_registers);
... ... @@ -448,12 +448,11 @@ int main(void)
448 448  
449 449 /** SLAVE REPLY **/
450 450 printf("\nTEST SLAVE REPLY:\n");
451   - modbus_set_slave(&mb_param, 18);
452   - rc = read_holding_registers(&mb_param,
  451 + rc = read_holding_registers(&mb_param, 18,
453 452 UT_HOLDING_REGISTERS_ADDRESS+1,
454 453 UT_HOLDING_REGISTERS_NB_POINTS,
455 454 tab_rp_registers);
456   - printf("1/2 No reply from slave %d: ", mb_param.slave);
  455 + printf("1/2 No reply from slave %d: ", 18);
457 456 if (rc == -1 && errno == ETIMEDOUT) {
458 457 printf("OK\n");
459 458 } else {
... ... @@ -461,8 +460,7 @@ int main(void)
461 460 goto close;
462 461 }
463 462  
464   - modbus_set_slave(&mb_param, MODBUS_BROADCAST_ADDRESS);
465   - rc = read_holding_registers(&mb_param,
  463 + rc = read_holding_registers(&mb_param, MODBUS_BROADCAST_ADDRESS,
466 464 UT_HOLDING_REGISTERS_ADDRESS,
467 465 UT_HOLDING_REGISTERS_NB_POINTS,
468 466 tab_rp_registers);
... ... @@ -480,7 +478,7 @@ int main(void)
480 478 /* Allocate only the required space */
481 479 tab_rp_registers_bad = (uint16_t *) malloc(
482 480 UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL * sizeof(uint16_t));
483   - rc = read_holding_registers(&mb_param,
  481 + rc = read_holding_registers(&mb_param, SERVER_ID,
484 482 UT_HOLDING_REGISTERS_ADDRESS,
485 483 UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL,
486 484 tab_rp_registers_bad);
... ...
tests/unit-test-slave.c
... ... @@ -20,8 +20,8 @@
20 20 #include <string.h>
21 21 #include <stdlib.h>
22 22 #include <errno.h>
23   -
24 23 #include <modbus.h>
  24 +
25 25 #include "unit-test.h"
26 26  
27 27 int main(void)
... ... @@ -32,7 +32,7 @@ int main(void)
32 32 int rc;
33 33 int i;
34 34  
35   - modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  35 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SERVER_ID);
36 36 modbus_set_debug(&mb_param, TRUE);
37 37 modbus_set_error_recovery(&mb_param, TRUE);
38 38  
... ...
tests/unit-test.h
... ... @@ -25,7 +25,8 @@
25 25 #include <stdint.h>
26 26 #endif
27 27  
28   -#define SLAVE 0x11
  28 +#define CLIENT_ID 15
  29 +#define SERVER_ID 17
29 30  
30 31 const uint16_t UT_COIL_STATUS_ADDRESS = 0x13;
31 32 const uint16_t UT_COIL_STATUS_NB_POINTS = 0x25;
... ...