-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,3 +1,4 @@ | ||
from .ip_connection import IPConnection | ||
from .modbus_connection import ModbusSerialConnection, ModbusTcpConnection, ModbusUdpConnection | ||
|
||
__all__ = ["IPConnection"] | ||
__all__ = ["IPConnection", "ModbusSerialConnection", "ModbusTcpConnection", "ModbusUdpConnection"] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import asyncio | ||
|
||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from pymodbus.client.base import ModbusBaseClient | ||
from pymodbus.client import ( | ||
AsyncModbusSerialClient, | ||
AsyncModbusTcpClient, | ||
AsyncModbusUdpClient, | ||
) | ||
from pymodbus.exceptions import ModbusException | ||
|
||
from pymodbus.framer import Framer | ||
from pymodbus.pdu import ExceptionResponse | ||
|
||
|
||
# Constants | ||
CR = "\r" | ||
TIMEOUT = 1.0 # Seconds | ||
RECV_BUFFER = 4096 # Bytes | ||
|
||
|
||
@dataclass | ||
class ModbusConnectionSettings: | ||
host: str = "127.0.0.1" | ||
port: int = 7001 | ||
slave: int = 0 | ||
|
||
class ModbusConnection: | ||
|
||
def __init__(self, settings: ModbusConnectionSettings) -> None: | ||
|
||
self.host, self.port, self.slave = settings.host, settings.port, settings.slave | ||
self.running: bool = False | ||
|
||
self._client: ModbusBaseClient | ||
|
||
async def connect(self, framer=Framer.SOCKET): | ||
raise NotImplementedError | ||
|
||
|
||
def disconnect(self): | ||
self._client.close() | ||
|
||
async def _read(self, address: int, count: int = 2) -> Optional[str]: | ||
# address -= 1 # modbus spec starts from 0 not 1 | ||
try: | ||
# address_hex = hex(address) | ||
rr = await self._client.read_holding_registers(address, count=count, slave=self.slave) # type: ignore | ||
print(f"Response: {rr}") | ||
except ModbusException as exc: # pragma no cover | ||
# Received ModbusException from library | ||
self.disconnect() | ||
return | ||
if rr.isError(): # pragma no cover | ||
# Received Modbus library error | ||
self.disconnect() | ||
return | ||
if isinstance(rr, ExceptionResponse): # pragma no cover | ||
# Received Modbus library exception | ||
# THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message | ||
self.disconnect() | ||
|
||
async def send(self, address: int, value: int) -> None: | ||
"""Send a request. | ||
Args: | ||
address: The register address to write to. | ||
value: The value to write. | ||
""" | ||
await self._client.write_registers(address, value, slave=self.slave) | ||
resp = await self._read(address, 2) | ||
|
||
class ModbusSerialConnection(ModbusConnection): | ||
|
||
def __init__(self, settings: ModbusConnectionSettings) -> None: | ||
super().__init__(settings) | ||
|
||
async def connect(self, framer=Framer.SOCKET): | ||
self._client: AsyncModbusSerialClient = AsyncModbusSerialClient( | ||
str(self.port), | ||
framer=framer, | ||
timeout=10, | ||
retries=3, | ||
retry_on_empty=False, | ||
close_comm_on_error=False, | ||
strict=True, | ||
baudrate=9600, | ||
bytesize=8, | ||
parity="N", | ||
stopbits=1, | ||
) | ||
|
||
await self._client.connect() | ||
assert self._client.connected | ||
|
||
class ModbusTcpConnection(ModbusConnection): | ||
|
||
def __init__(self, settings: ModbusConnectionSettings) -> None: | ||
super().__init__(settings) | ||
|
||
async def connect(self, framer=Framer.SOCKET): | ||
self._client: AsyncModbusTcpClient = AsyncModbusTcpClient( | ||
self.host, | ||
self.port, | ||
framer=framer, | ||
timeout=10, | ||
retries=3, | ||
retry_on_empty=False, | ||
close_comm_on_error=False, | ||
strict=True, | ||
source_address=("localhost", 0), | ||
) | ||
|
||
await self._client.connect() | ||
assert self._client.connected | ||
|
||
class ModbusUdpConnection(ModbusConnection): | ||
|
||
def __init__(self, settings: ModbusConnectionSettings) -> None: | ||
super().__init__(settings) | ||
|
||
async def connect(self, framer=Framer.SOCKET): | ||
self._client: AsyncModbusUdpClient = AsyncModbusUdpClient( | ||
self.host, | ||
self.port, | ||
framer=framer, | ||
timeout=10, | ||
retries=3, | ||
retry_on_empty=False, | ||
close_comm_on_error=False, | ||
strict=True, | ||
source_address=("localhost", 0), | ||
) | ||
|
||
await self._client.connect() | ||
assert self._client.connected | ||