MODBUS_RTU_SET_RTS(3)
π MODBUS_RTU_SET_RTS(3) π
okay so bestie, we're in the libmodbus Manual and today's episode is all about telling your RS485 bus when to talk. it's giving "who gets the microphone" energy and honestly? iconic. π€β¨
πΈ NAME πΈ
modbus_rtu_set_rts β set the RTS mode in RTU, aka decide whether your serial port does the little handshake dance before it speaks π«Ά
β¨ SYNOPSIS β¨
int modbus_rtu_set_rts(modbus_t *ctx, int mode)
like. that's it. one function. two args. slay. π
π« DESCRIPTION π«
So modbus_rtu_set_rts() sets the Request To Send mode so your thing can communicate on an RS485 serial bus. By default, the mode is set to MODBUS_RTU_RTS_NONE, and in that case no signal is issued before writing data on the wire. no vibe check, no announcement, it just goes. π
Now if you wanna actually enable the RTS mode, you gotta use MODBUS_RTU_RTS_UP or MODBUS_RTU_RTS_DOWN β these modes turn RTS on and set the polarity at the same time, two birds, one flag, we love efficiency. π¦
Here's the tea on MODBUS_RTU_RTS_UP: an ioctl call is made with the RTS flag enabled, then data is written on the bus after a delay of 1 ms, then another ioctl call is made with the RTS flag disabled, and again a delay of 1 ms occurs. it's a whole little routine, very choreographed, main character energy fr. π
And MODBUS_RTU_RTS_DOWN? same procedure, except the RTS flag is inverted. that's it. that's the difference. π
π¨ real talk: this function can only be used with a context using a RTU backend. don't try it on anything else, it will not go well bestie.
π RETURN VALUE π
The function shall return 0 if successful. π₯³
Otherwise it shall return -1 and set errno to one of the values defined below. sad girl hours. π
π ERRORS π
EINVALβ the libmodbus backend isn't RTU, or the mode you passed in as an argument is invalid. basically "wrong vibe, try again." π
π EXAMPLE π
Enable the RTS mode with positive polarity. look at her go β¨
modbus_t *ctx;
uint16_t tab_reg[10];
ctx = modbus_new_rtu("/dev/ttyS0", 115200, 'N', 8, 1);
modbus_set_slave(ctx, 1);
modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
modbus_rtu_set_rts(ctx, MODBUS_RTU_RTS_UP);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connexion failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
rc = modbus_read_registers(ctx, 0, 7, tab_reg);
if (rc == -1) {
fprintf(stderr, "%s\n", modbus_strerror(errno));
return -1;
}
modbus_close(ctx);
modbus_free(ctx);
no cap, this is the whole flow: open the context, set the slave, set the serial mode to MODBUS_RTU_RS485, set RTS to MODBUS_RTU_RTS_UP, connect, read, close, free. clean. periodt. π§
π SEE ALSO π
modbus_rtu_get_rts(3)β the other half of the duo, she reads what this one writes π
π AUTHORS π
The libmodbus documentation was written by StΓ©phane Raimbault <stephane.raimbault@gmail.com> π«Ά
okay that's the whole page bestie, you're officially RTS-literate, go forth and handshake responsibly πΈπ¦π
libmodbus v3.1.6 Β· 11/17/2025 Β· and yes, that's the version and the date, stamped at the bottom of the original like a little signature. mwah. π
π¦ where this page comes from
- debian bookworm: libmodbus-dev 3.1.6-2.1+deb12u1 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms