From c3742e733a3971c34840d91b79235fee4195aa07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Raimbault?= Date: Wed, 7 Sep 2016 19:50:13 +0200 Subject: [PATCH] Add new function modbus_get_slave() --- doc/Makefile.am | 1 + doc/modbus_get_slave.txt | 41 ++++++++++++++++++++++++++++++++++++++++ doc/modbus_set_slave.txt | 4 ++++ src/modbus.c | 10 ++++++++++ src/modbus.h | 1 + tests/unit-test-client.c | 5 ++++- 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 doc/modbus_get_slave.txt diff --git a/doc/Makefile.am b/doc/Makefile.am index 48c081975..5a52c0409 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -12,6 +12,7 @@ TXT3 = \ modbus_get_float_dcba.txt \ modbus_get_header_length.txt \ modbus_get_response_timeout.txt \ + modbus_get_slave.txt \ modbus_get_socket.txt \ modbus_mapping_free.txt \ modbus_mapping_new.txt \ diff --git a/doc/modbus_get_slave.txt b/doc/modbus_get_slave.txt new file mode 100644 index 000000000..89e2ac019 --- /dev/null +++ b/doc/modbus_get_slave.txt @@ -0,0 +1,41 @@ +modbus_get_slave(3) +=================== + + +NAME +---- +modbus_get_slave - get slave number in the context + + +SYNOPSIS +-------- +*int modbus_get_slave(modbus_t *'ctx');* + + +DESCRIPTION +----------- +The *modbus_get_slave()* function shall get the slave number in the libmodbus +context. + + +RETURN VALUE +------------ +The function shall return the slave number if successful. Otherwise it shall return -1 +and set errno to one of the values defined below. + + +ERRORS +------ +*EINVAL*:: +The libmodbus context is undefined. + + +SEE ALSO +-------- +linkmb:modbus_set_slave[3] + + +AUTHORS +------- +The libmodbus documentation was written by Stéphane Raimbault + diff --git a/doc/modbus_set_slave.txt b/doc/modbus_set_slave.txt index 5088856b0..7f9ecb099 100644 --- a/doc/modbus_set_slave.txt +++ b/doc/modbus_set_slave.txt @@ -74,6 +74,10 @@ if (modbus_connect(ctx) == -1) { } ------------------- +SEE ALSO +-------- +linkmb:modbus_get_slave[3] + AUTHORS ------- The libmodbus documentation was written by Stéphane Raimbault diff --git a/src/modbus.c b/src/modbus.c index f1da5c691..f117fd2c4 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -1577,6 +1577,16 @@ int modbus_set_slave(modbus_t *ctx, int slave) return ctx->backend->set_slave(ctx, slave); } +int modbus_get_slave(modbus_t *ctx) +{ + if (ctx == NULL) { + errno = EINVAL; + return -1; + } + + return ctx->slave; +} + int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery) { diff --git a/src/modbus.h b/src/modbus.h index e3e9f8662..f9f7449c4 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -177,6 +177,7 @@ typedef enum } modbus_error_recovery_mode; MODBUS_API int modbus_set_slave(modbus_t* ctx, int slave); +MODBUS_API int modbus_get_slave(modbus_t* ctx); MODBUS_API int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery); MODBUS_API int modbus_set_socket(modbus_t *ctx, int s); MODBUS_API int modbus_get_socket(modbus_t *ctx); diff --git a/tests/unit-test-client.c b/tests/unit-test-client.c index 3e315f48e..debd2bc42 100644 --- a/tests/unit-test-client.c +++ b/tests/unit-test-client.c @@ -64,6 +64,7 @@ int main(int argc, char *argv[]) uint32_t old_byte_to_usec; int use_backend; int success = FALSE; + int old_slave; if (argc > 1) { if (strcmp(argv[1], "tcp") == 0) { @@ -445,6 +446,8 @@ int main(int argc, char *argv[]) ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); /** SLAVE REPLY **/ + old_slave = modbus_get_slave(ctx); + printf("\nTEST SLAVE REPLY:\n"); modbus_set_slave(ctx, INVALID_SERVER_ID); rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, @@ -500,7 +503,7 @@ int main(int argc, char *argv[]) ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); /* Restore slave */ - modbus_set_slave(ctx, use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE); + modbus_set_slave(ctx, old_slave); printf("3/3 Response with an invalid TID or slave: "); rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,