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