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 | # |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame^] | 7 | # (C) 2001-2010 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 | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 16 | from 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 | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 39 | ASYNC_SPD_MASK = 0x1030 |
| 40 | ASYNC_SPD_CUST = 0x0030 |
| 41 | |
| 42 | def set_special_baudrate(port, baudrate): |
| 43 | import array |
| 44 | buf = array.array('i', [0] * 32) |
| 45 | |
| 46 | # get serial_struct |
| 47 | FCNTL.ioctl(port.fd, TERMIOS.TIOCGSERIAL, buf) |
| 48 | |
| 49 | # set custom divisor |
| 50 | buf[6] = buf[7] / baudrate |
| 51 | |
| 52 | # update flags |
| 53 | buf[4] &= ~ASYNC_SPD_MASK |
| 54 | buf[4] |= ASYNC_SPD_CUST |
| 55 | |
| 56 | # set serial_struct |
| 57 | try: |
| 58 | res = FCNTL.ioctl(port.fd, TERMIOS.TIOCSSERIAL, buf) |
| 59 | except IOError: |
| 60 | raise ValueError('Failed to set custom baud rate: %r' % baudrate) |
| 61 | |
cliechti | 99220a0 | 2009-08-14 00:21:25 +0000 | [diff] [blame] | 62 | baudrate_constants = { |
| 63 | 0: 0000000, # hang up |
| 64 | 50: 0000001, |
| 65 | 75: 0000002, |
| 66 | 110: 0000003, |
| 67 | 134: 0000004, |
| 68 | 150: 0000005, |
| 69 | 200: 0000006, |
| 70 | 300: 0000007, |
| 71 | 600: 0000010, |
| 72 | 1200: 0000011, |
| 73 | 1800: 0000012, |
| 74 | 2400: 0000013, |
| 75 | 4800: 0000014, |
| 76 | 9600: 0000015, |
| 77 | 19200: 0000016, |
| 78 | 38400: 0000017, |
| 79 | 57600: 0010001, |
| 80 | 115200: 0010002, |
| 81 | 230400: 0010003, |
| 82 | 460800: 0010004, |
| 83 | 500000: 0010005, |
| 84 | 576000: 0010006, |
| 85 | 921600: 0010007, |
| 86 | 1000000: 0010010, |
| 87 | 1152000: 0010011, |
| 88 | 1500000: 0010012, |
| 89 | 2000000: 0010013, |
| 90 | 2500000: 0010014, |
| 91 | 3000000: 0010015, |
| 92 | 3500000: 0010016, |
| 93 | 4000000: 0010017 |
| 94 | } |
| 95 | |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 96 | elif plat == 'cygwin': # cygwin/win32 (confirmed) |
| 97 | |
cliechti | f281fde | 2002-06-07 21:53:40 +0000 | [diff] [blame] | 98 | def device(port): |
cliechti | f5831e0 | 2002-12-05 23:15:27 +0000 | [diff] [blame] | 99 | return '/dev/com%d' % (port + 1) |
cliechti | f281fde | 2002-06-07 21:53:40 +0000 | [diff] [blame] | 100 | |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 101 | def set_special_baudrate(port, baudrate): |
cliechti | 99220a0 | 2009-08-14 00:21:25 +0000 | [diff] [blame] | 102 | 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] | 103 | |
cliechti | 99220a0 | 2009-08-14 00:21:25 +0000 | [diff] [blame] | 104 | baudrate_constants = {} |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 105 | |
cliechti | 99220a0 | 2009-08-14 00:21:25 +0000 | [diff] [blame] | 106 | elif plat == 'openbsd3': # BSD (confirmed) |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 107 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 108 | def device(port): |
| 109 | return '/dev/ttyp%d' % port |
| 110 | |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 111 | def set_special_baudrate(port, baudrate): |
| 112 | raise ValueError("sorry don't know how to handle non standard baud rate on this platform") |
| 113 | |
cliechti | 99220a0 | 2009-08-14 00:21:25 +0000 | [diff] [blame] | 114 | baudrate_constants = {} |
| 115 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 116 | elif plat[:3] == 'bsd' or \ |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 117 | plat[:7] == 'freebsd' or \ |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 118 | plat[:7] == 'openbsd': # BSD (confirmed for freebsd4: cuaa%d) |
| 119 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 120 | def device(port): |
cliechti | eaa9688 | 2008-06-16 22:59:20 +0000 | [diff] [blame] | 121 | return '/dev/cuad%d' % port |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 122 | |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 123 | def set_special_baudrate(port, baudrate): |
| 124 | raise ValueError("sorry don't know how to handle non standard baud rate on this platform") |
| 125 | |
cliechti | 99220a0 | 2009-08-14 00:21:25 +0000 | [diff] [blame] | 126 | baudrate_constants = {} |
| 127 | |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 128 | elif plat[:6] == 'darwin': # OS X |
| 129 | |
| 130 | version = os.uname()[2].split('.') |
| 131 | # Tiger or above can support arbitrary serial speeds |
| 132 | if int(version[0]) >= 8: |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 133 | def set_special_baudrate(port, baudrate): |
| 134 | # use IOKit-specific call to set up high speeds |
| 135 | import array, fcntl |
| 136 | buf = array.array('i', [baudrate]) |
| 137 | IOSSIOSPEED = 0x80045402 #_IOW('T', 2, speed_t) |
| 138 | fcntl.ioctl(port.fd, IOSSIOSPEED, buf, 1) |
| 139 | else: # version < 8 |
| 140 | def set_special_baudrate(port, baudrate): |
| 141 | raise ValueError("baud rate not supported") |
| 142 | |
| 143 | def device(port): |
| 144 | return '/dev/cuad%d' % port |
| 145 | |
cliechti | 99220a0 | 2009-08-14 00:21:25 +0000 | [diff] [blame] | 146 | baudrate_constants = {} |
| 147 | |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 148 | |
cliechti | 3172d3d | 2009-07-21 22:33:40 +0000 | [diff] [blame] | 149 | elif plat[:6] == 'netbsd': # NetBSD 1.6 testing by Erk |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 150 | |
cliechti | 835996a | 2004-06-02 19:45:07 +0000 | [diff] [blame] | 151 | def device(port): |
| 152 | return '/dev/dty%02d' % port |
| 153 | |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 154 | def set_special_baudrate(port, baudrate): |
| 155 | raise ValueError("sorry don't know how to handle non standard baud rate on this platform") |
| 156 | |
cliechti | 99220a0 | 2009-08-14 00:21:25 +0000 | [diff] [blame] | 157 | baudrate_constants = {} |
| 158 | |
cliechti | 3172d3d | 2009-07-21 22:33:40 +0000 | [diff] [blame] | 159 | elif plat[:4] == 'irix': # IRIX (partially tested) |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 160 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 161 | def device(port): |
cliechti | e2418e9 | 2006-06-05 20:03:17 +0000 | [diff] [blame] | 162 | 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] | 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[:2] == 'hp': # HP-UX (not 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): |
| 172 | return '/dev/tty%dp0' % (port+1) |
| 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[:5] == 'sunos': # Solaris/SunOS (confirmed) |
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%c' % (ord('a')+port) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 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[:3] == 'aix': # AIX |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 190 | |
cliechti | 40e1b07 | 2005-03-12 12:05:26 +0000 | [diff] [blame] | 191 | def device(port): |
| 192 | return '/dev/tty%d' % (port) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +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 | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 199 | else: |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame^] | 200 | # platform detection has failed... |
cliechti | 7aaead3 | 2009-07-23 14:02:41 +0000 | [diff] [blame] | 201 | sys.stderr.write("""\ |
| 202 | don't know how to number ttys on this system. |
cliechti | 895e830 | 2004-04-20 02:40:28 +0000 | [diff] [blame] | 203 | ! Use an explicit path (eg /dev/ttyS1) or send this information to |
| 204 | ! the author of this module: |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 205 | |
cliechti | 895e830 | 2004-04-20 02:40:28 +0000 | [diff] [blame] | 206 | sys.platform = %r |
| 207 | os.name = %r |
| 208 | serialposix.py version = %s |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 209 | |
| 210 | also add the device name of the serial port and where the |
| 211 | counting starts for the first serial port. |
| 212 | e.g. 'first serial port: /dev/ttyS0' |
| 213 | and with a bit luck you can get this module running... |
cliechti | 7aaead3 | 2009-07-23 14:02:41 +0000 | [diff] [blame] | 214 | """ % (sys.platform, os.name, VERSION)) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 215 | # no exception, just continue with a brave attempt to build a device name |
| 216 | # 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] | 217 | # to work using a string with the real device name as port parameter. |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 218 | def device(portum): |
| 219 | return '/dev/ttyS%d' % portnum |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 220 | def set_special_baudrate(port, baudrate): |
| 221 | 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] | 222 | baudrate_constants = {} |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 223 | #~ raise Exception, "this module does not run on this platform, sorry." |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 224 | |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 225 | # whats up with "aix", "beos", .... |
| 226 | # they should work, just need to know the device names. |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 227 | |
| 228 | |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 229 | # load some constants for later use. |
| 230 | # try to use values from TERMIOS, use defaults from linux otherwise |
cliechti | 8901aef | 2002-11-19 01:15:05 +0000 | [diff] [blame] | 231 | TIOCMGET = hasattr(TERMIOS, 'TIOCMGET') and TERMIOS.TIOCMGET or 0x5415 |
| 232 | TIOCMBIS = hasattr(TERMIOS, 'TIOCMBIS') and TERMIOS.TIOCMBIS or 0x5416 |
| 233 | TIOCMBIC = hasattr(TERMIOS, 'TIOCMBIC') and TERMIOS.TIOCMBIC or 0x5417 |
| 234 | TIOCMSET = hasattr(TERMIOS, 'TIOCMSET') and TERMIOS.TIOCMSET or 0x5418 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 235 | |
cliechti | 8901aef | 2002-11-19 01:15:05 +0000 | [diff] [blame] | 236 | #TIOCM_LE = hasattr(TERMIOS, 'TIOCM_LE') and TERMIOS.TIOCM_LE or 0x001 |
| 237 | TIOCM_DTR = hasattr(TERMIOS, 'TIOCM_DTR') and TERMIOS.TIOCM_DTR or 0x002 |
| 238 | TIOCM_RTS = hasattr(TERMIOS, 'TIOCM_RTS') and TERMIOS.TIOCM_RTS or 0x004 |
| 239 | #TIOCM_ST = hasattr(TERMIOS, 'TIOCM_ST') and TERMIOS.TIOCM_ST or 0x008 |
| 240 | #TIOCM_SR = hasattr(TERMIOS, 'TIOCM_SR') and TERMIOS.TIOCM_SR or 0x010 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 241 | |
cliechti | 8901aef | 2002-11-19 01:15:05 +0000 | [diff] [blame] | 242 | TIOCM_CTS = hasattr(TERMIOS, 'TIOCM_CTS') and TERMIOS.TIOCM_CTS or 0x020 |
| 243 | TIOCM_CAR = hasattr(TERMIOS, 'TIOCM_CAR') and TERMIOS.TIOCM_CAR or 0x040 |
| 244 | TIOCM_RNG = hasattr(TERMIOS, 'TIOCM_RNG') and TERMIOS.TIOCM_RNG or 0x080 |
| 245 | TIOCM_DSR = hasattr(TERMIOS, 'TIOCM_DSR') and TERMIOS.TIOCM_DSR or 0x100 |
| 246 | TIOCM_CD = hasattr(TERMIOS, 'TIOCM_CD') and TERMIOS.TIOCM_CD or TIOCM_CAR |
| 247 | TIOCM_RI = hasattr(TERMIOS, 'TIOCM_RI') and TERMIOS.TIOCM_RI or TIOCM_RNG |
| 248 | #TIOCM_OUT1 = hasattr(TERMIOS, 'TIOCM_OUT1') and TERMIOS.TIOCM_OUT1 or 0x2000 |
| 249 | #TIOCM_OUT2 = hasattr(TERMIOS, 'TIOCM_OUT2') and TERMIOS.TIOCM_OUT2 or 0x4000 |
cliechti | f5831e0 | 2002-12-05 23:15:27 +0000 | [diff] [blame] | 250 | TIOCINQ = hasattr(TERMIOS, 'FIONREAD') and TERMIOS.FIONREAD or 0x541B |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 251 | |
| 252 | TIOCM_zero_str = struct.pack('I', 0) |
| 253 | TIOCM_RTS_str = struct.pack('I', TIOCM_RTS) |
| 254 | TIOCM_DTR_str = struct.pack('I', TIOCM_DTR) |
| 255 | |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 256 | TIOCSBRK = hasattr(TERMIOS, 'TIOCSBRK') and TERMIOS.TIOCSBRK or 0x5427 |
| 257 | TIOCCBRK = hasattr(TERMIOS, 'TIOCCBRK') and TERMIOS.TIOCCBRK or 0x5428 |
| 258 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 259 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 260 | class PosixSerial(SerialBase): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 261 | """Serial port class POSIX implementation. Serial port configuration is |
| 262 | done with termios and fcntl. Runs on Linux and many other Un*x like |
| 263 | systems.""" |
| 264 | |
| 265 | def open(self): |
| 266 | """Open port with current settings. This may throw a SerialException |
| 267 | if the port cannot be opened.""" |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 268 | self.fd = None |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 269 | if self._port is None: |
| 270 | raise SerialException("Port must be configured before it can be used.") |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 271 | # open |
cliechti | 4616bf1 | 2002-04-08 23:13:14 +0000 | [diff] [blame] | 272 | try: |
| 273 | self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK) |
| 274 | except Exception, msg: |
| 275 | self.fd = None |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 276 | raise SerialException("could not open port %s: %s" % (self._port, msg)) |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 277 | #~ fcntl.fcntl(self.fd, FCNTL.F_SETFL, 0) # set blocking |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 278 | |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 279 | try: |
| 280 | self._reconfigurePort() |
| 281 | except: |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 282 | try: |
| 283 | os.close(self.fd) |
| 284 | except: |
| 285 | # ignore any exception when closing the port |
| 286 | # also to keep original exception that happened when setting up |
| 287 | pass |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 288 | self.fd = None |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 289 | raise |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 290 | else: |
| 291 | self._isOpen = True |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 292 | #~ self.flushInput() |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 293 | |
| 294 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 295 | def _reconfigurePort(self): |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 296 | """Set communication parameters on opened port.""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 297 | if self.fd is None: |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame^] | 298 | raise SerialException("Can only operate on a valid file descriptor") |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 299 | custom_baud = None |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 300 | |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 301 | vmin = vtime = 0 # timeout is done via select |
cliechti | 679bfa6 | 2008-06-20 23:58:15 +0000 | [diff] [blame] | 302 | if self._interCharTimeout is not None: |
| 303 | vmin = 1 |
| 304 | vtime = int(self._interCharTimeout * 10) |
cliechti | 6ce7ab1 | 2002-11-07 02:15:00 +0000 | [diff] [blame] | 305 | try: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 306 | iflag, oflag, cflag, lflag, ispeed, ospeed, cc = termios.tcgetattr(self.fd) |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 307 | except termios.error, 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] | 308 | raise SerialException("Could not configure port: %s" % msg) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 309 | # set up raw mode / no echo / binary |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 310 | cflag |= (TERMIOS.CLOCAL|TERMIOS.CREAD) |
| 311 | lflag &= ~(TERMIOS.ICANON|TERMIOS.ECHO|TERMIOS.ECHOE|TERMIOS.ECHOK|TERMIOS.ECHONL| |
cliechti | 835996a | 2004-06-02 19:45:07 +0000 | [diff] [blame] | 312 | TERMIOS.ISIG|TERMIOS.IEXTEN) #|TERMIOS.ECHOPRT |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 313 | for flag in ('ECHOCTL', 'ECHOKE'): # netbsd workaround for Erk |
cliechti | 835996a | 2004-06-02 19:45:07 +0000 | [diff] [blame] | 314 | if hasattr(TERMIOS, flag): |
| 315 | lflag &= ~getattr(TERMIOS, flag) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 316 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 317 | oflag &= ~(TERMIOS.OPOST) |
cliechti | 895e830 | 2004-04-20 02:40:28 +0000 | [diff] [blame] | 318 | iflag &= ~(TERMIOS.INLCR|TERMIOS.IGNCR|TERMIOS.ICRNL|TERMIOS.IGNBRK) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 319 | if hasattr(TERMIOS, 'IUCLC'): |
cliechti | 895e830 | 2004-04-20 02:40:28 +0000 | [diff] [blame] | 320 | iflag &= ~TERMIOS.IUCLC |
cliechti | 3e57b3d | 2005-08-12 21:04:44 +0000 | [diff] [blame] | 321 | if hasattr(TERMIOS, 'PARMRK'): |
| 322 | iflag &= ~TERMIOS.PARMRK |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 323 | |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 324 | # setup baud rate |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 325 | try: |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 326 | ispeed = ospeed = getattr(TERMIOS, 'B%s' % (self._baudrate)) |
cliechti | 895e830 | 2004-04-20 02:40:28 +0000 | [diff] [blame] | 327 | except AttributeError: |
cliechti | f1559d0 | 2007-11-08 23:43:58 +0000 | [diff] [blame] | 328 | try: |
| 329 | ispeed = ospeed = baudrate_constants[self._baudrate] |
| 330 | except KeyError: |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 331 | #~ raise ValueError('Invalid baud rate: %r' % self._baudrate) |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 332 | # may need custom baud rate, it isn't in our list. |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 333 | ispeed = ospeed = getattr(TERMIOS, 'B38400') |
cliechti | f0a4f0f | 2009-07-21 21:12:37 +0000 | [diff] [blame] | 334 | try: |
| 335 | custom_baud = int(self._baudrate) # store for later |
| 336 | except ValueError: |
| 337 | raise ValueError('Invalid baud rate: %r' % self._baudrate) |
| 338 | else: |
| 339 | if custom_baud < 0: |
| 340 | raise ValueError('Invalid baud rate: %r' % self._baudrate) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 341 | |
| 342 | # setup char len |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 343 | cflag &= ~TERMIOS.CSIZE |
| 344 | if self._bytesize == 8: |
| 345 | cflag |= TERMIOS.CS8 |
| 346 | elif self._bytesize == 7: |
| 347 | cflag |= TERMIOS.CS7 |
| 348 | elif self._bytesize == 6: |
| 349 | cflag |= TERMIOS.CS6 |
| 350 | elif self._bytesize == 5: |
| 351 | cflag |= TERMIOS.CS5 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 352 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 353 | raise ValueError('Invalid char len: %r' % self._bytesize) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 354 | # setup stopbits |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 355 | if self._stopbits == STOPBITS_ONE: |
| 356 | cflag &= ~(TERMIOS.CSTOPB) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 357 | elif self._stopbits == STOPBITS_ONE_POINT_FIVE: |
| 358 | 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] | 359 | elif self._stopbits == STOPBITS_TWO: |
| 360 | cflag |= (TERMIOS.CSTOPB) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 361 | else: |
cliechti | 3172d3d | 2009-07-21 22:33:40 +0000 | [diff] [blame] | 362 | raise ValueError('Invalid stop bit specification: %r' % self._stopbits) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 363 | # setup parity |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 364 | iflag &= ~(TERMIOS.INPCK|TERMIOS.ISTRIP) |
| 365 | if self._parity == PARITY_NONE: |
| 366 | cflag &= ~(TERMIOS.PARENB|TERMIOS.PARODD) |
| 367 | elif self._parity == PARITY_EVEN: |
| 368 | cflag &= ~(TERMIOS.PARODD) |
| 369 | cflag |= (TERMIOS.PARENB) |
| 370 | elif self._parity == PARITY_ODD: |
| 371 | cflag |= (TERMIOS.PARENB|TERMIOS.PARODD) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 372 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 373 | raise ValueError('Invalid parity: %r' % self._parity) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 374 | # setup flow control |
| 375 | # xonxoff |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 376 | if hasattr(TERMIOS, 'IXANY'): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 377 | if self._xonxoff: |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 378 | iflag |= (TERMIOS.IXON|TERMIOS.IXOFF) #|TERMIOS.IXANY) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 379 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 380 | iflag &= ~(TERMIOS.IXON|TERMIOS.IXOFF|TERMIOS.IXANY) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 381 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 382 | if self._xonxoff: |
| 383 | iflag |= (TERMIOS.IXON|TERMIOS.IXOFF) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 384 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 385 | iflag &= ~(TERMIOS.IXON|TERMIOS.IXOFF) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 386 | # rtscts |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 387 | if hasattr(TERMIOS, 'CRTSCTS'): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 388 | if self._rtscts: |
| 389 | cflag |= (TERMIOS.CRTSCTS) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 390 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 391 | cflag &= ~(TERMIOS.CRTSCTS) |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 392 | elif hasattr(TERMIOS, 'CNEW_RTSCTS'): # try it with alternate constant name |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 393 | if self._rtscts: |
| 394 | cflag |= (TERMIOS.CNEW_RTSCTS) |
cliechti | d474369 | 2002-04-08 22:39:53 +0000 | [diff] [blame] | 395 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 396 | cflag &= ~(TERMIOS.CNEW_RTSCTS) |
cliechti | 2750b83 | 2009-07-28 00:13:52 +0000 | [diff] [blame] | 397 | # XXX should there be a warning if setting up rtscts (and xonxoff etc) fails?? |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 398 | |
| 399 | # buffer |
| 400 | # vmin "minimal number of characters to be read. = for non blocking" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 401 | if vmin < 0 or vmin > 255: |
| 402 | raise ValueError('Invalid vmin: %r ' % vmin) |
| 403 | cc[TERMIOS.VMIN] = vmin |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 404 | # vtime |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 405 | if vtime < 0 or vtime > 255: |
| 406 | raise ValueError('Invalid vtime: %r' % vtime) |
| 407 | cc[TERMIOS.VTIME] = vtime |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 408 | # activate settings |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 409 | termios.tcsetattr(self.fd, TERMIOS.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 410 | |
cliechti | e8c4542 | 2008-06-20 23:23:14 +0000 | [diff] [blame] | 411 | # apply custom baud rate, if any |
| 412 | if custom_baud is not None: |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 413 | set_special_baudrate(self, custom_baud) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 414 | |
| 415 | def close(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 416 | """Close port""" |
| 417 | if self._isOpen: |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 418 | if self.fd is not None: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 419 | os.close(self.fd) |
| 420 | self.fd = None |
| 421 | self._isOpen = False |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 422 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 423 | def makeDeviceName(self, port): |
| 424 | return device(port) |
| 425 | |
| 426 | # - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 427 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 428 | def inWaiting(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 429 | """Return the number of characters currently in the input buffer.""" |
cliechti | f5831e0 | 2002-12-05 23:15:27 +0000 | [diff] [blame] | 430 | #~ s = fcntl.ioctl(self.fd, TERMIOS.FIONREAD, TIOCM_zero_str) |
| 431 | s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 432 | return struct.unpack('I',s)[0] |
| 433 | |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame^] | 434 | # select based implementation, proved to work on many systems |
| 435 | def read(self, size=1): |
| 436 | """Read size bytes from the serial port. If a timeout is set it may |
| 437 | return less characters as requested. With no timeout it will block |
| 438 | until the requested number of bytes is read.""" |
| 439 | if self.fd is None: raise portNotOpenError |
| 440 | read = bytearray() |
| 441 | while len(read) < size: |
| 442 | ready,_,_ = select.select([self.fd],[],[], self._timeout) |
| 443 | # If select was used with a timeout, and the timeout occurs, it |
| 444 | # returns with empty lists -> thus abort read operation. |
| 445 | # For timeout == 0 (non-blocking operation) also abort when there |
| 446 | # is nothing to read. |
| 447 | if not ready: |
| 448 | break # timeout |
| 449 | buf = os.read(self.fd, size-len(read)) |
| 450 | # read should always return some data as select reported it was |
| 451 | # ready to read when we get to this point. |
| 452 | if not buf: |
| 453 | # Disconnected devices, at least on Linux, show the |
| 454 | # behavior that they are always ready to read immediately |
| 455 | # but reading returns nothing. |
| 456 | raise SerialException('device reports readiness to read but returned no data (device disconnected?)') |
| 457 | read.extend(buf) |
| 458 | return bytes(read) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 459 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 460 | def write(self, data): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 461 | """Output the given string over the serial port.""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 462 | if self.fd is None: raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 463 | t = len(data) |
| 464 | d = data |
cliechti | 3cf46d6 | 2009-08-07 00:19:57 +0000 | [diff] [blame] | 465 | if self._writeTimeout is not None and self._writeTimeout > 0: |
| 466 | timeout = time.time() + self._writeTimeout |
| 467 | else: |
| 468 | timeout = None |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 469 | while t > 0: |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 470 | try: |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 471 | n = os.write(self.fd, d) |
cliechti | 3cf46d6 | 2009-08-07 00:19:57 +0000 | [diff] [blame] | 472 | if timeout: |
| 473 | # when timeout is set, use select to wait for being ready |
| 474 | # with the time left as timeout |
| 475 | timeleft = timeout - time.time() |
| 476 | if timeleft < 0: |
| 477 | raise writeTimeoutError |
| 478 | _, ready, _ = select.select([], [self.fd], [], timeleft) |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 479 | if not ready: |
| 480 | raise writeTimeoutError |
| 481 | d = d[n:] |
| 482 | t = t - n |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 483 | except OSError, v: |
cliechti | 5d4d0bd | 2004-11-13 03:27:39 +0000 | [diff] [blame] | 484 | if v.errno != errno.EAGAIN: |
cliechti | 65722c9 | 2009-08-07 00:48:53 +0000 | [diff] [blame] | 485 | raise SerialException('write failed: %s' % (v,)) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 486 | return len(data) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 487 | |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 488 | def flush(self): |
| 489 | """Flush of file like objects. In this case, wait until all data |
| 490 | is written.""" |
| 491 | self.drainOutput() |
| 492 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 493 | def flushInput(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 494 | """Clear input buffer, discarding all that is in the buffer.""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 495 | if self.fd is None: |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 496 | raise portNotOpenError |
| 497 | termios.tcflush(self.fd, TERMIOS.TCIFLUSH) |
| 498 | |
| 499 | def flushOutput(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 500 | """Clear output buffer, aborting the current output and |
| 501 | discarding all that is in the buffer.""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 502 | if self.fd is None: |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 503 | raise portNotOpenError |
| 504 | termios.tcflush(self.fd, TERMIOS.TCOFLUSH) |
| 505 | |
cliechti | aaa0460 | 2006-02-05 23:02:46 +0000 | [diff] [blame] | 506 | def sendBreak(self, duration=0.25): |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 507 | """Send break condition. Timed, returns to idle state after given duration.""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 508 | if self.fd is None: |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 509 | raise portNotOpenError |
cliechti | aaa0460 | 2006-02-05 23:02:46 +0000 | [diff] [blame] | 510 | termios.tcsendbreak(self.fd, int(duration/0.25)) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 511 | |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 512 | def setBreak(self, level=1): |
cliechti | dfec0c8 | 2009-07-21 01:35:41 +0000 | [diff] [blame] | 513 | """Set break: Controls TXD. When active, no transmitting is possible.""" |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 514 | if self.fd is None: raise portNotOpenError |
| 515 | if level: |
| 516 | fcntl.ioctl(self.fd, TIOCSBRK) |
| 517 | else: |
| 518 | fcntl.ioctl(self.fd, TIOCCBRK) |
| 519 | |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 520 | def setRTS(self, level=1): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 521 | """Set terminal status line: Request To Send""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 522 | if self.fd is None: raise portNotOpenError |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 523 | if level: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 524 | fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_RTS_str) |
| 525 | else: |
| 526 | fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_RTS_str) |
| 527 | |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 528 | def setDTR(self, level=1): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 529 | """Set terminal status line: Data Terminal Ready""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 530 | if self.fd is None: raise portNotOpenError |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 531 | if level: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 532 | fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str) |
| 533 | else: |
| 534 | fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str) |
| 535 | |
| 536 | def getCTS(self): |
| 537 | """Read terminal status line: Clear To Send""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 538 | if self.fd is None: raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 539 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
| 540 | return struct.unpack('I',s)[0] & TIOCM_CTS != 0 |
| 541 | |
| 542 | def getDSR(self): |
| 543 | """Read terminal status line: Data Set Ready""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 544 | if self.fd is None: raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 545 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
| 546 | return struct.unpack('I',s)[0] & TIOCM_DSR != 0 |
| 547 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 548 | def getRI(self): |
| 549 | """Read terminal status line: Ring Indicator""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 550 | if self.fd is None: raise portNotOpenError |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 551 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 552 | return struct.unpack('I',s)[0] & TIOCM_RI != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 553 | |
| 554 | def getCD(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 555 | """Read terminal status line: Carrier Detect""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 556 | if self.fd is None: raise portNotOpenError |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 557 | s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 558 | return struct.unpack('I',s)[0] & TIOCM_CD != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 559 | |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 560 | # - - platform specific - - - - |
| 561 | |
| 562 | def drainOutput(self): |
| 563 | """internal - not portable!""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 564 | if self.fd is None: raise portNotOpenError |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 565 | termios.tcdrain(self.fd) |
| 566 | |
| 567 | def nonblocking(self): |
| 568 | """internal - not portable!""" |
cliechti | c617826 | 2004-03-22 22:04:52 +0000 | [diff] [blame] | 569 | if self.fd is None: |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 570 | raise portNotOpenError |
| 571 | fcntl.fcntl(self.fd, FCNTL.F_SETFL, FCNTL.O_NONBLOCK) |
| 572 | |
cliechti | 8753bbc | 2005-01-15 20:32:51 +0000 | [diff] [blame] | 573 | def fileno(self): |
cliechti | fab0987 | 2009-02-07 00:25:44 +0000 | [diff] [blame] | 574 | """For easier use of the serial port instance with select. |
cliechti | 8753bbc | 2005-01-15 20:32:51 +0000 | [diff] [blame] | 575 | WARNING: this function is not portable to different platforms!""" |
| 576 | if self.fd is None: raise portNotOpenError |
| 577 | return self.fd |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 578 | |
cliechti | 57e48a6 | 2009-08-03 22:29:58 +0000 | [diff] [blame] | 579 | def flowControl(self, enable): |
| 580 | """manually control flow - when hardware or software flow control is |
| 581 | enabled""" |
| 582 | if enable: |
| 583 | termios.tcflow(self.fd, TERMIOS.TCION) |
| 584 | else: |
| 585 | termios.tcflow(self.fd, TERMIOS.TCIOFF) |
| 586 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 587 | |
| 588 | # assemble Serial class with the platform specifc implementation and the base |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 589 | # for file-like behavior. for Python 2.6 and newer, that provide the new I/O |
| 590 | # library, derrive from io.RawIOBase |
| 591 | try: |
| 592 | import io |
| 593 | except ImportError: |
| 594 | # classic version with our own file-like emulation |
| 595 | class Serial(PosixSerial, FileLike): |
| 596 | pass |
| 597 | else: |
| 598 | # io library present |
| 599 | class Serial(PosixSerial, io.RawIOBase): |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 600 | pass |
| 601 | |
cliechti | a9a093e | 2010-01-02 03:05:08 +0000 | [diff] [blame^] | 602 | class PosixPollSerial(Serial): |
| 603 | """poll based read implementation. not all systems support poll properly. |
| 604 | however this one has better handling of errors, such as a device |
| 605 | disconnecting while it's in use (e.g. USB-serial unplugged)""" |
| 606 | |
| 607 | def read(self, size=1): |
| 608 | """Read size bytes from the serial port. If a timeout is set it may |
| 609 | return less characters as requested. With no timeout it will block |
| 610 | until the requested number of bytes is read.""" |
| 611 | if self.fd is None: raise portNotOpenError |
| 612 | read = bytearray() |
| 613 | poll = select.poll() |
| 614 | poll.register(self.fd, select.POLLIN|select.POLLERR|select.POLLHUP|select.POLLNVAL) |
| 615 | if size > 0: |
| 616 | while len(read) < size: |
| 617 | # print "\tread(): size",size, "have", len(read) #debug |
| 618 | # wait until device becomes ready to read (or something fails) |
| 619 | for fd, event in poll.poll(self._timeout): |
| 620 | if event & (select.POLLERR|select.POLLHUP|select.POLLNVAL): |
| 621 | raise SerialException('device reports error (poll)') |
| 622 | # we don't care if it is select.POLLIN or timeout, that's |
| 623 | # handled below |
| 624 | buf = os.read(self.fd, size - len(read)) |
| 625 | read.extend(buf) |
| 626 | if ((self._timeout is not None and self._timeout >= 0) or |
| 627 | (self._interCharTimeout is not None and self._interCharTimeout > 0)) and not buf: |
| 628 | break # early abort on timeout |
| 629 | return bytes(read) |
| 630 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 631 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 632 | if __name__ == '__main__': |
| 633 | s = Serial(0, |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 634 | baudrate=19200, # baud rate |
| 635 | bytesize=EIGHTBITS, # number of data bits |
cliechti | 3172d3d | 2009-07-21 22:33:40 +0000 | [diff] [blame] | 636 | parity=PARITY_EVEN, # enable parity checking |
cliechti | 53c9fd4 | 2009-07-23 23:51:51 +0000 | [diff] [blame] | 637 | stopbits=STOPBITS_ONE, # number of stop bits |
cliechti | 3172d3d | 2009-07-21 22:33:40 +0000 | [diff] [blame] | 638 | timeout=3, # set a timeout value, None for waiting forever |
| 639 | xonxoff=0, # enable software flow control |
| 640 | rtscts=0, # enable RTS/CTS flow control |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 641 | ) |
| 642 | s.setRTS(1) |
| 643 | s.setDTR(1) |
| 644 | s.flushInput() |
| 645 | s.flushOutput() |
| 646 | s.write('hello') |
cliechti | 109486b | 2009-08-02 00:00:11 +0000 | [diff] [blame] | 647 | sys.stdout.write('%r\n' % s.read(5)) |
| 648 | sys.stdout.write('%s\n' % s.inWaiting()) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 649 | del s |
cliechti | 4569bac | 2007-11-08 21:57:19 +0000 | [diff] [blame] | 650 | |