-
-
Notifications
You must be signed in to change notification settings - Fork 276
feat: add remote modbus rtu driver support, refactor ModbusRTUDriver to use SerialPort directly #1394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
flxzt
wants to merge
1
commit into
labgrid-project:master
Choose a base branch
from
flxzt:remote-modbus-rtu
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add remote modbus rtu driver support, refactor ModbusRTUDriver to use SerialPort directly #1394
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| targets: | ||
| main: | ||
| resources: | ||
| ModbusRTU: | ||
| SerialPort: | ||
| port: "/dev/ttyUSB0" | ||
| address: 16 | ||
| speed: 115200 | ||
| timeout: 0.25 | ||
| drivers: | ||
| ModbusRTUDriver: {} | ||
| ModbusRTUDriver: | ||
| address: 16 | ||
| timeout: 0.25 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,107 @@ | ||
| from importlib import import_module | ||
|
|
||
| import attr | ||
| import serial | ||
| import serial.rfc2217 | ||
|
|
||
| from ..factory import target_factory | ||
| from ..resource import SerialPort | ||
| from ..util.proxy import proxymanager | ||
| from .common import Driver | ||
|
|
||
|
|
||
| @target_factory.reg_driver | ||
| @attr.s(eq=False) | ||
| class ModbusRTUDriver(Driver): | ||
| bindings = {"resource": "ModbusRTU"} | ||
| bindings = {"resource": {"SerialPort", "NetworkSerialPort"}} | ||
|
|
||
| timeout = attr.ib(default=0.25, validator=attr.validators.instance_of(float)) | ||
| address = attr.ib(default=0, validator=attr.validators.instance_of(int)) | ||
|
|
||
| def __attrs_post_init__(self): | ||
| super().__attrs_post_init__() | ||
| self._modbus = import_module('minimalmodbus') | ||
| self._modbus = import_module("minimalmodbus") | ||
| self.instrument = None | ||
|
|
||
| def on_activate(self): | ||
| self.instrument = self._modbus.Instrument( | ||
| self.resource.port, | ||
| self.resource.address, | ||
| debug=False) | ||
| if isinstance(self.resource, SerialPort): | ||
| self.instrument = self._modbus.Instrument( | ||
| self.resource.port, self.address, debug=False | ||
| ) | ||
| else: | ||
| if self.resource.protocol == "rfc2217": | ||
| serial_if = serial.rfc2217.Serial() | ||
| elif self.resource.protocol == "raw": | ||
| serial_if = serial.serial_for_url("socket://", do_not_open=True) | ||
| else: | ||
| raise Exception("ModbusRTUDriver: unknown protocol") | ||
|
|
||
| host, port = proxymanager.get_host_and_port(self.resource) | ||
| if self.resource.protocol == "rfc2217": | ||
| serial_if.port = ( | ||
| f"rfc2217://{host}:{port}?ign_set_control&timeout={self.timeout}" | ||
| ) | ||
| elif self.resource.protocol == "raw": | ||
| serial_if.port = f"socket://{host}:{port}/" | ||
| else: | ||
| raise Exception("ModbusRTUDriver: unknown protocol") | ||
| serial_if.baudrate = self.resource.speed | ||
| serial_if.open() | ||
|
|
||
| self.instrument = self._modbus.Instrument( | ||
| serial_if, | ||
| slaveaddress=self.address, | ||
| close_port_after_each_call=True, | ||
| debug=False, | ||
| ) | ||
|
|
||
| self.instrument.serial.baudrate = self.resource.speed | ||
| self.instrument.serial.timeout = self.resource.timeout | ||
| self.instrument.serial.timeout = self.timeout | ||
|
|
||
| self.instrument.mode = self._modbus.MODE_RTU | ||
| self.instrument.clear_buffers_before_each_transaction = True | ||
|
|
||
| def on_deactivate(self): | ||
| self.instrument = None | ||
|
|
||
| def read_register(self, *args, **kwargs): | ||
| return self.instrument.read_register(*args, **kwargs) | ||
|
|
||
| def write_register(self, *args, **kwargs): | ||
| return self.instrument.write_register(*args, **kwargs) | ||
|
|
||
| def read_registers(self, *args, **kwargs): | ||
| return self.instrument.read_registers(*args, **kwargs) | ||
|
|
||
| def write_registers(self, *args, **kwargs): | ||
| return self.instrument.write_registers(*args, **kwargs) | ||
|
|
||
| def read_bit(self, *args, **kwargs): | ||
| return self.instrument.read_bit(*args, **kwargs) | ||
|
|
||
| def write_bit(self, *args, **kwargs): | ||
| return self.instrument.write_bit(*args, **kwargs) | ||
|
|
||
| def read_bits(self, *args, **kwargs): | ||
| return self.instrument.read_bits(*args, **kwargs) | ||
|
|
||
| def write_bits(self, *args, **kwargs): | ||
| return self.instrument.write_bits(*args, **kwargs) | ||
|
|
||
| def read_long(self, *args, **kwargs): | ||
| return self.instrument.read_long(*args, **kwargs) | ||
|
|
||
| def write_long(self, *args, **kwargs): | ||
| return self.instrument.write_long(*args, **kwargs) | ||
|
|
||
| def read_float(self, *args, **kwargs): | ||
| return self.instrument.read_float(*args, **kwargs) | ||
|
|
||
| def write_float(self, *args, **kwargs): | ||
| return self.instrument.write_float(*args, **kwargs) | ||
|
|
||
| def read_string(self, *args, **kwargs): | ||
| return self.instrument.read_string(*args, **kwargs) | ||
|
|
||
| def write_string(self, *args, **kwargs): | ||
| return self.instrument.write_string(*args, **kwargs) | ||
|
|
||
| def read_register(self, *args, **kwargs): | ||
| return self.instrument.read_register(*args, **kwargs) | ||
|
|
||
| def write_register(self, *args, **kwargs): | ||
| return self.instrument.write_register(*args, **kwargs) | ||
|
|
||
| def read_registers(self, *args, **kwargs): | ||
| return self.instrument.read_registers(*args, **kwargs) | ||
|
|
||
| def write_registers(self, *args, **kwargs): | ||
| return self.instrument.write_registers(*args, **kwargs) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,18 @@ | ||
| import warnings | ||
|
|
||
| import attr | ||
|
|
||
| from ..factory import target_factory | ||
| from .common import Resource | ||
| from .base import SerialPort | ||
|
|
||
|
|
||
| @target_factory.reg_resource | ||
| @attr.s(eq=False) | ||
| class ModbusRTU(SerialPort, Resource): | ||
|
flxzt marked this conversation as resolved.
|
||
| """This resource describes Modbus RTU instrument. | ||
|
|
||
| Args: | ||
| port (str): tty the instrument is connected to, e.g. '/dev/ttyUSB0' | ||
| speed (int): optional, default is 115200 | ||
| address (int): slave address on the modbus, e.g. 16 | ||
| timeout (float): optional, timeout in seconds. Default is 0.25 s | ||
| """ | ||
|
|
||
| address = attr.ib(default=None, validator=attr.validators.instance_of(int)) | ||
| timeout = attr.ib(default=0.25, | ||
| validator=attr.validators.instance_of(float)) | ||
|
|
||
| def __attrs_post_init__(self): | ||
| super().__attrs_post_init__() | ||
| if self.port is None: | ||
| raise ValueError("ModbusRTU must be configured with a port") | ||
| if self.address is None: | ||
| raise ValueError("ModbusRTU must be configured with an slave address") | ||
| class ModbusRTU(): | ||
| def __new__(cls, *args, **kwargs): | ||
| warnings.warn( | ||
| "The ModbusRTU class is deprecated. Use SerialPort instead.", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
| return SerialPort(*args, **kwargs) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,20 @@ | ||
| from labgrid.resource.modbusrtu import ModbusRTU | ||
| from labgrid.driver.modbusrtudriver import ModbusRTUDriver | ||
|
|
||
| import pytest | ||
|
|
||
| def test_resource_with_minimum_argument(target): | ||
| dut = ModbusRTU(target, name=None, port="/dev/tty1", address=10) | ||
|
|
||
| assert dut.port == "/dev/tty1" | ||
| assert dut.address == 10 | ||
| assert dut.speed == 115200 | ||
| assert dut.timeout == 0.25 | ||
|
|
||
|
|
||
| def test_resource_with_non_default_argument(target): | ||
| dut = ModbusRTU(target, name=None, port="/dev/tty1", address=10, | ||
| speed=9600, timeout=0.5) | ||
|
|
||
| assert dut.port == "/dev/tty1" | ||
| assert dut.address == 10 | ||
| assert dut.speed == 9600 | ||
| assert dut.timeout == 0.5 | ||
| from labgrid.driver.modbusrtudriver import ModbusRTUDriver | ||
| from labgrid.resource.serialport import SerialPort | ||
|
|
||
|
|
||
| def test_driver(target, mocker): | ||
| pytest.importorskip("minimalmodbus") | ||
| mocker.patch('serial.Serial') | ||
| mocker.patch("serial.Serial") | ||
|
|
||
| SerialPort(target, name=None, port="/dev/tty0") | ||
| driver = ModbusRTUDriver(target, address=10, timeout=0.5, name=None) | ||
|
|
||
| ModbusRTU(target, name=None, port="/dev/tty0", address=10) | ||
| driver = ModbusRTUDriver(target, name=None) | ||
| assert driver.address == 10 | ||
| assert driver.timeout == 0.5 | ||
|
|
||
| target.activate(driver) | ||
|
|
||
| assert driver.instrument.serial.baudrate == 115200 | ||
| assert driver.instrument.serial.timeout == 0.25 | ||
| assert driver.instrument.serial.timeout == 0.5 |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use
close_port_after_each_call=True?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly: closing after each call leaves the port accessible for other users, otherwise it is locked.