Skip to content

Commit

Permalink
Interrupt port initialization when there is IOError exception (#15239)
Browse files Browse the repository at this point in the history
Fixes #15226

Summary of the issue:
Driver continued port initialization retries although there was IOError exception.

Description of user facing changes
Port initialization is stopped when there is IOError exception.

Description of development approach
If there is IoError exception, _initPort function returns None which causes _initConnection function to raise RuntimeError. No need to try with 9600 bps, and there is no other port to try.
  • Loading branch information
burmancomp committed Aug 22, 2023
1 parent 7b6e071 commit 186a8d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
56 changes: 28 additions & 28 deletions source/brailleDisplayDrivers/albatross/driver.py
Expand Up @@ -25,8 +25,10 @@
Lock,
)
from typing import (
Iterator,
List,
Optional,
Tuple,
)

import braille
Expand Down Expand Up @@ -201,11 +203,20 @@ def _initConnection(self) -> bool:
If no connection, Albatross sends continuously INIT_START_BYTE
followed by byte containing various settings like number of cells.
@return: C{True} on success, C{False} on failure
@raises: RuntimeError if port initialization fails
@return: C{True} on success, C{False} on connection failure
"""
for i in range(MAX_INIT_RETRIES):
if not self._dev:
if not self._initPort(i):
try:
initState: bool = self._initPort(i)
except IOError:
# Port initialization failed. No need to try with 9600 bps,
# and there is no other port to try.
log.debug(f"Port {self._currentPort} not initialized", exc_info=True)
raise RuntimeError(f"Port {self._currentPort} cannot be initialized for Albatross")
# I/O buffers reset failed, retried again in L{_openPort}
if not initState:
continue
elif not self._dev.is_open:
if not self._openPort(i):
Expand All @@ -226,38 +237,27 @@ def _initConnection(self) -> bool:
def _initPort(self, i: int = MAX_INIT_RETRIES - 1) -> bool:
"""Initializes port.
@param i: Just for logging retries.
@return: C{True} on success, C{False} on failure
@raises: IOError if port initialization fails
@return: C{True} on success, C{False} on I/O buffers reset failure
"""
try:
self._dev = serial.Serial(
self._currentPort,
baudrate=self._baudRate,
stopbits=serial.STOPBITS_ONE,
parity=serial.PARITY_NONE,
timeout=READ_TIMEOUT,
writeTimeout=WRITE_TIMEOUT
)
log.debug(f"Port {self._currentPort} initialized")
if not self._resetBuffers():
if i == MAX_INIT_RETRIES - 1:
return False
log(
f"sleeping {SLEEP_TIMEOUT} seconds before try {i + 2} / {MAX_INIT_RETRIES}"
)
time.sleep(SLEEP_TIMEOUT)
return False
return True
except IOError:
self._dev = serial.Serial(
self._currentPort,
baudrate=self._baudRate,
stopbits=serial.STOPBITS_ONE,
parity=serial.PARITY_NONE,
timeout=READ_TIMEOUT,
writeTimeout=WRITE_TIMEOUT
)
log.debug(f"Port {self._currentPort} initialized")
if not self._resetBuffers():
if i == MAX_INIT_RETRIES - 1:
log.debug(f"Port {self._currentPort} not initialized", exc_info=True)
return False
log.debug(
f"Port {self._currentPort} not initialized, sleeping {SLEEP_TIMEOUT} seconds "
f"before try {i + 2} / {MAX_INIT_RETRIES}",
exc_info=True
log(
f"sleeping {SLEEP_TIMEOUT} seconds before try {i + 2} / {MAX_INIT_RETRIES}"
)
time.sleep(SLEEP_TIMEOUT)
return False
return True

def _openPort(self, i: int = MAX_INIT_RETRIES - 1) -> bool:
"""Opens port.
Expand Down
1 change: 1 addition & 0 deletions user_docs/en/changes.t2t
Expand Up @@ -28,6 +28,7 @@ What's New in NVDA
- NVDA will no longer jump back to the last browse mode position when opening the context menu in Microsoft Edge. (#15309)
- Fixed support for System List view (``SysListView32``) controls in Windows Forms applications. (#15283)
- NVDA will no longer be sluggish in rich edit controls when braille is enabled, such as in MemPad. (#14285)
- Fixed bug where Albatross braille displays try to initialize although another braille device has been connected. (#15226)
- NVDA is once again able to read context menus of downloads in Microsoft Edge. (#14916)
-

Expand Down

0 comments on commit 186a8d7

Please sign in to comment.