1MODBUS_NEW_TCP(3) Libmodbus Manual MODBUS_NEW_TCP(3)
2
3
4
6 modbus_new_tcp - create a libmodbus context for TCP/IPv4
7
9 modbus_t *modbus_new_tcp(const char *ip, int port);
10
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 etablish a connection.
17
18 The port argument is the TCP port to use. Set the port to
19 MODBUS_TCP_DEFAULT_PORT to use the default one (502). It’s convenient
20 to use a port number greater than or equal to 1024 because it’s not
21 necessary to have administrator privileges.
22
24 The modbus_new_tcp() function shall return a pointer to a modbus_t
25 structure if successful. Otherwise it shall return NULL and set errno
26 to one of the values defined below.
27
29 EINVAL
30 An invalid IP address was given.
31
33 modbus_t *ctx;
34
35 ctx = modbus_new_tcp("127.0.0.1", 1502);
36 if (ctx == NULL) {
37 fprintf(stderr, "Unable to allocate libmodbus context\n");
38 return -1;
39 }
40
41 if (modbus_connect(ctx) == -1) {
42 fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
43 modbus_free(ctx);
44 return -1;
45 }
46
48 modbus_new_rtu(3) modbus_free(3)
49
51 The libmodbus documentation was written by Stéphane Raimbault
52 <stephane.raimbault@gmail.com[1]>
53
55 1. stephane.raimbault@gmail.com
56 mailto:stephane.raimbault@gmail.com
57
58
59
60libmodbus 3.0.6 04/02/2014 MODBUS_NEW_TCP(3)