Debian Codegraph @

docs/modbus_new_tcp.md

raw symbols SCIP
1 # modbus_new_tcp
2
3 ## Name
4
5 modbus_new_tcp - create a libmodbus context for TCP/IPv4
6
7 ## Synopsis
8
9 ```c
10 modbus_t *modbus_new_tcp(const char *ip, int port);
11 ```
12
13 ## Description
14
15 The *modbus_new_tcp()* function shall allocate and initialize a *modbus_t*
16 structure to communicate with a Modbus TCP IPv4 server.
17
18 The `ip` argument specifies the IP address of the server to which the client
19 wants to establish a connection. A NULL value can be used to listen on any addresses in
20 server mode.
21
22 The `port` argument is the TCP port to use. Set the port to
23 `MODBUS_TCP_DEFAULT_PORT` to use the default one (502). It's convenient to use a
24 port number greater than or equal to 1024 because it's not necessary to have
25 administrator privileges.
26
27 ## Return value
28
29 The function shall return a pointer to a *modbus_t* structure if
30 successful. Otherwise it shall return NULL and set errno to one of the values
31 defined below.
32
33 ## Errors
34
35 - *EINVAL*, an invalid IP address was given.
36 - *ENOMEM*, out of memory. Possibly, the application hits its memory limit
37 and/or whole system is running out of memory.
38
39 ## Example
40
41 ```c
42 modbus_t *ctx;
43
44 ctx = modbus_new_tcp("127.0.0.1", 502);
45 if (ctx == NULL) {
46 fprintf(stderr, "Unable to allocate libmodbus context\n");
47 return -1;
48 }
49
50 if (modbus_connect(ctx) == -1) {
51 fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
52 modbus_free(ctx);
53 return -1;
54 }
55 ```
56
57 ## See also
58
59 - [modbus_tcp_listen](modbus_tcp_listen.md)
60 - [modbus_free](modbus_free.md)