1MODBUS_NEW_TCP_PI(3) libmodbus Manual MODBUS_NEW_TCP_PI(3)
2
3
4
6 modbus_new_tcp_pi - create a libmodbus context for TCP Protocol
7 Independent
8
10 modbus_t *modbus_new_tcp_pi(const char *node, const char *service);
11
13 The modbus_new_tcp_pi() function shall allocate and initialize a
14 modbus_t structure to communicate with a Modbus TCP IPv4 or IPv6
15 server.
16
17 The node argument specifies the host name or IP address of the host to
18 connect to, eg. "192.168.0.5" , "::1" or "server.com". A NULL value can
19 be used to listen any addresses in server mode.
20
21 The service argument is the service name/port number to connect to. To
22 use the default Modbus port use the string "502". On many Unix systems,
23 it’s convenient to use a port number greater than or equal to 1024
24 because it’s not necessary to have administrator privileges.
25
27 The function shall return a pointer to a modbus_t structure if
28 successful. Otherwise it shall return NULL and set errno to one of the
29 values defined below.
30
32 EINVAL
33 The node string is empty or has been truncated. The service string
34 is empty or has been truncated.
35
36 ENOMEM
37 Out of memory. Possibly, the application hits its memory limit
38 and/or whole system is running out of memory.
39
41 modbus_t *ctx;
42
43 ctx = modbus_new_tcp_pi("::1", "1502");
44 if (ctx == NULL) {
45 fprintf(stderr, "Unable to allocate libmodbus context\n");
46 return -1;
47 }
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
56 modbus_new_tcp(3) modbus_tcp_pi_listen(3) modbus_free(3)
57
59 The libmodbus documentation was written by Stéphane Raimbault
60 <stephane.raimbault@gmail.com>
61
62
63
64libmodbus v3.1.6 07/21/2022 MODBUS_NEW_TCP_PI(3)