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