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