cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 2 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 3 | # backend for serial IO for POSIX compatible systems, like Linux, OSX |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 4 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 5 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | e13d0f6 | 2016-04-25 23:30:44 +0200 | [diff] [blame] | 6 | # (C) 2001-2016 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 7 | # |
| 8 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 9 | # |
cliechti | c54b2c8 | 2008-06-21 01:59:08 +0000 | [diff] [blame] | 10 | # parts based on code from Grant B. Edwards <grante@visi.com>: |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 11 | # ftp://ftp.visi.com/users/grante/python/PosixSerial.py |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 12 | # |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 13 | # references: http://www.easysw.com/~mike/serial/serial.html |
| 14 | |
Chris Liechti | e13d0f6 | 2016-04-25 23:30:44 +0200 | [diff] [blame] | 15 | # Collection of port names (was previously used by number_to_device which was |
| 16 | # removed. |
| 17 | # - Linux /dev/ttyS%d (confirmed) |
| 18 | # - cygwin/win32 /dev/com%d (confirmed) |
| 19 | # - openbsd (OpenBSD) /dev/cua%02d |
| 20 | # - bsd*, freebsd* /dev/cuad%d |
| 21 | # - darwin (OS X) /dev/cuad%d |
| 22 | # - netbsd /dev/dty%02d (NetBSD 1.6 testing by Erk) |
| 23 | # - irix (IRIX) /dev/ttyf%d (partially tested) names depending on flow control |
| 24 | # - hp (HP-UX) /dev/tty%dp0 (not tested) |
| 25 | # - sunos (Solaris/SunOS) /dev/tty%c (letters, 'a'..'z') (confirmed) |
| 26 | # - aix (AIX) /dev/tty%d |
| 27 | |
| 28 | |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 29 | # pylint: disable=abstract-method |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 30 | import errno |
| 31 | import fcntl |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 32 | import os |
| 33 | import select |
| 34 | import struct |
| 35 | import sys |
| 36 | import termios |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 37 | |
| 38 | import serial |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 39 | from serial.serialutil import SerialBase, SerialException, to_bytes, \ |
| 40 | portNotOpenError, writeTimeoutError, Timeout |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 41 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 42 | |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 43 | class PlatformSpecificBase(object): |
| 44 | BAUDRATE_CONSTANTS = {} |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 45 | |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 46 | def _set_special_baudrate(self, baudrate): |
| 47 | raise NotImplementedError('non-standard baudrates are not supported on this platform') |
| 48 | |
| 49 | def _set_rs485_mode(self, rs485_settings): |
| 50 | raise NotImplementedError('RS485 not supported on this platform') |
| 51 | |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 52 | |
| 53 | # some systems support an extra flag to enable the two in POSIX unsupported |
Chris Liechti | 1153824 | 2016-09-06 22:20:09 +0200 | [diff] [blame] | 54 | # paritiy settings for MARK and SPACE |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 55 | CMSPAR = 0 # default, for unsupported platforms, override below |
| 56 | |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 57 | # try to detect the OS so that a device can be selected... |
| 58 | # this code block should supply a device() and set_special_baudrate() function |
| 59 | # for the platform |
| 60 | plat = sys.platform.lower() |
| 61 | |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 62 | if plat[:5] == 'linux': # Linux (confirmed) # noqa |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 63 | import array |
| 64 | |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 65 | # extra termios flags |
| 66 | CMSPAR = 0o10000000000 # Use "stick" (mark/space) parity |
| 67 | |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 68 | # baudrate ioctls |
| 69 | TCGETS2 = 0x802C542A |
| 70 | TCSETS2 = 0x402C542B |
| 71 | BOTHER = 0o010000 |
| 72 | |
| 73 | # RS485 ioctls |
| 74 | TIOCGRS485 = 0x542E |
| 75 | TIOCSRS485 = 0x542F |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 76 | SER_RS485_ENABLED = 0b00000001 |
| 77 | SER_RS485_RTS_ON_SEND = 0b00000010 |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 78 | SER_RS485_RTS_AFTER_SEND = 0b00000100 |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 79 | SER_RS485_RX_DURING_TX = 0b00010000 |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 80 | |
| 81 | class PlatformSpecific(PlatformSpecificBase): |
| 82 | BAUDRATE_CONSTANTS = { |
| 83 | 0: 0o000000, # hang up |
| 84 | 50: 0o000001, |
| 85 | 75: 0o000002, |
| 86 | 110: 0o000003, |
| 87 | 134: 0o000004, |
| 88 | 150: 0o000005, |
| 89 | 200: 0o000006, |
| 90 | 300: 0o000007, |
| 91 | 600: 0o000010, |
| 92 | 1200: 0o000011, |
| 93 | 1800: 0o000012, |
| 94 | 2400: 0o000013, |
| 95 | 4800: 0o000014, |
| 96 | 9600: 0o000015, |
| 97 | 19200: 0o000016, |
| 98 | 38400: 0o000017, |
| 99 | 57600: 0o010001, |
| 100 | 115200: 0o010002, |
| 101 | 230400: 0o010003, |
| 102 | 460800: 0o010004, |
| 103 | 500000: 0o010005, |
| 104 | 576000: 0o010006, |
| 105 | 921600: 0o010007, |
| 106 | 1000000: 0o010010, |
| 107 | 1152000: 0o010011, |
| 108 | 1500000: 0o010012, |
| 109 | 2000000: 0o010013, |
| 110 | 2500000: 0o010014, |
| 111 | 3000000: 0o010015, |
| 112 | 3500000: 0o010016, |
| 113 | 4000000: 0o010017 |
| 114 | } |
| 115 | |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 116 | def _set_special_baudrate(self, baudrate): |
| 117 | # right size is 44 on x86_64, allow for some growth |
| 118 | buf = array.array('i', [0] * 64) |
| 119 | try: |
| 120 | # get serial_struct |
| 121 | fcntl.ioctl(self.fd, TCGETS2, buf) |
| 122 | # set custom speed |
| 123 | buf[2] &= ~termios.CBAUD |
| 124 | buf[2] |= BOTHER |
| 125 | buf[9] = buf[10] = baudrate |
| 126 | |
| 127 | # set serial_struct |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 128 | fcntl.ioctl(self.fd, TCSETS2, buf) |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 129 | except IOError as e: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 130 | raise ValueError('Failed to set custom baud rate ({}): {}'.format(baudrate, e)) |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 131 | |
| 132 | def _set_rs485_mode(self, rs485_settings): |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 133 | buf = array.array('i', [0] * 8) # flags, delaytx, delayrx, padding |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 134 | try: |
| 135 | fcntl.ioctl(self.fd, TIOCGRS485, buf) |
Chris Liechti | 7a55446 | 2016-03-24 21:17:22 +0100 | [diff] [blame] | 136 | buf[0] |= SER_RS485_ENABLED |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 137 | if rs485_settings is not None: |
| 138 | if rs485_settings.loopback: |
| 139 | buf[0] |= SER_RS485_RX_DURING_TX |
| 140 | else: |
| 141 | buf[0] &= ~SER_RS485_RX_DURING_TX |
| 142 | if rs485_settings.rts_level_for_tx: |
| 143 | buf[0] |= SER_RS485_RTS_ON_SEND |
| 144 | else: |
| 145 | buf[0] &= ~SER_RS485_RTS_ON_SEND |
| 146 | if rs485_settings.rts_level_for_rx: |
| 147 | buf[0] |= SER_RS485_RTS_AFTER_SEND |
| 148 | else: |
| 149 | buf[0] &= ~SER_RS485_RTS_AFTER_SEND |
Chris Liechti | 279201b | 2016-06-09 20:27:05 +0200 | [diff] [blame] | 150 | if rs485_settings.delay_before_tx is not None: |
| 151 | buf[1] = int(rs485_settings.delay_before_tx * 1000) |
| 152 | if rs485_settings.delay_before_rx is not None: |
| 153 | buf[2] = int(rs485_settings.delay_before_rx * 1000) |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 154 | else: |
| 155 | buf[0] = 0 # clear SER_RS485_ENABLED |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 156 | fcntl.ioctl(self.fd, TIOCSRS485, buf) |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 157 | except IOError as e: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 158 | raise ValueError('Failed to set RS485 mode: {}'.format(e)) |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 159 | |
| 160 | |
| 161 | elif plat == 'cygwin': # cygwin/win32 (confirmed) |
| 162 | |
| 163 | class PlatformSpecific(PlatformSpecificBase): |
| 164 | BAUDRATE_CONSTANTS = { |
| 165 | 128000: 0x01003, |
| 166 | 256000: 0x01005, |
| 167 | 500000: 0x01007, |
| 168 | 576000: 0x01008, |
| 169 | 921600: 0x01009, |
| 170 | 1000000: 0x0100a, |
| 171 | 1152000: 0x0100b, |
| 172 | 1500000: 0x0100c, |
| 173 | 2000000: 0x0100d, |
| 174 | 2500000: 0x0100e, |
| 175 | 3000000: 0x0100f |
| 176 | } |
| 177 | |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 178 | |
| 179 | elif plat[:6] == 'darwin': # OS X |
| 180 | import array |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 181 | IOSSIOSPEED = 0x80045402 # _IOW('T', 2, speed_t) |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 182 | |
| 183 | class PlatformSpecific(PlatformSpecificBase): |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 184 | osx_version = os.uname()[2].split('.') |
| 185 | # Tiger or above can support arbitrary serial speeds |
| 186 | if int(osx_version[0]) >= 8: |
| 187 | def _set_special_baudrate(self, baudrate): |
| 188 | # use IOKit-specific call to set up high speeds |
| 189 | buf = array.array('i', [baudrate]) |
| 190 | fcntl.ioctl(self.fd, IOSSIOSPEED, buf, 1) |
| 191 | |
Chris Liechti | 21a306b | 2016-10-09 23:41:26 +0200 | [diff] [blame] | 192 | elif plat[:3] == 'bsd' or \ |
| 193 | plat[:7] == 'freebsd' or \ |
| 194 | plat[:6] == 'netbsd' or \ |
| 195 | plat[:7] == 'openbsd': |
| 196 | |
| 197 | class ReturnBaudrate(object): |
| 198 | def __getitem__(self, key): |
| 199 | return key |
| 200 | |
| 201 | class PlatformSpecific(PlatformSpecificBase): |
| 202 | # Only tested on FreeBSD: |
| 203 | # The baud rate may be passed in as |
| 204 | # a literal value. |
| 205 | BAUDRATE_CONSTANTS = ReturnBaudrate() |
| 206 | |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 207 | else: |
| 208 | class PlatformSpecific(PlatformSpecificBase): |
| 209 | pass |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 210 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 211 | |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 212 | # load some constants for later use. |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 213 | # try to use values from termios, use defaults from linux otherwise |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 214 | TIOCMGET = getattr(termios, 'TIOCMGET', 0x5415) |
| 215 | TIOCMBIS = getattr(termios, 'TIOCMBIS', 0x5416) |
| 216 | TIOCMBIC = getattr(termios, 'TIOCMBIC', 0x5417) |
| 217 | TIOCMSET = getattr(termios, 'TIOCMSET', 0x5418) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 218 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 219 | # TIOCM_LE = getattr(termios, 'TIOCM_LE', 0x001) |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 220 | TIOCM_DTR = getattr(termios, 'TIOCM_DTR', 0x002) |
| 221 | TIOCM_RTS = getattr(termios, 'TIOCM_RTS', 0x004) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 222 | # TIOCM_ST = getattr(termios, 'TIOCM_ST', 0x008) |
| 223 | # TIOCM_SR = getattr(termios, 'TIOCM_SR', 0x010) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 224 | |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 225 | TIOCM_CTS = getattr(termios, 'TIOCM_CTS', 0x020) |
| 226 | TIOCM_CAR = getattr(termios, 'TIOCM_CAR', 0x040) |
| 227 | TIOCM_RNG = getattr(termios, 'TIOCM_RNG', 0x080) |
| 228 | TIOCM_DSR = getattr(termios, 'TIOCM_DSR', 0x100) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 229 | TIOCM_CD = getattr(termios, 'TIOCM_CD', TIOCM_CAR) |
| 230 | TIOCM_RI = getattr(termios, 'TIOCM_RI', TIOCM_RNG) |
| 231 | # TIOCM_OUT1 = getattr(termios, 'TIOCM_OUT1', 0x2000) |
| 232 | # TIOCM_OUT2 = getattr(termios, 'TIOCM_OUT2', 0x4000) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 233 | if hasattr(termios, 'TIOCINQ'): |
| 234 | TIOCINQ = termios.TIOCINQ |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 235 | else: |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 236 | TIOCINQ = getattr(termios, 'FIONREAD', 0x541B) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 237 | TIOCOUTQ = getattr(termios, 'TIOCOUTQ', 0x5411) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 238 | |
| 239 | TIOCM_zero_str = struct.pack('I', 0) |
| 240 | TIOCM_RTS_str = struct.pack('I', TIOCM_RTS) |
| 241 | TIOCM_DTR_str = struct.pack('I', TIOCM_DTR) |
| 242 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 243 | TIOCSBRK = getattr(termios, 'TIOCSBRK', 0x5427) |
| 244 | TIOCCBRK = getattr(termios, 'TIOCCBRK', 0x5428) |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 245 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 246 | |
Chris Liechti | ef6b7b4 | 2015-08-06 22:19:26 +0200 | [diff] [blame] | 247 | class Serial(SerialBase, PlatformSpecific): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 248 | """\ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 249 | Serial port class POSIX implementation. Serial port configuration is |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 250 | done with termios and fcntl. Runs on Linux and many other Un*x like |
cliechti | f0a81d4 | 2014-08-04 14:03:53 +0000 | [diff] [blame] | 251 | systems. |
| 252 | """ |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 253 | |
| 254 | def open(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 255 | """\ |
| 256 | Open port with current settings. This may throw a SerialException |
| 257 | if the port cannot be opened.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 258 | if self._port is None: |
| 259 | raise SerialException("Port must be configured before it can be used.") |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 260 | if self.is_open: |
cliechti | 02ef43a | 2011-03-24 23:33:12 +0000 | [diff] [blame] | 261 | raise SerialException("Port is already open.") |
| 262 | self.fd = None |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 263 | # open |
cliechti | 4616bf1 | 2002-04-08 23:13:14 +0000 | [diff] [blame] | 264 | try: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 265 | self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK) |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 266 | except OSError as msg: |
cliechti | 4616bf1 | 2002-04-08 23:13:14 +0000 | [diff] [blame] | 267 | self.fd = None |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 268 | raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg)) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 269 | #~ fcntl.fcntl(self.fd, fcntl.F_SETFL, 0) # set blocking |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 270 | |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 271 | try: |
Chris Liechti | 9428470 | 2015-11-15 01:21:48 +0100 | [diff] [blame] | 272 | self._reconfigure_port(force_update=True) |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 273 | except: |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 274 | try: |
| 275 | os.close(self.fd) |
| 276 | except: |
| 277 | # ignore any exception when closing the port |
| 278 | # also to keep original exception that happened when setting up |
| 279 | pass |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 280 | self.fd = None |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 281 | raise |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 282 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 283 | self.is_open = True |
Chris Liechti | f2fdeb9 | 2016-05-07 23:57:50 +0200 | [diff] [blame] | 284 | try: |
| 285 | if not self._dsrdtr: |
| 286 | self._update_dtr_state() |
| 287 | if not self._rtscts: |
| 288 | self._update_rts_state() |
| 289 | except IOError as e: |
Chris Liechti | e102962 | 2016-10-02 23:54:36 +0200 | [diff] [blame] | 290 | if e.errno in (errno.EINVAL, errno.ENOTTY): |
| 291 | # ignore Invalid argument and Inappropriate ioctl |
Chris Liechti | f2fdeb9 | 2016-05-07 23:57:50 +0200 | [diff] [blame] | 292 | pass |
| 293 | else: |
| 294 | raise |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 295 | self.reset_input_buffer() |
Chris Liechti | 42ab2b4 | 2016-05-15 23:35:05 +0200 | [diff] [blame] | 296 | self.pipe_abort_read_r, self.pipe_abort_read_w = os.pipe() |
Chris Liechti | 13949c6 | 2016-05-16 22:45:38 +0200 | [diff] [blame] | 297 | self.pipe_abort_write_r, self.pipe_abort_write_w = os.pipe() |
Chris Liechti | 68cecce | 2016-06-03 23:52:34 +0200 | [diff] [blame] | 298 | fcntl.fcntl(self.pipe_abort_read_r, fcntl.F_SETFL, os.O_NONBLOCK) |
| 299 | fcntl.fcntl(self.pipe_abort_write_r, fcntl.F_SETFL, os.O_NONBLOCK) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 300 | |
Chris Liechti | 9428470 | 2015-11-15 01:21:48 +0100 | [diff] [blame] | 301 | def _reconfigure_port(self, force_update=False): |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 302 | """Set communication parameters on opened port.""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 303 | if self.fd is None: |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 304 | raise SerialException("Can only operate on a valid file descriptor") |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 305 | custom_baud = None |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 306 | |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 307 | vmin = vtime = 0 # timeout is done via select |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 308 | if self._inter_byte_timeout is not None: |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 309 | vmin = 1 |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 310 | vtime = int(self._inter_byte_timeout * 10) |
cliechti | 6ce7ab1 | 2002-11-07 02:15:00 +0000 | [diff] [blame] | 311 | try: |
cliechti | 4d0af5e | 2011-08-05 02:18:16 +0000 | [diff] [blame] | 312 | orig_attr = termios.tcgetattr(self.fd) |
| 313 | iflag, oflag, cflag, lflag, ispeed, ospeed, cc = orig_attr |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 314 | except termios.error as msg: # if a port is nonexistent but has a /dev file, it'll fail here |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 315 | raise SerialException("Could not configure port: {}".format(msg)) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 316 | # set up raw mode / no echo / binary |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 317 | cflag |= (termios.CLOCAL | termios.CREAD) |
| 318 | lflag &= ~(termios.ICANON | termios.ECHO | termios.ECHOE | |
| 319 | termios.ECHOK | termios.ECHONL | |
| 320 | termios.ISIG | termios.IEXTEN) # |termios.ECHOPRT |
| 321 | for flag in ('ECHOCTL', 'ECHOKE'): # netbsd workaround for Erk |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 322 | if hasattr(termios, flag): |
| 323 | lflag &= ~getattr(termios, flag) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 324 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 325 | oflag &= ~(termios.OPOST | termios.ONLCR | termios.OCRNL) |
| 326 | iflag &= ~(termios.INLCR | termios.IGNCR | termios.ICRNL | termios.IGNBRK) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 327 | if hasattr(termios, 'IUCLC'): |
| 328 | iflag &= ~termios.IUCLC |
| 329 | if hasattr(termios, 'PARMRK'): |
| 330 | iflag &= ~termios.PARMRK |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 331 | |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 332 | # setup baud rate |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 333 | try: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 334 | ispeed = ospeed = getattr(termios, 'B{}'.format(self._baudrate)) |
cliechti | 895e830 | 2004-04-20 02:40:28 +0000 | [diff] [blame] | 335 | except AttributeError: |
cliechti | f1559d0 | 2007-11-08 23:43:58 +0000 | [diff] [blame] | 336 | try: |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 337 | ispeed = ospeed = self.BAUDRATE_CONSTANTS[self._baudrate] |
cliechti | f1559d0 | 2007-11-08 23:43:58 +0000 | [diff] [blame] | 338 | except KeyError: |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 339 | #~ raise ValueError('Invalid baud rate: %r' % self._baudrate) |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 340 | # may need custom baud rate, it isn't in our list. |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 341 | ispeed = ospeed = getattr(termios, 'B38400') |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 342 | try: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 343 | custom_baud = int(self._baudrate) # store for later |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 344 | except ValueError: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 345 | raise ValueError('Invalid baud rate: {!r}'.format(self._baudrate)) |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 346 | else: |
| 347 | if custom_baud < 0: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 348 | raise ValueError('Invalid baud rate: {!r}'.format(self._baudrate)) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 349 | |
| 350 | # setup char len |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 351 | cflag &= ~termios.CSIZE |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 352 | if self._bytesize == 8: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 353 | cflag |= termios.CS8 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 354 | elif self._bytesize == 7: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 355 | cflag |= termios.CS7 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 356 | elif self._bytesize == 6: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 357 | cflag |= termios.CS6 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 358 | elif self._bytesize == 5: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 359 | cflag |= termios.CS5 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 360 | else: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 361 | raise ValueError('Invalid char len: {!r}'.format(self._bytesize)) |
cliechti | f0a81d4 | 2014-08-04 14:03:53 +0000 | [diff] [blame] | 362 | # setup stop bits |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 363 | if self._stopbits == serial.STOPBITS_ONE: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 364 | cflag &= ~(termios.CSTOPB) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 365 | elif self._stopbits == serial.STOPBITS_ONE_POINT_FIVE: |
| 366 | cflag |= (termios.CSTOPB) # XXX same as TWO.. there is no POSIX support for 1.5 |
| 367 | elif self._stopbits == serial.STOPBITS_TWO: |
| 368 | cflag |= (termios.CSTOPB) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 369 | else: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 370 | raise ValueError('Invalid stop bit specification: {!r}'.format(self._stopbits)) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 371 | # setup parity |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 372 | iflag &= ~(termios.INPCK | termios.ISTRIP) |
| 373 | if self._parity == serial.PARITY_NONE: |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 374 | cflag &= ~(termios.PARENB | termios.PARODD | CMSPAR) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 375 | elif self._parity == serial.PARITY_EVEN: |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 376 | cflag &= ~(termios.PARODD | CMSPAR) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 377 | cflag |= (termios.PARENB) |
| 378 | elif self._parity == serial.PARITY_ODD: |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 379 | cflag &= ~CMSPAR |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 380 | cflag |= (termios.PARENB | termios.PARODD) |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 381 | elif self._parity == serial.PARITY_MARK and CMSPAR: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 382 | cflag |= (termios.PARENB | CMSPAR | termios.PARODD) |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 383 | elif self._parity == serial.PARITY_SPACE and CMSPAR: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 384 | cflag |= (termios.PARENB | CMSPAR) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 385 | cflag &= ~(termios.PARODD) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 386 | else: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 387 | raise ValueError('Invalid parity: {!r}'.format(self._parity)) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 388 | # setup flow control |
| 389 | # xonxoff |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 390 | if hasattr(termios, 'IXANY'): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 391 | if self._xonxoff: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 392 | iflag |= (termios.IXON | termios.IXOFF) # |termios.IXANY) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 393 | else: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 394 | iflag &= ~(termios.IXON | termios.IXOFF | termios.IXANY) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 395 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 396 | if self._xonxoff: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 397 | iflag |= (termios.IXON | termios.IXOFF) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 398 | else: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 399 | iflag &= ~(termios.IXON | termios.IXOFF) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 400 | # rtscts |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 401 | if hasattr(termios, 'CRTSCTS'): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 402 | if self._rtscts: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 403 | cflag |= (termios.CRTSCTS) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 404 | else: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 405 | cflag &= ~(termios.CRTSCTS) |
| 406 | elif hasattr(termios, 'CNEW_RTSCTS'): # try it with alternate constant name |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 407 | if self._rtscts: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 408 | cflag |= (termios.CNEW_RTSCTS) |
cliechti | d474369 | 2002-04-08 22:39:53 +0000 | [diff] [blame] | 409 | else: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 410 | cflag &= ~(termios.CNEW_RTSCTS) |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 411 | # XXX should there be a warning if setting up rtscts (and xonxoff etc) fails?? |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 412 | |
| 413 | # buffer |
cliechti | f0a81d4 | 2014-08-04 14:03:53 +0000 | [diff] [blame] | 414 | # vmin "minimal number of characters to be read. 0 for non blocking" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 415 | if vmin < 0 or vmin > 255: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 416 | raise ValueError('Invalid vmin: {!r}'.format(vmin)) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 417 | cc[termios.VMIN] = vmin |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 418 | # vtime |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 419 | if vtime < 0 or vtime > 255: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 420 | raise ValueError('Invalid vtime: {!r}'.format(vtime)) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 421 | cc[termios.VTIME] = vtime |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 422 | # activate settings |
Chris Liechti | 9428470 | 2015-11-15 01:21:48 +0100 | [diff] [blame] | 423 | if force_update or [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] != orig_attr: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 424 | termios.tcsetattr( |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 425 | self.fd, |
| 426 | termios.TCSANOW, |
| 427 | [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 428 | |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 429 | # apply custom baud rate, if any |
| 430 | if custom_baud is not None: |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 431 | self._set_special_baudrate(custom_baud) |
| 432 | |
| 433 | if self._rs485_mode is not None: |
| 434 | self._set_rs485_mode(self._rs485_mode) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 435 | |
| 436 | def close(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 437 | """Close port""" |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 438 | if self.is_open: |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 439 | if self.fd is not None: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 440 | os.close(self.fd) |
| 441 | self.fd = None |
Chris Liechti | b658eac | 2016-05-22 20:51:44 +0200 | [diff] [blame] | 442 | os.close(self.pipe_abort_read_w) |
| 443 | os.close(self.pipe_abort_read_r) |
| 444 | os.close(self.pipe_abort_write_w) |
| 445 | os.close(self.pipe_abort_write_r) |
| 446 | self.pipe_abort_read_r, self.pipe_abort_read_w = None, None |
| 447 | self.pipe_abort_write_r, self.pipe_abort_write_w = None, None |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 448 | self.is_open = False |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 449 | |
| 450 | # - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 451 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 452 | @property |
| 453 | def in_waiting(self): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 454 | """Return the number of bytes currently in the input buffer.""" |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 455 | #~ s = fcntl.ioctl(self.fd, termios.FIONREAD, TIOCM_zero_str) |
cliechti | f5831e0 | 2002-12-05 23:15:27 +0000 | [diff] [blame] | 456 | s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 457 | return struct.unpack('I', s)[0] |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 458 | |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 459 | # select based implementation, proved to work on many systems |
| 460 | def read(self, size=1): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 461 | """\ |
| 462 | Read size bytes from the serial port. If a timeout is set it may |
| 463 | return less characters as requested. With no timeout it will block |
| 464 | until the requested number of bytes is read. |
| 465 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 466 | if not self.is_open: |
| 467 | raise portNotOpenError |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 468 | read = bytearray() |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 469 | timeout = Timeout(self._timeout) |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 470 | while len(read) < size: |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 471 | try: |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 472 | ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout.time_left()) |
Chris Liechti | 42ab2b4 | 2016-05-15 23:35:05 +0200 | [diff] [blame] | 473 | if self.pipe_abort_read_r in ready: |
Chris Liechti | 68cecce | 2016-06-03 23:52:34 +0200 | [diff] [blame] | 474 | os.read(self.pipe_abort_read_r, 1000) |
Chris Liechti | 42ab2b4 | 2016-05-15 23:35:05 +0200 | [diff] [blame] | 475 | break |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 476 | # If select was used with a timeout, and the timeout occurs, it |
| 477 | # returns with empty lists -> thus abort read operation. |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 478 | # For timeout == 0 (non-blocking operation) also abort when |
| 479 | # there is nothing to read. |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 480 | if not ready: |
| 481 | break # timeout |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 482 | buf = os.read(self.fd, size - len(read)) |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 483 | # read should always return some data as select reported it was |
| 484 | # ready to read when we get to this point. |
| 485 | if not buf: |
| 486 | # Disconnected devices, at least on Linux, show the |
| 487 | # behavior that they are always ready to read immediately |
| 488 | # but reading returns nothing. |
Chris Liechti | 92df95a | 2016-02-09 23:30:37 +0100 | [diff] [blame] | 489 | raise SerialException( |
| 490 | 'device reports readiness to read but returned no data ' |
| 491 | '(device disconnected or multiple access on port?)') |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 492 | read.extend(buf) |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 493 | except OSError as e: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 494 | # this is for Python 3.x where select.error is a subclass of |
| 495 | # OSError ignore EAGAIN errors. all other errors are shown |
nexcvon | 50ec223 | 2016-05-24 15:48:46 +0800 | [diff] [blame] | 496 | if e.errno != errno.EAGAIN and e.errno != errno.EINTR: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 497 | raise SerialException('read failed: {}'.format(e)) |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 498 | except select.error as e: |
cliechti | c7cd721 | 2014-08-03 21:34:38 +0000 | [diff] [blame] | 499 | # this is for Python 2.x |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 500 | # ignore EAGAIN errors. all other errors are shown |
| 501 | # see also http://www.python.org/dev/peps/pep-3151/#select |
| 502 | if e[0] != errno.EAGAIN: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 503 | raise SerialException('read failed: {}'.format(e)) |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 504 | if timeout.expired(): |
| 505 | break |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 506 | return bytes(read) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 507 | |
Chris Liechti | 42ab2b4 | 2016-05-15 23:35:05 +0200 | [diff] [blame] | 508 | def cancel_read(self): |
| 509 | os.write(self.pipe_abort_read_w, b"x") |
| 510 | |
Chris Liechti | 13949c6 | 2016-05-16 22:45:38 +0200 | [diff] [blame] | 511 | def cancel_write(self): |
| 512 | os.write(self.pipe_abort_write_w, b"x") |
| 513 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 514 | def write(self, data): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 515 | """Output the given byte string over the serial port.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 516 | if not self.is_open: |
| 517 | raise portNotOpenError |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 518 | d = to_bytes(data) |
| 519 | tx_len = len(d) |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 520 | timeout = Timeout(self._write_timeout) |
cliechti | 9f7c235 | 2013-10-11 01:13:46 +0000 | [diff] [blame] | 521 | while tx_len > 0: |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 522 | try: |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 523 | n = os.write(self.fd, d) |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 524 | if timeout.is_non_blocking: |
Robert Smallshire | 325a738 | 2016-03-25 21:18:38 +0100 | [diff] [blame] | 525 | # Zero timeout indicates non-blocking - simply return the |
| 526 | # number of bytes of data actually written |
| 527 | return n |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 528 | elif not timeout.is_infinite: |
cliechti | 3cf46d6 | 2009-08-07 00:19:57 +0000 | [diff] [blame] | 529 | # when timeout is set, use select to wait for being ready |
| 530 | # with the time left as timeout |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 531 | if timeout.expired(): |
cliechti | 3cf46d6 | 2009-08-07 00:19:57 +0000 | [diff] [blame] | 532 | raise writeTimeoutError |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 533 | abort, ready, _ = select.select([self.pipe_abort_write_r], [self.fd], [], timeout.time_left()) |
Chris Liechti | 13949c6 | 2016-05-16 22:45:38 +0200 | [diff] [blame] | 534 | if abort: |
Chris Liechti | 68cecce | 2016-06-03 23:52:34 +0200 | [diff] [blame] | 535 | os.read(self.pipe_abort_write_r, 1000) |
Chris Liechti | 13949c6 | 2016-05-16 22:45:38 +0200 | [diff] [blame] | 536 | break |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 537 | if not ready: |
| 538 | raise writeTimeoutError |
cliechti | 88c6244 | 2013-10-12 04:03:16 +0000 | [diff] [blame] | 539 | else: |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 540 | assert timeout.time_left() is None |
cliechti | 88c6244 | 2013-10-12 04:03:16 +0000 | [diff] [blame] | 541 | # wait for write operation |
Chris Liechti | 13949c6 | 2016-05-16 22:45:38 +0200 | [diff] [blame] | 542 | abort, ready, _ = select.select([self.pipe_abort_write_r], [self.fd], [], None) |
| 543 | if abort: |
| 544 | os.read(self.pipe_abort_write_r, 1) |
| 545 | break |
cliechti | 88c6244 | 2013-10-12 04:03:16 +0000 | [diff] [blame] | 546 | if not ready: |
| 547 | raise SerialException('write failed (select)') |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 548 | d = d[n:] |
cliechti | 9f7c235 | 2013-10-11 01:13:46 +0000 | [diff] [blame] | 549 | tx_len -= n |
Chris Liechti | 675f7e1 | 2015-08-03 15:48:41 +0200 | [diff] [blame] | 550 | except SerialException: |
| 551 | raise |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 552 | except OSError as v: |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 553 | if v.errno != errno.EAGAIN: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 554 | raise SerialException('write failed: {}'.format(v)) |
Chris Liechti | c6362db | 2015-12-13 23:44:35 +0100 | [diff] [blame] | 555 | # still calculate and check timeout |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 556 | if timeout.expired(): |
Chris Liechti | c6362db | 2015-12-13 23:44:35 +0100 | [diff] [blame] | 557 | raise writeTimeoutError |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 558 | return len(data) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 559 | |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 560 | def flush(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 561 | """\ |
| 562 | Flush of file like objects. In this case, wait until all data |
| 563 | is written. |
| 564 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 565 | if not self.is_open: |
| 566 | raise portNotOpenError |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 567 | termios.tcdrain(self.fd) |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 568 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 569 | def reset_input_buffer(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 570 | """Clear input buffer, discarding all that is in the buffer.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 571 | if not self.is_open: |
| 572 | raise portNotOpenError |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 573 | termios.tcflush(self.fd, termios.TCIFLUSH) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 574 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 575 | def reset_output_buffer(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 576 | """\ |
| 577 | Clear output buffer, aborting the current output and discarding all |
| 578 | that is in the buffer. |
| 579 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 580 | if not self.is_open: |
| 581 | raise portNotOpenError |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 582 | termios.tcflush(self.fd, termios.TCOFLUSH) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 583 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 584 | def send_break(self, duration=0.25): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 585 | """\ |
| 586 | Send break condition. Timed, returns to idle state after given |
| 587 | duration. |
| 588 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 589 | if not self.is_open: |
| 590 | raise portNotOpenError |
| 591 | termios.tcsendbreak(self.fd, int(duration / 0.25)) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 592 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 593 | def _update_break_state(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 594 | """\ |
| 595 | Set break: Controls TXD. When active, no transmitting is possible. |
| 596 | """ |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 597 | if self._break_state: |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 598 | fcntl.ioctl(self.fd, TIOCSBRK) |
| 599 | else: |
| 600 | fcntl.ioctl(self.fd, TIOCCBRK) |
| 601 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 602 | def _update_rts_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 603 | """Set terminal status line: Request To Send""" |
Chris Liechti | f7534c8 | 2016-05-07 23:35:54 +0200 | [diff] [blame] | 604 | if self._rts_state: |
| 605 | fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_RTS_str) |
| 606 | else: |
| 607 | fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_RTS_str) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 608 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 609 | def _update_dtr_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 610 | """Set terminal status line: Data Terminal Ready""" |
Chris Liechti | f7534c8 | 2016-05-07 23:35:54 +0200 | [diff] [blame] | 611 | if self._dtr_state: |
| 612 | fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str) |
| 613 | else: |
| 614 | fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 615 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 616 | @property |
| 617 | def cts(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 618 | """Read terminal status line: Clear To Send""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 619 | if not self.is_open: |
| 620 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 621 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 622 | return struct.unpack('I', s)[0] & TIOCM_CTS != 0 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 623 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 624 | @property |
| 625 | def dsr(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 626 | """Read terminal status line: Data Set Ready""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 627 | if not self.is_open: |
| 628 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 629 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 630 | return struct.unpack('I', s)[0] & TIOCM_DSR != 0 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 631 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 632 | @property |
| 633 | def ri(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 634 | """Read terminal status line: Ring Indicator""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 635 | if not self.is_open: |
| 636 | raise portNotOpenError |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 637 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 638 | return struct.unpack('I', s)[0] & TIOCM_RI != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 639 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 640 | @property |
| 641 | def cd(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 642 | """Read terminal status line: Carrier Detect""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 643 | if not self.is_open: |
| 644 | raise portNotOpenError |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 645 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 646 | return struct.unpack('I', s)[0] & TIOCM_CD != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 647 | |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 648 | # - - platform specific - - - - |
| 649 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 650 | @property |
| 651 | def out_waiting(self): |
| 652 | """Return the number of bytes currently in the output buffer.""" |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 653 | #~ s = fcntl.ioctl(self.fd, termios.FIONREAD, TIOCM_zero_str) |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 654 | s = fcntl.ioctl(self.fd, TIOCOUTQ, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 655 | return struct.unpack('I', s)[0] |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 656 | |
cliechti | 8753bbc | 2005-01-15 20:32:51 +0000 | [diff] [blame] | 657 | def fileno(self): |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 658 | """\ |
| 659 | For easier use of the serial port instance with select. |
| 660 | WARNING: this function is not portable to different platforms! |
| 661 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 662 | if not self.is_open: |
| 663 | raise portNotOpenError |
cliechti | 8753bbc | 2005-01-15 20:32:51 +0000 | [diff] [blame] | 664 | return self.fd |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 665 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 666 | def set_input_flow_control(self, enable=True): |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 667 | """\ |
| 668 | Manually control flow - when software flow control is enabled. |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 669 | This will send XON (true) or XOFF (false) to the other device. |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 670 | WARNING: this function is not portable to different platforms! |
| 671 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 672 | if not self.is_open: |
| 673 | raise portNotOpenError |
cliechti | 4a60134 | 2011-12-29 02:22:17 +0000 | [diff] [blame] | 674 | if enable: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 675 | termios.tcflow(self.fd, termios.TCION) |
cliechti | 57e48a6 | 2009-08-03 22:29:58 +0000 | [diff] [blame] | 676 | else: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 677 | termios.tcflow(self.fd, termios.TCIOFF) |
cliechti | 57e48a6 | 2009-08-03 22:29:58 +0000 | [diff] [blame] | 678 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 679 | def set_output_flow_control(self, enable=True): |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 680 | """\ |
| 681 | Manually control flow of outgoing data - when hardware or software flow |
| 682 | control is enabled. |
| 683 | WARNING: this function is not portable to different platforms! |
| 684 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 685 | if not self.is_open: |
| 686 | raise portNotOpenError |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 687 | if enable: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 688 | termios.tcflow(self.fd, termios.TCOON) |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 689 | else: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 690 | termios.tcflow(self.fd, termios.TCOOFF) |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 691 | |
Chris Liechti | 5984842 | 2016-06-04 22:28:14 +0200 | [diff] [blame] | 692 | def nonblocking(self): |
| 693 | """DEPRECATED - has no use""" |
| 694 | import warnings |
| 695 | warnings.warn("nonblocking() has no effect, already nonblocking", DeprecationWarning) |
| 696 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 697 | |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 698 | class PosixPollSerial(Serial): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 699 | """\ |
cliechti | f0a81d4 | 2014-08-04 14:03:53 +0000 | [diff] [blame] | 700 | Poll based read implementation. Not all systems support poll properly. |
| 701 | However this one has better handling of errors, such as a device |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 702 | disconnecting while it's in use (e.g. USB-serial unplugged). |
| 703 | """ |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 704 | |
| 705 | def read(self, size=1): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 706 | """\ |
| 707 | Read size bytes from the serial port. If a timeout is set it may |
| 708 | return less characters as requested. With no timeout it will block |
| 709 | until the requested number of bytes is read. |
| 710 | """ |
Chris Liechti | acac236 | 2016-03-29 22:37:48 +0200 | [diff] [blame] | 711 | if not self.is_open: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 712 | raise portNotOpenError |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 713 | read = bytearray() |
| 714 | poll = select.poll() |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 715 | poll.register(self.fd, select.POLLIN | select.POLLERR | select.POLLHUP | select.POLLNVAL) |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 716 | if size > 0: |
| 717 | while len(read) < size: |
| 718 | # print "\tread(): size",size, "have", len(read) #debug |
| 719 | # wait until device becomes ready to read (or something fails) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 720 | for fd, event in poll.poll(self._timeout * 1000): |
| 721 | if event & (select.POLLERR | select.POLLHUP | select.POLLNVAL): |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 722 | raise SerialException('device reports error (poll)') |
| 723 | # we don't care if it is select.POLLIN or timeout, that's |
| 724 | # handled below |
| 725 | buf = os.read(self.fd, size - len(read)) |
| 726 | read.extend(buf) |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 727 | if ((self._timeout is not None and self._timeout >= 0) or |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 728 | (self._inter_byte_timeout is not None and self._inter_byte_timeout > 0)) and not buf: |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 729 | break # early abort on timeout |
| 730 | return bytes(read) |
| 731 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 732 | |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 733 | class VTIMESerial(Serial): |
| 734 | """\ |
| 735 | Implement timeout using vtime of tty device instead of using select. |
| 736 | This means that no inter character timeout can be specified and that |
| 737 | the error handling is degraded. |
| 738 | |
| 739 | Overall timeout is disabled when inter-character timeout is used. |
| 740 | """ |
| 741 | |
Chris Liechti | 9428470 | 2015-11-15 01:21:48 +0100 | [diff] [blame] | 742 | def _reconfigure_port(self, force_update=True): |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 743 | """Set communication parameters on opened port.""" |
| 744 | super(VTIMESerial, self)._reconfigure_port() |
Chris Liechti | d6bcaaf | 2016-02-01 22:55:26 +0100 | [diff] [blame] | 745 | fcntl.fcntl(self.fd, fcntl.F_SETFL, 0) # clear O_NONBLOCK |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 746 | |
| 747 | if self._inter_byte_timeout is not None: |
| 748 | vmin = 1 |
| 749 | vtime = int(self._inter_byte_timeout * 10) |
Chris Liechti | 3ec9c41 | 2016-08-08 01:06:46 +0200 | [diff] [blame] | 750 | elif self._timeout is None: |
| 751 | vmin = 1 |
| 752 | vtime = 0 |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 753 | else: |
| 754 | vmin = 0 |
| 755 | vtime = int(self._timeout * 10) |
| 756 | try: |
| 757 | orig_attr = termios.tcgetattr(self.fd) |
| 758 | iflag, oflag, cflag, lflag, ispeed, ospeed, cc = orig_attr |
| 759 | except termios.error as msg: # if a port is nonexistent but has a /dev file, it'll fail here |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 760 | raise serial.SerialException("Could not configure port: {}".format(msg)) |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 761 | |
| 762 | if vtime < 0 or vtime > 255: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 763 | raise ValueError('Invalid vtime: {!r}'.format(vtime)) |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 764 | cc[termios.VTIME] = vtime |
| 765 | cc[termios.VMIN] = vmin |
| 766 | |
| 767 | termios.tcsetattr( |
| 768 | self.fd, |
| 769 | termios.TCSANOW, |
| 770 | [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]) |
| 771 | |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 772 | def read(self, size=1): |
| 773 | """\ |
| 774 | Read size bytes from the serial port. If a timeout is set it may |
| 775 | return less characters as requested. With no timeout it will block |
| 776 | until the requested number of bytes is read. |
| 777 | """ |
| 778 | if not self.is_open: |
| 779 | raise portNotOpenError |
| 780 | read = bytearray() |
| 781 | while len(read) < size: |
| 782 | buf = os.read(self.fd, size - len(read)) |
| 783 | if not buf: |
| 784 | break |
| 785 | read.extend(buf) |
| 786 | return bytes(read) |
Chris Liechti | 3ec9c41 | 2016-08-08 01:06:46 +0200 | [diff] [blame] | 787 | |
| 788 | # hack to make hasattr return false |
| 789 | cancel_read = property() |