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