'\" t
.\"     Title: modbus_new_tcp
.\"    Author: [see the "AUTHORS" section]
.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
.\"      Date: 05/26/2012
.\"    Manual: Libmodbus Manual
.\"    Source: libmodbus 3.0.3
.\"  Language: English
.\"
.TH "MODBUS_NEW_TCP" "3" "05/26/2012" "libmodbus 3\&.0\&.3" "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 \fImodbus_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 etablish a connection\&.
.sp
The \fIport\fR argument is the TCP port to use\&. Set the port to \fIMODBUS_TCP_DEFAULT_PORT\fR 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 \fImodbus_new_tcp()\fR 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
.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_new_rtu\fR(3) \fBmodbus_free\fR(3)
.SH "AUTHORS"
.sp
The libmodbus documentation was written by St\('ephane Raimbault <\m[blue]\fBstephane\&.raimbault@gmail\&.com\fR\m[]\&\s-2\u[1]\d\s+2>
.SH "NOTES"
.IP " 1." 4
stephane.raimbault@gmail.com
.RS 4
\%mailto:stephane.raimbault@gmail.com
.RE
