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
ModbusInterface
ModbusProtocol
ModbusMemoryType
ModbusFunctionCode
ModbusFunctionCode::unknown
ModbusFunctionCode::readCoils
ModbusFunctionCode::readDiscreteInputs
ModbusFunctionCode::readHoldingRegisters
ModbusFunctionCode::readInputRegisters
ModbusFunctionCode::writeSingleCoil
ModbusFunctionCode::writeSingleHoldingRegister
ModbusFunctionCode::writeMultipleCoils
ModbusFunctionCode::writeMultipleHoldingRegisters
ModbusException
ModbusException::noException
ModbusException::illegalFunction
ModbusException::illegalDataAddress
ModbusException::illegalDataValue
ModbusException::serverDeviceFailure
ModbusException::acknowledge
ModbusException::serverDeviceBusy
ModbusException::negativeAcknowledge
ModbusException::memoryParityError
ModbusException::gatewayPathUnavailable
ModbusException::gatewayTargetDeviceFailedToRespond
- PL::ModbusClient class
ModbusClient
ModbusClient::ModbusClient()
ModbusClient::ModbusClient()
ModbusClient::ModbusClient()
ModbusClient::ModbusClient()
ModbusClient::Lock()
ModbusClient::Unlock()
ModbusClient::Command()
ModbusClient::ReadCoils()
ModbusClient::ReadDiscreteInputs()
ModbusClient::ReadHoldingRegisters()
ModbusClient::ReadInputRegisters()
ModbusClient::WriteSingleCoil()
ModbusClient::WriteSingleHoldingRegister()
ModbusClient::WriteMultipleCoils()
ModbusClient::WriteMultipleHoldingRegisters()
ModbusClient::GetStationAddress()
ModbusClient::SetStationAddress()
ModbusClient::GetProtocol()
ModbusClient::SetProtocol()
ModbusClient::GetReadTimeout()
ModbusClient::SetReadTimeout()
ModbusClient::GetDelayAfterRead()
ModbusClient::SetDelayAfterRead()
ModbusClient::defaultReadTimeout
ModbusClient::defaultBufferSize
ModbusClient::defaultNetworkProtocol
ModbusClient::defaultNetworkStationAddress
ModbusClient::maxNumberOfModbusBitsToRead
ModbusClient::maxNumberOfModbusBitsToWrite
ModbusClient::maxNumberOfModbusRegistersToRead
ModbusClient::maxNumberOfModbusRegistersToWrite
ModbusClient::ReadRtuData()
ModbusClient::ReadFrame()
ModbusClient::WriteFrame()
ModbusClient::GetDataBuffer()
- PL::ModbusServer class
ModbusServer
ModbusServer::ModbusServer()
ModbusServer::ModbusServer()
ModbusServer::ModbusServer()
ModbusServer::ModbusServer()
ModbusServer::Lock()
ModbusServer::Unlock()
ModbusServer::Enable()
ModbusServer::Disable()
ModbusServer::AddMemoryArea()
ModbusServer::AddMemoryArea()
ModbusServer::IsEnabled()
ModbusServer::GetStationAddress()
ModbusServer::SetStationAddress()
ModbusServer::SetTaskParameters()
ModbusServer::GetBaseServer()
ModbusServer::GetProtocol()
ModbusServer::SetProtocol()
ModbusServer::GetReadTimeout()
ModbusServer::SetReadTimeout()
ModbusServer::GetDelayAfterRead()
ModbusServer::SetDelayAfterRead()
ModbusServer::GetName()
ModbusServer::SetName()
ModbusServer::enabledEvent
ModbusServer::disabledEvent
ModbusServer::defaultName
ModbusServer::defaultReadTimeout
ModbusServer::defaultBufferSize
ModbusServer::defaultNetworkProtocol
ModbusServer::defaultNetworkStationAddress
ModbusServer::maxNumberOfModbusBitsToRead
ModbusServer::maxNumberOfModbusBitsToWrite
ModbusServer::maxNumberOfModbusRegistersToRead
ModbusServer::maxNumberOfModbusRegistersToWrite
ModbusServer::ReadRtuData()
ModbusServer::HandleRequest()
ModbusServer::WriteExceptionFrame()
ModbusServer::ReadFrame()
ModbusServer::WriteFrame()
ModbusServer::GetDataBuffer()
- PL::ModbusMemoryArea class
ModbusMemoryArea
ModbusMemoryArea::ModbusMemoryArea()
ModbusMemoryArea::ModbusMemoryArea()
ModbusMemoryArea::ModbusMemoryArea()
ModbusMemoryArea::OnRead()
ModbusMemoryArea::OnWrite()
ModbusMemoryArea::Lock()
ModbusMemoryArea::Unlock()
ModbusMemoryArea::Clear()
ModbusMemoryArea::type
ModbusMemoryArea::address
ModbusMemoryArea::numberOfItems
ModbusMemoryArea::data
ModbusMemoryArea::size
- PL::ModbusTypedMemoryArea class
ModbusTypedMemoryArea
ModbusTypedMemoryArea::ModbusTypedMemoryArea()
ModbusTypedMemoryArea::ModbusTypedMemoryArea()
ModbusTypedMemoryArea::ModbusTypedMemoryArea()
ModbusTypedMemoryArea::OnRead()
ModbusTypedMemoryArea::OnWrite()
ModbusTypedMemoryArea::Lock()
ModbusTypedMemoryArea::Unlock()
ModbusTypedMemoryArea::Clear()
ModbusTypedMemoryArea::data
ModbusTypedMemoryArea::type
ModbusTypedMemoryArea::address
ModbusTypedMemoryArea::numberOfItems
ModbusTypedMemoryArea::size