Myriota Flex SDK 2.5.0
Loading...
Searching...
No Matches
Modbus Library

Modbus communication interface. More...

Classes

struct  MYRIOTA_ModbusSerialInterface
 
struct  MYRIOTA_ModbusInitOptions
 

Typedefs

typedef uint8_t MYRIOTA_ModbusHandle
 
typedef uint8_t MYRIOTA_ModbusDeviceAddress
 
typedef uint16_t MYRIOTA_ModbusDataAddress
 
typedef int(* MYRIOTA_ModbusSerialInterfaceInitFn_t) (void *const ctx)
 
typedef void(* MYRIOTA_ModbusSerialInterfaceDeinitFn_t) (void *const ctx)
 
typedef ssize_t(* MYRIOTA_ModbusSerialInterfaceReadFn_t) (void *const ctx, uint8_t *const buffer, const size_t count)
 
typedef ssize_t(* MYRIOTA_ModbusSerialInterfaceWriteFn_t) (void *const ctx, const uint8_t *const buffer, const size_t count)
 

Enumerations

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 }
 

Functions

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)
 

Detailed Description

Modbus communication interface.

Typedef Documentation

◆ MYRIOTA_ModbusHandle

typedef uint8_t MYRIOTA_ModbusHandle

Modbus driver instance handle type.

◆ MYRIOTA_ModbusDeviceAddress

typedef uint8_t MYRIOTA_ModbusDeviceAddress

Modbus device address type.

◆ MYRIOTA_ModbusDataAddress

typedef uint16_t 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]ctxThe 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]ctxThe 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]ctxThe user defined data context used by the serial interface.
[out]bufferThe buffer for filling with bytes read by the serial device.
[in]countThe 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]ctxThe user defined data context used by the serial interface.
[out]bufferThe buffer of bytes to write to the serial device.
[in]countThe number of bytes to write from the buffer.
Returns
the number of bytes written on success, else < 0 on error.

Enumeration Type Documentation

◆ 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

Function Documentation

◆ MYRIOTA_ModbusInit()

MYRIOTA_ModbusHandle MYRIOTA_ModbusInit ( const MYRIOTA_ModbusInitOptions options)

Initializes a Modbus driver instance.

Parameters
[in]optionsThe driver options to initialise with.
Returns
a Modus handle > 0 on success else <= 0 on error.

◆ MYRIOTA_ModbusDeinit()

void MYRIOTA_ModbusDeinit ( const MYRIOTA_ModbusHandle handle)

De-initializes a Modbus driver instance.

Parameters
[in]handleThe handle for the Modbus driver to de-initialize.

◆ MYRIOTA_ModbusEnable()

int MYRIOTA_ModbusEnable ( const MYRIOTA_ModbusHandle handle)

Enables the Modbus driver.

Note
Disabling the modbus driver helps to preserve device power.
Parameters
[in]handleThe handle for the Modbus driver to enable.
Returns
a Modus handle > 0 on success else <= 0 on error.

◆ MYRIOTA_ModbusDisable()

int MYRIOTA_ModbusDisable ( const MYRIOTA_ModbusHandle handle)

Disable the Modbus driver.

Note
Disabling the modbus driver helps to preserve device power.
Parameters
[in]handleThe 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]bytesA buffer of bytes using Modbus's byte packing format.
[in]countThe size of the buffer must be at least 1
[in]bit_indexThe index of the bit you would like to set, where 0 =< index < (count / 8).
[in]valueThe 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]bytesA buffer of bytes using Modbus's byte packing format.
[in]countThe size of the buffer must be at least 1
[in]bit_indexThe index of the bit you would like to set, where 0 =< index < (count / 8).
[out]valueThe boolean value of the bit where 1 = true and 0 = false.

◆ MYRIOTA_ModbusReadCoils()

int MYRIOTA_ModbusReadCoils ( const MYRIOTA_ModbusHandle handle,
const MYRIOTA_ModbusDeviceAddress slave,
const MYRIOTA_ModbusDataAddress addr,
const size_t count,
uint8_t *const bytes )

Read the values of a list of consecutive coils.

Parameters
[in]handleThe handle for the Modbus driver to read from.
[in]slaveThe address of the slave device to read from.
[in]addrThe start address of the coils to read from.
[in]countThe number of coils to read.
[out]bytesThe 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()

int MYRIOTA_ModbusReadDiscreteInputs ( const MYRIOTA_ModbusHandle handle,
const MYRIOTA_ModbusDeviceAddress slave,
const MYRIOTA_ModbusDataAddress addr,
const size_t count,
uint8_t *const bytes )

Read the values of a list of consecutive discrete inputs.

Parameters
[in]handleThe handle for the Modbus driver to read from.
[in]slaveThe address of the slave device to read from.
[in]addrThe start address of the discrete inputs to read from.
[in]countThe number of discrete inputs to read.
[out]bytesThe 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()

int MYRIOTA_ModbusReadHoldingRegisters ( const MYRIOTA_ModbusHandle handle,
const MYRIOTA_ModbusDeviceAddress slave,
const MYRIOTA_ModbusDataAddress addr,
const size_t count,
uint8_t *const bytes )

Read the values of a list of consecutive holding registers.

Parameters
[in]handleThe handle for the Modbus driver to read from.
[in]slaveThe address of the slave device to read from.
[in]addrThe start address of the holding registers to read from.
[in]countThe number of holding registers to read.
[out]bytesThe 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()

int MYRIOTA_ModbusReadInputRegisters ( const MYRIOTA_ModbusHandle handle,
const MYRIOTA_ModbusDeviceAddress slave,
const MYRIOTA_ModbusDataAddress addr,
const size_t count,
uint8_t *const bytes )

Read the values of a list of consecutive input registers.

Parameters
[in]handleThe handle for the Modbus driver to read from.
[in]slaveThe address of the slave device to read from.
[in]addrThe start address of the holding registers to read from.
[in]countThe number of holding registers to read.
[out]bytesThe 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()

int MYRIOTA_ModbusWriteCoil ( const MYRIOTA_ModbusHandle handle,
const MYRIOTA_ModbusDeviceAddress slave,
const MYRIOTA_ModbusDataAddress addr,
const uint16_t word )

Write a value to a coil.

Parameters
[in]handleThe handle for the Modbus driver to write to.
[in]slaveThe address of the slave device to write to.
[in]addrThe address of the coils to write to.
[in]wordThe 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()

int MYRIOTA_ModbusWriteHoldingRegister ( const MYRIOTA_ModbusHandle handle,
const MYRIOTA_ModbusDeviceAddress slave,
const MYRIOTA_ModbusDataAddress addr,
const uint16_t word )

Write a value to a holding register.

Parameters
[in]handleThe handle for the Modbus driver to write to.
[in]slaveThe address of the slave device to write to.
[in]addrThe address of the holding registers to write to.
[in]wordThe word to write to the holding registers.
Returns
0 on success else < 0 on error.

◆ MYRIOTA_ModbusWriteCoils()

int MYRIOTA_ModbusWriteCoils ( const MYRIOTA_ModbusHandle handle,
const MYRIOTA_ModbusDeviceAddress slave,
const MYRIOTA_ModbusDataAddress addr,
const size_t count,
const uint8_t *const bytes )

Writes values to a list of consecutive coils.

Parameters
[in]handleThe handle for the Modbus driver to write from.
[in]slaveThe address of the slave device to write from.
[in]addrThe start address of the coils to write from.
[in]countThe number of coils to write.
[in]bytesThe 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()

int MYRIOTA_ModbusWriteHoldingRegisters ( const MYRIOTA_ModbusHandle handle,
const MYRIOTA_ModbusDeviceAddress slave,
const MYRIOTA_ModbusDataAddress addr,
const size_t count,
const uint8_t *const bytes )

Write values to a list of consecutive holding registers.

Parameters
[in]handleThe handle for the Modbus driver to write from.
[in]slaveThe address of the slave device to write from.
[in]addrThe start address of the input registers to write from.
[in]countThe number of input registers to write.
[in]bytesThe 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.