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