2525 Lock ,
2626)
2727from typing import (
28+ Iterator ,
2829 List ,
2930 Optional ,
31+ Tuple ,
3032)
3133
3234import braille
@@ -204,11 +206,20 @@ def _initConnection(self) -> bool:
204206 If no connection, Albatross sends continuously INIT_START_BYTE
205207 followed by byte containing various settings like number of cells.
206208
207- @return: C{True} on success, C{False} on failure
209+ @raises: RuntimeError if port initialization fails
210+ @return: C{True} on success, C{False} on connection failure
208211 """
209212 for i in range (MAX_INIT_RETRIES ):
210213 if not self ._dev :
211- if not self ._initPort (i ):
214+ initState : Optional [bool ] = self ._initPort (i )
215+ # Port initialization failed. No need to try with 9600 bps,
216+ # and there is no other port to try.
217+ if initState is None :
218+ raise RuntimeError (
219+ f"Port { self ._currentPort } cannot be initialized for Albatross"
220+ )
221+ # I/O buffers reset failed, retried again in L{_openPort}
222+ elif not initState :
212223 continue
213224 elif not self ._dev .is_open :
214225 if not self ._openPort (i ):
@@ -226,10 +237,11 @@ def _initConnection(self) -> bool:
226237 return True
227238 return False
228239
229- def _initPort (self , i : int = MAX_INIT_RETRIES - 1 ) -> bool :
240+ def _initPort (self , i : int = MAX_INIT_RETRIES - 1 ) -> Optional [ bool ] :
230241 """Initializes port.
231242 @param i: Just for logging retries.
232- @return: C{True} on success, C{False} on failure
243+ @return: C{True} on success, C{False} on I/O buffers reset failure,
244+ C{None} on port initialization failure
233245 """
234246 try :
235247 self ._dev = serial .Serial (
@@ -251,16 +263,8 @@ def _initPort(self, i: int = MAX_INIT_RETRIES - 1) -> bool:
251263 return False
252264 return True
253265 except IOError :
254- if i == MAX_INIT_RETRIES - 1 :
255- log .debug (f"Port { self ._currentPort } not initialized" , exc_info = True )
256- return False
257- log .debug (
258- f"Port { self ._currentPort } not initialized, sleeping { SLEEP_TIMEOUT } seconds "
259- f"before try { i + 2 } / { MAX_INIT_RETRIES } " ,
260- exc_info = True
261- )
262- time .sleep (SLEEP_TIMEOUT )
263- return False
266+ log .debug (f"Port { self ._currentPort } not initialized" , exc_info = True )
267+ return None
264268
265269 def _openPort (self , i : int = MAX_INIT_RETRIES - 1 ) -> bool :
266270 """Opens port.
0 commit comments