13:32
  #   ¯ˆl–Ãa¾”jClÊ
€ÆÚ¸ÛÊbÑ¿ö„âqÏÀô ?÷     ¯#! python
# Python Serial Port Extension for Win32, Linux, BSD, Jython
# see __init__.py
#
# (C) 2001-2010 Chris Liechti <cliechti@gmx.net>
# this is distributed under a free software license, see license.txt

# compatibility for older Python < 2.6
try:
    bytes
    bytearray
except (NameError, AttributeError):
    # Python older than 2.6 do not have these types. Like for Python 2.6 they
    # should behave like str. For Python older than 3.0 we want to work with
    # strings anyway, only later versions have a true bytes type.
    bytes = str
    # bytearray is a mutable type that is easily turned into an instance of
    # bytes
    class bytearray(list):
        # for bytes(bytearray()) usage
        def __str__(self): return ''.join(self)
        def __repr__(self): return 'bytearray(%r)' % ''.join(self)
        # append automatically converts integers to characters
        def append(self, item):
            if isinstance(item, str):
                list.append(self, item)
            else:
                list.append(self, chr(item))
        # +=
        def __iadd__(self, other):
            for byte in other:
                self.append(byte)
            return self

        def __getslice__(self, i, j):
            return bytearray(list.__getslice__(self, i, j))

        def __getitem__(self, item):
            if isinstance(item, slice):
                return bytearray(list.__getitem__(self, item))
            else:
                return ord(list.__getitem__(self, item))

        def __eq__(self, other):
            if isinstance(other, basestring):
                other = bytearray(other)
            return list.__eq__(self, other)

# all Python versions prior 3.x convert str([17]) to '[17]' instead of '\x11'
# so a simple bytes(sequence) doesn't work for all versions
def to_bytes(seq):
    """convert a sequence to a bytes type"""
    b = bytearray()
    for item in seq:
        b.append(item)  # this one handles int and str
    return bytes(b)

# create control bytes
XON  = to_bytes([17])
XOFF = to_bytes([19])

CR = to_bytes([13])
LF = to_bytes([10])


PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE = 'N', 'E', 'O', 'M', 'S'
STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO = (1, 1.5, 2)
FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5, 6, 7, 8)

PARITY_NAMES = {
    PARITY_NONE:  'None',
    PARITY_EVEN:  'Even',
    PARITY_ODD:   'Odd',
    PARITY_MARK:  'Mark',
    PARITY_SPACE: 'Space',
}


class SerialException(IOError):
    """Base class for serial port related exceptions."""


class SerialTimeoutException(SerialException):
    """Write timeouts give an exception"""


writeTimeoutError = SerialTimeoutException("Write timeout")
portNotOpenError = ValueError('Attempting to use a port that is not open')


class FileLike(object):
    """An abstract file like class.

    This class implements readline and readlines based on read and
    writelines based on write.
    This class is used to provide the above functions for to Serial
    port objects.

    Note that when the serial port was opened with _NO_ timeout that
    readline blocks until it sees a newline (or the specified size is
    reached) and that readlines would never return and therefore
    refuses to work (it raises an exception in this case)!
    """

    def __init__(self):
        self.closed = True

    def close(self):
        self.closed = True

    # so that ports are closed when objects are discarded
    def __del__(self):
        """Destructor.  Calls close()."""
        # The try/except block is in case this is called at program
        # exit time, when it's possible that globals have already been
        # deleted, and then the close() call might fail.  Since
        # there's nothing we can do about such failures and they annoy
        # the end users, we suppress the traceback.
        try:
            self.close()
        except:
            pass

    def writelines(self, sequence):
        for line in sequence:
            self.write(line)

    def flush(s