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