MODBUS_SET_ERROR_REC(3)
π NAME π
modbus_set_error_recovery β set the error recovery mode, bestie β¨
So this is a page from the libmodbus Manual, aka MODBUS_SET_ERROR_REC(3), and it's literally the one that decides how your code acts when the connection ghosts you or the bytes coming back are giving absolutely unhinged energy. Let's get into it π«Ά
πΈ SYNOPSIS πΈ
int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode
error_recovery);
β¨ DESCRIPTION β¨
okay so modbus_set_error_recovery() shall set the error recovery mode that gets applied when the connection fails, or when the byte you received is not the one you expected. Rude of it, honestly. π
The argument error_recovery may be bitwise-or'ed with zero or more of the constants below. Mix and match, queen π
By default there is no error recovery (MODBUS_ERROR_RECOVERY_NONE), which means the application is the one responsible for controlling the error values returned by libmodbus functions and for handling them if necessary. No babysitter, you're the main character now π
When MODBUS_ERROR_RECOVERY_LINK is set, the library will attempt a reconnection after a delay defined by the response timeout of the libmodbus context. This mode will try an infinite close/connect loop until success on a send call, and will just try one time to re-establish the connection on select/read calls. (And fr fr, if the connection was down, the values to read are certainly not available any more after reconnection β except for slave/server.) This mode will also run flush requests after a delay based on the current response timeout in some situations (eg. timeout of select call).
β οΈ heads up bestie: the reconnection attempt can hang for several seconds if the network to the remote target unit is down. Not a vibe.
When MODBUS_ERROR_RECOVERY_PROTOCOL is set, a sleep and flush sequence will be used to clean up the ongoing communication. This can occur when the message length is invalid, the TID is wrong, or the received function code is not the expected one. The response timeout delay will be used to sleep. π€
The modes are mask values and so they are complementary. π
π« It's not recommended to enable error recovery for slave/server. Just don't, okay?
β RETURN VALUE β
The function shall return 0 if successful. Otherwise it shall return -1 and set errno to one of the values defined below. Slay when it works, sad when it doesn't π
π ERRORS π
EINVALβ The value of the argumenterror_recoveryis not positive. Not even zero is allowed here, bestie.
π EXAMPLE π
modbus_set_error_recovery(ctx,
MODBUS_ERROR_RECOVERY_LINK |
MODBUS_ERROR_RECOVERY_PROTOCOL);
Look at that bitwise-or. Gorgeous. Iconic. Ate. π₯
π AUTHORS π
The libmodbus documentation was written by StΓ©phane Raimbault <stephane.raimbault@gmail.com> β thank you for the lore, king π«Ά
and that's the whole page, bestie!! this one's from libmodbus v3.1.6, dated 11/17/2025 β go forth and recover your errors responsibly πβ¨π
π¦ 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