PL::ModbusServer class
-
class ModbusServer : public PL::ModbusBase, public PL::Server
Modbus server class.
Subclassed by PL::BlackBoxModbusServer
Public Functions
Creates a stream Modbus server with shared transaction buffer.
- Parameters:
stream – stream
protocol – Modbus protocol
stationAddress – station address
buffer – transaction buffer
Creates an stream Modbus server and allocates transaction buffer.
- Parameters:
stream – stream
protocol – Modbus protocol
stationAddress – station address
bufferSize – transaction buffer size
Creates a network Modbus server with shared transaction buffer.
- Parameters:
port – network port
buffer – transaction buffer
-
ModbusServer(uint16_t port, size_t bufferSize = defaultBufferSize)
Creates a network Modbus server and allocates transaction buffer.
- Parameters:
port – network port
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
-
virtual esp_err_t Enable() override
Enables the server.
- Returns:
error code
-
virtual esp_err_t Disable() override
Disables the server.
- Returns:
error code
Adds a Modbus memory area to the server.
- Parameters:
memoryArea – memory area
- Returns:
error code
Adds buffer as a Modbus memory area to the server.
- Parameters:
type – memory area type
address – memory area address
buffer – buffer
-
virtual bool IsEnabled() override
Checks if the server is enabled.
- Returns:
true if the server is enabled
-
uint8_t GetStationAddress()
Gets the Modbus station address.
- Returns:
station address
-
esp_err_t SetStationAddress(uint8_t stationAddress)
Sets the Modbus station address.
- Parameters:
stationAddress – station address
- Returns:
error code
-
esp_err_t SetTaskParameters(const TaskParameters &taskParameters)
Sets the server task parameters.
- Parameters:
taskParameters – task parameters
- Returns:
error code
-
std::weak_ptr<Server> GetBaseServer()
Gets the base server (StreamServer or TcpServer)
- Returns:
base server
-
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
-
std::string GetName()
Gets the server name.
- Returns:
name
-
void SetName(const std::string &name)
Sets the server name.
- Parameters:
name – name
Public Members
Public Static Attributes
-
static const std::string defaultName
Default server name.
-
static const TickType_t defaultReadTimeout = 2
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
-
virtual esp_err_t HandleRequest(Stream &stream, uint8_t stationAddress, ModbusFunctionCode functionCode, size_t dataSize, uint16_t transactionId)
Handles the Modbus client request.
- Parameters:
stream – client stream
stationAddress – request station address
functionCode – request function code
dataSize – request data size
transactionId – request transaction ID (for Modbus TCP protocol)
- Returns:
error code
-
esp_err_t WriteExceptionFrame(Stream &stream, uint8_t stationAddress, ModbusFunctionCode functionCode, ModbusException exception, uint16_t transactionId)
Writes the Modbus exception frame.
- Parameters:
stream – client stream
stationAddress – frame station address
functionCode – frame function code
exception – Modbus exception
transactionId – frame transaction ID (for Modbus TCP protocol)
- 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