diff --git a/README.md b/README.md index 4fe663a..f5a7987 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ pymcuprog supports: * EDBG - on-board debugger on Xplained Pro/Ultra * mEDBG - on-board debugger on Xplained Mini/Nano * JTAGICE3 (firmware version 3.0 or newer) +* Serial (TTY/COM) and RFC2217 compatible devices (serialupdi) Although not all functionality is provided on all debuggers/boards. See device support section below. diff --git a/pymcuprog/serialupdi/physical.py b/pymcuprog/serialupdi/physical.py index 4e6a9ed..7b4dae1 100644 --- a/pymcuprog/serialupdi/physical.py +++ b/pymcuprog/serialupdi/physical.py @@ -4,6 +4,7 @@ import time from logging import getLogger import serial +import serial.rfc2217 from serial.serialutil import SerialException from . import constants @@ -48,6 +49,20 @@ def __init__(self, port, baud=DEFAULT_SERIALUPDI_BAUD, timeout=None): # send an initial break as handshake self.send([constants.UPDI_BREAK]) + def _open_serial(self, port, *args, **kwargs): + """ + Open a serial device (tty/com or rfc2217) based on the provided port type + + :param port: Serial port name to connect to. + :param \**args: Additional arguments passed to Serial class. + :param \**args: Additional keyword arguments passed to Serial class. + """ + + if port.startswith("rfc2217://"): + return serial.rfc2217.Serial(port, *args, **kwargs) + else: + return serial.Serial(port, *args, **kwargs) + def initialise_serial(self, port, baud, timeout): """ Standard serial port initialisation @@ -62,7 +77,7 @@ def initialise_serial(self, port, baud, timeout): """ self.logger.info("Opening port '%s' at %d baud (timeout %.01fs)", port, baud, timeout) try: - self.ser = serial.Serial(port, baud, parity=serial.PARITY_EVEN, timeout=timeout, stopbits=serial.STOPBITS_TWO) + self.ser = self._open_serial(port, baud, parity=serial.PARITY_EVEN, timeout=timeout, stopbits=serial.STOPBITS_TWO) except SerialException: self.logger.error("Unable to open serial port '%s'", port) raise @@ -90,7 +105,7 @@ def send_double_break(self): # At 300 bauds, the break character will pull the line low for 30ms # Which is slightly above the recommended 24.6ms self.ser.close() - temporary_serial = serial.Serial(self.port, 300, parity=serial.PARITY_EVEN, timeout=self.timeout, + temporary_serial = self._open_serial(self.port, 300, parity=serial.PARITY_EVEN, timeout=self.timeout, stopbits=serial.STOPBITS_ONE) # Send two break characters, with 1 stop bit in between