1MODBUS_NEW_TCP(3)              libmodbus Manual              MODBUS_NEW_TCP(3)
2
3
4

NAME

6       modbus_new_tcp - create a libmodbus context for TCP/IPv4
7

SYNOPSIS

9       modbus_t *modbus_new_tcp(const char *ip, int port);
10

DESCRIPTION

12       The modbus_new_tcp() function shall allocate and initialize a modbus_t
13       structure to communicate with a Modbus TCP IPv4 server.
14
15       The ip argument specifies the IP address of the server to which the
16       client wants to establish a connection. A NULL value can be used to
17       listen any addresses in server mode.
18
19       The port argument is the TCP port to use. Set the port to
20       MODBUS_TCP_DEFAULT_PORT to use the default one (502). It’s convenient
21       to use a port number greater than or equal to 1024 because it’s not
22       necessary to have administrator privileges.
23

RETURN VALUE

25       The function shall return a pointer to a modbus_t structure if
26       successful. Otherwise it shall return NULL and set errno to one of the
27       values defined below.
28

ERRORS

30       EINVAL
31           An invalid IP address was given.
32
33       ENOMEM
34           Out of memory. Possibly, the application hits its memory limit
35           and/or whole system is running out of memory.
36

EXAMPLE

38           modbus_t *ctx;
39
40           ctx = modbus_new_tcp("127.0.0.1", 1502);
41           if (ctx == NULL) {
42               fprintf(stderr, "Unable to allocate libmodbus context\n");
43               return -1;
44           }
45
46           if (modbus_connect(ctx) == -1) {
47               fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
48               modbus_free(ctx);
49               return -1;
50           }
51

SEE ALSO

53       modbus_tcp_listen(3) modbus_free(3)
54

AUTHORS

56       The libmodbus documentation was written by Stéphane Raimbault
57       <stephane.raimbault@gmail.com>
58
59
60
61libmodbus v3.1.7                  07/20/2023                 MODBUS_NEW_TCP(3)
Impressum