emdbg.serial.utils
1# Copyright (c) 2023, Auterion AG 2# SPDX-License-Identifier: BSD-3-Clause 3 4from __future__ import annotations 5from serial.tools import list_ports 6import re 7import logging 8_LOGGER = logging.getLogger("serial") 9 10 11class SerialException(Exception): 12 """Exception for all serial port related errors.""" 13 pass 14 15 16def find_serial_port(identifier=None): 17 """ 18 Finds the serial port with the `identifier` serial number. 19 If not found, an exception is raised with all discovered serial ports in 20 the message, or a list of serial ports, if `identifier` is None. 21 22 :raises `SerialException`: if no serial port matches `identifier`. 23 :return: 24 - The serial port whose serial number matches the `identifier`, or 25 - A list of all serial ports if `identifier` is not `None`. 26 """ 27 serials = [] 28 for port in list_ports.comports(): 29 if not identifier: 30 _LOGGER.debug(f"Using first found serial port '{port.serial_number}'.") 31 return port 32 if port.serial_number == identifier: 33 return port 34 serials.append(port) 35 36 if not serials: 37 raise SerialException("Unable to find any serial ports!") 38 39 if identifier is not None: 40 msg = f"Unable to find '{identifier}' serial port!\n" 41 serials = "\n\t- ".join(f"{s}: serial={s.serial_number}" for s in serials) 42 msg += f"Available serial ports are:\n\t- {serials}\n" 43 raise SerialException(msg) 44 45 return serials 46 47 48_ANSI_ESCAPE = re.compile(r"\x1b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") 49 50def ansi_escape(line: str) -> str: 51 """Removes ANSI escape sequences from a string""" 52 return _ANSI_ESCAPE.sub("", line)
class
SerialException(builtins.Exception):
Exception for all serial port related errors.
def
find_serial_port(identifier=None):
17def find_serial_port(identifier=None): 18 """ 19 Finds the serial port with the `identifier` serial number. 20 If not found, an exception is raised with all discovered serial ports in 21 the message, or a list of serial ports, if `identifier` is None. 22 23 :raises `SerialException`: if no serial port matches `identifier`. 24 :return: 25 - The serial port whose serial number matches the `identifier`, or 26 - A list of all serial ports if `identifier` is not `None`. 27 """ 28 serials = [] 29 for port in list_ports.comports(): 30 if not identifier: 31 _LOGGER.debug(f"Using first found serial port '{port.serial_number}'.") 32 return port 33 if port.serial_number == identifier: 34 return port 35 serials.append(port) 36 37 if not serials: 38 raise SerialException("Unable to find any serial ports!") 39 40 if identifier is not None: 41 msg = f"Unable to find '{identifier}' serial port!\n" 42 serials = "\n\t- ".join(f"{s}: serial={s.serial_number}" for s in serials) 43 msg += f"Available serial ports are:\n\t- {serials}\n" 44 raise SerialException(msg) 45 46 return serials
Finds the serial port with the identifier
serial number.
If not found, an exception is raised with all discovered serial ports in
the message, or a list of serial ports, if identifier
is None.
Raises
SerialException
: if no serial port matchesidentifier
.
Returns
- The serial port whose serial number matches the
identifier
, or- A list of all serial ports if
identifier
is notNone
.
def
ansi_escape(line: str) -> str:
51def ansi_escape(line: str) -> str: 52 """Removes ANSI escape sequences from a string""" 53 return _ANSI_ESCAPE.sub("", line)
Removes ANSI escape sequences from a string