Эх сурвалжийг харах

Add new function modbus_get_slave()

Stéphane Raimbault 8 жил өмнө
parent
commit
c3742e733a

+ 1 - 0
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 \

+ 41 - 0
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 - 0
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

+ 10 - 0
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)
 {

+ 1 - 0
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);

+ 4 - 1
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,