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