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