Modbus Component
Installation
The component requires ESP-IDF 5.3 and higher and is installed using IDF Component Manager.
Add this to the idf_component.yml
in the project main
directory or in the component directory:
dependencies:
plasmapper/pl_modbus: "^1.2.1"
Add this to the source code:
#include "pl_modbus.h"
Add extern "C"
to the app_main
function:
extern "C" void app_main(void) {...}
Add this to the sdkconfig.defaults
in the project directory or configure the values using Project Configuration:
CONFIG_COMPILER_CXX_RTTI=y
CONFIG_LWIP_SO_RCVBUF=y
Features
PL::ModbusClient
- a Modbus client class.RTU, ASCII and TCP protocols via a single stream (UART, USB etc) or a network connection.
Implemented read/write functions (Modbus function codes):
PL::ModbusClient::ReadCoils()
/PL::ModbusClient::ReadDiscreteInputs()
/PL::ModbusClient::ReadHoldingRegisters()
/PL::ModbusClient::ReadInputRegisters()
(1/2/3/4)PL::ModbusClient::WriteSingleCoil()
/PL::ModbusClient::WriteSingleHoldingRegister()
(5/6)PL::ModbusClient::WriteMultipleCoils()
/PL::ModbusClient::WriteMultipleHoldingRegisters()
(15/16)
Splitting single read/write requests into multiple requests with valid number of memory elements.
Automatic reconnection to the device.
Support of multiple devices on the same stream or TCP client.
To implement other Modbus function codes:
Inherit
PL::ModbusClient
and overridePL::ModbusClient::ReadRtuData()
method to read custom function response data.Use public or protected
PL::ModbusClient::Command()
method (see the implemented read/write methods).
PL::ModbusServer
- a Modbus server class.RTU, ASCII and TCP protocols via a single stream (UART, USB etc) or a network connection.
Several
PL::ModbusServer::AddMemoryArea()
methods,PL::ModbusMemoryArea
andPL::ModbusTypedMemoryArea
classes to create simple and complex combinations of Modbus server memory areas.Same implemented read/write functions as for the client.
To implement other Modbus function codes:
Inherit
PL::ModbusServer
class and overridePL::ModbusServer::ReadRtuData()
method to read custom function request data.Override
PL::ModbusServer::HandleRequest()
method to handle the client request with a custom function code.
Thread safety
Class method thread safety is implemented by having the PL::Lockable
as a base class and creating the class object lock guard at the beginning of the methods.
The stream PL::ModbusClient
locks both the PL::ModbusClient
and the PL::Stream
objects for the duration of the transaction.
The network PL::ModbusClient
locks both the PL::ModbusClient
and the PL::TcpClient
objects for the duration of the transaction.
The stream PL::ModbusServer
task method locks both the underlying PL::StreamServer
and the PL::Stream
objects for the duration of the transaction.
The network PL::ModbusServer
task method locks both the underlying PL::TcpServer
and the client PL::NetworkStream
objects for the duration of the transaction.
The default PL::ModbusServer::HandleRequest()
locks the accessed PL::ModbusMemoryArea
for the duration of the transaction.
Examples
API reference
- Types
- PL::ModbusClient class
PL::ModbusClient
ModbusClient()
ModbusClient()
ModbusClient()
ModbusClient()
Lock()
Unlock()
Command()
ReadCoils()
ReadDiscreteInputs()
ReadHoldingRegisters()
ReadInputRegisters()
WriteSingleCoil()
WriteSingleHoldingRegister()
WriteMultipleCoils()
WriteMultipleHoldingRegisters()
GetStationAddress()
SetStationAddress()
GetProtocol()
SetProtocol()
GetReadTimeout()
SetReadTimeout()
GetDelayAfterRead()
SetDelayAfterRead()
defaultReadTimeout
defaultBufferSize
defaultNetworkProtocol
defaultNetworkStationAddress
maxNumberOfModbusBitsToRead
maxNumberOfModbusBitsToWrite
maxNumberOfModbusRegistersToRead
maxNumberOfModbusRegistersToWrite
ReadRtuData()
ReadFrame()
WriteFrame()
GetDataBuffer()
- PL::ModbusServer class
PL::ModbusServer
ModbusServer()
ModbusServer()
ModbusServer()
ModbusServer()
Lock()
Unlock()
Enable()
Disable()
AddMemoryArea()
AddMemoryArea()
IsEnabled()
GetStationAddress()
SetStationAddress()
SetTaskParameters()
GetBaseServer()
GetProtocol()
SetProtocol()
GetReadTimeout()
SetReadTimeout()
GetDelayAfterRead()
SetDelayAfterRead()
GetName()
SetName()
enabledEvent
disabledEvent
defaultName
defaultReadTimeout
defaultBufferSize
defaultNetworkProtocol
defaultNetworkStationAddress
maxNumberOfModbusBitsToRead
maxNumberOfModbusBitsToWrite
maxNumberOfModbusRegistersToRead
maxNumberOfModbusRegistersToWrite
ReadRtuData()
HandleRequest()
WriteExceptionFrame()
ReadFrame()
WriteFrame()
GetDataBuffer()
- PL::ModbusMemoryArea class
- PL::ModbusTypedMemoryArea class