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