1
1
import asyncio
2
-
3
2
from dataclasses import dataclass
4
3
from typing import Optional
5
4
6
- from pymodbus .client .base import ModbusBaseClient
7
5
from pymodbus .client import (
8
6
AsyncModbusSerialClient ,
9
7
AsyncModbusTcpClient ,
10
8
AsyncModbusUdpClient ,
9
+ ModbusBaseClient ,
11
10
)
12
- from pymodbus .exceptions import ModbusException
13
11
12
+ from pymodbus .exceptions import ModbusException
14
13
from pymodbus .framer import Framer
15
- from pymodbus .pdu import ExceptionResponse
16
-
14
+ from pymodbus .pdu import ExceptionResponse , ModbusResponse
17
15
18
16
# Constants
19
17
CR = "\r "
@@ -27,10 +25,9 @@ class ModbusConnectionSettings:
27
25
port : int = 7001
28
26
slave : int = 0
29
27
30
- class ModbusConnection :
31
28
29
+ class ModbusConnection :
32
30
def __init__ (self , settings : ModbusConnectionSettings ) -> None :
33
-
34
31
self .host , self .port , self .slave = settings .host , settings .port , settings .slave
35
32
self .running : bool = False
36
33
@@ -39,11 +36,10 @@ def __init__(self, settings: ModbusConnectionSettings) -> None:
39
36
async def connect (self , framer = Framer .SOCKET ):
40
37
raise NotImplementedError
41
38
42
-
43
39
def disconnect (self ):
44
40
self ._client .close ()
45
41
46
- async def _read (self , address : int , count : int = 2 ) -> Optional [str ]:
42
+ async def _read (self , address : int , count : int = 2 ) -> Optional [ModbusResponse ]:
47
43
# address -= 1 # modbus spec starts from 0 not 1
48
44
try :
49
45
# address_hex = hex(address)
@@ -61,6 +57,8 @@ async def _read(self, address: int, count: int = 2) -> Optional[str]:
61
57
# Received Modbus library exception
62
58
# THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message
63
59
self .disconnect ()
60
+ else :
61
+ return rr
64
62
65
63
async def send (self , address : int , value : int ) -> None :
66
64
"""Send a request.
@@ -72,13 +70,13 @@ async def send(self, address: int, value: int) -> None:
72
70
await self ._client .write_registers (address , value , slave = self .slave )
73
71
resp = await self ._read (address , 2 )
74
72
75
- class ModbusSerialConnection (ModbusConnection ):
76
73
74
+ class ModbusSerialConnection (ModbusConnection ):
77
75
def __init__ (self , settings : ModbusConnectionSettings ) -> None :
78
76
super ().__init__ (settings )
79
77
80
78
async def connect (self , framer = Framer .SOCKET ):
81
- self ._client : AsyncModbusSerialClient = AsyncModbusSerialClient (
79
+ self ._client = AsyncModbusSerialClient (
82
80
str (self .port ),
83
81
framer = framer ,
84
82
timeout = 10 ,
@@ -95,13 +93,13 @@ async def connect(self, framer=Framer.SOCKET):
95
93
await self ._client .connect ()
96
94
assert self ._client .connected
97
95
98
- class ModbusTcpConnection (ModbusConnection ):
99
96
97
+ class ModbusTcpConnection (ModbusConnection ):
100
98
def __init__ (self , settings : ModbusConnectionSettings ) -> None :
101
99
super ().__init__ (settings )
102
100
103
101
async def connect (self , framer = Framer .SOCKET ):
104
- self ._client : AsyncModbusTcpClient = AsyncModbusTcpClient (
102
+ self ._client = AsyncModbusTcpClient (
105
103
self .host ,
106
104
self .port ,
107
105
framer = framer ,
@@ -116,13 +114,13 @@ async def connect(self, framer=Framer.SOCKET):
116
114
await self ._client .connect ()
117
115
assert self ._client .connected
118
116
119
- class ModbusUdpConnection (ModbusConnection ):
120
117
118
+ class ModbusUdpConnection (ModbusConnection ):
121
119
def __init__ (self , settings : ModbusConnectionSettings ) -> None :
122
120
super ().__init__ (settings )
123
121
124
122
async def connect (self , framer = Framer .SOCKET ):
125
- self ._client : AsyncModbusUdpClient = AsyncModbusUdpClient (
123
+ self ._client = AsyncModbusUdpClient (
126
124
self .host ,
127
125
self .port ,
128
126
framer = framer ,
@@ -135,4 +133,4 @@ async def connect(self, framer=Framer.SOCKET):
135
133
)
136
134
137
135
await self ._client .connect ()
138
- assert self ._client .connected
136
+ assert self ._client .connected
0 commit comments