Source code for inkex.ports

# coding=utf-8
# Copyright (C) 2019 Martin Owens <>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Common access to serial and other computer ports.

import os
import sys
import time
from .utils import DependencyError, AbortExtension

    import serial
    from import list_ports
except ImportError:
    serial = None

[docs]class Serial: """ Attempt to get access to the computer's serial port. with Serial(port_name, ...) as com: com.write(...) Provides access to the debug/testing ports which are pretend ports able to accept the same input but allow for debugging. """ def __init__(self, port, baud=9600, timeout=0.1, **options): self.test = port == '[test]' if self.test: import pty # This does not work on windows self.controller, self.peripheral = pty.openpty() port = os.ttyname(self.peripheral) self.has_serial() = serial.Serial() = port = int(baud) = timeout self.set_options(**options)
[docs] def set_options(self, stop=1, size=8, flow=None, parity=None): """Set further options on the serial port""" size = {5: 'five', 6: 'six', 7: 'seven', 8: 'eight'}.get(size, size) stop = {'onepointfive': 1.5}.get(stop.lower(), stop) stop = {1: 'one', 1.5: 'one_point_five', 2: 'two'}.get(stop, stop) = getattr(serial, str(str(size).upper()) + 'BITS') = getattr(serial, 'STOPBITS_' + str(stop).upper()) = getattr(serial, 'PARITY_' + str(parity).upper()) # set flow control = flow == 'xonxoff' = flow in ('rtscts', 'dsrdtrrtscts') = flow == 'dsrdtrrtscts'
def __enter__(self): try: # try to establish connection except serial.SerialException: raise AbortExtension("Could not open serial port. Please check your device"\ " is running, connected and the settings are correct") return def __exit__(self, exc, value, traceback): if not traceback and self.test: output = ' ' * 1024 while len(output) == 1024: time.sleep(0.01) output =, 1024) sys.stderr.write(output.decode('utf8'))
[docs] @staticmethod def has_serial(): """Late importing of pySerial module""" if serial is None: raise DependencyError("pySerial is required to open serial ports.")
[docs] @staticmethod def list_ports(): """Return a list of available serial ports""" Serial.has_serial() # Cause DependencyError error return [ for hw in list_ports.comports(True)]