Skip to content

Modbus improvements #84

Merged
thiagoralves merged 6 commits into
mainfrom
development
Jan 22, 2026
Merged

Modbus improvements #84
thiagoralves merged 6 commits into
mainfrom
development

Conversation

@thiagoralves
Copy link
Copy Markdown
Contributor

This pull request adds support for Modbus RTU (serial) devices in addition to Modbus TCP, significantly expanding the flexibility of the Modbus Master plugin. It introduces new configuration options, validation logic, and connection management for RTU devices, while maintaining backward compatibility for existing TCP configurations. Additionally, a utility endpoint for listing available serial ports is added to the webserver.

Modbus RTU support and configuration enhancements:

  • Extended ModbusConnectionManager to support both TCP and RTU transports, including all necessary serial port parameters, and added logic to create and manage either ModbusTcpClient or ModbusSerialClient as appropriate. [1] [2]
  • Updated ModbusDeviceConfig and its parsing/validation logic to handle RTU-specific fields (e.g., serial_port, baud_rate, parity, etc.), and added comprehensive validation for both TCP and RTU devices, including checks for duplicate slave IDs on the same RTU bus. [1] [2] [3] [4] [5]
  • Improved string representations and error messages throughout to provide clear feedback for both TCP and RTU devices. [1] [2] [3] [4]

Dependency and utility improvements:

  • Added pyserial as a dependency for RTU support in requirements.txt.
  • Introduced a new webserver handler serial-ports to list available serial ports on the system, facilitating configuration and troubleshooting of RTU devices.

thiagoralves and others added 6 commits January 19, 2026 12:05
Add Slave ID (Unit ID) support to the Modbus master plugin to enable
communication through Modbus TCP gateways that forward messages to RS485
serial networks. The Slave ID is passed to all pymodbus read/write
operations via the 'slave' parameter.

Changes:
- Add slave_id field to ModbusDeviceConfig with validation (0-255)
- Parse slave_id from JSON configuration (default 1)
- Store slave_id in ModbusConnectionManager
- Pass slave parameter to all Modbus operations (FC 1-6, 15, 16)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
pymodbus 3.10+ renamed the 'slave' parameter to 'device_id' for all
client read/write methods. Update all Modbus operations to use the
correct parameter name.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
feat: Add Slave ID support for Modbus TCP master plugin
This commit adds Modbus RTU (serial) communication support to the
modbus master plugin, enabling serial communication with Modbus slave
devices alongside existing TCP functionality.

Key changes:

1. Serial ports REST endpoint (webserver/app.py):
   - New GET endpoint at /api/serial-ports
   - Lists available serial ports using pyserial

2. Connection manager refactoring (modbus_master_connection.py):
   - Support for both TCP and RTU transport types
   - Factory method _create_client() for transport-specific clients
   - RTU parameters: serial_port, baud_rate, parity, stop_bits, data_bits

3. Config model updates (modbus_master_config_model.py):
   - TransportType and ParityType type literals
   - RTU-specific fields in ModbusDeviceConfig
   - Transport-specific validation rules
   - Multi-drop validation (duplicate slave IDs on same bus)

4. Plugin updates (modbus_master_plugin.py):
   - New ModbusRtuBusHandler class for multi-drop RTU support
   - One thread per serial bus, handling multiple slave IDs
   - group_rtu_devices_by_bus() helper function
   - Updated start_loop() to separate TCP and RTU device handling

5. Dependencies (requirements.txt):
   - Added pyserial>=3.5

Multi-drop RTU architecture:
- Devices on same serial port share a single connection
- Each device has unique slave_id (1-247)
- IO operations include slave_id to route to correct device
- Bus grouping key: serial_port:baud_rate:parity:stop_bits:data_bits

Backward compatibility:
- Existing TCP configurations work without changes
- Missing transport field defaults to "tcp"

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The handle_list_serial_ports endpoint in app.py needs pyserial to
enumerate available serial ports, but it runs in the main webserver
context. Previously pyserial was only installed in the modbus_master
plugin's virtual environment.

This fix adds pyserial to the main requirements.txt so the serial
port listing API endpoint works correctly.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
feat: Add Modbus RTU support with multi-drop architecture
@thiagoralves thiagoralves merged commit 238107a into main Jan 22, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant