11import asyncio
2-
32from dataclasses import dataclass
43from typing import Optional
54
6- from pymodbus .client .base import ModbusBaseClient
75from pymodbus .client import (
86 AsyncModbusSerialClient ,
97 AsyncModbusTcpClient ,
108 AsyncModbusUdpClient ,
9+ ModbusBaseClient ,
1110)
12- from pymodbus .exceptions import ModbusException
1311
12+ from pymodbus .exceptions import ModbusException
1413from pymodbus .framer import Framer
15- from pymodbus .pdu import ExceptionResponse
16-
14+ from pymodbus .pdu import ExceptionResponse , ModbusResponse
1715
1816# Constants
1917CR = "\r "
@@ -27,10 +25,9 @@ class ModbusConnectionSettings:
2725 port : int = 7001
2826 slave : int = 0
2927
30- class ModbusConnection :
3128
29+ class ModbusConnection :
3230 def __init__ (self , settings : ModbusConnectionSettings ) -> None :
33-
3431 self .host , self .port , self .slave = settings .host , settings .port , settings .slave
3532 self .running : bool = False
3633
@@ -39,11 +36,10 @@ def __init__(self, settings: ModbusConnectionSettings) -> None:
3936 async def connect (self , framer = Framer .SOCKET ):
4037 raise NotImplementedError
4138
42-
4339 def disconnect (self ):
4440 self ._client .close ()
4541
46- async def _read (self , address : int , count : int = 2 ) -> Optional [str ]:
42+ async def _read (self , address : int , count : int = 2 ) -> Optional [ModbusResponse ]:
4743 # address -= 1 # modbus spec starts from 0 not 1
4844 try :
4945 # address_hex = hex(address)
@@ -61,6 +57,8 @@ async def _read(self, address: int, count: int = 2) -> Optional[str]:
6157 # Received Modbus library exception
6258 # THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message
6359 self .disconnect ()
60+ else :
61+ return rr
6462
6563 async def send (self , address : int , value : int ) -> None :
6664 """Send a request.
@@ -72,13 +70,13 @@ async def send(self, address: int, value: int) -> None:
7270 await self ._client .write_registers (address , value , slave = self .slave )
7371 resp = await self ._read (address , 2 )
7472
75- class ModbusSerialConnection (ModbusConnection ):
7673
74+ class ModbusSerialConnection (ModbusConnection ):
7775 def __init__ (self , settings : ModbusConnectionSettings ) -> None :
7876 super ().__init__ (settings )
7977
8078 async def connect (self , framer = Framer .SOCKET ):
81- self ._client : AsyncModbusSerialClient = AsyncModbusSerialClient (
79+ self ._client = AsyncModbusSerialClient (
8280 str (self .port ),
8381 framer = framer ,
8482 timeout = 10 ,
@@ -95,13 +93,13 @@ async def connect(self, framer=Framer.SOCKET):
9593 await self ._client .connect ()
9694 assert self ._client .connected
9795
98- class ModbusTcpConnection (ModbusConnection ):
9996
97+ class ModbusTcpConnection (ModbusConnection ):
10098 def __init__ (self , settings : ModbusConnectionSettings ) -> None :
10199 super ().__init__ (settings )
102100
103101 async def connect (self , framer = Framer .SOCKET ):
104- self ._client : AsyncModbusTcpClient = AsyncModbusTcpClient (
102+ self ._client = AsyncModbusTcpClient (
105103 self .host ,
106104 self .port ,
107105 framer = framer ,
@@ -116,13 +114,13 @@ async def connect(self, framer=Framer.SOCKET):
116114 await self ._client .connect ()
117115 assert self ._client .connected
118116
119- class ModbusUdpConnection (ModbusConnection ):
120117
118+ class ModbusUdpConnection (ModbusConnection ):
121119 def __init__ (self , settings : ModbusConnectionSettings ) -> None :
122120 super ().__init__ (settings )
123121
124122 async def connect (self , framer = Framer .SOCKET ):
125- self ._client : AsyncModbusUdpClient = AsyncModbusUdpClient (
123+ self ._client = AsyncModbusUdpClient (
126124 self .host ,
127125 self .port ,
128126 framer = framer ,
@@ -135,4 +133,4 @@ async def connect(self, framer=Framer.SOCKET):
135133 )
136134
137135 await self ._client .connect ()
138- assert self ._client .connected
136+ assert self ._client .connected
0 commit comments