| 1 | # modbus_rtu_set_rts |
| 2 | |
| 3 | ## Name |
| 4 | |
| 5 | modbus_rtu_set_rts - set the RTS mode in RTU |
| 6 | |
| 7 | ## Synopsis |
| 8 | |
| 9 | ```c |
| 10 | int modbus_rtu_set_rts(modbus_t *ctx, int mode) |
| 11 | ``` |
| 12 | |
| 13 | ## Description |
| 14 | |
| 15 | The *modbus_rtu_set_rts()* function shall set the Request To Send mode to |
| 16 | communicate on a RS-485 serial bus. By default, the mode is set to |
| 17 | `MODBUS_RTU_RTS_NONE` and no signal is issued before writing data on the wire. |
| 18 | |
| 19 | To enable the RTS mode, the values `MODBUS_RTU_RTS_UP` or `MODBUS_RTU_RTS_DOWN` |
| 20 | must be used, these modes enable the RTS mode and set the polarity at the same |
| 21 | time. When `MODBUS_RTU_RTS_UP` is used, an ioctl call is made with RTS flag |
| 22 | enabled then data is written on the bus after a delay of 1 ms, then another |
| 23 | ioctl call is made with the RTS flag disabled and again a delay of 1 ms occurs. |
| 24 | The `MODBUS_RTU_RTS_DOWN` mode applies the same procedure but with an inverted |
| 25 | RTS flag. |
| 26 | |
| 27 | This function can only be used with a context using a RTU backend. |
| 28 | |
| 29 | ## Return value |
| 30 | |
| 31 | The function shall return 0 if successful. Otherwise it shall return -1 and set |
| 32 | errno to one of the values defined below. |
| 33 | |
| 34 | ## Errors |
| 35 | |
| 36 | - *EINVAL*, the libmodbus backend isn't RTU or the mode given in argument is invalid. |
| 37 | |
| 38 | ## Example |
| 39 | |
| 40 | Enable the RTS mode with positive polarity: |
| 41 | |
| 42 | ```c |
| 43 | modbus_t *ctx; |
| 44 | uint16_t tab_reg[10]; |
| 45 | |
| 46 | ctx = modbus_new_rtu("/dev/ttyS0", 115200, 'N', 8, 1); |
| 47 | modbus_set_slave(ctx, 1); |
| 48 | |
| 49 | if (modbus_connect(ctx) == -1) { |
| 50 | fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); |
| 51 | modbus_free(ctx); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485); |
| 56 | modbus_rtu_set_rts(ctx, MODBUS_RTU_RTS_UP); |
| 57 | |
| 58 | rc = modbus_read_registers(ctx, 0, 7, tab_reg); |
| 59 | if (rc == -1) { |
| 60 | fprintf(stderr, "%s\n", modbus_strerror(errno)); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | modbus_close(ctx); |
| 65 | modbus_free(ctx); |
| 66 | ``` |
| 67 | |
| 68 | ## See also |
| 69 | |
| 70 | - [modbus_rtu_get_rts](modbus_rtu_get_rts.md) |