Skip to content

Commit

Permalink
Add new function modbus_get_slave()
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane committed Sep 7, 2016
1 parent 75543b2 commit c3742e7
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/Makefile.am
Expand Up @@ -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 \
Expand Down
41 changes: 41 additions & 0 deletions 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
<stephane.raimbault@gmail.com>
4 changes: 4 additions & 0 deletions doc/modbus_set_slave.txt
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/modbus.c
Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions src/modbus.h
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion tests/unit-test-client.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c3742e7

Please sign in to comment.