Modbus communication interface.
More...
|
enum | MYRIOTA_ModbusErrors {
MODBUS_SUCCESS = 0
, MODBUS_ERROR_EXCEPTION_ILLEGAL_FUNCTION = 0x01
, MODBUS_ERROR_EXCEPTION_ILLEGAL_DATA_ADDRESS = 0x02
, MODBUS_ERROR_EXCEPTION_ILLEGAL_DATA_VALUE = 0x03
,
MODBUS_ERROR_EXCEPTION_SLAVE_DEVICE_FAILURE = 0x04
, MODBUS_ERROR_EXCEPTION_ACKNOWLEDGE = 0x05
, MODBUS_ERROR_EXCEPTION_SLAVE_DEVICE_BUSY = 0x06
, MODBUS_ERROR_EXCEPTION_NEGATIVE_ACKNOWLEDGMENT = 0x07
,
MODBUS_ERROR_EXCEPTION_MEMORY_PARITY_ERROR = 0x08
, MODBUS_ERROR_EXCEPTION_GATEWAY_PATH_UNAVAILABLE = 0x0A
, MODBUS_ERROR_EXCEPTION_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND = 0x0B
, MODBUS_ERROR_INVALID_HANDLE
,
MODBUS_ERROR_INVALID_CRC16
, MODBUS_ERROR_MALFORMED_RESPONSE
, MODBUS_ERROR_RESPONSE_FROM_WRONG_SLAVE_ADDRESS
, MODBUS_ERROR_IO_FAILURE
,
MODBUS_ERROR_BAD_STATE
, MODBUS_ERROR_OVERFLOW
} |
|
enum | MYRIOTA_ModbusFramingMode { MODBUS_FRAMING_MODE_RTU
} |
|
|
MYRIOTA_ModbusHandle | MYRIOTA_ModbusInit (const MYRIOTA_ModbusInitOptions options) |
|
void | MYRIOTA_ModbusDeinit (const MYRIOTA_ModbusHandle handle) |
|
int | MYRIOTA_ModbusEnable (const MYRIOTA_ModbusHandle handle) |
|
int | MYRIOTA_ModbusDisable (const MYRIOTA_ModbusHandle handle) |
|
void | MYRIOTA_ModbusBytesSetBit (uint8_t *const bytes, const size_t count, const uint8_t bit_index, const bool value) |
|
void | MYRIOTA_ModbusBytesGetBit (const uint8_t *const bytes, const size_t count, const uint8_t bit_index, bool *const value) |
|
int | MYRIOTA_ModbusReadCoils (const MYRIOTA_ModbusHandle handle, const MYRIOTA_ModbusDeviceAddress slave, const MYRIOTA_ModbusDataAddress addr, const size_t count, uint8_t *const bytes) |
|
int | MYRIOTA_ModbusReadDiscreteInputs (const MYRIOTA_ModbusHandle handle, const MYRIOTA_ModbusDeviceAddress slave, const MYRIOTA_ModbusDataAddress addr, const size_t count, uint8_t *const bytes) |
|
int | MYRIOTA_ModbusReadHoldingRegisters (const MYRIOTA_ModbusHandle handle, const MYRIOTA_ModbusDeviceAddress slave, const MYRIOTA_ModbusDataAddress addr, const size_t count, uint8_t *const bytes) |
|
int | MYRIOTA_ModbusReadInputRegisters (const MYRIOTA_ModbusHandle handle, const MYRIOTA_ModbusDeviceAddress slave, const MYRIOTA_ModbusDataAddress addr, const size_t count, uint8_t *const bytes) |
|
int | MYRIOTA_ModbusWriteCoil (const MYRIOTA_ModbusHandle handle, const MYRIOTA_ModbusDeviceAddress slave, const MYRIOTA_ModbusDataAddress addr, const uint16_t word) |
|
int | MYRIOTA_ModbusWriteHoldingRegister (const MYRIOTA_ModbusHandle handle, const MYRIOTA_ModbusDeviceAddress slave, const MYRIOTA_ModbusDataAddress addr, const uint16_t word) |
|
int | MYRIOTA_ModbusWriteCoils (const MYRIOTA_ModbusHandle handle, const MYRIOTA_ModbusDeviceAddress slave, const MYRIOTA_ModbusDataAddress addr, const size_t count, const uint8_t *const bytes) |
|
int | MYRIOTA_ModbusWriteHoldingRegisters (const MYRIOTA_ModbusHandle handle, const MYRIOTA_ModbusDeviceAddress slave, const MYRIOTA_ModbusDataAddress addr, const size_t count, const uint8_t *const bytes) |
|
Modbus communication interface.
◆ MYRIOTA_ModbusHandle
Modbus driver instance handle type.
◆ MYRIOTA_ModbusDeviceAddress
Modbus device address type.
◆ MYRIOTA_ModbusDataAddress
Modbus data (i.e. coils/registers) address type.
◆ MYRIOTA_ModbusSerialInterfaceInitFn_t
typedef int(* MYRIOTA_ModbusSerialInterfaceInitFn_t) (void *const ctx) |
Initialization function for the serial interface used by Modbus driver.
- Parameters
-
[in,out] | ctx | The user defined data context used by the serial interface. |
- Returns
- 0 on success else < 0 on error.
◆ MYRIOTA_ModbusSerialInterfaceDeinitFn_t
typedef void(* MYRIOTA_ModbusSerialInterfaceDeinitFn_t) (void *const ctx) |
De-initialization function for the serial interface used by Modbus driver.
- Parameters
-
[in,out] | ctx | The user defined data context used by the serial interface. |
- Returns
- the number of bytes written on success else < 0 on error.
◆ MYRIOTA_ModbusSerialInterfaceReadFn_t
typedef ssize_t(* MYRIOTA_ModbusSerialInterfaceReadFn_t) (void *const ctx, uint8_t *const buffer, const size_t count) |
Read function for the serial interface used by Modbus driver.
- Parameters
-
[in,out] | ctx | The user defined data context used by the serial interface. |
[out] | buffer | The buffer for filling with bytes read by the serial device. |
[in] | count | The total size of the buffer in bytes. |
- Returns
- the number of bytes read on success, else < 0 on error.
◆ MYRIOTA_ModbusSerialInterfaceWriteFn_t
typedef ssize_t(* MYRIOTA_ModbusSerialInterfaceWriteFn_t) (void *const ctx, const uint8_t *const buffer, const size_t count) |
Write function for the serial interface used by Modbus driver.
- Parameters
-
[in,out] | ctx | The user defined data context used by the serial interface. |
[out] | buffer | The buffer of bytes to write to the serial device. |
[in] | count | The number of bytes to write from the buffer. |
- Returns
- the number of bytes written on success, else < 0 on error.
◆ MYRIOTA_ModbusErrors
Error and exception codes for the modbus driver.
◆ MYRIOTA_ModbusFramingMode
The framing mode to be used by the Modbus driver.
- Note
- Only RTU framing is supported at the moment, but ASCII framing will be added in the future.
Enumerator |
---|
MODBUS_FRAMING_MODE_RTU | RTU (Remote Transmission Unit) Framing
|
◆ MYRIOTA_ModbusInit()
Initializes a Modbus driver instance.
- Parameters
-
[in] | options | The driver options to initialise with. |
- Returns
- a Modus handle > 0 on success else <= 0 on error.
◆ MYRIOTA_ModbusDeinit()
De-initializes a Modbus driver instance.
- Parameters
-
[in] | handle | The handle for the Modbus driver to de-initialize. |
◆ MYRIOTA_ModbusEnable()
Enables the Modbus driver.
- Note
- Disabling the modbus driver helps to preserve device power.
- Parameters
-
[in] | handle | The handle for the Modbus driver to enable. |
- Returns
- a Modus handle > 0 on success else <= 0 on error.
◆ MYRIOTA_ModbusDisable()
Disable the Modbus driver.
- Note
- Disabling the modbus driver helps to preserve device power.
- Parameters
-
[in] | handle | The handle for the Modbus driver to disable. |
- Returns
- a Modus handle > 0 on success else <= 0 on error.
◆ MYRIOTA_ModbusBytesSetBit()
void MYRIOTA_ModbusBytesSetBit |
( |
uint8_t *const | bytes, |
|
|
const size_t | count, |
|
|
const uint8_t | bit_index, |
|
|
const bool | value ) |
Given a buffer of bytes packed using Modbus's byte format set the value of the bit at the given index.
- Note
- In this context
bit_index
can be interpreted as the (coil_address - coil_start_address) where coil_start_address is bit_index zero.
- Parameters
-
[out] | bytes | A buffer of bytes using Modbus's byte packing format. |
[in] | count | The size of the buffer must be at least 1 |
[in] | bit_index | The index of the bit you would like to set, where 0 =< index < (count / 8). |
[in] | value | The boolean value of the bit where 1 = true and 0 = false. |
◆ MYRIOTA_ModbusBytesGetBit()
void MYRIOTA_ModbusBytesGetBit |
( |
const uint8_t *const | bytes, |
|
|
const size_t | count, |
|
|
const uint8_t | bit_index, |
|
|
bool *const | value ) |
Given a buffer of bytes packed using Modbus's byte format get the value of the bit at the given index.
- Note
- In this context
bit_index
can be interpreted as the (coil_address - coil_start_address) where coil_start_address is bit_index zero.
- Parameters
-
[in] | bytes | A buffer of bytes using Modbus's byte packing format. |
[in] | count | The size of the buffer must be at least 1 |
[in] | bit_index | The index of the bit you would like to set, where 0 =< index < (count / 8). |
[out] | value | The boolean value of the bit where 1 = true and 0 = false. |
◆ MYRIOTA_ModbusReadCoils()
Read the values of a list of consecutive coils.
- Parameters
-
[in] | handle | The handle for the Modbus driver to read from. |
[in] | slave | The address of the slave device to read from. |
[in] | addr | The start address of the coils to read from. |
[in] | count | The number of coils to read. |
[out] | bytes | The buffer to fill with the values of the coils where the size of the buffer must = ceil((count / 8)). (NOTE: Shall be in Modbus MSB bit packed format) |
- Returns
- 0 on success else < 0 on error.
◆ MYRIOTA_ModbusReadDiscreteInputs()
Read the values of a list of consecutive discrete inputs.
- Parameters
-
[in] | handle | The handle for the Modbus driver to read from. |
[in] | slave | The address of the slave device to read from. |
[in] | addr | The start address of the discrete inputs to read from. |
[in] | count | The number of discrete inputs to read. |
[out] | bytes | The buffer to fill with the values of the discrete inputs where the size of the buffer must = ceil((count / 8)). (NOTE: Shall be in Modbus MSB bit packed format) |
- Returns
- 0 on success else < 0 on error
◆ MYRIOTA_ModbusReadHoldingRegisters()
Read the values of a list of consecutive holding registers.
- Parameters
-
[in] | handle | The handle for the Modbus driver to read from. |
[in] | slave | The address of the slave device to read from. |
[in] | addr | The start address of the holding registers to read from. |
[in] | count | The number of holding registers to read. |
[out] | bytes | The buffer to fill with the values of the holding registers, where the size of the buffer must = count * 2. |
- Returns
- 0 on success else < 0 on error.
◆ MYRIOTA_ModbusReadInputRegisters()
Read the values of a list of consecutive input registers.
- Parameters
-
[in] | handle | The handle for the Modbus driver to read from. |
[in] | slave | The address of the slave device to read from. |
[in] | addr | The start address of the holding registers to read from. |
[in] | count | The number of holding registers to read. |
[out] | bytes | The buffer to fill with the values of the input registers, where the size of the buffer must = count * 2. |
- Returns
- 0 on success else < 0 on error.
◆ MYRIOTA_ModbusWriteCoil()
Write a value to a coil.
- Parameters
-
[in] | handle | The handle for the Modbus driver to write to. |
[in] | slave | The address of the slave device to write to. |
[in] | addr | The address of the coils to write to. |
[in] | word | The word to write to the coil, where word must be either 0x00FF = 1 or 0x0000 = 0 (NOTE: These values are the Modbus MSB packed format). |
- Returns
- 0 on success else < 0 on error.
◆ MYRIOTA_ModbusWriteHoldingRegister()
Write a value to a holding register.
- Parameters
-
[in] | handle | The handle for the Modbus driver to write to. |
[in] | slave | The address of the slave device to write to. |
[in] | addr | The address of the holding registers to write to. |
[in] | word | The word to write to the holding registers. |
- Returns
- 0 on success else < 0 on error.
◆ MYRIOTA_ModbusWriteCoils()
Writes values to a list of consecutive coils.
- Parameters
-
[in] | handle | The handle for the Modbus driver to write from. |
[in] | slave | The address of the slave device to write from. |
[in] | addr | The start address of the coils to write from. |
[in] | count | The number of coils to write. |
[in] | bytes | The buffer to fill with the values of the coils where the size of the buffer must = ceil((count / 8)). (NOTE: Must be in Modbus MSB bit packed format) |
- Returns
- 0 on success else < 0 on error.
◆ MYRIOTA_ModbusWriteHoldingRegisters()
Write values to a list of consecutive holding registers.
- Parameters
-
[in] | handle | The handle for the Modbus driver to write from. |
[in] | slave | The address of the slave device to write from. |
[in] | addr | The start address of the input registers to write from. |
[in] | count | The number of input registers to write. |
[in] | bytes | The buffer to fill with the values of the holding registers where the size of the buffer must = count * 2. |
- Returns
- 0 on success else < 0 on error.