PL::ModbusClient class
-
class ModbusClient : public PL::ModbusBase
Modbus client class.
Public Functions
Creates a stream Modbus client.
- Parameters:
stream – stream
protocol – Modbus protocol
stationAddress – station address
bufferSize – transaction buffer size
-
ModbusClient(IpV4Address address, uint16_t port, size_t bufferSize = defaultBufferSize)
Creates a network Modbus client with IPv4 remote address.
- Parameters:
address – remote IPv4 address
port – remote port
bufferSize – transaction buffer size
-
ModbusClient(IpV6Address address, uint16_t port, size_t bufferSize = defaultBufferSize)
Creates a network Modbus client with IPv6 remote address.
- Parameters:
address – remote IPv6 address
port – remote port
bufferSize – transaction buffer size
Creates a network Modbus client using shared TCP client.
- Parameters:
tcpClient – TCP client
bufferSize – transaction buffer size
-
virtual esp_err_t Lock(TickType_t timeout = portMAX_DELAY) override
Locks the object.
- Parameters:
timeout – timeout in FreeRTOS ticks
- Returns:
error code
-
virtual esp_err_t Unlock() override
Unlocks the object.
- Returns:
error code
-
esp_err_t Command(ModbusFunctionCode functionCode, const void *requestData, size_t requestDataSize, void *responseData, size_t maxResponseDataSize, size_t *responseDataSize, ModbusException *exception)
Sends a Modbus request and returns response data.
- Parameters:
functionCode – request function code
requestData – request data pointer
requestDataSize – request data size
responseData – response data pointer
maxResponseDataSize – maximum response data size
responseDataSize – response data size
exception – Modbus exception
- Returns:
error code
-
esp_err_t ReadCoils(uint16_t address, uint16_t numberOfItems, void *responseData, ModbusException *exception)
Reads coils.
- Parameters:
address – first coil address
numberOfItems – number of coils
responseData – coil values (8 values per byte)
exception – Modbus exception
- Returns:
error code
-
esp_err_t ReadDiscreteInputs(uint16_t address, uint16_t numberOfItems, void *responseData, ModbusException *exception)
Reads discrete inputs.
- Parameters:
address – first discrete input address
numberOfItems – number of discrete inputs
responseData – discrete input values (8 values per byte)
exception – Modbus exception
- Returns:
error code
-
esp_err_t ReadHoldingRegisters(uint16_t address, uint16_t numberOfItems, void *responseData, ModbusException *exception)
Reads holding registers.
- Parameters:
address – first holding register address
numberOfItems – number of holding registers
responseData – holding register values
exception – Modbus exception
- Returns:
error code
-
esp_err_t ReadInputRegisters(uint16_t address, uint16_t numberOfItems, void *responseData, ModbusException *exception)
Reads input registers.
- Parameters:
address – first input register address
numberOfItems – number of input registers
responseData – input register values
exception – Modbus exception
- Returns:
error code
-
esp_err_t WriteSingleCoil(uint16_t address, bool value, ModbusException *exception)
Writes single coil.
- Parameters:
address – coil address
value – coil value
exception – Modbus exception
- Returns:
error code
-
esp_err_t WriteSingleHoldingRegister(uint16_t address, uint16_t value, ModbusException *exception)
Writes single holding register.
- Parameters:
address – holding register address
value – holding register value
exception – Modbus exception
- Returns:
error code
-
esp_err_t WriteMultipleCoils(uint16_t address, uint16_t numberOfItems, const void *requestData, ModbusException *exception)
Writes multiple coils.
- Parameters:
address – first coil address
numberOfItems – number of coils
requestData – coil values (8 values per byte)
exception – Modbus exception
- Returns:
error code
-
esp_err_t WriteMultipleHoldingRegisters(uint16_t address, uint16_t numberOfItems, const void *requestData, ModbusException *exception)
Writes multiple holding registers.
- Parameters:
address – first holding register address
numberOfItems – number of holding registers
requestData – holding register values
exception – Modbus exception
- Returns:
error code
-
uint8_t GetStationAddress()
Gets the Modbus station address.
- Returns:
station address
-
esp_err_t SetStationAddress(uint8_t address)
Sets the Modbus station address.
- Parameters:
address – station address
- Returns:
error code
-
ModbusProtocol GetProtocol()
Gets Modbus protocol.
- Returns:
protocol
-
virtual esp_err_t SetProtocol(ModbusProtocol protocol)
Sets Modbus protocol.
- Parameters:
protocol – protocol
- Returns:
error code
-
TickType_t GetReadTimeout()
Gets the read operation timeout.
- Returns:
timeout in FreeRTOS ticks
-
esp_err_t SetReadTimeout(TickType_t timeout)
Sets the read operation timeout.
- Parameters:
timeout – timeout in FreeRTOS ticks
- Returns:
error code
-
TickType_t GetDelayAfterRead()
Gets the delay between the end of the read operation and unlocking the stream.
- Returns:
delay in FreeRTOS ticks
-
esp_err_t SetDelayAfterRead(TickType_t delay)
Sets the delay between the end of the read operation and unlocking the stream.
- Parameters:
delay – delay in FreeRTOS ticks
- Returns:
error code
Public Static Attributes
-
static const TickType_t defaultReadTimeout = 300 / portTICK_PERIOD_MS
Default read operation timeout in FreeRTOS ticks.
-
static const size_t defaultBufferSize = 260
Default transaction buffer size.
-
static const ModbusProtocol defaultNetworkProtocol = ModbusProtocol::tcp
Default protocol for network interface.
-
static const uint8_t defaultNetworkStationAddress = 255
Default station address for network interface.
-
static const uint16_t maxNumberOfModbusBitsToRead = 2000
Maximum number of coils or discrete inputs that can be read in one request.
-
static const uint16_t maxNumberOfModbusBitsToWrite = 1968
Maximum number of coils that can be written in one request.
-
static const uint16_t maxNumberOfModbusRegistersToRead = 125
Maximum number of holding or input registers that can be read in one request.
-
static const uint16_t maxNumberOfModbusRegistersToWrite = 123
Maximum number of holding registers that can be written in one request.
Protected Functions
-
virtual esp_err_t ReadRtuData(Stream &stream, ModbusFunctionCode functionCode, size_t &dataSize) override
Reads the data for the specified function code (for Modbus RTU protocol).
- Parameters:
stream – stream to read from
functionCode – frame function code
dataSize – frame data size
- Returns:
error code
-
esp_err_t ReadFrame(Stream &stream, uint8_t &stationAddress, ModbusFunctionCode &functionCode, size_t &dataSize, uint16_t &transactionId)
Reads the Modbus frame.
- Parameters:
stream – stream to read from
stationAddress – frame station address
functionCode – frame function code
dataSize – frame data size
transactionId – frame transaction ID (for Modbus TCP protocol)
- Returns:
error code
-
esp_err_t WriteFrame(Stream &stream, uint8_t stationAddress, ModbusFunctionCode functionCode, size_t dataSize, uint16_t transactionId)
Writes the Modbus frame.
- Parameters:
stream – stream to write to
stationAddress – frame station address
functionCode – frame function code
dataSize – frame data size
transactionId – frame transaction ID (for Modbus TCP protocol)
- Returns:
error code