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