MODBUS_SEND_RAW_REQU(3)

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

πŸ’– modbus_send_raw_request β€” the libmodbus manual, but make it make sense πŸ’–

okay bestie, welcome to the libmodbus manual page for modbus_send_raw_request, page MODBUS_SEND_RAW_REQU(3). yeah the title got cut off, that's just how manpages do, it's giving βœ‚οΈ energy. let's get into it πŸ¦‹


✨ NAME ✨

modbus_send_raw_request β€” bestie, it sends a raw request πŸ’…


✨ SYNOPSIS ✨

int modbus_send_raw_request(modbus_t *ctx, const uint8_t *raw_req, int
raw_req_length);

that's the whole vibe right there πŸ‘‘


✨ DESCRIPTION ✨

so modbus_send_raw_request() shall send a request through the socket of the context ctx. that's the job. that's what it does 🫢

🚨 real talk though: this function must be used for debugging purposes because you have to take care to make a valid request by hand. it's not doing the thinking for you, bestie, you're the main character here 😭

here's the thing: the function only adds to the message the header or CRC of the selected backend. that's it. nothing else. so raw_req must start with β€” and contain at least β€” a slave/unit identifier and a function code. no skipping, no shortcuts 🌸

also? this function can be used to send requests that aren't handled by the library at all. it's the flexible one. it's giving "I got you, do your thing" πŸ’œ

and the public header of libmodbus provides a list of supported Modbus function codes, prefixed by MODBUS_FC_ (eg. MODBUS_FC_READ_HOLDING_REGISTERS), to help build those raw requests. use them, they're iconic ⭐


✨ RETURN VALUE ✨

if it works, the function shall return the full message length, counting the extra data relating to the backend. that's your win πŸŽ€

otherwise? it shall return -1 and set errno. sad girl hours, check your errno πŸ’€


✨ EXAMPLE ✨

here's the whole thing, verbatim, no notes πŸ’–

modbus_t *ctx;
/* Read 5 holding registers from address 1 */
uint8_t raw_req[] = { 0xFF, MODBUS_FC_READ_HOLDING_REGISTERS, 0x00, 0x01, 0x0, 0x05 };
int req_length;
uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];

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

req_length = modbus_send_raw_request(ctx, raw_req, 6 * sizeof(uint8_t));
modbus_receive_confirmation(ctx, rsp);

modbus_close(ctx);
modbus_free(ctx);

see how the raw request is built by hand? yeah. that's the whole point. handcrafted. artisanal. like you 🧚


✨ SEE ALSO ✨


✨ AUTHORS ✨

the libmodbus documentation was written by Stéphane Raimbault <stephane.raimbault@gmail.com> 🌈


and that's the whole page, bestie!! this one's from libmodbus v3.1.6, dated 11/17/2025 πŸ’– go forth and send some valid-by-hand requests, no cap 🫢✨🌸

πŸ“¦ where this page comes from