blob: 5542d2854c2fa12681fd341f5725d98925f41dae [file] [log] [blame]
cliechti89b4af12002-02-12 23:24:41 +00001#!/usr/bin/env python
cliechti58b481c2009-02-16 20:42:32 +00002#
cliechtic54b2c82008-06-21 01:59:08 +00003# Python Serial Port Extension for Win32, Linux, BSD, Jython
4# module for serial IO for POSIX compatible systems, like Linux
5# see __init__.py
cliechti89b4af12002-02-12 23:24:41 +00006#
Chris Liechti68340d72015-08-03 14:15:48 +02007# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
cliechti89b4af12002-02-12 23:24:41 +00008# this is distributed under a free software license, see license.txt
9#
cliechtic54b2c82008-06-21 01:59:08 +000010# parts based on code from Grant B. Edwards <grante@visi.com>:
cliechti89b4af12002-02-12 23:24:41 +000011# ftp://ftp.visi.com/users/grante/python/PosixSerial.py
cliechti53c9fd42009-07-23 23:51:51 +000012#
cliechti89b4af12002-02-12 23:24:41 +000013# references: http://www.easysw.com/~mike/serial/serial.html
14
cliechti3cf46d62009-08-07 00:19:57 +000015import sys, os, fcntl, termios, struct, select, errno, time
Chris Liechtiaf6d0462015-08-03 17:21:14 +020016import io
cliechti39cfb7b2011-08-22 00:30:07 +000017from serial.serialutil import *
cliechti89b4af12002-02-12 23:24:41 +000018
cliechti53c9fd42009-07-23 23:51:51 +000019# Do check the Python version as some constants have moved.
cliechti89b4af12002-02-12 23:24:41 +000020if (sys.hexversion < 0x020100f0):
21 import TERMIOS
22else:
23 TERMIOS = termios
24
25if (sys.hexversion < 0x020200f0):
26 import FCNTL
27else:
28 FCNTL = fcntl
29
cliechti3172d3d2009-07-21 22:33:40 +000030# try to detect the OS so that a device can be selected...
cliechti53c9fd42009-07-23 23:51:51 +000031# this code block should supply a device() and set_special_baudrate() function
32# for the platform
cliechtid6bf52c2003-10-01 02:28:12 +000033plat = sys.platform.lower()
cliechti89b4af12002-02-12 23:24:41 +000034
cliechti3172d3d2009-07-21 22:33:40 +000035if plat[:5] == 'linux': # Linux (confirmed)
cliechti53c9fd42009-07-23 23:51:51 +000036
cliechti89b4af12002-02-12 23:24:41 +000037 def device(port):
38 return '/dev/ttyS%d' % port
39
cliechti1877c522013-10-11 14:02:26 +000040 TCGETS2 = 0x802C542A
41 TCSETS2 = 0x402C542B
42 BOTHER = 0o010000
cliechti53c9fd42009-07-23 23:51:51 +000043
44 def set_special_baudrate(port, baudrate):
cliechti1877c522013-10-11 14:02:26 +000045 # right size is 44 on x86_64, allow for some growth
cliechti53c9fd42009-07-23 23:51:51 +000046 import array
cliechti1877c522013-10-11 14:02:26 +000047 buf = array.array('i', [0] * 64)
cliechti53c9fd42009-07-23 23:51:51 +000048
cliechti53c9fd42009-07-23 23:51:51 +000049 try:
cliechti95772d32013-10-11 14:38:13 +000050 # get serial_struct
51 FCNTL.ioctl(port.fd, TCGETS2, buf)
52 # set custom speed
53 buf[2] &= ~TERMIOS.CBAUD
54 buf[2] |= BOTHER
55 buf[9] = buf[10] = baudrate
56
57 # set serial_struct
cliechti1877c522013-10-11 14:02:26 +000058 res = FCNTL.ioctl(port.fd, TCSETS2, buf)
Chris Liechti68340d72015-08-03 14:15:48 +020059 except IOError as e:
cliechti95772d32013-10-11 14:38:13 +000060 raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e))
cliechti53c9fd42009-07-23 23:51:51 +000061
cliechti99220a02009-08-14 00:21:25 +000062 baudrate_constants = {
Chris Liechti68340d72015-08-03 14:15:48 +020063 0: 0o000000, # hang up
64 50: 0o000001,
65 75: 0o000002,
66 110: 0o000003,
67 134: 0o000004,
68 150: 0o000005,
69 200: 0o000006,
70 300: 0o000007,
71 600: 0o000010,
72 1200: 0o000011,
73 1800: 0o000012,
74 2400: 0o000013,
75 4800: 0o000014,
76 9600: 0o000015,
77 19200: 0o000016,
78 38400: 0o000017,
79 57600: 0o010001,
80 115200: 0o010002,
81 230400: 0o010003,
82 460800: 0o010004,
83 500000: 0o010005,
84 576000: 0o010006,
85 921600: 0o010007,
86 1000000: 0o010010,
87 1152000: 0o010011,
88 1500000: 0o010012,
89 2000000: 0o010013,
90 2500000: 0o010014,
91 3000000: 0o010015,
92 3500000: 0o010016,
93 4000000: 0o010017
cliechti99220a02009-08-14 00:21:25 +000094 }
95
cliechti53c9fd42009-07-23 23:51:51 +000096elif plat == 'cygwin': # cygwin/win32 (confirmed)
97
cliechtif281fde2002-06-07 21:53:40 +000098 def device(port):
cliechtif5831e02002-12-05 23:15:27 +000099 return '/dev/com%d' % (port + 1)
cliechtif281fde2002-06-07 21:53:40 +0000100
cliechti53c9fd42009-07-23 23:51:51 +0000101 def set_special_baudrate(port, baudrate):
cliechti99220a02009-08-14 00:21:25 +0000102 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
cliechti53c9fd42009-07-23 23:51:51 +0000103
cliechtia505de32013-05-31 01:27:25 +0000104 baudrate_constants = {
105 128000: 0x01003,
106 256000: 0x01005,
107 500000: 0x01007,
108 576000: 0x01008,
109 921600: 0x01009,
110 1000000: 0x0100a,
111 1152000: 0x0100b,
112 1500000: 0x0100c,
113 2000000: 0x0100d,
114 2500000: 0x0100e,
115 3000000: 0x0100f
116 }
cliechti53c9fd42009-07-23 23:51:51 +0000117
cliechti4a601342011-12-29 02:22:17 +0000118elif plat[:7] == 'openbsd': # OpenBSD
cliechti53c9fd42009-07-23 23:51:51 +0000119
cliechti89b4af12002-02-12 23:24:41 +0000120 def device(port):
cliechti4a601342011-12-29 02:22:17 +0000121 return '/dev/cua%02d' % port
cliechti89b4af12002-02-12 23:24:41 +0000122
cliechti53c9fd42009-07-23 23:51:51 +0000123 def set_special_baudrate(port, baudrate):
124 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
125
cliechti99220a02009-08-14 00:21:25 +0000126 baudrate_constants = {}
127
cliechti89b4af12002-02-12 23:24:41 +0000128elif plat[:3] == 'bsd' or \
cliechti4a601342011-12-29 02:22:17 +0000129 plat[:7] == 'freebsd':
cliechti53c9fd42009-07-23 23:51:51 +0000130
cliechti89b4af12002-02-12 23:24:41 +0000131 def device(port):
cliechtieaa96882008-06-16 22:59:20 +0000132 return '/dev/cuad%d' % port
cliechti89b4af12002-02-12 23:24:41 +0000133
cliechti53c9fd42009-07-23 23:51:51 +0000134 def set_special_baudrate(port, baudrate):
135 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
136
cliechti99220a02009-08-14 00:21:25 +0000137 baudrate_constants = {}
138
cliechti53c9fd42009-07-23 23:51:51 +0000139elif plat[:6] == 'darwin': # OS X
140
141 version = os.uname()[2].split('.')
142 # Tiger or above can support arbitrary serial speeds
143 if int(version[0]) >= 8:
cliechti53c9fd42009-07-23 23:51:51 +0000144 def set_special_baudrate(port, baudrate):
145 # use IOKit-specific call to set up high speeds
146 import array, fcntl
147 buf = array.array('i', [baudrate])
148 IOSSIOSPEED = 0x80045402 #_IOW('T', 2, speed_t)
149 fcntl.ioctl(port.fd, IOSSIOSPEED, buf, 1)
150 else: # version < 8
151 def set_special_baudrate(port, baudrate):
152 raise ValueError("baud rate not supported")
153
154 def device(port):
155 return '/dev/cuad%d' % port
156
cliechti99220a02009-08-14 00:21:25 +0000157 baudrate_constants = {}
158
cliechti53c9fd42009-07-23 23:51:51 +0000159
cliechti3172d3d2009-07-21 22:33:40 +0000160elif plat[:6] == 'netbsd': # NetBSD 1.6 testing by Erk
cliechti53c9fd42009-07-23 23:51:51 +0000161
cliechti835996a2004-06-02 19:45:07 +0000162 def device(port):
163 return '/dev/dty%02d' % port
164
cliechti53c9fd42009-07-23 23:51:51 +0000165 def set_special_baudrate(port, baudrate):
166 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
167
cliechti99220a02009-08-14 00:21:25 +0000168 baudrate_constants = {}
169
cliechti3172d3d2009-07-21 22:33:40 +0000170elif plat[:4] == 'irix': # IRIX (partially tested)
cliechti53c9fd42009-07-23 23:51:51 +0000171
cliechti89b4af12002-02-12 23:24:41 +0000172 def device(port):
cliechtie2418e92006-06-05 20:03:17 +0000173 return '/dev/ttyf%d' % (port+1) #XXX different device names depending on flow control
cliechti89b4af12002-02-12 23:24:41 +0000174
cliechti53c9fd42009-07-23 23:51:51 +0000175 def set_special_baudrate(port, baudrate):
176 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
177
cliechti99220a02009-08-14 00:21:25 +0000178 baudrate_constants = {}
179
cliechti3172d3d2009-07-21 22:33:40 +0000180elif plat[:2] == 'hp': # HP-UX (not tested)
cliechti53c9fd42009-07-23 23:51:51 +0000181
cliechti89b4af12002-02-12 23:24:41 +0000182 def device(port):
183 return '/dev/tty%dp0' % (port+1)
184
cliechti53c9fd42009-07-23 23:51:51 +0000185 def set_special_baudrate(port, baudrate):
186 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
187
cliechti99220a02009-08-14 00:21:25 +0000188 baudrate_constants = {}
189
cliechti3172d3d2009-07-21 22:33:40 +0000190elif plat[:5] == 'sunos': # Solaris/SunOS (confirmed)
cliechti53c9fd42009-07-23 23:51:51 +0000191
cliechti89b4af12002-02-12 23:24:41 +0000192 def device(port):
193 return '/dev/tty%c' % (ord('a')+port)
cliechti58b481c2009-02-16 20:42:32 +0000194
cliechti53c9fd42009-07-23 23:51:51 +0000195 def set_special_baudrate(port, baudrate):
196 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
197
cliechti99220a02009-08-14 00:21:25 +0000198 baudrate_constants = {}
199
cliechti3172d3d2009-07-21 22:33:40 +0000200elif plat[:3] == 'aix': # AIX
cliechti53c9fd42009-07-23 23:51:51 +0000201
cliechti40e1b072005-03-12 12:05:26 +0000202 def device(port):
203 return '/dev/tty%d' % (port)
cliechti89b4af12002-02-12 23:24:41 +0000204
cliechti53c9fd42009-07-23 23:51:51 +0000205 def set_special_baudrate(port, baudrate):
206 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
207
cliechti99220a02009-08-14 00:21:25 +0000208 baudrate_constants = {}
209
cliechti89b4af12002-02-12 23:24:41 +0000210else:
cliechtia9a093e2010-01-02 03:05:08 +0000211 # platform detection has failed...
cliechti7aaead32009-07-23 14:02:41 +0000212 sys.stderr.write("""\
213don't know how to number ttys on this system.
cliechti895e8302004-04-20 02:40:28 +0000214! Use an explicit path (eg /dev/ttyS1) or send this information to
215! the author of this module:
cliechti89b4af12002-02-12 23:24:41 +0000216
cliechti895e8302004-04-20 02:40:28 +0000217sys.platform = %r
218os.name = %r
219serialposix.py version = %s
cliechti89b4af12002-02-12 23:24:41 +0000220
221also add the device name of the serial port and where the
222counting starts for the first serial port.
223e.g. 'first serial port: /dev/ttyS0'
224and with a bit luck you can get this module running...
cliechti7aaead32009-07-23 14:02:41 +0000225""" % (sys.platform, os.name, VERSION))
cliechti58b481c2009-02-16 20:42:32 +0000226 # no exception, just continue with a brave attempt to build a device name
227 # even if the device name is not correct for the platform it has chances
cliechti3172d3d2009-07-21 22:33:40 +0000228 # to work using a string with the real device name as port parameter.
cliechtid6bf52c2003-10-01 02:28:12 +0000229 def device(portum):
230 return '/dev/ttyS%d' % portnum
cliechti53c9fd42009-07-23 23:51:51 +0000231 def set_special_baudrate(port, baudrate):
232 raise SerialException("sorry don't know how to handle non standard baud rate on this platform")
cliechti99220a02009-08-14 00:21:25 +0000233 baudrate_constants = {}
cliechtid6bf52c2003-10-01 02:28:12 +0000234 #~ raise Exception, "this module does not run on this platform, sorry."
cliechti89b4af12002-02-12 23:24:41 +0000235
cliechti58b481c2009-02-16 20:42:32 +0000236# whats up with "aix", "beos", ....
237# they should work, just need to know the device names.
cliechti89b4af12002-02-12 23:24:41 +0000238
239
cliechti58b481c2009-02-16 20:42:32 +0000240# load some constants for later use.
241# try to use values from TERMIOS, use defaults from linux otherwise
cliechti8901aef2002-11-19 01:15:05 +0000242TIOCMGET = hasattr(TERMIOS, 'TIOCMGET') and TERMIOS.TIOCMGET or 0x5415
243TIOCMBIS = hasattr(TERMIOS, 'TIOCMBIS') and TERMIOS.TIOCMBIS or 0x5416
244TIOCMBIC = hasattr(TERMIOS, 'TIOCMBIC') and TERMIOS.TIOCMBIC or 0x5417
245TIOCMSET = hasattr(TERMIOS, 'TIOCMSET') and TERMIOS.TIOCMSET or 0x5418
cliechti89b4af12002-02-12 23:24:41 +0000246
cliechti8901aef2002-11-19 01:15:05 +0000247#TIOCM_LE = hasattr(TERMIOS, 'TIOCM_LE') and TERMIOS.TIOCM_LE or 0x001
248TIOCM_DTR = hasattr(TERMIOS, 'TIOCM_DTR') and TERMIOS.TIOCM_DTR or 0x002
249TIOCM_RTS = hasattr(TERMIOS, 'TIOCM_RTS') and TERMIOS.TIOCM_RTS or 0x004
250#TIOCM_ST = hasattr(TERMIOS, 'TIOCM_ST') and TERMIOS.TIOCM_ST or 0x008
251#TIOCM_SR = hasattr(TERMIOS, 'TIOCM_SR') and TERMIOS.TIOCM_SR or 0x010
cliechti89b4af12002-02-12 23:24:41 +0000252
cliechti8901aef2002-11-19 01:15:05 +0000253TIOCM_CTS = hasattr(TERMIOS, 'TIOCM_CTS') and TERMIOS.TIOCM_CTS or 0x020
254TIOCM_CAR = hasattr(TERMIOS, 'TIOCM_CAR') and TERMIOS.TIOCM_CAR or 0x040
255TIOCM_RNG = hasattr(TERMIOS, 'TIOCM_RNG') and TERMIOS.TIOCM_RNG or 0x080
256TIOCM_DSR = hasattr(TERMIOS, 'TIOCM_DSR') and TERMIOS.TIOCM_DSR or 0x100
257TIOCM_CD = hasattr(TERMIOS, 'TIOCM_CD') and TERMIOS.TIOCM_CD or TIOCM_CAR
258TIOCM_RI = hasattr(TERMIOS, 'TIOCM_RI') and TERMIOS.TIOCM_RI or TIOCM_RNG
259#TIOCM_OUT1 = hasattr(TERMIOS, 'TIOCM_OUT1') and TERMIOS.TIOCM_OUT1 or 0x2000
260#TIOCM_OUT2 = hasattr(TERMIOS, 'TIOCM_OUT2') and TERMIOS.TIOCM_OUT2 or 0x4000
cliechti28b8fd02011-12-28 21:39:42 +0000261if hasattr(TERMIOS, 'TIOCINQ'):
262 TIOCINQ = TERMIOS.TIOCINQ
263else:
264 TIOCINQ = hasattr(TERMIOS, 'FIONREAD') and TERMIOS.FIONREAD or 0x541B
265TIOCOUTQ = hasattr(TERMIOS, 'TIOCOUTQ') and TERMIOS.TIOCOUTQ or 0x5411
cliechti89b4af12002-02-12 23:24:41 +0000266
267TIOCM_zero_str = struct.pack('I', 0)
268TIOCM_RTS_str = struct.pack('I', TIOCM_RTS)
269TIOCM_DTR_str = struct.pack('I', TIOCM_DTR)
270
cliechti997b63c2008-06-21 00:09:31 +0000271TIOCSBRK = hasattr(TERMIOS, 'TIOCSBRK') and TERMIOS.TIOCSBRK or 0x5427
272TIOCCBRK = hasattr(TERMIOS, 'TIOCCBRK') and TERMIOS.TIOCCBRK or 0x5428
273
Chris Liechti68340d72015-08-03 14:15:48 +0200274CMSPAR = 0o10000000000 # Use "stick" (mark/space) parity
cliechtiaec27ab2014-07-31 22:21:24 +0000275
cliechti89b4af12002-02-12 23:24:41 +0000276
Chris Liechtiaf6d0462015-08-03 17:21:14 +0200277class Serial(SerialBase, io.RawIOBase):
cliechti7d448562014-08-03 21:57:45 +0000278 """\
279 Serial port class POSIX implementation. Serial port configuration is
cliechtid6bf52c2003-10-01 02:28:12 +0000280 done with termios and fcntl. Runs on Linux and many other Un*x like
cliechtif0a81d42014-08-04 14:03:53 +0000281 systems.
282 """
cliechtid6bf52c2003-10-01 02:28:12 +0000283
284 def open(self):
cliechti7d448562014-08-03 21:57:45 +0000285 """\
286 Open port with current settings. This may throw a SerialException
287 if the port cannot be opened."""
cliechtid6bf52c2003-10-01 02:28:12 +0000288 if self._port is None:
289 raise SerialException("Port must be configured before it can be used.")
cliechti02ef43a2011-03-24 23:33:12 +0000290 if self._isOpen:
291 raise SerialException("Port is already open.")
292 self.fd = None
cliechti58b481c2009-02-16 20:42:32 +0000293 # open
cliechti4616bf12002-04-08 23:13:14 +0000294 try:
295 self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK)
Chris Liechti68340d72015-08-03 14:15:48 +0200296 except OSError as msg:
cliechti4616bf12002-04-08 23:13:14 +0000297 self.fd = None
cliechtiaf84daa2013-10-10 23:57:00 +0000298 raise SerialException(msg.errno, "could not open port %s: %s" % (self._port, msg))
cliechti2750b832009-07-28 00:13:52 +0000299 #~ fcntl.fcntl(self.fd, FCNTL.F_SETFL, 0) # set blocking
cliechti58b481c2009-02-16 20:42:32 +0000300
cliechtib2f5fc82006-10-20 00:09:07 +0000301 try:
302 self._reconfigurePort()
303 except:
cliechti2750b832009-07-28 00:13:52 +0000304 try:
305 os.close(self.fd)
306 except:
307 # ignore any exception when closing the port
308 # also to keep original exception that happened when setting up
309 pass
cliechtib2f5fc82006-10-20 00:09:07 +0000310 self.fd = None
cliechtif0a4f0f2009-07-21 21:12:37 +0000311 raise
cliechtib2f5fc82006-10-20 00:09:07 +0000312 else:
313 self._isOpen = True
cliechti5c9b0722013-10-17 03:19:39 +0000314 self.flushInput()
cliechti58b481c2009-02-16 20:42:32 +0000315
316
cliechtid6bf52c2003-10-01 02:28:12 +0000317 def _reconfigurePort(self):
cliechtib2f5fc82006-10-20 00:09:07 +0000318 """Set communication parameters on opened port."""
cliechtic6178262004-03-22 22:04:52 +0000319 if self.fd is None:
cliechtia9a093e2010-01-02 03:05:08 +0000320 raise SerialException("Can only operate on a valid file descriptor")
cliechtie8c45422008-06-20 23:23:14 +0000321 custom_baud = None
cliechti58b481c2009-02-16 20:42:32 +0000322
cliechti2750b832009-07-28 00:13:52 +0000323 vmin = vtime = 0 # timeout is done via select
cliechti679bfa62008-06-20 23:58:15 +0000324 if self._interCharTimeout is not None:
325 vmin = 1
326 vtime = int(self._interCharTimeout * 10)
cliechti6ce7ab12002-11-07 02:15:00 +0000327 try:
cliechti4d0af5e2011-08-05 02:18:16 +0000328 orig_attr = termios.tcgetattr(self.fd)
329 iflag, oflag, cflag, lflag, ispeed, ospeed, cc = orig_attr
Chris Liechti68340d72015-08-03 14:15:48 +0200330 except termios.error as msg: # if a port is nonexistent but has a /dev file, it'll fail here
cliechtid6bf52c2003-10-01 02:28:12 +0000331 raise SerialException("Could not configure port: %s" % msg)
cliechti58b481c2009-02-16 20:42:32 +0000332 # set up raw mode / no echo / binary
cliechtid6bf52c2003-10-01 02:28:12 +0000333 cflag |= (TERMIOS.CLOCAL|TERMIOS.CREAD)
334 lflag &= ~(TERMIOS.ICANON|TERMIOS.ECHO|TERMIOS.ECHOE|TERMIOS.ECHOK|TERMIOS.ECHONL|
cliechti835996a2004-06-02 19:45:07 +0000335 TERMIOS.ISIG|TERMIOS.IEXTEN) #|TERMIOS.ECHOPRT
cliechti2750b832009-07-28 00:13:52 +0000336 for flag in ('ECHOCTL', 'ECHOKE'): # netbsd workaround for Erk
cliechti835996a2004-06-02 19:45:07 +0000337 if hasattr(TERMIOS, flag):
338 lflag &= ~getattr(TERMIOS, flag)
cliechti58b481c2009-02-16 20:42:32 +0000339
cliechtid6bf52c2003-10-01 02:28:12 +0000340 oflag &= ~(TERMIOS.OPOST)
cliechti895e8302004-04-20 02:40:28 +0000341 iflag &= ~(TERMIOS.INLCR|TERMIOS.IGNCR|TERMIOS.ICRNL|TERMIOS.IGNBRK)
cliechti89b4af12002-02-12 23:24:41 +0000342 if hasattr(TERMIOS, 'IUCLC'):
cliechti895e8302004-04-20 02:40:28 +0000343 iflag &= ~TERMIOS.IUCLC
cliechti3e57b3d2005-08-12 21:04:44 +0000344 if hasattr(TERMIOS, 'PARMRK'):
345 iflag &= ~TERMIOS.PARMRK
cliechti58b481c2009-02-16 20:42:32 +0000346
cliechtif0a4f0f2009-07-21 21:12:37 +0000347 # setup baud rate
cliechti89b4af12002-02-12 23:24:41 +0000348 try:
cliechti2750b832009-07-28 00:13:52 +0000349 ispeed = ospeed = getattr(TERMIOS, 'B%s' % (self._baudrate))
cliechti895e8302004-04-20 02:40:28 +0000350 except AttributeError:
cliechtif1559d02007-11-08 23:43:58 +0000351 try:
352 ispeed = ospeed = baudrate_constants[self._baudrate]
353 except KeyError:
cliechtie8c45422008-06-20 23:23:14 +0000354 #~ raise ValueError('Invalid baud rate: %r' % self._baudrate)
cliechtif0a4f0f2009-07-21 21:12:37 +0000355 # may need custom baud rate, it isn't in our list.
cliechtie8c45422008-06-20 23:23:14 +0000356 ispeed = ospeed = getattr(TERMIOS, 'B38400')
cliechtif0a4f0f2009-07-21 21:12:37 +0000357 try:
358 custom_baud = int(self._baudrate) # store for later
359 except ValueError:
360 raise ValueError('Invalid baud rate: %r' % self._baudrate)
361 else:
362 if custom_baud < 0:
363 raise ValueError('Invalid baud rate: %r' % self._baudrate)
cliechti58b481c2009-02-16 20:42:32 +0000364
365 # setup char len
cliechtid6bf52c2003-10-01 02:28:12 +0000366 cflag &= ~TERMIOS.CSIZE
367 if self._bytesize == 8:
368 cflag |= TERMIOS.CS8
369 elif self._bytesize == 7:
370 cflag |= TERMIOS.CS7
371 elif self._bytesize == 6:
372 cflag |= TERMIOS.CS6
373 elif self._bytesize == 5:
374 cflag |= TERMIOS.CS5
cliechti89b4af12002-02-12 23:24:41 +0000375 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000376 raise ValueError('Invalid char len: %r' % self._bytesize)
cliechtif0a81d42014-08-04 14:03:53 +0000377 # setup stop bits
cliechtid6bf52c2003-10-01 02:28:12 +0000378 if self._stopbits == STOPBITS_ONE:
379 cflag &= ~(TERMIOS.CSTOPB)
cliechti58b481c2009-02-16 20:42:32 +0000380 elif self._stopbits == STOPBITS_ONE_POINT_FIVE:
381 cflag |= (TERMIOS.CSTOPB) # XXX same as TWO.. there is no POSIX support for 1.5
cliechtid6bf52c2003-10-01 02:28:12 +0000382 elif self._stopbits == STOPBITS_TWO:
383 cflag |= (TERMIOS.CSTOPB)
cliechti89b4af12002-02-12 23:24:41 +0000384 else:
cliechti3172d3d2009-07-21 22:33:40 +0000385 raise ValueError('Invalid stop bit specification: %r' % self._stopbits)
cliechti58b481c2009-02-16 20:42:32 +0000386 # setup parity
cliechtid6bf52c2003-10-01 02:28:12 +0000387 iflag &= ~(TERMIOS.INPCK|TERMIOS.ISTRIP)
388 if self._parity == PARITY_NONE:
389 cflag &= ~(TERMIOS.PARENB|TERMIOS.PARODD)
390 elif self._parity == PARITY_EVEN:
391 cflag &= ~(TERMIOS.PARODD)
392 cflag |= (TERMIOS.PARENB)
393 elif self._parity == PARITY_ODD:
394 cflag |= (TERMIOS.PARENB|TERMIOS.PARODD)
cliechtiaec27ab2014-07-31 22:21:24 +0000395 elif self._parity == PARITY_MARK and plat[:5] == 'linux':
396 cflag |= (TERMIOS.PARENB|CMSPAR|TERMIOS.PARODD)
397 elif self._parity == PARITY_SPACE and plat[:5] == 'linux':
398 cflag |= (TERMIOS.PARENB|CMSPAR)
399 cflag &= ~(TERMIOS.PARODD)
cliechti89b4af12002-02-12 23:24:41 +0000400 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000401 raise ValueError('Invalid parity: %r' % self._parity)
cliechti58b481c2009-02-16 20:42:32 +0000402 # setup flow control
403 # xonxoff
cliechti89b4af12002-02-12 23:24:41 +0000404 if hasattr(TERMIOS, 'IXANY'):
cliechtid6bf52c2003-10-01 02:28:12 +0000405 if self._xonxoff:
cliechti62611612004-04-20 01:55:43 +0000406 iflag |= (TERMIOS.IXON|TERMIOS.IXOFF) #|TERMIOS.IXANY)
cliechti89b4af12002-02-12 23:24:41 +0000407 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000408 iflag &= ~(TERMIOS.IXON|TERMIOS.IXOFF|TERMIOS.IXANY)
cliechti89b4af12002-02-12 23:24:41 +0000409 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000410 if self._xonxoff:
411 iflag |= (TERMIOS.IXON|TERMIOS.IXOFF)
cliechti89b4af12002-02-12 23:24:41 +0000412 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000413 iflag &= ~(TERMIOS.IXON|TERMIOS.IXOFF)
cliechti58b481c2009-02-16 20:42:32 +0000414 # rtscts
cliechti89b4af12002-02-12 23:24:41 +0000415 if hasattr(TERMIOS, 'CRTSCTS'):
cliechtid6bf52c2003-10-01 02:28:12 +0000416 if self._rtscts:
417 cflag |= (TERMIOS.CRTSCTS)
cliechti89b4af12002-02-12 23:24:41 +0000418 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000419 cflag &= ~(TERMIOS.CRTSCTS)
cliechti2750b832009-07-28 00:13:52 +0000420 elif hasattr(TERMIOS, 'CNEW_RTSCTS'): # try it with alternate constant name
cliechtid6bf52c2003-10-01 02:28:12 +0000421 if self._rtscts:
422 cflag |= (TERMIOS.CNEW_RTSCTS)
cliechtid4743692002-04-08 22:39:53 +0000423 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000424 cflag &= ~(TERMIOS.CNEW_RTSCTS)
cliechti2750b832009-07-28 00:13:52 +0000425 # XXX should there be a warning if setting up rtscts (and xonxoff etc) fails??
cliechti58b481c2009-02-16 20:42:32 +0000426
427 # buffer
cliechtif0a81d42014-08-04 14:03:53 +0000428 # vmin "minimal number of characters to be read. 0 for non blocking"
cliechtid6bf52c2003-10-01 02:28:12 +0000429 if vmin < 0 or vmin > 255:
430 raise ValueError('Invalid vmin: %r ' % vmin)
431 cc[TERMIOS.VMIN] = vmin
cliechti58b481c2009-02-16 20:42:32 +0000432 # vtime
cliechtid6bf52c2003-10-01 02:28:12 +0000433 if vtime < 0 or vtime > 255:
434 raise ValueError('Invalid vtime: %r' % vtime)
435 cc[TERMIOS.VTIME] = vtime
cliechti58b481c2009-02-16 20:42:32 +0000436 # activate settings
cliechti4d0af5e2011-08-05 02:18:16 +0000437 if [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] != orig_attr:
438 termios.tcsetattr(self.fd, TERMIOS.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
cliechti58b481c2009-02-16 20:42:32 +0000439
cliechtie8c45422008-06-20 23:23:14 +0000440 # apply custom baud rate, if any
441 if custom_baud is not None:
cliechti53c9fd42009-07-23 23:51:51 +0000442 set_special_baudrate(self, custom_baud)
cliechti89b4af12002-02-12 23:24:41 +0000443
444 def close(self):
cliechtid6bf52c2003-10-01 02:28:12 +0000445 """Close port"""
446 if self._isOpen:
cliechtic6178262004-03-22 22:04:52 +0000447 if self.fd is not None:
cliechtid6bf52c2003-10-01 02:28:12 +0000448 os.close(self.fd)
449 self.fd = None
450 self._isOpen = False
cliechti89b4af12002-02-12 23:24:41 +0000451
cliechtid6bf52c2003-10-01 02:28:12 +0000452 def makeDeviceName(self, port):
453 return device(port)
454
455 # - - - - - - - - - - - - - - - - - - - - - - - -
cliechti95c62212002-03-04 22:17:53 +0000456
cliechti89b4af12002-02-12 23:24:41 +0000457 def inWaiting(self):
cliechtid6bf52c2003-10-01 02:28:12 +0000458 """Return the number of characters currently in the input buffer."""
cliechtif5831e02002-12-05 23:15:27 +0000459 #~ s = fcntl.ioctl(self.fd, TERMIOS.FIONREAD, TIOCM_zero_str)
460 s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
cliechti89b4af12002-02-12 23:24:41 +0000461 return struct.unpack('I',s)[0]
462
cliechtia9a093e2010-01-02 03:05:08 +0000463 # select based implementation, proved to work on many systems
464 def read(self, size=1):
cliechti7d448562014-08-03 21:57:45 +0000465 """\
466 Read size bytes from the serial port. If a timeout is set it may
467 return less characters as requested. With no timeout it will block
468 until the requested number of bytes is read.
469 """
cliechti899c9c42011-06-14 23:01:23 +0000470 if not self._isOpen: raise portNotOpenError
cliechtia9a093e2010-01-02 03:05:08 +0000471 read = bytearray()
472 while len(read) < size:
cliechti8d744de2013-10-11 14:31:13 +0000473 try:
474 ready,_,_ = select.select([self.fd],[],[], self._timeout)
475 # If select was used with a timeout, and the timeout occurs, it
476 # returns with empty lists -> thus abort read operation.
477 # For timeout == 0 (non-blocking operation) also abort when there
478 # is nothing to read.
479 if not ready:
480 break # timeout
481 buf = os.read(self.fd, size-len(read))
482 # read should always return some data as select reported it was
483 # ready to read when we get to this point.
484 if not buf:
485 # Disconnected devices, at least on Linux, show the
486 # behavior that they are always ready to read immediately
487 # but reading returns nothing.
488 raise SerialException('device reports readiness to read but returned no data (device disconnected or multiple access on port?)')
489 read.extend(buf)
Chris Liechti68340d72015-08-03 14:15:48 +0200490 except OSError as e:
cliechtic7cd7212014-08-03 21:34:38 +0000491 # this is for Python 3.x where select.error is a subclass of OSError
492 # ignore EAGAIN errors. all other errors are shown
493 if e.errno != errno.EAGAIN:
494 raise SerialException('read failed: %s' % (e,))
Chris Liechti68340d72015-08-03 14:15:48 +0200495 except select.error as e:
cliechtic7cd7212014-08-03 21:34:38 +0000496 # this is for Python 2.x
cliechti8d744de2013-10-11 14:31:13 +0000497 # ignore EAGAIN errors. all other errors are shown
498 # see also http://www.python.org/dev/peps/pep-3151/#select
499 if e[0] != errno.EAGAIN:
500 raise SerialException('read failed: %s' % (e,))
cliechtia9a093e2010-01-02 03:05:08 +0000501 return bytes(read)
cliechti89b4af12002-02-12 23:24:41 +0000502
cliechti4a567a02009-07-27 22:09:31 +0000503 def write(self, data):
cliechtid6bf52c2003-10-01 02:28:12 +0000504 """Output the given string over the serial port."""
cliechti899c9c42011-06-14 23:01:23 +0000505 if not self._isOpen: raise portNotOpenError
cliechti38077122013-10-16 02:57:27 +0000506 d = to_bytes(data)
507 tx_len = len(d)
cliechti3cf46d62009-08-07 00:19:57 +0000508 if self._writeTimeout is not None and self._writeTimeout > 0:
509 timeout = time.time() + self._writeTimeout
510 else:
511 timeout = None
cliechti9f7c2352013-10-11 01:13:46 +0000512 while tx_len > 0:
cliechti5d4d0bd2004-11-13 03:27:39 +0000513 try:
cliechti5d4d0bd2004-11-13 03:27:39 +0000514 n = os.write(self.fd, d)
cliechti3cf46d62009-08-07 00:19:57 +0000515 if timeout:
516 # when timeout is set, use select to wait for being ready
517 # with the time left as timeout
518 timeleft = timeout - time.time()
519 if timeleft < 0:
520 raise writeTimeoutError
521 _, ready, _ = select.select([], [self.fd], [], timeleft)
cliechti5d4d0bd2004-11-13 03:27:39 +0000522 if not ready:
523 raise writeTimeoutError
cliechti88c62442013-10-12 04:03:16 +0000524 else:
525 # wait for write operation
526 _, ready, _ = select.select([], [self.fd], [], None)
527 if not ready:
528 raise SerialException('write failed (select)')
cliechti5d4d0bd2004-11-13 03:27:39 +0000529 d = d[n:]
cliechti9f7c2352013-10-11 01:13:46 +0000530 tx_len -= n
Chris Liechti675f7e12015-08-03 15:48:41 +0200531 except SerialException:
532 raise
Chris Liechti68340d72015-08-03 14:15:48 +0200533 except OSError as v:
cliechti5d4d0bd2004-11-13 03:27:39 +0000534 if v.errno != errno.EAGAIN:
cliechti65722c92009-08-07 00:48:53 +0000535 raise SerialException('write failed: %s' % (v,))
cliechtif81362e2009-07-25 03:44:33 +0000536 return len(data)
cliechtid6bf52c2003-10-01 02:28:12 +0000537
cliechtia30a8a02003-10-05 12:28:13 +0000538 def flush(self):
cliechti7d448562014-08-03 21:57:45 +0000539 """\
540 Flush of file like objects. In this case, wait until all data
541 is written.
542 """
cliechtia30a8a02003-10-05 12:28:13 +0000543 self.drainOutput()
544
cliechti89b4af12002-02-12 23:24:41 +0000545 def flushInput(self):
cliechtid6bf52c2003-10-01 02:28:12 +0000546 """Clear input buffer, discarding all that is in the buffer."""
cliechti899c9c42011-06-14 23:01:23 +0000547 if not self._isOpen: raise portNotOpenError
cliechti89b4af12002-02-12 23:24:41 +0000548 termios.tcflush(self.fd, TERMIOS.TCIFLUSH)
549
550 def flushOutput(self):
cliechti7d448562014-08-03 21:57:45 +0000551 """\
552 Clear output buffer, aborting the current output and discarding all
553 that is in the buffer.
554 """
cliechti899c9c42011-06-14 23:01:23 +0000555 if not self._isOpen: raise portNotOpenError
cliechti89b4af12002-02-12 23:24:41 +0000556 termios.tcflush(self.fd, TERMIOS.TCOFLUSH)
557
cliechtiaaa04602006-02-05 23:02:46 +0000558 def sendBreak(self, duration=0.25):
cliechti7d448562014-08-03 21:57:45 +0000559 """\
560 Send break condition. Timed, returns to idle state after given
561 duration.
562 """
cliechti899c9c42011-06-14 23:01:23 +0000563 if not self._isOpen: raise portNotOpenError
cliechtiaaa04602006-02-05 23:02:46 +0000564 termios.tcsendbreak(self.fd, int(duration/0.25))
cliechti89b4af12002-02-12 23:24:41 +0000565
cliechti997b63c2008-06-21 00:09:31 +0000566 def setBreak(self, level=1):
cliechti7d448562014-08-03 21:57:45 +0000567 """\
568 Set break: Controls TXD. When active, no transmitting is possible.
569 """
cliechti997b63c2008-06-21 00:09:31 +0000570 if self.fd is None: raise portNotOpenError
571 if level:
572 fcntl.ioctl(self.fd, TIOCSBRK)
573 else:
574 fcntl.ioctl(self.fd, TIOCCBRK)
575
cliechti93db61b2006-08-26 19:16:18 +0000576 def setRTS(self, level=1):
cliechtid6bf52c2003-10-01 02:28:12 +0000577 """Set terminal status line: Request To Send"""
cliechti899c9c42011-06-14 23:01:23 +0000578 if not self._isOpen: raise portNotOpenError
cliechtib2f5fc82006-10-20 00:09:07 +0000579 if level:
cliechtid6bf52c2003-10-01 02:28:12 +0000580 fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_RTS_str)
581 else:
582 fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_RTS_str)
583
cliechti93db61b2006-08-26 19:16:18 +0000584 def setDTR(self, level=1):
cliechtid6bf52c2003-10-01 02:28:12 +0000585 """Set terminal status line: Data Terminal Ready"""
cliechti899c9c42011-06-14 23:01:23 +0000586 if not self._isOpen: raise portNotOpenError
cliechtib2f5fc82006-10-20 00:09:07 +0000587 if level:
cliechtid6bf52c2003-10-01 02:28:12 +0000588 fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
589 else:
590 fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str)
591
592 def getCTS(self):
593 """Read terminal status line: Clear To Send"""
cliechti899c9c42011-06-14 23:01:23 +0000594 if not self._isOpen: raise portNotOpenError
cliechtid6bf52c2003-10-01 02:28:12 +0000595 s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
596 return struct.unpack('I',s)[0] & TIOCM_CTS != 0
597
598 def getDSR(self):
599 """Read terminal status line: Data Set Ready"""
cliechti899c9c42011-06-14 23:01:23 +0000600 if not self._isOpen: raise portNotOpenError
cliechtid6bf52c2003-10-01 02:28:12 +0000601 s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
602 return struct.unpack('I',s)[0] & TIOCM_DSR != 0
603
cliechtid6bf52c2003-10-01 02:28:12 +0000604 def getRI(self):
605 """Read terminal status line: Ring Indicator"""
cliechti899c9c42011-06-14 23:01:23 +0000606 if not self._isOpen: raise portNotOpenError
cliechti89b4af12002-02-12 23:24:41 +0000607 s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
cliechtid6bf52c2003-10-01 02:28:12 +0000608 return struct.unpack('I',s)[0] & TIOCM_RI != 0
cliechti89b4af12002-02-12 23:24:41 +0000609
610 def getCD(self):
cliechtid6bf52c2003-10-01 02:28:12 +0000611 """Read terminal status line: Carrier Detect"""
cliechti899c9c42011-06-14 23:01:23 +0000612 if not self._isOpen: raise portNotOpenError
cliechti89b4af12002-02-12 23:24:41 +0000613 s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
cliechtid6bf52c2003-10-01 02:28:12 +0000614 return struct.unpack('I',s)[0] & TIOCM_CD != 0
cliechti89b4af12002-02-12 23:24:41 +0000615
cliechtia30a8a02003-10-05 12:28:13 +0000616 # - - platform specific - - - -
617
cliechti28b8fd02011-12-28 21:39:42 +0000618 def outWaiting(self):
619 """Return the number of characters currently in the output buffer."""
620 #~ s = fcntl.ioctl(self.fd, TERMIOS.FIONREAD, TIOCM_zero_str)
621 s = fcntl.ioctl(self.fd, TIOCOUTQ, TIOCM_zero_str)
622 return struct.unpack('I',s)[0]
623
cliechtia30a8a02003-10-05 12:28:13 +0000624 def drainOutput(self):
625 """internal - not portable!"""
cliechti899c9c42011-06-14 23:01:23 +0000626 if not self._isOpen: raise portNotOpenError
cliechtia30a8a02003-10-05 12:28:13 +0000627 termios.tcdrain(self.fd)
628
629 def nonblocking(self):
630 """internal - not portable!"""
cliechti899c9c42011-06-14 23:01:23 +0000631 if not self._isOpen: raise portNotOpenError
cliechti198b7e72010-05-25 00:38:23 +0000632 fcntl.fcntl(self.fd, FCNTL.F_SETFL, os.O_NONBLOCK)
cliechtia30a8a02003-10-05 12:28:13 +0000633
cliechti8753bbc2005-01-15 20:32:51 +0000634 def fileno(self):
cliechti2f0f8a32011-12-28 22:10:00 +0000635 """\
636 For easier use of the serial port instance with select.
637 WARNING: this function is not portable to different platforms!
638 """
cliechti899c9c42011-06-14 23:01:23 +0000639 if not self._isOpen: raise portNotOpenError
cliechti8753bbc2005-01-15 20:32:51 +0000640 return self.fd
cliechti89b4af12002-02-12 23:24:41 +0000641
cliechti2f0f8a32011-12-28 22:10:00 +0000642 def setXON(self, level=True):
643 """\
644 Manually control flow - when software flow control is enabled.
645 This will send XON (true) and XOFF (false) to the other device.
646 WARNING: this function is not portable to different platforms!
647 """
Chris Liechti905d5072015-08-03 21:41:49 +0200648 if not self._isOpen: raise portNotOpenError
cliechti4a601342011-12-29 02:22:17 +0000649 if enable:
cliechti57e48a62009-08-03 22:29:58 +0000650 termios.tcflow(self.fd, TERMIOS.TCION)
651 else:
652 termios.tcflow(self.fd, TERMIOS.TCIOFF)
653
cliechti2f0f8a32011-12-28 22:10:00 +0000654 def flowControlOut(self, enable):
655 """\
656 Manually control flow of outgoing data - when hardware or software flow
657 control is enabled.
658 WARNING: this function is not portable to different platforms!
659 """
660 if not self._isOpen: raise portNotOpenError
661 if enable:
662 termios.tcflow(self.fd, TERMIOS.TCOON)
663 else:
664 termios.tcflow(self.fd, TERMIOS.TCOOFF)
665
cliechtif81362e2009-07-25 03:44:33 +0000666
cliechtif81362e2009-07-25 03:44:33 +0000667
cliechtia9a093e2010-01-02 03:05:08 +0000668class PosixPollSerial(Serial):
cliechti7d448562014-08-03 21:57:45 +0000669 """\
cliechtif0a81d42014-08-04 14:03:53 +0000670 Poll based read implementation. Not all systems support poll properly.
671 However this one has better handling of errors, such as a device
cliechti7d448562014-08-03 21:57:45 +0000672 disconnecting while it's in use (e.g. USB-serial unplugged).
673 """
cliechtia9a093e2010-01-02 03:05:08 +0000674
675 def read(self, size=1):
cliechti7d448562014-08-03 21:57:45 +0000676 """\
677 Read size bytes from the serial port. If a timeout is set it may
678 return less characters as requested. With no timeout it will block
679 until the requested number of bytes is read.
680 """
cliechtia9a093e2010-01-02 03:05:08 +0000681 if self.fd is None: raise portNotOpenError
682 read = bytearray()
683 poll = select.poll()
684 poll.register(self.fd, select.POLLIN|select.POLLERR|select.POLLHUP|select.POLLNVAL)
685 if size > 0:
686 while len(read) < size:
687 # print "\tread(): size",size, "have", len(read) #debug
688 # wait until device becomes ready to read (or something fails)
cliechti4cd0d2e2010-07-21 01:15:25 +0000689 for fd, event in poll.poll(self._timeout*1000):
cliechtia9a093e2010-01-02 03:05:08 +0000690 if event & (select.POLLERR|select.POLLHUP|select.POLLNVAL):
691 raise SerialException('device reports error (poll)')
692 # we don't care if it is select.POLLIN or timeout, that's
693 # handled below
694 buf = os.read(self.fd, size - len(read))
695 read.extend(buf)
696 if ((self._timeout is not None and self._timeout >= 0) or
697 (self._interCharTimeout is not None and self._interCharTimeout > 0)) and not buf:
698 break # early abort on timeout
699 return bytes(read)
700
cliechtif81362e2009-07-25 03:44:33 +0000701
cliechti89b4af12002-02-12 23:24:41 +0000702if __name__ == '__main__':
703 s = Serial(0,
cliechti53c9fd42009-07-23 23:51:51 +0000704 baudrate=19200, # baud rate
705 bytesize=EIGHTBITS, # number of data bits
cliechti3172d3d2009-07-21 22:33:40 +0000706 parity=PARITY_EVEN, # enable parity checking
cliechti53c9fd42009-07-23 23:51:51 +0000707 stopbits=STOPBITS_ONE, # number of stop bits
cliechti3172d3d2009-07-21 22:33:40 +0000708 timeout=3, # set a timeout value, None for waiting forever
709 xonxoff=0, # enable software flow control
710 rtscts=0, # enable RTS/CTS flow control
cliechti89b4af12002-02-12 23:24:41 +0000711 )
712 s.setRTS(1)
713 s.setDTR(1)
714 s.flushInput()
715 s.flushOutput()
716 s.write('hello')
cliechti109486b2009-08-02 00:00:11 +0000717 sys.stdout.write('%r\n' % s.read(5))
718 sys.stdout.write('%s\n' % s.inWaiting())
cliechti89b4af12002-02-12 23:24:41 +0000719 del s
cliechti4569bac2007-11-08 21:57:19 +0000720