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 | 8e99b6f | 2010-07-21 15:46:39 +0000 | [diff] [blame] | 8 | # compatibility for older Python < 2.6 |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 9 | try: |
| 10 | bytes |
| 11 | bytearray |
cliechti | a3a811f | 2009-07-29 21:59:03 +0000 | [diff] [blame] | 12 | except (NameError, AttributeError): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 13 | # Python older than 2.6 do not have these types. Like for Python 2.6 they |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 14 | # should behave like str. For Python older than 3.0 we want to work with |
| 15 | # strings anyway, only later versions have a true bytes type. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 16 | bytes = str |
| 17 | # bytearray is a mutable type that is easily turned into an instance of |
| 18 | # bytes |
| 19 | class bytearray(list): |
| 20 | # for bytes(bytearray()) usage |
| 21 | def __str__(self): return ''.join(self) |
cliechti | c323f1f | 2010-07-22 00:14:26 +0000 | [diff] [blame] | 22 | def __repr__(self): return 'bytearray(%r)' % ''.join(self) |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 23 | # append automatically converts integers to characters |
| 24 | def append(self, item): |
| 25 | if isinstance(item, str): |
| 26 | list.append(self, item) |
| 27 | else: |
| 28 | list.append(self, chr(item)) |
cliechti | 1de32cd | 2009-08-07 19:05:09 +0000 | [diff] [blame] | 29 | # += |
| 30 | def __iadd__(self, other): |
| 31 | for byte in other: |
| 32 | self.append(byte) |
| 33 | return self |
cliechti | 0bbebbf | 2009-07-30 17:23:42 +0000 | [diff] [blame] | 34 | |
cliechti | c323f1f | 2010-07-22 00:14:26 +0000 | [diff] [blame] | 35 | def __getslice__(self, i, j): |
| 36 | return bytearray(list.__getslice__(self, i, j)) |
| 37 | |
| 38 | def __getitem__(self, item): |
| 39 | if isinstance(item, slice): |
| 40 | return bytearray(list.__getitem__(self, item)) |
| 41 | else: |
| 42 | return ord(list.__getitem__(self, item)) |
| 43 | |
| 44 | def __eq__(self, other): |
| 45 | if isinstance(other, basestring): |
| 46 | other = bytearray(other) |
| 47 | return list.__eq__(self, other) |
| 48 | |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 49 | # ``memoryview`` was introduced in Python 2.7 and ``bytes(some_memoryview)`` |
| 50 | # isn't returning the contents (very unfortunate). Therefore we need special |
| 51 | # cases and test for it. Ensure that there is a ``memoryview`` object for older |
| 52 | # Python versions. This is easier than making every test dependent on its |
| 53 | # existence. |
| 54 | try: |
| 55 | memoryview |
| 56 | except (NameError, AttributeError): |
| 57 | # implementation does not matter as we do not realy use it. |
| 58 | # it just must not inherit from something else we might care for. |
| 59 | class memoryview: |
| 60 | pass |
| 61 | |
| 62 | |
| 63 | # all Python versions prior 3.x convert ``str([17])`` to '[17]' instead of '\x11' |
| 64 | # so a simple ``bytes(sequence)`` doesn't work for all versions |
cliechti | 32c1033 | 2009-08-05 13:23:43 +0000 | [diff] [blame] | 65 | def to_bytes(seq): |
| 66 | """convert a sequence to a bytes type""" |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 67 | if isinstance(seq, bytes): |
| 68 | return seq |
| 69 | elif isinstance(seq, bytearray): |
| 70 | return bytes(seq) |
| 71 | elif isinstance(seq, memoryview): |
| 72 | return seq.tobytes() |
| 73 | else: |
| 74 | b = bytearray() |
| 75 | for item in seq: |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 76 | 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] | 77 | return bytes(b) |
cliechti | 32c1033 | 2009-08-05 13:23:43 +0000 | [diff] [blame] | 78 | |
| 79 | # create control bytes |
| 80 | XON = to_bytes([17]) |
| 81 | XOFF = to_bytes([19]) |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 82 | |
cliechti | 8e99b6f | 2010-07-21 15:46:39 +0000 | [diff] [blame] | 83 | CR = to_bytes([13]) |
| 84 | LF = to_bytes([10]) |
| 85 | |
cliechti | a3a811f | 2009-07-29 21:59:03 +0000 | [diff] [blame] | 86 | |
cliechti | 0d6029a | 2008-06-21 01:28:46 +0000 | [diff] [blame] | 87 | 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] | 88 | STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO = (1, 1.5, 2) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 89 | FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5, 6, 7, 8) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 90 | |
| 91 | PARITY_NAMES = { |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 92 | PARITY_NONE: 'None', |
| 93 | PARITY_EVEN: 'Even', |
| 94 | PARITY_ODD: 'Odd', |
| 95 | PARITY_MARK: 'Mark', |
| 96 | PARITY_SPACE: 'Space', |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 97 | } |
| 98 | |
cliechti | 1dbe4b6 | 2002-02-14 02:49:25 +0000 | [diff] [blame] | 99 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 100 | class SerialException(IOError): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 101 | """Base class for serial port related exceptions.""" |
cliechti | 7fe54d5 | 2002-03-03 20:11:47 +0000 | [diff] [blame] | 102 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 103 | |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 104 | class SerialTimeoutException(SerialException): |
| 105 | """Write timeouts give an exception""" |
| 106 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 107 | |
cliechti | 4b20ec6 | 2012-08-16 01:04:44 +0000 | [diff] [blame] | 108 | writeTimeoutError = SerialTimeoutException('Write timeout') |
| 109 | portNotOpenError = SerialException('Attempting to use a port that is not open') |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 110 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 111 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 112 | class FileLike(object): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 113 | """\ |
| 114 | An abstract file like class. |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 115 | |
cliechti | 1dbe4b6 | 2002-02-14 02:49:25 +0000 | [diff] [blame] | 116 | This class implements readline and readlines based on read and |
| 117 | writelines based on write. |
| 118 | This class is used to provide the above functions for to Serial |
| 119 | port objects. |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 120 | |
cliechti | 1dbe4b6 | 2002-02-14 02:49:25 +0000 | [diff] [blame] | 121 | Note that when the serial port was opened with _NO_ timeout that |
| 122 | readline blocks until it sees a newline (or the specified size is |
| 123 | reached) and that readlines would never return and therefore |
| 124 | refuses to work (it raises an exception in this case)! |
| 125 | """ |
| 126 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 127 | def __init__(self): |
| 128 | self.closed = True |
| 129 | |
| 130 | def close(self): |
| 131 | self.closed = True |
| 132 | |
| 133 | # so that ports are closed when objects are discarded |
| 134 | def __del__(self): |
| 135 | """Destructor. Calls close().""" |
| 136 | # The try/except block is in case this is called at program |
| 137 | # exit time, when it's possible that globals have already been |
| 138 | # deleted, and then the close() call might fail. Since |
| 139 | # there's nothing we can do about such failures and they annoy |
| 140 | # the end users, we suppress the traceback. |
| 141 | try: |
| 142 | self.close() |
| 143 | except: |
| 144 | pass |
| 145 | |
cliechti | 1dbe4b6 | 2002-02-14 02:49:25 +0000 | [diff] [blame] | 146 | def writelines(self, sequence): |
| 147 | for line in sequence: |
| 148 | self.write(line) |
| 149 | |
| 150 | def flush(self): |
cliechti | 1bb1bb2 | 2002-08-18 00:43:58 +0000 | [diff] [blame] | 151 | """flush of file like objects""" |
| 152 | pass |
cliechti | 1dbe4b6 | 2002-02-14 02:49:25 +0000 | [diff] [blame] | 153 | |
cliechti | 980e4b0 | 2005-12-20 23:19:58 +0000 | [diff] [blame] | 154 | # iterator for e.g. "for line in Serial(0): ..." usage |
| 155 | def next(self): |
| 156 | line = self.readline() |
| 157 | if not line: raise StopIteration |
| 158 | return line |
| 159 | |
| 160 | def __iter__(self): |
| 161 | return self |
| 162 | |
cliechti | 8e99b6f | 2010-07-21 15:46:39 +0000 | [diff] [blame] | 163 | def readline(self, size=None, eol=LF): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 164 | """\ |
| 165 | Read a line which is terminated with end-of-line (eol) character |
| 166 | ('\n' by default) or until timeout. |
| 167 | """ |
cliechti | 8e99b6f | 2010-07-21 15:46:39 +0000 | [diff] [blame] | 168 | leneol = len(eol) |
| 169 | line = bytearray() |
| 170 | while True: |
| 171 | c = self.read(1) |
| 172 | if c: |
| 173 | line += c |
| 174 | if line[-leneol:] == eol: |
| 175 | break |
| 176 | if size is not None and len(line) >= size: |
| 177 | break |
| 178 | else: |
| 179 | break |
| 180 | return bytes(line) |
| 181 | |
cliechti | c323f1f | 2010-07-22 00:14:26 +0000 | [diff] [blame] | 182 | def readlines(self, sizehint=None, eol=LF): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 183 | """\ |
| 184 | Read a list of lines, until timeout. |
| 185 | sizehint is ignored. |
| 186 | """ |
cliechti | 8e99b6f | 2010-07-21 15:46:39 +0000 | [diff] [blame] | 187 | if self.timeout is None: |
| 188 | raise ValueError("Serial port MUST have enabled timeout for this function!") |
cliechti | 0f2bd58 | 2011-08-05 02:58:37 +0000 | [diff] [blame] | 189 | leneol = len(eol) |
cliechti | 8e99b6f | 2010-07-21 15:46:39 +0000 | [diff] [blame] | 190 | lines = [] |
| 191 | while True: |
| 192 | line = self.readline(eol=eol) |
| 193 | if line: |
| 194 | lines.append(line) |
cliechti | 0f2bd58 | 2011-08-05 02:58:37 +0000 | [diff] [blame] | 195 | if line[-leneol:] != eol: # was the line received with a timeout? |
cliechti | 8e99b6f | 2010-07-21 15:46:39 +0000 | [diff] [blame] | 196 | break |
| 197 | else: |
| 198 | break |
| 199 | return lines |
| 200 | |
| 201 | def xreadlines(self, sizehint=None): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 202 | """\ |
| 203 | Read lines, implemented as generator. It will raise StopIteration on |
| 204 | timeout (empty read). sizehint is ignored. |
| 205 | """ |
cliechti | 8e99b6f | 2010-07-21 15:46:39 +0000 | [diff] [blame] | 206 | while True: |
| 207 | line = self.readline() |
| 208 | if not line: break |
| 209 | yield line |
| 210 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 211 | # other functions of file-likes - not used by pySerial |
cliechti | 980e4b0 | 2005-12-20 23:19:58 +0000 | [diff] [blame] | 212 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 213 | #~ readinto(b) |
| 214 | |
| 215 | def seek(self, pos, whence=0): |
| 216 | raise IOError("file is not seekable") |
| 217 | |
| 218 | def tell(self): |
| 219 | raise IOError("file is not seekable") |
| 220 | |
| 221 | def truncate(self, n=None): |
| 222 | raise IOError("file is not seekable") |
| 223 | |
| 224 | def isatty(self): |
| 225 | return False |
| 226 | |
| 227 | |
| 228 | class SerialBase(object): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 229 | """\ |
| 230 | Serial port base class. Provides __init__ function and properties to |
| 231 | get/set port settings. |
| 232 | """ |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 233 | |
cliechti | dfec0c8 | 2009-07-21 01:35:41 +0000 | [diff] [blame] | 234 | # default values, may be overridden in subclasses that do not support all values |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 235 | BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 236 | 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, |
| 237 | 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, |
| 238 | 3000000, 3500000, 4000000) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 239 | BYTESIZES = (FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 240 | PARITIES = (PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE) |
cliechti | 6ffdb8f | 2009-07-22 00:48:57 +0000 | [diff] [blame] | 241 | STOPBITS = (STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 242 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 243 | def __init__(self, |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 244 | port = None, # number of device, numbering starts at |
| 245 | # zero. if everything fails, the user |
| 246 | # can specify a device string, note |
| 247 | # that this isn't portable anymore |
| 248 | # port will be opened if one is specified |
cliechti | dfec0c8 | 2009-07-21 01:35:41 +0000 | [diff] [blame] | 249 | baudrate=9600, # baud rate |
| 250 | bytesize=EIGHTBITS, # number of data bits |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 251 | parity=PARITY_NONE, # enable parity checking |
cliechti | dfec0c8 | 2009-07-21 01:35:41 +0000 | [diff] [blame] | 252 | stopbits=STOPBITS_ONE, # number of stop bits |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 253 | timeout=None, # set a timeout value, None to wait forever |
cliechti | 74308e4 | 2010-07-21 14:03:59 +0000 | [diff] [blame] | 254 | xonxoff=False, # enable software flow control |
| 255 | rtscts=False, # enable RTS/CTS flow control |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 256 | writeTimeout=None, # set a timeout for writes |
cliechti | 58a2aee | 2010-05-20 23:37:57 +0000 | [diff] [blame] | 257 | dsrdtr=False, # None: use rtscts setting, dsrdtr override if True or False |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 258 | interCharTimeout=None # Inter-character timeout, None to disable |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 259 | ): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 260 | """\ |
| 261 | Initialize comm port object. If a port is given, then the port will be |
| 262 | opened immediately. Otherwise a Serial port object in closed state |
| 263 | is returned. |
| 264 | """ |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 265 | |
| 266 | self._isOpen = False |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 267 | self._port = None # correct value is assigned below through properties |
| 268 | self._baudrate = None # correct value is assigned below through properties |
| 269 | self._bytesize = None # correct value is assigned below through properties |
| 270 | self._parity = None # correct value is assigned below through properties |
| 271 | self._stopbits = None # correct value is assigned below through properties |
| 272 | self._timeout = None # correct value is assigned below through properties |
| 273 | self._writeTimeout = None # correct value is assigned below through properties |
| 274 | self._xonxoff = None # correct value is assigned below through properties |
| 275 | self._rtscts = None # correct value is assigned below through properties |
| 276 | self._dsrdtr = None # correct value is assigned below through properties |
| 277 | self._interCharTimeout = None # correct value is assigned below through properties |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 278 | |
| 279 | # assign values using get/set methods using the properties feature |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 280 | self.port = port |
| 281 | self.baudrate = baudrate |
| 282 | self.bytesize = bytesize |
| 283 | self.parity = parity |
| 284 | self.stopbits = stopbits |
| 285 | self.timeout = timeout |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 286 | self.writeTimeout = writeTimeout |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 287 | self.xonxoff = xonxoff |
| 288 | self.rtscts = rtscts |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 289 | self.dsrdtr = dsrdtr |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 290 | self.interCharTimeout = interCharTimeout |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 291 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 292 | if port is not None: |
| 293 | self.open() |
| 294 | |
| 295 | def isOpen(self): |
| 296 | """Check if the port is opened.""" |
| 297 | return self._isOpen |
| 298 | |
| 299 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 300 | |
cliechti | 9147c44 | 2009-07-21 22:12:16 +0000 | [diff] [blame] | 301 | # TODO: these are not really needed as the is the BAUDRATES etc. attribute... |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 302 | # maybe i remove them before the final release... |
| 303 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 304 | def getSupportedBaudrates(self): |
| 305 | return [(str(b), b) for b in self.BAUDRATES] |
| 306 | |
| 307 | def getSupportedByteSizes(self): |
| 308 | return [(str(b), b) for b in self.BYTESIZES] |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 309 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 310 | def getSupportedStopbits(self): |
| 311 | return [(str(b), b) for b in self.STOPBITS] |
| 312 | |
| 313 | def getSupportedParities(self): |
| 314 | return [(PARITY_NAMES[b], b) for b in self.PARITIES] |
| 315 | |
| 316 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 317 | |
| 318 | def setPort(self, port): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 319 | """\ |
| 320 | Change the port. The attribute portstr is set to a string that |
| 321 | contains the name of the port. |
| 322 | """ |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 323 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 324 | was_open = self._isOpen |
| 325 | if was_open: self.close() |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame^] | 326 | self.portstr = port |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 327 | self._port = port |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 328 | self.name = self.portstr |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 329 | if was_open: self.open() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 330 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 331 | def getPort(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 332 | """\ |
| 333 | Get the current port setting. The value that was passed on init or using |
| 334 | setPort() is passed back. See also the attribute portstr which contains |
| 335 | the name of the port as a string. |
| 336 | """ |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 337 | return self._port |
| 338 | |
cliechti | 0276f5e | 2004-11-13 03:14:11 +0000 | [diff] [blame] | 339 | port = property(getPort, setPort, doc="Port setting") |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 340 | |
| 341 | |
| 342 | def setBaudrate(self, baudrate): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 343 | """\ |
| 344 | 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] | 345 | 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] | 346 | accepted and the exception is raised when the port is opened. |
| 347 | """ |
cliechti | 107db8d | 2004-01-15 01:20:23 +0000 | [diff] [blame] | 348 | try: |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 349 | b = int(baudrate) |
cliechti | 107db8d | 2004-01-15 01:20:23 +0000 | [diff] [blame] | 350 | except TypeError: |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 351 | raise ValueError("Not a valid baudrate: %r" % (baudrate,)) |
cliechti | 107db8d | 2004-01-15 01:20:23 +0000 | [diff] [blame] | 352 | else: |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 353 | if b <= 0: |
| 354 | raise ValueError("Not a valid baudrate: %r" % (baudrate,)) |
| 355 | self._baudrate = b |
cliechti | 107db8d | 2004-01-15 01:20:23 +0000 | [diff] [blame] | 356 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 357 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 358 | def getBaudrate(self): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 359 | """Get the current baud rate setting.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 360 | return self._baudrate |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 361 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 362 | baudrate = property(getBaudrate, setBaudrate, doc="Baud rate setting") |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 363 | |
| 364 | |
| 365 | def setByteSize(self, bytesize): |
| 366 | """Change byte size.""" |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 367 | 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] | 368 | self._bytesize = bytesize |
| 369 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 370 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 371 | def getByteSize(self): |
| 372 | """Get the current byte size setting.""" |
| 373 | return self._bytesize |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 374 | |
cliechti | 0276f5e | 2004-11-13 03:14:11 +0000 | [diff] [blame] | 375 | bytesize = property(getByteSize, setByteSize, doc="Byte size setting") |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 376 | |
| 377 | |
| 378 | def setParity(self, parity): |
| 379 | """Change parity setting.""" |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 380 | 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] | 381 | self._parity = parity |
| 382 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 383 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 384 | def getParity(self): |
| 385 | """Get the current parity setting.""" |
| 386 | return self._parity |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 387 | |
cliechti | 0276f5e | 2004-11-13 03:14:11 +0000 | [diff] [blame] | 388 | parity = property(getParity, setParity, doc="Parity setting") |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 389 | |
| 390 | |
| 391 | def setStopbits(self, stopbits): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 392 | """Change stop bits size.""" |
| 393 | 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] | 394 | self._stopbits = stopbits |
| 395 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 396 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 397 | def getStopbits(self): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 398 | """Get the current stop bits setting.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 399 | return self._stopbits |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 400 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 401 | stopbits = property(getStopbits, setStopbits, doc="Stop bits setting") |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 402 | |
| 403 | |
| 404 | def setTimeout(self, timeout): |
| 405 | """Change timeout setting.""" |
| 406 | if timeout is not None: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 407 | try: |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 408 | 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] | 409 | except TypeError: |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 410 | raise ValueError("Not a valid timeout: %r" % (timeout,)) |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 411 | if timeout < 0: raise ValueError("Not a valid timeout: %r" % (timeout,)) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 412 | self._timeout = timeout |
| 413 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 414 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 415 | def getTimeout(self): |
| 416 | """Get the current timeout setting.""" |
| 417 | return self._timeout |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 418 | |
cliechti | 0276f5e | 2004-11-13 03:14:11 +0000 | [diff] [blame] | 419 | timeout = property(getTimeout, setTimeout, doc="Timeout setting for read()") |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 420 | |
| 421 | |
| 422 | def setWriteTimeout(self, timeout): |
| 423 | """Change timeout setting.""" |
| 424 | if timeout is not None: |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 425 | if timeout < 0: raise ValueError("Not a valid timeout: %r" % (timeout,)) |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 426 | try: |
| 427 | timeout + 1 #test if it's a number, will throw a TypeError if not... |
| 428 | except TypeError: |
| 429 | raise ValueError("Not a valid timeout: %r" % timeout) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 430 | |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 431 | self._writeTimeout = timeout |
| 432 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 433 | |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 434 | def getWriteTimeout(self): |
| 435 | """Get the current timeout setting.""" |
| 436 | return self._writeTimeout |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 437 | |
cliechti | 0276f5e | 2004-11-13 03:14:11 +0000 | [diff] [blame] | 438 | writeTimeout = property(getWriteTimeout, setWriteTimeout, doc="Timeout setting for write()") |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 439 | |
| 440 | |
| 441 | def setXonXoff(self, xonxoff): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 442 | """Change XON/XOFF setting.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 443 | self._xonxoff = xonxoff |
| 444 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 445 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 446 | def getXonXoff(self): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 447 | """Get the current XON/XOFF setting.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 448 | return self._xonxoff |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 449 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 450 | xonxoff = property(getXonXoff, setXonXoff, doc="XON/XOFF setting") |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 451 | |
| 452 | def setRtsCts(self, rtscts): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 453 | """Change RTS/CTS flow control setting.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 454 | self._rtscts = rtscts |
| 455 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 456 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 457 | def getRtsCts(self): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 458 | """Get the current RTS/CTS flow control setting.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 459 | return self._rtscts |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 460 | |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 461 | rtscts = property(getRtsCts, setRtsCts, doc="RTS/CTS flow control setting") |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 462 | |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 463 | def setDsrDtr(self, dsrdtr=None): |
| 464 | """Change DsrDtr flow control setting.""" |
| 465 | if dsrdtr is None: |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 466 | # if not set, keep backwards compatibility and follow rtscts setting |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 467 | self._dsrdtr = self._rtscts |
| 468 | else: |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 469 | # if defined independently, follow its value |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 470 | self._dsrdtr = dsrdtr |
| 471 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 472 | |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 473 | def getDsrDtr(self): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 474 | """Get the current DSR/DTR flow control setting.""" |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 475 | return self._dsrdtr |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 476 | |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 477 | dsrdtr = property(getDsrDtr, setDsrDtr, "DSR/DTR flow control setting") |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 478 | |
| 479 | def setInterCharTimeout(self, interCharTimeout): |
| 480 | """Change inter-character timeout setting.""" |
| 481 | if interCharTimeout is not None: |
| 482 | if interCharTimeout < 0: raise ValueError("Not a valid timeout: %r" % interCharTimeout) |
| 483 | try: |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 484 | 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] | 485 | except TypeError: |
| 486 | raise ValueError("Not a valid timeout: %r" % interCharTimeout) |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 487 | |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 488 | self._interCharTimeout = interCharTimeout |
| 489 | if self._isOpen: self._reconfigurePort() |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 490 | |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 491 | def getInterCharTimeout(self): |
| 492 | """Get the current inter-character timeout setting.""" |
| 493 | return self._interCharTimeout |
cliechti | 14b274a | 2009-02-07 00:27:05 +0000 | [diff] [blame] | 494 | |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 495 | interCharTimeout = property(getInterCharTimeout, setInterCharTimeout, doc="Inter-character timeout setting for read()") |
| 496 | |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 497 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 498 | |
| 499 | _SETTINGS = ('baudrate', 'bytesize', 'parity', 'stopbits', 'xonxoff', |
| 500 | 'dsrdtr', 'rtscts', 'timeout', 'writeTimeout', 'interCharTimeout') |
| 501 | |
| 502 | def getSettingsDict(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 503 | """\ |
| 504 | Get current port settings as a dictionary. For use with |
| 505 | applySettingsDict. |
| 506 | """ |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 507 | return dict([(key, getattr(self, '_'+key)) for key in self._SETTINGS]) |
| 508 | |
| 509 | def applySettingsDict(self, d): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 510 | """\ |
| 511 | apply stored settings from a dictionary returned from |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 512 | getSettingsDict. it's allowed to delete keys from the dictionary. these |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 513 | values will simply left unchanged. |
| 514 | """ |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 515 | for key in self._SETTINGS: |
| 516 | if d[key] != getattr(self, '_'+key): # check against internal "_" value |
| 517 | setattr(self, key, d[key]) # set non "_" value to use properties write function |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 518 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 519 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 520 | |
| 521 | def __repr__(self): |
| 522 | """String representation of the current port settings and its state.""" |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 523 | 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] | 524 | self.__class__.__name__, |
| 525 | id(self), |
| 526 | self._isOpen, |
| 527 | self.portstr, |
| 528 | self.baudrate, |
| 529 | self.bytesize, |
| 530 | self.parity, |
| 531 | self.stopbits, |
| 532 | self.timeout, |
| 533 | self.xonxoff, |
| 534 | self.rtscts, |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 535 | self.dsrdtr, |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 536 | ) |
| 537 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 538 | |
| 539 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 540 | # compatibility with io library |
| 541 | |
| 542 | def readable(self): return True |
| 543 | def writable(self): return True |
| 544 | def seekable(self): return False |
| 545 | def readinto(self, b): |
| 546 | data = self.read(len(b)) |
| 547 | n = len(data) |
| 548 | try: |
| 549 | b[:n] = data |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame^] | 550 | except TypeError as err: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 551 | import array |
| 552 | if not isinstance(b, array.array): |
| 553 | raise err |
| 554 | b[:n] = array.array('b', data) |
| 555 | return n |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 556 | |
| 557 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 558 | if __name__ == '__main__': |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 559 | import sys |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 560 | s = SerialBase() |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 561 | sys.stdout.write('port name: %s\n' % s.portstr) |
| 562 | sys.stdout.write('baud rates: %s\n' % s.getSupportedBaudrates()) |
| 563 | sys.stdout.write('byte sizes: %s\n' % s.getSupportedByteSizes()) |
| 564 | sys.stdout.write('parities: %s\n' % s.getSupportedParities()) |
| 565 | sys.stdout.write('stop bits: %s\n' % s.getSupportedStopbits()) |
| 566 | sys.stdout.write('%s\n' % s) |