File: modbus_rtu_set_rts.md

package info (click to toggle)
libmodbus 3.1.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,112 kB
  • sloc: ansic: 5,495; javascript: 137; makefile: 90; sh: 26
file content (69 lines) | stat: -rw-r--r-- 1,830 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# modbus_rtu_set_rts

## Name

modbus_rtu_set_rts - set the RTS mode in RTU

## Synopsis

```c
int modbus_rtu_set_rts(modbus_t *ctx, int mode)
```

## Description

The *modbus_rtu_set_rts()* function shall set the Request To Send mode to
communicate on a RS-485 serial bus. By default, the mode is set to
`MODBUS_RTU_RTS_NONE` and no signal is issued before writing data on the wire.

To enable the RTS mode, the values `MODBUS_RTU_RTS_UP` or `MODBUS_RTU_RTS_DOWN`
must be used, these modes enable the RTS mode and set the polarity at the same
time. When `MODBUS_RTU_RTS_UP` is used, an ioctl call is made with RTS flag
enabled then data is written on the bus after a delay of 1 ms, then another
ioctl call is made with the RTS flag disabled and again a delay of 1 ms occurs.
The `MODBUS_RTU_RTS_DOWN` mode applies the same procedure but with an inverted
RTS flag.

This function can only be used with a context using a RTU backend.

## Return value

The function shall return 0 if successful. Otherwise it shall return -1 and set
errno to one of the values defined below.

## Errors

- *EINVAL*, the libmodbus backend isn't RTU or the mode given in argument is invalid.

## Example

Enable the RTS mode with positive polarity:

```c
modbus_t *ctx;
uint16_t tab_reg[10];

ctx = modbus_new_rtu("/dev/ttyS0", 115200, 'N', 8, 1);
modbus_set_slave(ctx, 1);
modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
modbus_rtu_set_rts(ctx, MODBUS_RTU_RTS_UP);

if (modbus_connect(ctx) == -1) {
    fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
    modbus_free(ctx);
    return -1;
}

rc = modbus_read_registers(ctx, 0, 7, tab_reg);
if (rc == -1) {
    fprintf(stderr, "%s\n", modbus_strerror(errno));
    return -1;
}

modbus_close(ctx);
modbus_free(ctx);
```

## See also

- [modbus_rtu_get_rts](modbus_rtu_get_rts.md)