| 1 | # modbus_set_error_recovery |
| 2 | |
| 3 | ## Name |
| 4 | |
| 5 | modbus_set_error_recovery - set the error recovery mode |
| 6 | |
| 7 | ## Synopsis |
| 8 | |
| 9 | ```c |
| 10 | int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery); |
| 11 | ``` |
| 12 | |
| 13 | ## Description |
| 14 | |
| 15 | The *modbus_set_error_recovery()* function shall set the error recovery mode to |
| 16 | apply when the connection fails or the byte received is not expected. The |
| 17 | argument `error_recovery` may be bitwise-or'ed with zero or more of the |
| 18 | following constants. |
| 19 | |
| 20 | By default there is no error recovery (`MODBUS_ERROR_RECOVERY_NONE`) so the |
| 21 | application is responsible for controlling the error values returned by |
| 22 | libmodbus functions and for handling them if necessary. |
| 23 | |
| 24 | When `MODBUS_ERROR_RECOVERY_LINK` is set, the library will attempt an |
| 25 | reconnection after a delay defined by response timeout of the libmodbus context. |
| 26 | This mode will try an infinite close/connect loop until success on send call and |
| 27 | will just try one time to re-establish the connection on select/read calls (if the |
| 28 | connection was down, the values to read are certainly not available any more after |
| 29 | reconnection, except for slave/server). This mode will also run flush requests |
| 30 | after a delay based on the current response timeout in some situations (eg. |
| 31 | timeout of select call). The reconnection attempt can hang for several seconds |
| 32 | if the network to the remote target unit is down. |
| 33 | |
| 34 | When `MODBUS_ERROR_RECOVERY_PROTOCOL` is set, a sleep and flush sequence will be |
| 35 | used to clean up the ongoing communication, this can occurs when the message |
| 36 | length is invalid, the TID is wrong or the received function code is not the |
| 37 | expected one. The response timeout delay will be used to sleep. |
| 38 | |
| 39 | The modes are mask values and so they are complementary. |
| 40 | |
| 41 | It's not recommended to enable error recovery for a Modbus slave/server. |
| 42 | |
| 43 | ## Return value |
| 44 | |
| 45 | The function shall return 0 if successful. Otherwise it shall return -1 and set |
| 46 | errno to one of the values defined below. |
| 47 | |
| 48 | ## Errors |
| 49 | |
| 50 | - *EINVAL*, the value of the argument `error_recovery` is not positive. |
| 51 | |
| 52 | ## Example |
| 53 | |
| 54 | ```c |
| 55 | modbus_set_error_recovery( |
| 56 | ctx, |
| 57 | MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL |
| 58 | ); |
| 59 | ``` |