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 | e102962 | 2016-10-02 23:54:36 +0200 | [diff] [blame] | 276 | if e.errno in (errno.EINVAL, errno.ENOTTY): |
| 277 | # ignore Invalid argument and Inappropriate ioctl |
Chris Liechti | f2fdeb9 | 2016-05-07 23:57:50 +0200 | [diff] [blame] | 278 | pass |
| 279 | else: |
| 280 | raise |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 281 | self.reset_input_buffer() |
Chris Liechti | 42ab2b4 | 2016-05-15 23:35:05 +0200 | [diff] [blame] | 282 | self.pipe_abort_read_r, self.pipe_abort_read_w = os.pipe() |
Chris Liechti | 13949c6 | 2016-05-16 22:45:38 +0200 | [diff] [blame] | 283 | self.pipe_abort_write_r, self.pipe_abort_write_w = os.pipe() |
Chris Liechti | 68cecce | 2016-06-03 23:52:34 +0200 | [diff] [blame] | 284 | fcntl.fcntl(self.pipe_abort_read_r, fcntl.F_SETFL, os.O_NONBLOCK) |
| 285 | fcntl.fcntl(self.pipe_abort_write_r, fcntl.F_SETFL, os.O_NONBLOCK) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 286 | |
Chris Liechti | 9428470 | 2015-11-15 01:21:48 +0100 | [diff] [blame] | 287 | def _reconfigure_port(self, force_update=False): |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 288 | """Set communication parameters on opened port.""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 289 | if self.fd is None: |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 290 | raise SerialException("Can only operate on a valid file descriptor") |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 291 | custom_baud = None |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 292 | |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 293 | vmin = vtime = 0 # timeout is done via select |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 294 | if self._inter_byte_timeout is not None: |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 295 | vmin = 1 |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 296 | vtime = int(self._inter_byte_timeout * 10) |
cliechti | 6ce7ab1 | 2002-11-07 02:15:00 +0000 | [diff] [blame] | 297 | try: |
cliechti | 4d0af5e | 2011-08-05 02:18:16 +0000 | [diff] [blame] | 298 | orig_attr = termios.tcgetattr(self.fd) |
| 299 | iflag, oflag, cflag, lflag, ispeed, ospeed, cc = orig_attr |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 300 | 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] | 301 | raise SerialException("Could not configure port: {}".format(msg)) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 302 | # set up raw mode / no echo / binary |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 303 | cflag |= (termios.CLOCAL | termios.CREAD) |
| 304 | lflag &= ~(termios.ICANON | termios.ECHO | termios.ECHOE | |
| 305 | termios.ECHOK | termios.ECHONL | |
| 306 | termios.ISIG | termios.IEXTEN) # |termios.ECHOPRT |
| 307 | for flag in ('ECHOCTL', 'ECHOKE'): # netbsd workaround for Erk |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 308 | if hasattr(termios, flag): |
| 309 | lflag &= ~getattr(termios, flag) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 310 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 311 | oflag &= ~(termios.OPOST | termios.ONLCR | termios.OCRNL) |
| 312 | iflag &= ~(termios.INLCR | termios.IGNCR | termios.ICRNL | termios.IGNBRK) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 313 | if hasattr(termios, 'IUCLC'): |
| 314 | iflag &= ~termios.IUCLC |
| 315 | if hasattr(termios, 'PARMRK'): |
| 316 | iflag &= ~termios.PARMRK |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 317 | |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 318 | # setup baud rate |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 319 | try: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 320 | ispeed = ospeed = getattr(termios, 'B{}'.format(self._baudrate)) |
cliechti | 895e830 | 2004-04-20 02:40:28 +0000 | [diff] [blame] | 321 | except AttributeError: |
cliechti | f1559d0 | 2007-11-08 23:43:58 +0000 | [diff] [blame] | 322 | try: |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 323 | ispeed = ospeed = self.BAUDRATE_CONSTANTS[self._baudrate] |
cliechti | f1559d0 | 2007-11-08 23:43:58 +0000 | [diff] [blame] | 324 | except KeyError: |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 325 | #~ raise ValueError('Invalid baud rate: %r' % self._baudrate) |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 326 | # may need custom baud rate, it isn't in our list. |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 327 | ispeed = ospeed = getattr(termios, 'B38400') |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 328 | try: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 329 | custom_baud = int(self._baudrate) # store for later |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 330 | except ValueError: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 331 | raise ValueError('Invalid baud rate: {!r}'.format(self._baudrate)) |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 332 | else: |
| 333 | if custom_baud < 0: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 334 | raise ValueError('Invalid baud rate: {!r}'.format(self._baudrate)) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 335 | |
| 336 | # setup char len |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 337 | cflag &= ~termios.CSIZE |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 338 | if self._bytesize == 8: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 339 | cflag |= termios.CS8 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 340 | elif self._bytesize == 7: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 341 | cflag |= termios.CS7 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 342 | elif self._bytesize == 6: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 343 | cflag |= termios.CS6 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 344 | elif self._bytesize == 5: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 345 | cflag |= termios.CS5 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 346 | else: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 347 | raise ValueError('Invalid char len: {!r}'.format(self._bytesize)) |
cliechti | f0a81d4 | 2014-08-04 14:03:53 +0000 | [diff] [blame] | 348 | # setup stop bits |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 349 | if self._stopbits == serial.STOPBITS_ONE: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 350 | cflag &= ~(termios.CSTOPB) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 351 | elif self._stopbits == serial.STOPBITS_ONE_POINT_FIVE: |
| 352 | cflag |= (termios.CSTOPB) # XXX same as TWO.. there is no POSIX support for 1.5 |
| 353 | elif self._stopbits == serial.STOPBITS_TWO: |
| 354 | cflag |= (termios.CSTOPB) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 355 | else: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 356 | raise ValueError('Invalid stop bit specification: {!r}'.format(self._stopbits)) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 357 | # setup parity |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 358 | iflag &= ~(termios.INPCK | termios.ISTRIP) |
| 359 | if self._parity == serial.PARITY_NONE: |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 360 | cflag &= ~(termios.PARENB | termios.PARODD | CMSPAR) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 361 | elif self._parity == serial.PARITY_EVEN: |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 362 | cflag &= ~(termios.PARODD | CMSPAR) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 363 | cflag |= (termios.PARENB) |
| 364 | elif self._parity == serial.PARITY_ODD: |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 365 | cflag &= ~CMSPAR |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 366 | cflag |= (termios.PARENB | termios.PARODD) |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 367 | elif self._parity == serial.PARITY_MARK and CMSPAR: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 368 | cflag |= (termios.PARENB | CMSPAR | termios.PARODD) |
Chris Liechti | 6dc58e8 | 2016-08-29 23:02:13 +0200 | [diff] [blame] | 369 | elif self._parity == serial.PARITY_SPACE and CMSPAR: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 370 | cflag |= (termios.PARENB | CMSPAR) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 371 | cflag &= ~(termios.PARODD) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 372 | else: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 373 | raise ValueError('Invalid parity: {!r}'.format(self._parity)) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 374 | # setup flow control |
| 375 | # xonxoff |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 376 | if hasattr(termios, 'IXANY'): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 377 | if self._xonxoff: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 378 | iflag |= (termios.IXON | termios.IXOFF) # |termios.IXANY) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 379 | else: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 380 | iflag &= ~(termios.IXON | termios.IXOFF | termios.IXANY) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 381 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 382 | if self._xonxoff: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 383 | iflag |= (termios.IXON | termios.IXOFF) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 384 | else: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 385 | iflag &= ~(termios.IXON | termios.IXOFF) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 386 | # rtscts |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 387 | if hasattr(termios, 'CRTSCTS'): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 388 | if self._rtscts: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 389 | cflag |= (termios.CRTSCTS) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 390 | else: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 391 | cflag &= ~(termios.CRTSCTS) |
| 392 | elif hasattr(termios, 'CNEW_RTSCTS'): # try it with alternate constant name |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 393 | if self._rtscts: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 394 | cflag |= (termios.CNEW_RTSCTS) |
cliechti | d474369 | 2002-04-08 22:39:53 +0000 | [diff] [blame] | 395 | else: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 396 | cflag &= ~(termios.CNEW_RTSCTS) |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 397 | # 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] | 398 | |
| 399 | # buffer |
cliechti | f0a81d4 | 2014-08-04 14:03:53 +0000 | [diff] [blame] | 400 | # vmin "minimal number of characters to be read. 0 for non blocking" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 401 | if vmin < 0 or vmin > 255: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 402 | raise ValueError('Invalid vmin: {!r}'.format(vmin)) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 403 | cc[termios.VMIN] = vmin |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 404 | # vtime |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 405 | if vtime < 0 or vtime > 255: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 406 | raise ValueError('Invalid vtime: {!r}'.format(vtime)) |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 407 | cc[termios.VTIME] = vtime |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 408 | # activate settings |
Chris Liechti | 9428470 | 2015-11-15 01:21:48 +0100 | [diff] [blame] | 409 | 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] | 410 | termios.tcsetattr( |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 411 | self.fd, |
| 412 | termios.TCSANOW, |
| 413 | [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 414 | |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 415 | # apply custom baud rate, if any |
| 416 | if custom_baud is not None: |
Chris Liechti | d6847af | 2015-08-06 17:54:30 +0200 | [diff] [blame] | 417 | self._set_special_baudrate(custom_baud) |
| 418 | |
| 419 | if self._rs485_mode is not None: |
| 420 | self._set_rs485_mode(self._rs485_mode) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 421 | |
| 422 | def close(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 423 | """Close port""" |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 424 | if self.is_open: |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 425 | if self.fd is not None: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 426 | os.close(self.fd) |
| 427 | self.fd = None |
Chris Liechti | b658eac | 2016-05-22 20:51:44 +0200 | [diff] [blame] | 428 | os.close(self.pipe_abort_read_w) |
| 429 | os.close(self.pipe_abort_read_r) |
| 430 | os.close(self.pipe_abort_write_w) |
| 431 | os.close(self.pipe_abort_write_r) |
| 432 | self.pipe_abort_read_r, self.pipe_abort_read_w = None, None |
| 433 | self.pipe_abort_write_r, self.pipe_abort_write_w = None, None |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 434 | self.is_open = False |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 435 | |
| 436 | # - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 437 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 438 | @property |
| 439 | def in_waiting(self): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 440 | """Return the number of bytes currently in the input buffer.""" |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 441 | #~ s = fcntl.ioctl(self.fd, termios.FIONREAD, TIOCM_zero_str) |
cliechti | f5831e0 | 2002-12-05 23:15:27 +0000 | [diff] [blame] | 442 | s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 443 | return struct.unpack('I', s)[0] |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 444 | |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 445 | # select based implementation, proved to work on many systems |
| 446 | def read(self, size=1): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 447 | """\ |
| 448 | Read size bytes from the serial port. If a timeout is set it may |
| 449 | return less characters as requested. With no timeout it will block |
| 450 | until the requested number of bytes is read. |
| 451 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 452 | if not self.is_open: |
| 453 | raise portNotOpenError |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 454 | read = bytearray() |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 455 | timeout = Timeout(self._timeout) |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 456 | while len(read) < size: |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 457 | try: |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 458 | 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] | 459 | if self.pipe_abort_read_r in ready: |
Chris Liechti | 68cecce | 2016-06-03 23:52:34 +0200 | [diff] [blame] | 460 | os.read(self.pipe_abort_read_r, 1000) |
Chris Liechti | 42ab2b4 | 2016-05-15 23:35:05 +0200 | [diff] [blame] | 461 | break |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 462 | # If select was used with a timeout, and the timeout occurs, it |
| 463 | # returns with empty lists -> thus abort read operation. |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 464 | # For timeout == 0 (non-blocking operation) also abort when |
| 465 | # there is nothing to read. |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 466 | if not ready: |
| 467 | break # timeout |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 468 | buf = os.read(self.fd, size - len(read)) |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 469 | # read should always return some data as select reported it was |
| 470 | # ready to read when we get to this point. |
| 471 | if not buf: |
| 472 | # Disconnected devices, at least on Linux, show the |
| 473 | # behavior that they are always ready to read immediately |
| 474 | # but reading returns nothing. |
Chris Liechti | 92df95a | 2016-02-09 23:30:37 +0100 | [diff] [blame] | 475 | raise SerialException( |
| 476 | 'device reports readiness to read but returned no data ' |
| 477 | '(device disconnected or multiple access on port?)') |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 478 | read.extend(buf) |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 479 | except OSError as e: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 480 | # this is for Python 3.x where select.error is a subclass of |
| 481 | # OSError ignore EAGAIN errors. all other errors are shown |
nexcvon | 50ec223 | 2016-05-24 15:48:46 +0800 | [diff] [blame] | 482 | if e.errno != errno.EAGAIN and e.errno != errno.EINTR: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 483 | raise SerialException('read failed: {}'.format(e)) |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 484 | except select.error as e: |
cliechti | c7cd721 | 2014-08-03 21:34:38 +0000 | [diff] [blame] | 485 | # this is for Python 2.x |
cliechti | 8d744de | 2013-10-11 14:31:13 +0000 | [diff] [blame] | 486 | # ignore EAGAIN errors. all other errors are shown |
| 487 | # see also http://www.python.org/dev/peps/pep-3151/#select |
| 488 | if e[0] != errno.EAGAIN: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 489 | raise SerialException('read failed: {}'.format(e)) |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 490 | if timeout.expired(): |
| 491 | break |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 492 | return bytes(read) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 493 | |
Chris Liechti | 42ab2b4 | 2016-05-15 23:35:05 +0200 | [diff] [blame] | 494 | def cancel_read(self): |
| 495 | os.write(self.pipe_abort_read_w, b"x") |
| 496 | |
Chris Liechti | 13949c6 | 2016-05-16 22:45:38 +0200 | [diff] [blame] | 497 | def cancel_write(self): |
| 498 | os.write(self.pipe_abort_write_w, b"x") |
| 499 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 500 | def write(self, data): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 501 | """Output the given byte string over the serial port.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 502 | if not self.is_open: |
| 503 | raise portNotOpenError |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 504 | d = to_bytes(data) |
| 505 | tx_len = len(d) |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 506 | timeout = Timeout(self._write_timeout) |
cliechti | 9f7c235 | 2013-10-11 01:13:46 +0000 | [diff] [blame] | 507 | while tx_len > 0: |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 508 | try: |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 509 | n = os.write(self.fd, d) |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 510 | if timeout.is_non_blocking: |
Robert Smallshire | 325a738 | 2016-03-25 21:18:38 +0100 | [diff] [blame] | 511 | # Zero timeout indicates non-blocking - simply return the |
| 512 | # number of bytes of data actually written |
| 513 | return n |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 514 | elif not timeout.is_infinite: |
cliechti | 3cf46d6 | 2009-08-07 00:19:57 +0000 | [diff] [blame] | 515 | # when timeout is set, use select to wait for being ready |
| 516 | # with the time left as timeout |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 517 | if timeout.expired(): |
cliechti | 3cf46d6 | 2009-08-07 00:19:57 +0000 | [diff] [blame] | 518 | raise writeTimeoutError |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 519 | 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] | 520 | if abort: |
Chris Liechti | 68cecce | 2016-06-03 23:52:34 +0200 | [diff] [blame] | 521 | os.read(self.pipe_abort_write_r, 1000) |
Chris Liechti | 13949c6 | 2016-05-16 22:45:38 +0200 | [diff] [blame] | 522 | break |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 523 | if not ready: |
| 524 | raise writeTimeoutError |
cliechti | 88c6244 | 2013-10-12 04:03:16 +0000 | [diff] [blame] | 525 | else: |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 526 | assert timeout.time_left() is None |
cliechti | 88c6244 | 2013-10-12 04:03:16 +0000 | [diff] [blame] | 527 | # wait for write operation |
Chris Liechti | 13949c6 | 2016-05-16 22:45:38 +0200 | [diff] [blame] | 528 | abort, ready, _ = select.select([self.pipe_abort_write_r], [self.fd], [], None) |
| 529 | if abort: |
| 530 | os.read(self.pipe_abort_write_r, 1) |
| 531 | break |
cliechti | 88c6244 | 2013-10-12 04:03:16 +0000 | [diff] [blame] | 532 | if not ready: |
| 533 | raise SerialException('write failed (select)') |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 534 | d = d[n:] |
cliechti | 9f7c235 | 2013-10-11 01:13:46 +0000 | [diff] [blame] | 535 | tx_len -= n |
Chris Liechti | 675f7e1 | 2015-08-03 15:48:41 +0200 | [diff] [blame] | 536 | except SerialException: |
| 537 | raise |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 538 | except OSError as v: |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 539 | if v.errno != errno.EAGAIN: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 540 | raise SerialException('write failed: {}'.format(v)) |
Chris Liechti | c6362db | 2015-12-13 23:44:35 +0100 | [diff] [blame] | 541 | # still calculate and check timeout |
Chris Liechti | 935a262 | 2016-08-30 23:39:15 +0200 | [diff] [blame] | 542 | if timeout.expired(): |
Chris Liechti | c6362db | 2015-12-13 23:44:35 +0100 | [diff] [blame] | 543 | raise writeTimeoutError |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 544 | return len(data) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 545 | |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 546 | def flush(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 547 | """\ |
| 548 | Flush of file like objects. In this case, wait until all data |
| 549 | is written. |
| 550 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 551 | if not self.is_open: |
| 552 | raise portNotOpenError |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 553 | termios.tcdrain(self.fd) |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 554 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 555 | def reset_input_buffer(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 556 | """Clear input buffer, discarding all that is in the buffer.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 557 | if not self.is_open: |
| 558 | raise portNotOpenError |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 559 | termios.tcflush(self.fd, termios.TCIFLUSH) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 560 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 561 | def reset_output_buffer(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 562 | """\ |
| 563 | Clear output buffer, aborting the current output and discarding all |
| 564 | that is in the buffer. |
| 565 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 566 | if not self.is_open: |
| 567 | raise portNotOpenError |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 568 | termios.tcflush(self.fd, termios.TCOFLUSH) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 569 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 570 | def send_break(self, duration=0.25): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 571 | """\ |
| 572 | Send break condition. Timed, returns to idle state after given |
| 573 | duration. |
| 574 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 575 | if not self.is_open: |
| 576 | raise portNotOpenError |
| 577 | termios.tcsendbreak(self.fd, int(duration / 0.25)) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 578 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 579 | def _update_break_state(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 580 | """\ |
| 581 | Set break: Controls TXD. When active, no transmitting is possible. |
| 582 | """ |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 583 | if self._break_state: |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 584 | fcntl.ioctl(self.fd, TIOCSBRK) |
| 585 | else: |
| 586 | fcntl.ioctl(self.fd, TIOCCBRK) |
| 587 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 588 | def _update_rts_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 589 | """Set terminal status line: Request To Send""" |
Chris Liechti | f7534c8 | 2016-05-07 23:35:54 +0200 | [diff] [blame] | 590 | if self._rts_state: |
| 591 | fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_RTS_str) |
| 592 | else: |
| 593 | fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_RTS_str) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 594 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 595 | def _update_dtr_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 596 | """Set terminal status line: Data Terminal Ready""" |
Chris Liechti | f7534c8 | 2016-05-07 23:35:54 +0200 | [diff] [blame] | 597 | if self._dtr_state: |
| 598 | fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str) |
| 599 | else: |
| 600 | fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 601 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 602 | @property |
| 603 | def cts(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 604 | """Read terminal status line: Clear To Send""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 605 | if not self.is_open: |
| 606 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 607 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 608 | return struct.unpack('I', s)[0] & TIOCM_CTS != 0 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 609 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 610 | @property |
| 611 | def dsr(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 612 | """Read terminal status line: Data Set Ready""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 613 | if not self.is_open: |
| 614 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 615 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 616 | return struct.unpack('I', s)[0] & TIOCM_DSR != 0 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 617 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 618 | @property |
| 619 | def ri(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 620 | """Read terminal status line: Ring Indicator""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 621 | if not self.is_open: |
| 622 | raise portNotOpenError |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 623 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 624 | return struct.unpack('I', s)[0] & TIOCM_RI != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 625 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 626 | @property |
| 627 | def cd(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 628 | """Read terminal status line: Carrier Detect""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 629 | if not self.is_open: |
| 630 | raise portNotOpenError |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 631 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 632 | return struct.unpack('I', s)[0] & TIOCM_CD != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 633 | |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 634 | # - - platform specific - - - - |
| 635 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 636 | @property |
| 637 | def out_waiting(self): |
| 638 | """Return the number of bytes currently in the output buffer.""" |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 639 | #~ s = fcntl.ioctl(self.fd, termios.FIONREAD, TIOCM_zero_str) |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 640 | s = fcntl.ioctl(self.fd, TIOCOUTQ, TIOCM_zero_str) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 641 | return struct.unpack('I', s)[0] |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 642 | |
cliechti | 8753bbc | 2005-01-15 20:32:51 +0000 | [diff] [blame] | 643 | def fileno(self): |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 644 | """\ |
| 645 | For easier use of the serial port instance with select. |
| 646 | WARNING: this function is not portable to different platforms! |
| 647 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 648 | if not self.is_open: |
| 649 | raise portNotOpenError |
cliechti | 8753bbc | 2005-01-15 20:32:51 +0000 | [diff] [blame] | 650 | return self.fd |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 651 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 652 | def set_input_flow_control(self, enable=True): |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 653 | """\ |
| 654 | Manually control flow - when software flow control is enabled. |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 655 | This will send XON (true) or XOFF (false) to the other device. |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 656 | WARNING: this function is not portable to different platforms! |
| 657 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 658 | if not self.is_open: |
| 659 | raise portNotOpenError |
cliechti | 4a60134 | 2011-12-29 02:22:17 +0000 | [diff] [blame] | 660 | if enable: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 661 | termios.tcflow(self.fd, termios.TCION) |
cliechti | 57e48a6 | 2009-08-03 22:29:58 +0000 | [diff] [blame] | 662 | else: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 663 | termios.tcflow(self.fd, termios.TCIOFF) |
cliechti | 57e48a6 | 2009-08-03 22:29:58 +0000 | [diff] [blame] | 664 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 665 | def set_output_flow_control(self, enable=True): |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 666 | """\ |
| 667 | Manually control flow of outgoing data - when hardware or software flow |
| 668 | control is enabled. |
| 669 | WARNING: this function is not portable to different platforms! |
| 670 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 671 | if not self.is_open: |
| 672 | raise portNotOpenError |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 673 | if enable: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 674 | termios.tcflow(self.fd, termios.TCOON) |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 675 | else: |
Chris Liechti | 11465c8 | 2015-08-04 15:55:22 +0200 | [diff] [blame] | 676 | termios.tcflow(self.fd, termios.TCOOFF) |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 677 | |
Chris Liechti | 5984842 | 2016-06-04 22:28:14 +0200 | [diff] [blame] | 678 | def nonblocking(self): |
| 679 | """DEPRECATED - has no use""" |
| 680 | import warnings |
| 681 | warnings.warn("nonblocking() has no effect, already nonblocking", DeprecationWarning) |
| 682 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 683 | |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 684 | class PosixPollSerial(Serial): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 685 | """\ |
cliechti | f0a81d4 | 2014-08-04 14:03:53 +0000 | [diff] [blame] | 686 | Poll based read implementation. Not all systems support poll properly. |
| 687 | However this one has better handling of errors, such as a device |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 688 | disconnecting while it's in use (e.g. USB-serial unplugged). |
| 689 | """ |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 690 | |
| 691 | def read(self, size=1): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 692 | """\ |
| 693 | Read size bytes from the serial port. If a timeout is set it may |
| 694 | return less characters as requested. With no timeout it will block |
| 695 | until the requested number of bytes is read. |
| 696 | """ |
Chris Liechti | acac236 | 2016-03-29 22:37:48 +0200 | [diff] [blame] | 697 | if not self.is_open: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 698 | raise portNotOpenError |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 699 | read = bytearray() |
| 700 | poll = select.poll() |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 701 | poll.register(self.fd, select.POLLIN | select.POLLERR | select.POLLHUP | select.POLLNVAL) |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 702 | if size > 0: |
| 703 | while len(read) < size: |
| 704 | # print "\tread(): size",size, "have", len(read) #debug |
| 705 | # wait until device becomes ready to read (or something fails) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 706 | for fd, event in poll.poll(self._timeout * 1000): |
| 707 | if event & (select.POLLERR | select.POLLHUP | select.POLLNVAL): |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame] | 708 | raise SerialException('device reports error (poll)') |
| 709 | # we don't care if it is select.POLLIN or timeout, that's |
| 710 | # handled below |
| 711 | buf = os.read(self.fd, size - len(read)) |
| 712 | read.extend(buf) |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 713 | if ((self._timeout is not None and self._timeout >= 0) or |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 714 | (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] | 715 | break # early abort on timeout |
| 716 | return bytes(read) |
| 717 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 718 | |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 719 | class VTIMESerial(Serial): |
| 720 | """\ |
| 721 | Implement timeout using vtime of tty device instead of using select. |
| 722 | This means that no inter character timeout can be specified and that |
| 723 | the error handling is degraded. |
| 724 | |
| 725 | Overall timeout is disabled when inter-character timeout is used. |
| 726 | """ |
| 727 | |
Chris Liechti | 9428470 | 2015-11-15 01:21:48 +0100 | [diff] [blame] | 728 | def _reconfigure_port(self, force_update=True): |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 729 | """Set communication parameters on opened port.""" |
| 730 | super(VTIMESerial, self)._reconfigure_port() |
Chris Liechti | d6bcaaf | 2016-02-01 22:55:26 +0100 | [diff] [blame] | 731 | fcntl.fcntl(self.fd, fcntl.F_SETFL, 0) # clear O_NONBLOCK |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 732 | |
| 733 | if self._inter_byte_timeout is not None: |
| 734 | vmin = 1 |
| 735 | vtime = int(self._inter_byte_timeout * 10) |
Chris Liechti | 3ec9c41 | 2016-08-08 01:06:46 +0200 | [diff] [blame] | 736 | elif self._timeout is None: |
| 737 | vmin = 1 |
| 738 | vtime = 0 |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 739 | else: |
| 740 | vmin = 0 |
| 741 | vtime = int(self._timeout * 10) |
| 742 | try: |
| 743 | orig_attr = termios.tcgetattr(self.fd) |
| 744 | iflag, oflag, cflag, lflag, ispeed, ospeed, cc = orig_attr |
| 745 | 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] | 746 | raise serial.SerialException("Could not configure port: {}".format(msg)) |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 747 | |
| 748 | if vtime < 0 or vtime > 255: |
Chris Liechti | 984c5c5 | 2016-02-15 23:48:45 +0100 | [diff] [blame] | 749 | raise ValueError('Invalid vtime: {!r}'.format(vtime)) |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 750 | cc[termios.VTIME] = vtime |
| 751 | cc[termios.VMIN] = vmin |
| 752 | |
| 753 | termios.tcsetattr( |
| 754 | self.fd, |
| 755 | termios.TCSANOW, |
| 756 | [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]) |
| 757 | |
Chris Liechti | 4cf5470 | 2015-10-18 00:21:56 +0200 | [diff] [blame] | 758 | def read(self, size=1): |
| 759 | """\ |
| 760 | Read size bytes from the serial port. If a timeout is set it may |
| 761 | return less characters as requested. With no timeout it will block |
| 762 | until the requested number of bytes is read. |
| 763 | """ |
| 764 | if not self.is_open: |
| 765 | raise portNotOpenError |
| 766 | read = bytearray() |
| 767 | while len(read) < size: |
| 768 | buf = os.read(self.fd, size - len(read)) |
| 769 | if not buf: |
| 770 | break |
| 771 | read.extend(buf) |
| 772 | return bytes(read) |
Chris Liechti | 3ec9c41 | 2016-08-08 01:06:46 +0200 | [diff] [blame] | 773 | |
| 774 | # hack to make hasattr return false |
| 775 | cancel_read = property() |