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