liblightmodbus 3.0
A lightweight, header-only, hardware-agnostic Modbus RTU/TCP library
Loading...
Searching...
No Matches
Error handling

Liblightmodbus v3.0 introduces a new type for error handling - ModbusErrorInfo - returned by majority of the library functions. This new type allows to store both error type and its source - whether it was caused by an invalid request/response frame or by an actual library/user error.

In older versions, error checking would usually look like this:

ModbusError err = modbusDoSomething();
if (err != MODBUS_OK)
doSomething();
ModbusError
Represtents different kinds of errors.
Definition base.h:137
@ MODBUS_OK
No error.
Definition base.h:143

In the new version, it's a bit more sophisticated:

ModbusErrorInfo err = modbusDoSomething();
// We can check if any error happened at all.
// modbusIsOk() returning true indicates that request/response
// frame can be safely accessed and is ready to be sent.
if (modbusIsOk(err))
sendData();
else
doSomething();
// We can check if the library reported any general errors.
// Those usually mean that the user provided an invalid argument
// or that something failed internally. These are the serious ones.
handleSeriousError();
// We can check if the request frame contains an error.
// This includes parsing a frame meant for other slave, invalid CRC value, etc.
// Usually, you want to ignore these, because they are not application errors
// and only provide additional information about what prevented the library
// from processing request/response.
handleRequestError();
// We can also check if the response frame contained an error
// Interpretation is analogous to the one for modbusGetRequestError()
handleResponseError();
static ModbusError modbusGetRequestError(ModbusErrorInfo err)
Returns request error from ModbusErrorInfo.
Definition base.h:462
static uint8_t modbusIsOk(ModbusErrorInfo err)
Checks if ModbusErrorInfo contains an error.
Definition base.h:442
static ModbusError modbusGetGeneralError(ModbusErrorInfo err)
Returns general error from ModbusErrorInfo.
Definition base.h:452
static ModbusError modbusGetResponseError(ModbusErrorInfo err)
Returns response error from ModbusErrorInfo.
Definition base.h:472
Richer error represenation - source and type of error.
Definition base.h:126

modbusGetGeneralError(), modbusGetRequestError() and modbusGetResponseError() return a ModbusError enum value which can be easily handled with a switch statement.

You can find more information about errors returned by certain functions in their documentation. For example, please take a look at modbusParseResponseRTU().

See also
MODBUS_NO_ERROR()
MODBUS_GENERAL_ERROR()
MODBUS_REQUEST_ERROR()
MODBUS_RESPONSE_ERROR()
modbusGetErrorSource()
modbusGetErrorCode()
ModbusErrorInfo
Warning
You should not be accessing the fields of ModbusErrorInfo directly. They are not considered a part of the library interface, and hence are subject to change in future versions of the library. Instead, please use functions described above.