ModbusTCP Class Documentation
ModbusTCP
Section titled “ModbusTCP”Path: src/modbus/ModbusTCP.h
Revision History: Initial documentation
The ModbusTCP class manages Modbus TCP communication, acting as a Server/Slave. It listens for Modbus TCP requests, finds the target component, and uses the Component’s network interface (read/mb_tcp_write) to process the request.
REQUIREMENTS
Section titled “REQUIREMENTS”- ESP32 with TCP/IP connectivity
- ModbusServerTCPasync instance (from eModbus library)
- Component hierarchy with a parent owner component (typically PHApp)
PROVIDES
Section titled “PROVIDES”- ModbusTCP Class: Main class for managing Modbus TCP server functionality
- Address Mapping Functionality: Maps Modbus addresses to component functions
FEATURES
Section titled “FEATURES”- Handles Modbus TCP slave/server functionality
- Supports standard Modbus function codes:
- Read Coils (01)
- Read Holding Registers (03)
- Write Single Coil (05)
- Write Single Register (06)
- Write Multiple Coils (15)
- Write Multiple Registers (16)
- Manages address mapping between Modbus addresses and components
- Routes Modbus requests to appropriate component handlers
- Provides component registration system for Modbus address ranges
- Maps internal errors to Modbus exception codes
DEPENDENCIES
Section titled “DEPENDENCIES”BEHAVIOUR
Section titled “BEHAVIOUR”EXAMPLE
Section titled “EXAMPLE”#include "ModbusTCP.h"
// Create ModbusServerTCPasync instanceModbusServerTCPasync mbTCPServer;
// Create and initialize ModbusTCPModbusTCP modbusTCP(appComponent, &mbTCPServer);
// Register component with Modbus address blockMB_Registers tempSensorRegisters = { .startAddress = 1000, .count = 10, .type = FN_READ_HOLD_REGISTER, .access = MB_ACCESS_READ_WRITE, .componentId = 0 // Will be filled by ModbusTCP};modbusTCP.registerModbus(temperatureComponent, tempSensorRegisters);
// Setup and start the Modbus TCP managermodbusTCP.setup();
// Call this regularly in the main loopmodbusTCP.loop();
PERFORMANCE
Section titled “PERFORMANCE”- Consider implementing a more efficient lookup method for address mappings (e.g., hash map) for projects with many components
- Add optional caching for frequently read values to reduce component calls
- Optimize byte packing/unpacking for multi-coil operations
SECURITY
Section titled “SECURITY”- Add IP filtering capability to restrict access to authorized clients
- Implement authentication mechanism for Modbus TCP connections
- Consider adding encrypted Modbus TCP support for sensitive applications
COMPLIANCE
Section titled “COMPLIANCE”- Ensure full compliance with Modbus Application Protocol Specification V1.1b3
- Add support for other function codes (Read Input Registers, Read Discrete Inputs) if needed
RECOMMENDATIONS
Section titled “RECOMMENDATIONS”- Create unit tests to validate mapping and routing functionality
- Maintain detailed logs of Modbus communications for debugging
- Implement a strict mode that returns exceptions for unmapped addresses (currently configurable)
- Consider adding diagnostic counters for monitoring communication quality