1MODBUS_NEW_RTU(3) libmodbus Manual MODBUS_NEW_RTU(3)
2
3
4
6 modbus_new_rtu - create a libmodbus context for RTU
7
9 modbus_t *modbus_new_rtu(const char *device, int baud, char parity, int
10 data_bit, int stop_bit);
11
13 The modbus_new_rtu() function shall allocate and initialize a modbus_t
14 structure to communicate in RTU mode on a serial line.
15
16 The device argument specifies the name of the serial port handled by
17 the OS, eg. "/dev/ttyS0" or "/dev/ttyUSB0". On Windows, it’s necessary
18 to prepend COM name with "\\.\" for COM number greater than 9, eg.
19 "\\\\.\\COM10". See
20 http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx for
21 details
22
23 The baud argument specifies the baud rate of the communication, eg.
24 9600, 19200, 57600, 115200, etc.
25
26 The parity argument can have one of the following values
27
28 • N for none
29
30 • E for even
31
32 • O for odd
33
34 The data_bits argument specifies the number of bits of data, the
35 allowed values are 5, 6, 7 and 8.
36
37 The stop_bits argument specifies the bits of stop, the allowed values
38 are 1 and 2.
39
40 Once the modbus_t structure is initialized, you must set the slave of
41 your device with modbus_set_slave(3) and connect to the serial bus with
42 modbus_connect(3).
43
45 The function shall return a pointer to a modbus_t structure if
46 successful. Otherwise it shall return NULL and set errno to one of the
47 values defined below.
48
50 EINVAL
51 An invalid argument was given.
52
53 ENOMEM
54 Out of memory. Possibly, the application hits its memory limit
55 and/or whole system is running out of memory.
56
58 modbus_t *ctx;
59
60 ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
61 if (ctx == NULL) {
62 fprintf(stderr, "Unable to create the libmodbus context\n");
63 return -1;
64 }
65
66 modbus_set_slave(ctx, YOUR_DEVICE_ID);
67
68 if (modbus_connect(ctx) == -1) {
69 fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
70 modbus_free(ctx);
71 return -1;
72 }
73
75 modbus_new_tcp(3) modbus_free(3)
76
78 The libmodbus documentation was written by Stéphane Raimbault
79 <stephane.raimbault@gmail.com>
80
81
82
83libmodbus v3.1.7 07/20/2023 MODBUS_NEW_RTU(3)