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