Commit c3742e733a3971c34840d91b79235fee4195aa07

Authored by Stéphane Raimbault
1 parent 75543b21

Add new function modbus_get_slave()

doc/Makefile.am
... ... @@ -12,6 +12,7 @@ TXT3 = \
12 12 modbus_get_float_dcba.txt \
13 13 modbus_get_header_length.txt \
14 14 modbus_get_response_timeout.txt \
  15 + modbus_get_slave.txt \
15 16 modbus_get_socket.txt \
16 17 modbus_mapping_free.txt \
17 18 modbus_mapping_new.txt \
... ...
doc/modbus_get_slave.txt 0 → 100644
  1 +modbus_get_slave(3)
  2 +===================
  3 +
  4 +
  5 +NAME
  6 +----
  7 +modbus_get_slave - get slave number in the context
  8 +
  9 +
  10 +SYNOPSIS
  11 +--------
  12 +*int modbus_get_slave(modbus_t *'ctx');*
  13 +
  14 +
  15 +DESCRIPTION
  16 +-----------
  17 +The *modbus_get_slave()* function shall get the slave number in the libmodbus
  18 +context.
  19 +
  20 +
  21 +RETURN VALUE
  22 +------------
  23 +The function shall return the slave number if successful. Otherwise it shall return -1
  24 +and set errno to one of the values defined below.
  25 +
  26 +
  27 +ERRORS
  28 +------
  29 +*EINVAL*::
  30 +The libmodbus context is undefined.
  31 +
  32 +
  33 +SEE ALSO
  34 +--------
  35 +linkmb:modbus_set_slave[3]
  36 +
  37 +
  38 +AUTHORS
  39 +-------
  40 +The libmodbus documentation was written by Stéphane Raimbault
  41 +<stephane.raimbault@gmail.com>
... ...
doc/modbus_set_slave.txt
... ... @@ -74,6 +74,10 @@ if (modbus_connect(ctx) == -1) {
74 74 }
75 75 -------------------
76 76  
  77 +SEE ALSO
  78 +--------
  79 +linkmb:modbus_get_slave[3]
  80 +
77 81 AUTHORS
78 82 -------
79 83 The libmodbus documentation was written by Stéphane Raimbault
... ...
src/modbus.c
... ... @@ -1577,6 +1577,16 @@ int modbus_set_slave(modbus_t *ctx, int slave)
1577 1577 return ctx->backend->set_slave(ctx, slave);
1578 1578 }
1579 1579  
  1580 +int modbus_get_slave(modbus_t *ctx)
  1581 +{
  1582 + if (ctx == NULL) {
  1583 + errno = EINVAL;
  1584 + return -1;
  1585 + }
  1586 +
  1587 + return ctx->slave;
  1588 +}
  1589 +
1580 1590 int modbus_set_error_recovery(modbus_t *ctx,
1581 1591 modbus_error_recovery_mode error_recovery)
1582 1592 {
... ...
src/modbus.h
... ... @@ -177,6 +177,7 @@ typedef enum
177 177 } modbus_error_recovery_mode;
178 178  
179 179 MODBUS_API int modbus_set_slave(modbus_t* ctx, int slave);
  180 +MODBUS_API int modbus_get_slave(modbus_t* ctx);
180 181 MODBUS_API int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery);
181 182 MODBUS_API int modbus_set_socket(modbus_t *ctx, int s);
182 183 MODBUS_API int modbus_get_socket(modbus_t *ctx);
... ...
tests/unit-test-client.c
... ... @@ -64,6 +64,7 @@ int main(int argc, char *argv[])
64 64 uint32_t old_byte_to_usec;
65 65 int use_backend;
66 66 int success = FALSE;
  67 + int old_slave;
67 68  
68 69 if (argc > 1) {
69 70 if (strcmp(argv[1], "tcp") == 0) {
... ... @@ -445,6 +446,8 @@ int main(int argc, char *argv[])
445 446 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
446 447  
447 448 /** SLAVE REPLY **/
  449 + old_slave = modbus_get_slave(ctx);
  450 +
448 451 printf("\nTEST SLAVE REPLY:\n");
449 452 modbus_set_slave(ctx, INVALID_SERVER_ID);
450 453 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
... ... @@ -500,7 +503,7 @@ int main(int argc, char *argv[])
500 503 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
501 504  
502 505 /* Restore slave */
503   - modbus_set_slave(ctx, use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE);
  506 + modbus_set_slave(ctx, old_slave);
504 507  
505 508 printf("3/3 Response with an invalid TID or slave: ");
506 509 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
... ...