cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 1 | #! python |
cliechti | c54b2c8 | 2008-06-21 01:59:08 +0000 | [diff] [blame] | 2 | # Python Serial Port Extension for Win32, Linux, BSD, Jython |
| 3 | # see __init__.py |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 4 | # |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 5 | # (C) 2001-2015 Chris Liechti <cliechti@gmx.net> |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 6 | # this is distributed under a free software license, see license.txt |
| 7 | |
cliechti | c323f1f | 2010-07-22 00:14:26 +0000 | [diff] [blame] | 8 | |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 9 | # ``memoryview`` was introduced in Python 2.7 and ``bytes(some_memoryview)`` |
| 10 | # isn't returning the contents (very unfortunate). Therefore we need special |
| 11 | # cases and test for it. Ensure that there is a ``memoryview`` object for older |
| 12 | # Python versions. This is easier than making every test dependent on its |
| 13 | # existence. |
| 14 | try: |
| 15 | memoryview |
| 16 | except (NameError, AttributeError): |
| 17 | # implementation does not matter as we do not realy use it. |
| 18 | # it just must not inherit from something else we might care for. |
Chris Liechti | 70b8923 | 2015-08-04 03:00:52 +0200 | [diff] [blame] | 19 | class memoryview(object): |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 20 | pass |
| 21 | |
| 22 | |
| 23 | # all Python versions prior 3.x convert ``str([17])`` to '[17]' instead of '\x11' |
| 24 | # so a simple ``bytes(sequence)`` doesn't work for all versions |
cliechti | 32c1033 | 2009-08-05 13:23:43 +0000 | [diff] [blame] | 25 | def to_bytes(seq): |
| 26 | """convert a sequence to a bytes type""" |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 27 | if isinstance(seq, bytes): |
| 28 | return seq |
| 29 | elif isinstance(seq, bytearray): |
| 30 | return bytes(seq) |
| 31 | elif isinstance(seq, memoryview): |
| 32 | return seq.tobytes() |
| 33 | else: |
| 34 | b = bytearray() |
| 35 | for item in seq: |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 36 | b.append(item) # this one handles int and str for our emulation and ints for Python 3.x |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 37 | return bytes(b) |
cliechti | 32c1033 | 2009-08-05 13:23:43 +0000 | [diff] [blame] | 38 | |
| 39 | # create control bytes |
| 40 | XON = to_bytes([17]) |
| 41 | XOFF = to_bytes([19]) |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 42 | |
cliechti | 8e99b6f | 2010-07-21 15:46:39 +0000 | [diff] [blame] | 43 | CR = to_bytes([13]) |
| 44 | LF = to_bytes([10]) |
| 45 | |
cliechti | a3a811f | 2009-07-29 21:59:03 +0000 | [diff] [blame] | 46 | |
cliechti | 0d6029a | 2008-06-21 01:28:46 +0000 | [diff] [blame] | 47 | PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE = 'N', 'E', 'O', 'M', 'S' |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 48 | STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO = (1, 1.5, 2) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 49 | FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5, 6, 7, 8) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 50 | |
| 51 | PARITY_NAMES = { |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 52 | PARITY_NONE: 'None', |
| 53 | PARITY_EVEN: 'Even', |
| 54 | PARITY_ODD: 'Odd', |
| 55 | PARITY_MARK: 'Mark', |
| 56 | PARITY_SPACE: 'Space', |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 57 | } |
| 58 | |
cliechti | 1dbe4b6 | 2002-02-14 02:49:25 +0000 | [diff] [blame] | 59 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 60 | class SerialException(IOError): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 61 | """Base class for serial port related exceptions.""" |
cliechti | 7fe54d5 | 2002-03-03 20:11:47 +0000 | [diff] [blame] | 62 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 63 | |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 64 | class SerialTimeoutException(SerialException): |
| 65 | """Write timeouts give an exception""" |
| 66 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 67 | |
cliechti | 4b20ec6 | 2012-08-16 01:04:44 +0000 | [diff] [blame] | 68 | writeTimeoutError = SerialTimeoutException('Write timeout') |
| 69 | portNotOpenError = SerialException('Attempting to use a port that is not open') |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 70 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 71 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 72 | class SerialBase(object): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 73 | """\ |
| 74 | Serial port base class. Provides __init__ function and properties to |
| 75 | get/set port settings. |
| 76 | """ |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 77 | |
cliechti | dfec0c8 | 2009-07-21 01:35:41 +0000 | [diff] [blame] | 78 | # default values, may be overridden in subclasses that do not support all values |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 79 | BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 80 | 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, |
| 81 | 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, |
| 82 | 3000000, 3500000, 4000000) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 83 | BYTESIZES = (FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 84 | PARITIES = (PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE) |
cliechti | 6ffdb8f | 2009-07-22 00:48:57 +0000 | [diff] [blame] | 85 | STOPBITS = (STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 86 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 87 | def __init__(self, |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 88 | port = None, # number of device, numbering starts at |
| 89 | # zero. if everything fails, the user |
| 90 | # can specify a device string, note |
| 91 | # that this isn't portable anymore |
| 92 | # port will be opened if one is specified |
cliechti | dfec0c8 | 2009-07-21 01:35:41 +0000 | [diff] [blame] | 93 | baudrate=9600, # baud rate |
| 94 | bytesize=EIGHTBITS, # number of data bits |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 95 | parity=PARITY_NONE, # enable parity checking |
cliechti | dfec0c8 | 2009-07-21 01:35:41 +0000 | [diff] [blame] | 96 | stopbits=STOPBITS_ONE, # number of stop bits |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 97 | timeout=None, # set a timeout value, None to wait forever |
cliechti | 74308e4 | 2010-07-21 14:03:59 +0000 | [diff] [blame] | 98 | xonxoff=False, # enable software flow control |
| 99 | rtscts=False, # enable RTS/CTS flow control |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 100 | writeTimeout=None, # set a timeout for writes |
cliechti | 58a2aee | 2010-05-20 23:37:57 +0000 | [diff] [blame] | 101 | dsrdtr=False, # None: use rtscts setting, dsrdtr override if True or False |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 102 | interCharTimeout=None # Inter-character timeout, None to disable |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 103 | ): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 104 | """\ |
| 105 | Initialize comm port object. If a port is given, then the port will be |
| 106 | opened immediately. Otherwise a Serial port object in closed state |
| 107 | is returned. |
| 108 | """ |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 109 | |
| 110 | self._isOpen = False |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 111 | self._port = None # correct value is assigned below through properties |
| 112 | self._baudrate = None # correct value is assigned below through properties |
| 113 | self._bytesize = None # correct value is assigned below through properties |
| 114 | self._parity = None # correct value is assigned below through properties |
| 115 | self._stopbits = None # correct value is assigned below through properties |
| 116 | self._timeout = None # correct value is assigned below through properties |
| 117 | self._writeTimeout = None # correct value is assigned below through properties |
| 118 | self._xonxoff = None # correct value is assigned below through properties |
| 119 | self._rtscts = None # correct value is assigned below through properties |
| 120 | self._dsrdtr = None # correct value is assigned below through properties |
| 121 | self._interCharTimeout = None # correct value is assigned below through properties |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 122 | |
| 123 | # assign values using get/set methods using the properties feature |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 124 | self.port = port |
| 125 | self.baudrate = baudrate |
| 126 | self.bytesize = bytesize |
| 127 | self.parity = parity |
| 128 | self.stopbits = stopbits |
| 129 | self.timeout = timeout |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 130 | self.writeTimeout = writeTimeout |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 131 | self.xonxoff = xonxoff |
| 132 | self.rtscts = rtscts |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 133 | self.dsrdtr = dsrdtr |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 134 | self.interCharTimeout = interCharTimeout |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 135 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 136 | if port is not None: |
| 137 | self.open() |
| 138 | |
| 139 | def isOpen(self): |
| 140 | """Check if the port is opened.""" |
| 141 | return self._isOpen |
| 142 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 143 | |
| 144 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 145 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 146 | @property |
| 147 | def port(self): |
| 148 | """\ |
| 149 | Get the current port setting. The value that was passed on init or using |
| 150 | setPort() is passed back. See also the attribute portstr which contains |
| 151 | the name of the port as a string. |
| 152 | """ |
| 153 | return self._port |
| 154 | |
| 155 | @port.setter |
| 156 | def port(self, port): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 157 | """\ |
| 158 | Change the port. The attribute portstr is set to a string that |
| 159 | contains the name of the port. |
| 160 | """ |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 161 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 162 | was_open = self._isOpen |
| 163 | if was_open: self.close() |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 164 | self.portstr = port |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 165 | self._port = port |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 166 | self.name = self.portstr |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 167 | if was_open: self.open() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 168 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 169 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 170 | @property |
| 171 | def baudrate(self): |
| 172 | """Get the current baud rate setting.""" |
| 173 | return self._baudrate |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 174 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 175 | @baudrate.setter |
| 176 | def baudrate(self, baudrate): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 177 | """\ |
| 178 | Change baud rate. It raises a ValueError if the port is open and the |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 179 | baud rate is not possible. If the port is closed, then the value is |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 180 | accepted and the exception is raised when the port is opened. |
| 181 | """ |
cliechti | 107db8d | 2004-01-15 01:20:23 +0000 | [diff] [blame] | 182 | try: |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 183 | b = int(baudrate) |
cliechti | 107db8d | 2004-01-15 01:20:23 +0000 | [diff] [blame] | 184 | except TypeError: |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 185 | raise ValueError("Not a valid baudrate: %r" % (baudrate,)) |
cliechti | 107db8d | 2004-01-15 01:20:23 +0000 | [diff] [blame] | 186 | else: |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 187 | if b <= 0: |
| 188 | raise ValueError("Not a valid baudrate: %r" % (baudrate,)) |
| 189 | self._baudrate = b |
cliechti | 107db8d | 2004-01-15 01:20:23 +0000 | [diff] [blame] | 190 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 191 | |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 192 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 193 | @property |
| 194 | def bytesize(self): |
| 195 | """Get the current byte size setting.""" |
| 196 | return self._bytesize |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 197 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 198 | @bytesize.setter |
| 199 | def bytesize(self, bytesize): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 200 | """Change byte size.""" |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 201 | if bytesize not in self.BYTESIZES: raise ValueError("Not a valid byte size: %r" % (bytesize,)) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 202 | self._bytesize = bytesize |
| 203 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 204 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 205 | |
| 206 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 207 | @property |
| 208 | def parity(self): |
| 209 | """Get the current parity setting.""" |
| 210 | return self._parity |
| 211 | |
| 212 | @parity.setter |
| 213 | def parity(self, parity): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 214 | """Change parity setting.""" |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 215 | if parity not in self.PARITIES: raise ValueError("Not a valid parity: %r" % (parity,)) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 216 | self._parity = parity |
| 217 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 218 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 219 | |
| 220 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 221 | @property |
| 222 | def stopbits(self): |
| 223 | """Get the current stop bits setting.""" |
| 224 | return self._stopbits |
| 225 | |
| 226 | @stopbits.setter |
| 227 | def stopbits(self, stopbits): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 228 | """Change stop bits size.""" |
| 229 | if stopbits not in self.STOPBITS: raise ValueError("Not a valid stop bit size: %r" % (stopbits,)) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 230 | self._stopbits = stopbits |
| 231 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 232 | |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 233 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 234 | @property |
| 235 | def timeout(self): |
| 236 | """Get the current timeout setting.""" |
| 237 | return self._timeout |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 238 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 239 | @timeout.setter |
| 240 | def timeout(self, timeout): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 241 | """Change timeout setting.""" |
| 242 | if timeout is not None: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 243 | try: |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 244 | timeout + 1 # test if it's a number, will throw a TypeError if not... |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 245 | except TypeError: |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 246 | raise ValueError("Not a valid timeout: %r" % (timeout,)) |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 247 | if timeout < 0: raise ValueError("Not a valid timeout: %r" % (timeout,)) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 248 | self._timeout = timeout |
| 249 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 250 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 251 | |
| 252 | @property |
| 253 | def writeTimeout(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 254 | """Get the current timeout setting.""" |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 255 | return self._writeTimeout |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 256 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 257 | @writeTimeout.setter |
| 258 | def writeTimeout(self, timeout): |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 259 | """Change timeout setting.""" |
| 260 | if timeout is not None: |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 261 | if timeout < 0: raise ValueError("Not a valid timeout: %r" % (timeout,)) |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 262 | try: |
| 263 | timeout + 1 #test if it's a number, will throw a TypeError if not... |
| 264 | except TypeError: |
| 265 | raise ValueError("Not a valid timeout: %r" % timeout) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 266 | |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 267 | self._writeTimeout = timeout |
| 268 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 269 | |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 270 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 271 | @property |
| 272 | def xonxoff(self): |
| 273 | """Get the current XON/XOFF setting.""" |
| 274 | return self._xonxoff |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 275 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 276 | @xonxoff.setter |
| 277 | def xonxoff(self, xonxoff): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 278 | """Change XON/XOFF setting.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 279 | self._xonxoff = xonxoff |
| 280 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 281 | |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 282 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 283 | @property |
| 284 | def rtscts(self): |
| 285 | """Get the current RTS/CTS flow control setting.""" |
| 286 | return self._rtscts |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 287 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 288 | @rtscts.setter |
| 289 | def rtscts(self, rtscts): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 290 | """Change RTS/CTS flow control setting.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 291 | self._rtscts = rtscts |
| 292 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 293 | |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 294 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 295 | @property |
| 296 | def dsrdtr(self): |
| 297 | """Get the current DSR/DTR flow control setting.""" |
| 298 | return self._dsrdtr |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 299 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 300 | @dsrdtr.setter |
| 301 | def dsrdtr(self, dsrdtr=None): |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 302 | """Change DsrDtr flow control setting.""" |
| 303 | if dsrdtr is None: |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 304 | # if not set, keep backwards compatibility and follow rtscts setting |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 305 | self._dsrdtr = self._rtscts |
| 306 | else: |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 307 | # if defined independently, follow its value |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 308 | self._dsrdtr = dsrdtr |
| 309 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 310 | |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 311 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 312 | @property |
| 313 | def interCharTimeout(self): |
| 314 | """Get the current inter-character timeout setting.""" |
| 315 | return self._interCharTimeout |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 316 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 317 | @interCharTimeout.setter |
| 318 | def interCharTimeout(self, interCharTimeout): |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 319 | """Change inter-character timeout setting.""" |
| 320 | if interCharTimeout is not None: |
| 321 | if interCharTimeout < 0: raise ValueError("Not a valid timeout: %r" % interCharTimeout) |
| 322 | try: |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 323 | interCharTimeout + 1 # test if it's a number, will throw a TypeError if not... |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 324 | except TypeError: |
| 325 | raise ValueError("Not a valid timeout: %r" % interCharTimeout) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 326 | |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 327 | self._interCharTimeout = interCharTimeout |
| 328 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 329 | |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 330 | |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 331 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 332 | |
| 333 | _SETTINGS = ('baudrate', 'bytesize', 'parity', 'stopbits', 'xonxoff', |
| 334 | 'dsrdtr', 'rtscts', 'timeout', 'writeTimeout', 'interCharTimeout') |
| 335 | |
| 336 | def getSettingsDict(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 337 | """\ |
| 338 | Get current port settings as a dictionary. For use with |
| 339 | applySettingsDict. |
| 340 | """ |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 341 | return dict([(key, getattr(self, '_'+key)) for key in self._SETTINGS]) |
| 342 | |
| 343 | def applySettingsDict(self, d): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 344 | """\ |
| 345 | apply stored settings from a dictionary returned from |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 346 | getSettingsDict. it's allowed to delete keys from the dictionary. these |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 347 | values will simply left unchanged. |
| 348 | """ |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 349 | for key in self._SETTINGS: |
Chris Liechti | b74be01 | 2015-08-04 16:36:01 +0200 | [diff] [blame^] | 350 | if key in d and d[key] != getattr(self, '_'+key): # check against internal "_" value |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 351 | setattr(self, key, d[key]) # set non "_" value to use properties write function |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 352 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 353 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 354 | |
| 355 | def __repr__(self): |
| 356 | """String representation of the current port settings and its state.""" |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 357 | return "%s<id=0x%x, open=%s>(port=%r, baudrate=%r, bytesize=%r, parity=%r, stopbits=%r, timeout=%r, xonxoff=%r, rtscts=%r, dsrdtr=%r)" % ( |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 358 | self.__class__.__name__, |
| 359 | id(self), |
| 360 | self._isOpen, |
| 361 | self.portstr, |
| 362 | self.baudrate, |
| 363 | self.bytesize, |
| 364 | self.parity, |
| 365 | self.stopbits, |
| 366 | self.timeout, |
| 367 | self.xonxoff, |
| 368 | self.rtscts, |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 369 | self.dsrdtr, |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 370 | ) |
| 371 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 372 | |
| 373 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 374 | # compatibility with io library |
| 375 | |
| 376 | def readable(self): return True |
| 377 | def writable(self): return True |
| 378 | def seekable(self): return False |
| 379 | def readinto(self, b): |
| 380 | data = self.read(len(b)) |
| 381 | n = len(data) |
| 382 | try: |
| 383 | b[:n] = data |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 384 | except TypeError as err: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 385 | import array |
| 386 | if not isinstance(b, array.array): |
| 387 | raise err |
| 388 | b[:n] = array.array('b', data) |
| 389 | return n |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 390 | |
Chris Liechti | 70b8923 | 2015-08-04 03:00:52 +0200 | [diff] [blame] | 391 | # - - - - - - - - - - - - - - - - - - - - - - - - |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 392 | # context manager |
| 393 | |
| 394 | def __enter__(self): |
| 395 | return self |
| 396 | |
| 397 | def __exit__(self, *args, **kwargs): |
| 398 | self.close() |
| 399 | |
| 400 | # - - - - - - - - - - - - - - - - - - - - - - - - |
Chris Liechti | 70b8923 | 2015-08-04 03:00:52 +0200 | [diff] [blame] | 401 | # additional functionality |
| 402 | |
| 403 | def read_until(self, terminator=LF, size=None): |
| 404 | """\ |
| 405 | Read until a termination sequence is found ('\n' by default), the size |
| 406 | is exceeded or until timeout occurs. |
| 407 | """ |
| 408 | lenterm = len(terminator) |
| 409 | line = bytearray() |
| 410 | while True: |
| 411 | c = self.read(1) |
| 412 | if c: |
| 413 | line += c |
| 414 | if line[-lenterm:] == terminator: |
| 415 | break |
| 416 | if size is not None and len(line) >= size: |
| 417 | break |
| 418 | else: |
| 419 | break |
| 420 | return bytes(line) |
| 421 | |
| 422 | |
| 423 | def iread_until(self, *args, **kwargs): |
| 424 | """\ |
| 425 | Read lines, implemented as generator. It will raise StopIteration on |
| 426 | timeout (empty read). |
| 427 | """ |
| 428 | while True: |
| 429 | line = self.read_until(*args, **kwargs) |
| 430 | if not line: break |
| 431 | yield line |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 432 | |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 433 | |
| 434 | # - - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 435 | if __name__ == '__main__': |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 436 | import sys |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 437 | s = SerialBase() |
Chris Liechti | 779b1a2 | 2015-08-04 14:54:22 +0200 | [diff] [blame] | 438 | sys.stdout.write('port name: %s\n' % s.name) |
| 439 | sys.stdout.write('baud rates: %s\n' % s.BAUDRATES) |
| 440 | sys.stdout.write('byte sizes: %s\n' % s.BYTESIZES) |
| 441 | sys.stdout.write('parities: %s\n' % s.PARITIES) |
| 442 | sys.stdout.write('stop bits: %s\n' % s.STOPBITS) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 443 | sys.stdout.write('%s\n' % s) |