'\" t
.\" Title: modbus_new_tcp
.\" Author: [see the "AUTHORS" section]
.\" Generator: DocBook XSL Stylesheets v1.79.1
.\" Date: 02/28/2020
.\" Manual: libmodbus Manual
.\" Source: libmodbus v3.1.6
.\" Language: English
.\"
.TH "MODBUS_NEW_TCP" "3" "02/28/2020" "libmodbus v3\&.1\&.6" "libmodbus Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
modbus_new_tcp \- create a libmodbus context for TCP/IPv4
.SH "SYNOPSIS"
.sp
\fBmodbus_t *modbus_new_tcp(const char *\fR\fB\fIip\fR\fR\fB, int \fR\fB\fIport\fR\fR\fB);\fR
.SH "DESCRIPTION"
.sp
The \fBmodbus_new_tcp()\fR function shall allocate and initialize a modbus_t structure to communicate with a Modbus TCP IPv4 server\&.
.sp
The \fIip\fR argument specifies the IP address of the server to which the client wants to establish a connection\&. A NULL value can be used to listen any addresses in server mode\&.
.sp
The \fIport\fR argument is the TCP port to use\&. Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one (502)\&. It\(cqs convenient to use a port number greater than or equal to 1024 because it\(cqs not necessary to have administrator privileges\&.
.SH "RETURN VALUE"
.sp
The function shall return a pointer to a \fBmodbus_t\fR structure if successful\&. Otherwise it shall return NULL and set errno to one of the values defined below\&.
.SH "ERRORS"
.PP
\fBEINVAL\fR
.RS 4
An invalid IP address was given\&.
.RE
.PP
\fBENOMEM\fR
.RS 4
Out of memory\&. Possibly, the application hits its memory limit and/or whole system is running out of memory\&.
.RE
.SH "EXAMPLE"
.sp
.if n \{\
.RS 4
.\}
.nf
modbus_t *ctx;
ctx = modbus_new_tcp("127\&.0\&.0\&.1", 1502);
if (ctx == NULL) {
fprintf(stderr, "Unable to allocate libmodbus context\en");
return \-1;
}
if (modbus_connect(ctx) == \-1) {
fprintf(stderr, "Connection failed: %s\en", modbus_strerror(errno));
modbus_free(ctx);
return \-1;
}
.fi
.if n \{\
.RE
.\}
.SH "SEE ALSO"
.sp
\fBmodbus_tcp_listen\fR(3) \fBmodbus_free\fR(3)
.SH "AUTHORS"
.sp
The libmodbus documentation was written by Stéphane Raimbault