7
7
from pymodbus .client import (
8
8
AsyncModbusSerialClient as InternalAsyncModbusSerialClient ,
9
9
AsyncModbusTcpClient as InternalAsyncModbusTcpClient ,
10
- ModbusBaseClient as InternalModbusBaseClient ,
11
10
)
11
+ from pymodbus .client .base import ModbusBaseClient as InternalModbusBaseClient
12
12
from pymodbus .pdu import ExceptionResponse
13
13
14
14
from qtoggleserver .core import ports as core_ports
@@ -101,22 +101,25 @@ async def ensure_client(self) -> bool:
101
101
self .debug ('waiting %d seconds for initial delay' , self .initial_delay )
102
102
await asyncio .sleep (self .initial_delay )
103
103
104
+ while not self ._pymodbus_client .connected :
105
+ await asyncio .sleep (0.1 )
106
+
104
107
if not self ._pymodbus_client .connected :
105
108
self .warning ('could not connect to unit' )
106
109
# Don't leave the client (partially) connected
107
110
try :
108
- await self ._pymodbus_client .close ()
111
+ self ._pymodbus_client .close ()
109
112
except Exception :
110
113
# We don't care if connection closing fails - we're going to recreate the client from scratch anyway
111
114
pass
112
115
return False
113
116
114
117
return True
115
118
116
- async def close_client (self ) -> None :
119
+ def close_client (self ) -> None :
117
120
self .info ('disconnecting from unit' )
118
121
try :
119
- await self ._pymodbus_client .close ()
122
+ self ._pymodbus_client .close ()
120
123
except Exception :
121
124
# We don't care if connection closing fails - we're going to recreate the client from scratch anyway
122
125
pass
@@ -142,7 +145,7 @@ async def handle_enable(self) -> None:
142
145
143
146
async def handle_disable (self ) -> None :
144
147
await super ().handle_disable ()
145
- await self .close_client ()
148
+ self .close_client ()
146
149
147
150
async def poll (self ) -> None :
148
151
if not await self .ensure_client ():
@@ -271,14 +274,14 @@ def __init__(
271
274
serial_baud : int = DEFAULT_SERIAL_BAUD ,
272
275
serial_stopbits : int = DEFAULT_SERIAL_STOPBITS ,
273
276
serial_bytesize : int = DEFAULT_SERIAL_BYTESIZE ,
274
- serial_parity : int = DEFAULT_SERIAL_PARITY ,
277
+ serial_parity : str = DEFAULT_SERIAL_PARITY ,
275
278
** kwargs
276
279
) -> None :
277
280
self .serial_port : str = serial_port
278
281
self .serial_baud : int = serial_baud
279
282
self .serial_stopbits : int = serial_stopbits
280
283
self .serial_bytesize : int = serial_bytesize
281
- self .serial_parity : int = serial_parity
284
+ self .serial_parity : str = serial_parity
282
285
283
286
kwargs .setdefault ('method' , self .DEFAULT_METHOD )
284
287
@@ -329,7 +332,7 @@ async def make_internal_client(self) -> InternalModbusBaseClient:
329
332
def handle_offline (self ) -> None :
330
333
# Automatically close and cleanup any existing client if peripheral goes offline. We want to start from scratch
331
334
# with a brand-new client.
332
- asyncio . create_task ( self .close_client () )
335
+ self .close_client ()
333
336
334
337
335
338
class BasePassiveModbusClient (BaseModbusClient , metaclass = abc .ABCMeta ):
0 commit comments