MODBUS_WRITE_AND_REA(3)

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

πŸ’– NAME πŸ’–

okay so, bestie, this is modbus_write_and_read_registers β€” it writes AND reads a whole bunch of registers in a single transaction. yes, one trip. no cap. we love a multitasking icon πŸ‘‘

this is from the libmodbus Manual, and honestly? she's serving function-code-0x17 realness. periodt.


✨ SYNOPSIS ✨

int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int
write_nb, const uint16_t *src, int read_addr, int read_nb, const uint16_t *dest);

seven arguments, one slay. let's get into it 🫢


πŸŽ€ DESCRIPTION πŸŽ€

so here's the vibe. modbus_write_and_read_registers() does two things in one go, which is lowkey the whole point:

  1. it writes the content of the write_nb holding registers from the array src over to the address write_addr of the remote device
  2. then it reads the content of the read_nb holding registers from the address read_addr of the remote device

and where does the reading land? in the dest array, stored as word values (16 bits). cute, right? πŸ¦‹

⚠️ okay bestie, listen to me on this one β€” you must take care to allocate enough memory to store the results in dest (at least nb * sizeof(uint16_t)). do NOT skimp on the malloc. that's how you get the undefined-behaviour jumpscare 😭

the function uses the Modbus function code 0x17 (write/read registers). it's giving efficient. it's giving iconic. πŸ’…


🌸 RETURN VALUE 🌸

  • on success? it shall return the number of read registers. very demure, very mindful ✨
  • on failure? it shall return -1 and set errno. tragic. we hate to see it πŸ’€

πŸ’œ ERRORS πŸ’œ

  • EMBMDATA β€” Too many registers requested, Too many registers to write

that's the one. she's the only error here and she's doing the absolute most πŸ™ˆ


🌈 SEE ALSO 🌈

(the rest of the modbus girlies, go give them a read, iykyk 🧚)


πŸ‘‘ AUTHORS πŸ‘‘

the libmodbus documentation was written by StΓ©phane Raimbault <stephane.raimbault@gmail.com> β€” thank you for your service, king 🫢🌸


and that's the whole page, bestie!! πŸ’– this was libmodbus v3.1.6, dated 11/17/2025 β€” go write and read those registers in one single transaction, you absolute main character β­πŸŽ€βœ¨

πŸ“¦ where this page comes from