Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[serial] Support serial both by id, by path name #8

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.5.4

- Allow NSH port to be opened by filepath.

## 1.5.3

- Fix enabling vector catch with OpenOCD.
Expand Down
17 changes: 11 additions & 6 deletions src/emdbg/serial/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,22 +253,27 @@ def is_alive(self, timeout: float = _TIMEOUT, attempts: int = 4) -> bool:

# -----------------------------------------------------------------------------
@contextmanager
def nsh(serial: str, baudrate: int = 57600):
def nsh(serial_or_port: str, baudrate: int = 57600):
"""
Opens a serial port with the `serial` number and closes it again.
Opens a serial port with the `serial` number or filepath and closes it again.

:param serial: the serial number of the port to connect to.
:param serial_or_port: the serial number or the filepath of the port to
connect to.
:param baudrate: the baudrate to use.

:raises `SerialException`: if serial port is not found.

:return: yields an initialized `Nsh` object.
"""
nsh = None
port = find_serial_port(serial)
if "/" in serial_or_port:
ttyDevice = serial_or_port
else:
ttyDevice = find_serial_port(serial_or_port).device
try:
LOGGER.info(f"Starting on port '{serial}'..." if serial else "Starting...")
device = Serial(port.device, baudrate=baudrate)
LOGGER.info(f"Starting on port '{serial_or_port}'..."
if serial_or_port else "Starting...")
device = Serial(ttyDevice, baudrate=baudrate)
reader_thread = ReaderThread(device, lambda: _NshReader(device))
with reader_thread as reader:
nsh = Nsh(reader_thread, reader)
Expand Down
Loading