Commit c7c7c768a6b60259b9e38df39a8da909b53a8791
1 parent
0e2f470a
Improve new_rtu and set_slave documentation (related to #276)
Showing
2 changed files
with
38 additions
and
1 deletions
doc/modbus_new_rtu.txt
| @@ -15,7 +15,7 @@ SYNOPSIS | @@ -15,7 +15,7 @@ SYNOPSIS | ||
| 15 | 15 | ||
| 16 | DESCRIPTION | 16 | DESCRIPTION |
| 17 | ----------- | 17 | ----------- |
| 18 | -The *modbus_new_rtu()* function shall allocate and initialize a modbus_t | 18 | +The *modbus_new_rtu()* function shall allocate and initialize a _modbus_t_ |
| 19 | structure to communicate in RTU mode on a serial line. | 19 | structure to communicate in RTU mode on a serial line. |
| 20 | 20 | ||
| 21 | The _device_ argument specifies the name of the serial port handled by the OS, | 21 | 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. | @@ -37,6 +37,9 @@ values are 5, 6, 7 and 8. | ||
| 37 | The _stop_bits_ argument specifies the bits of stop, the allowed values are 1 | 37 | The _stop_bits_ argument specifies the bits of stop, the allowed values are 1 |
| 38 | and 2. | 38 | and 2. |
| 39 | 39 | ||
| 40 | +Once the _modbus_t_ structure is initialized, you must set the slave of your | ||
| 41 | +device with linkmb:modbus_set_slave[3] and connect to the serial bus with | ||
| 42 | +linkmb:modbus_connect[3]. | ||
| 40 | 43 | ||
| 41 | RETURN VALUE | 44 | RETURN VALUE |
| 42 | ------------ | 45 | ------------ |
| @@ -62,6 +65,14 @@ if (ctx == NULL) { | @@ -62,6 +65,14 @@ if (ctx == NULL) { | ||
| 62 | fprintf(stderr, "Unable to create the libmodbus context\n"); | 65 | fprintf(stderr, "Unable to create the libmodbus context\n"); |
| 63 | return -1; | 66 | return -1; |
| 64 | } | 67 | } |
| 68 | + | ||
| 69 | +modbus_set_slave(ctx, YOUR_DEVICE_ID); | ||
| 70 | + | ||
| 71 | +if (modbus_connect(ctx) == -1) { | ||
| 72 | + fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); | ||
| 73 | + modbus_free(ctx); | ||
| 74 | + return -1; | ||
| 75 | +} | ||
| 65 | ------------------- | 76 | ------------------- |
| 66 | 77 | ||
| 67 | SEE ALSO | 78 | SEE ALSO |
doc/modbus_set_slave.txt
| @@ -45,6 +45,32 @@ ERRORS | @@ -45,6 +45,32 @@ ERRORS | ||
| 45 | The slave number is invalid. | 45 | The slave number is invalid. |
| 46 | 46 | ||
| 47 | 47 | ||
| 48 | +EXAMPLE | ||
| 49 | +------- | ||
| 50 | +[source,c] | ||
| 51 | +------------------- | ||
| 52 | +modbus_t *ctx; | ||
| 53 | + | ||
| 54 | +ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1); | ||
| 55 | +if (ctx == NULL) { | ||
| 56 | + fprintf(stderr, "Unable to create the libmodbus context\n"); | ||
| 57 | + return -1; | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +rc = modbus_set_slave(ctx, YOUR_DEVICE_ID); | ||
| 61 | +if (rc == -1) { | ||
| 62 | + fprintf(stderr, "Invalid slave ID\n"); | ||
| 63 | + modbus_free(ctx); | ||
| 64 | + return -1; | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +if (modbus_connect(ctx) == -1) { | ||
| 68 | + fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); | ||
| 69 | + modbus_free(ctx); | ||
| 70 | + return -1; | ||
| 71 | +} | ||
| 72 | +------------------- | ||
| 73 | + | ||
| 48 | AUTHORS | 74 | AUTHORS |
| 49 | ------- | 75 | ------- |
| 50 | The libmodbus documentation was written by Stéphane Raimbault | 76 | The libmodbus documentation was written by Stéphane Raimbault |