Modbus improvements #84
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
ModbusConnectionManagerto support both TCP and RTU transports, including all necessary serial port parameters, and added logic to create and manage eitherModbusTcpClientorModbusSerialClientas appropriate. [1] [2]ModbusDeviceConfigand 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]Dependency and utility improvements:
pyserialas a dependency for RTU support inrequirements.txt.serial-portsto list available serial ports on the system, facilitating configuration and troubleshooting of RTU devices.