55
66import serial
77import serial .threaded
8- from async_timeout import timeout
98
109from ..rs232 import RS232SerialProtocol
1110from .base import GPIBTransport
@@ -64,7 +63,9 @@ async def get_response(self):
6463 async def send_and_read (self , send ):
6564 """Send a line, read the response. NOTE: This is for talking with the controller, device responses
6665 need to use get_response as usual"""
67- with timeout (READ_TIMEOUT ):
66+
67+ async def _send_and_read (send ) -> str :
68+ """Wrap the actual work"""
6869 async with self .lock :
6970 response = None
7071
@@ -80,6 +81,8 @@ def set_response(message):
8081 await asyncio .get_event_loop ().run_in_executor (None , self .blevent .wait )
8182 return response
8283
84+ return await asyncio .wait_for (_send_and_read (send ), timeout = READ_TIMEOUT )
85+
8386 async def set_address (self , address , secondary = None ):
8487 """Set the address we want to talk to"""
8588 if secondary is None :
@@ -148,13 +151,18 @@ async def scan_devices(self):
148151 prev_read_tmo_ms = await self .send_and_read ("++read_tmo_ms" )
149152 self .serialhandler .protocol .write_line ("++read_tmo_ms %d" % int ((SCAN_DEVICE_TIMEOUT / 2 ) * 1000 ))
150153 for addr in range (0 , 31 ): # 0-30 inclusive
151- with timeout (SCAN_DEVICE_TIMEOUT ):
152- try :
153- await self .set_address (addr )
154- await self .poll ()
155- found_addresses .append (addr )
156- except (asyncio .TimeoutError , asyncio .CancelledError ):
157- pass
154+
155+ async def _scan_addr (addr ) -> None :
156+ """Sacn single address"""
157+ nonlocal found_addresses
158+ await self .set_address (addr )
159+ await self .poll ()
160+ found_addresses .append (addr )
161+
162+ try :
163+ await asyncio .wait_for (_scan_addr (addr ), timeout = SCAN_DEVICE_TIMEOUT )
164+ except (asyncio .TimeoutError , asyncio .CancelledError ):
165+ pass
158166 self .serialhandler .protocol .write_line ("++read_tmo_ms " + prev_read_tmo_ms )
159167 # Wait a moment for things to settle
160168 await asyncio .sleep (float (prev_read_tmo_ms ) / 1000 )
0 commit comments