'\" t
.\" Title: modbus_set_slave
.\" 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_SET_SLAVE" "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_set_slave \- set slave number in the context
.SH "SYNOPSIS"
.sp
\fBint modbus_set_slave(modbus_t *\fR\fB\fIctx\fR\fR\fB, int \fR\fB\fIslave\fR\fR\fB);\fR
.SH "DESCRIPTION"
.sp
The \fBmodbus_set_slave()\fR function shall set the slave number in the libmodbus context\&.
.sp
The behavior depends of network and the role of the device:
.PP
\fBRTU\fR
.RS 4
Define the slave ID of the remote device to talk in master mode or set the internal slave ID in slave mode\&. According to the protocol, a Modbus device must only accept message holding its slave number or the special broadcast number\&.
.RE
.PP
\fBTCP\fR
.RS 4
The slave number is only required in TCP if the message must reach a device on a serial network\&. Some not compliant devices or software (such as modpoll) uses the slave ID as unit identifier, that\(cqs incorrect (cf page 23 of Modbus Messaging Implementation Guide v1\&.0b) but without the slave value, the faulty remote device or software drops the requests! The special value
MODBUS_TCP_SLAVE
(0xFF) can be used in TCP mode to restore the default value\&.
.RE
.sp
The broadcast address is MODBUS_BROADCAST_ADDRESS\&. This special value must be use when you want all Modbus devices of the network receive the request\&.
.SH "RETURN VALUE"
.sp
The function shall return 0 if successful\&. Otherwise it shall return \-1 and set errno to one of the values defined below\&.
.SH "ERRORS"
.PP
\fBEINVAL\fR
.RS 4
The slave number is invalid\&.
.RE
.SH "EXAMPLE"
.sp
.if n \{\
.RS 4
.\}
.nf
modbus_t *ctx;
ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, \*(AqN\*(Aq, 8, 1);
if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context\en");
return \-1;
}
rc = modbus_set_slave(ctx, YOUR_DEVICE_ID);
if (rc == \-1) {
fprintf(stderr, "Invalid slave ID\en");
modbus_free(ctx);
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_get_slave\fR(3)
.SH "AUTHORS"
.sp
The libmodbus documentation was written by Stéphane Raimbault