MODBUS_RTU_SET_RTS(3)

πŸ“¦
version 3.1.6 Β· 3 versions
, in debian bookworm

πŸ’– 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 πŸ‘‘


πŸ“ 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