MODBUS_NEW_RTU(3)
β¨ 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:
Nfor noneEfor evenOfor 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
- 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