MODBUS_NEW_RTU(3)

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

✨ MODBUS_NEW_RTU(3) β€” the libmodbus manual, bestie edition ✨

okay so you found the modbus_new_rtu(3) page from the libmodbus Manual and honestly? you're in the right place. this is where you spin up a whole libmodbus context for RTU mode. group chat, grab a snack, we're doing serial comms πŸŽ€


🌸 NAME 🌸

modbus_new_rtu β€” create a libmodbus context for RTU

like, it makes the thing. the RTU thing. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

modbus_t *modbus_new_rtu(const char *device, int baud, char parity, int
data_bit, int stop_bit);

✨ DESCRIPTION ✨

The modbus_new_rtu() function shall allocate and initialize a modbus_t structure so you can talk in RTU mode over a serial line. it's giving "hello serial port, it's me" 🫢

The device argument is the name of the serial port that the OS is handling for you. examples, because examples are everything: "/dev/ttyS0" or "/dev/ttyUSB0". On Windows πŸͺŸ it's a whole thing β€” you have to prepend the COM name with "\\.\" for COM numbers greater than 9, eg. "\\\\.\\COM10". yes the backslashes are like that, don't @ me. details live here if you wanna read: http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx

The baud argument is the baud rate of the communication, eg. 9600, 19200, 57600, 115200, etc. pick your speed, main character πŸ’¨

The parity argument can have one of these values:

  • N for none
  • E for even
  • O for odd

The data_bits argument is the number of bits of data, and the allowed values are 5, 6, 7 and 8. no cap. πŸ“

The stop_bits argument is the bits of stop, and the allowed values are 1 and 2. πŸ›‘

Once the modbus_t structure is initialized, you must set the slave of your device with modbus_set_slave(3) and then connect to the serial bus with modbus_connect(3). don't skip steps bestie, the order matters, periodt πŸ‘‘


πŸ’– RETURN VALUE πŸ’–

The function shall return a pointer to a modbus_t structure if it ate and left no crumbs. Otherwise it shall return NULL and set errno to one of the values defined below. 😭


πŸ’€ ERRORS πŸ’€

  • EINVAL β€” an invalid argument was given. something you passed was not it πŸ™ˆ
  • ENOMEM β€” out of memory. Possibly, the application hits its memory limit and/or the whole system is running out of memory. your RAM said no πŸ’€

🌈 EXAMPLE 🌈

modbus_t *ctx;

ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
if (ctx == NULL) {
    fprintf(stderr, "Unable to create the libmodbus context\n");
    return -1;
}

modbus_set_slave(ctx, YOUR_DEVICE_ID);

if (modbus_connect(ctx) == -1) {
    fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
    modbus_free(ctx);
    return -1;
}

look at that. clean. iconic. copy-paste-able 🧚


πŸŽ€ SEE ALSO πŸŽ€

modbus_new_tcp(3) modbus_free(3)

(the siblings. go say hi 🫢)


πŸ‘‘ AUTHORS πŸ‘‘

The libmodbus documentation was written by StΓ©phane Raimbault <stephane.raimbault@gmail.com> πŸ’œ


and that's the whole page, bestie!! ✨ this was libmodbus v3.1.6, dated 11/17/2025, and you read the MODBUS_NEW_RTU(3) page all the way to the end which is lowkey iconic of you. go build something, mwah πŸ’…πŸŒΈπŸ’–

πŸ“¦ where this page comes from