Skip to content

ModbusTCP Class Documentation

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.

  • ESP32 with TCP/IP connectivity
  • ModbusServerTCPasync instance (from eModbus library)
  • Component hierarchy with a parent owner component (typically PHApp)
  • ModbusTCP Class: Main class for managing Modbus TCP server functionality
  • Address Mapping Functionality: Maps Modbus addresses to component functions
  • 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

ModbusTCP

Component

enums

ModbusTypes

ModbusServerTCPasync

Vector

ArduinoLog

config-modbus.h

ComponentModbusTCP ServerModbus TCP ClientComponentModbusTCP ServerModbus TCP Clientalt[Component found][No component or mapping found]TCP ConnectionModbus Request (Function Code + Address)Find component for addressmb_tcp_read() or mb_tcp_write()Return data/statusModbus Response with dataException Response

#include "ModbusTCP.h"
// Create ModbusServerTCPasync instance
ModbusServerTCPasync mbTCPServer;
// Create and initialize ModbusTCP
ModbusTCP modbusTCP(appComponent, &mbTCPServer);
// Register component with Modbus address block
MB_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 manager
modbusTCP.setup();
// Call this regularly in the main loop
modbusTCP.loop();
  • 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
  • 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
  • Ensure full compliance with Modbus Application Protocol Specification V1.1b3
  • Add support for other function codes (Read Input Registers, Read Discrete Inputs) if needed
  • 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