From c7c7c768a6b60259b9e38df39a8da909b53a8791 Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Tue, 4 Aug 2015 15:30:17 +0200 Subject: [PATCH] Improve new_rtu and set_slave documentation (related to #276) --- doc/modbus_new_rtu.txt | 13 ++++++++++++- doc/modbus_set_slave.txt | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/doc/modbus_new_rtu.txt b/doc/modbus_new_rtu.txt index 8cc9cc0..34fc419 100644 --- a/doc/modbus_new_rtu.txt +++ b/doc/modbus_new_rtu.txt @@ -15,7 +15,7 @@ SYNOPSIS DESCRIPTION ----------- -The *modbus_new_rtu()* function shall allocate and initialize a modbus_t +The *modbus_new_rtu()* function shall allocate and initialize a _modbus_t_ structure to communicate in RTU mode on a serial line. The _device_ argument specifies the name of the serial port handled by the OS, @@ -37,6 +37,9 @@ values are 5, 6, 7 and 8. The _stop_bits_ argument specifies the bits of stop, the allowed values are 1 and 2. +Once the _modbus_t_ structure is initialized, you must set the slave of your +device with linkmb:modbus_set_slave[3] and connect to the serial bus with +linkmb:modbus_connect[3]. RETURN VALUE ------------ @@ -62,6 +65,14 @@ if (ctx == NULL) { fprintf(stderr, "Unable to create the libmodbus context\n"); return -1; } + +modbus_set_slave(ctx, YOUR_DEVICE_ID); + +if (modbus_connect(ctx) == -1) { + fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); + modbus_free(ctx); + return -1; +} ------------------- SEE ALSO diff --git a/doc/modbus_set_slave.txt b/doc/modbus_set_slave.txt index 7a7ff49..23d786d 100644 --- a/doc/modbus_set_slave.txt +++ b/doc/modbus_set_slave.txt @@ -45,6 +45,32 @@ ERRORS The slave number is invalid. +EXAMPLE +------- +[source,c] +------------------- +modbus_t *ctx; + +ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1); +if (ctx == NULL) { + fprintf(stderr, "Unable to create the libmodbus context\n"); + return -1; +} + +rc = modbus_set_slave(ctx, YOUR_DEVICE_ID); +if (rc == -1) { + fprintf(stderr, "Invalid slave ID\n"); + modbus_free(ctx); + return -1; +} + +if (modbus_connect(ctx) == -1) { + fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); + modbus_free(ctx); + return -1; +} +------------------- + AUTHORS ------- The libmodbus documentation was written by Stéphane Raimbault -- libgit2 0.21.4