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