blob: fed0d2e0a6d0cfdb749135025e73e6a7715b6d6d [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
cliechti39cfb7b2011-08-22 00:30:07 +000016from serial.serialutil import *
cliechti89b4af12002-02-12 23:24:41 +000017
cliechti53c9fd42009-07-23 23:51:51 +000018# Do check the Python version as some constants have moved.
cliechti89b4af12002-02-12 23:24:41 +000019if (sys.hexversion < 0x020100f0):
20 import TERMIOS
21else:
22 TERMIOS = termios
23
24if (sys.hexversion < 0x020200f0):
25 import FCNTL
26else:
27 FCNTL = fcntl
28
cliechti3172d3d2009-07-21 22:33:40 +000029# try to detect the OS so that a device can be selected...
cliechti53c9fd42009-07-23 23:51:51 +000030# this code block should supply a device() and set_special_baudrate() function
31# for the platform
cliechtid6bf52c2003-10-01 02:28:12 +000032plat = sys.platform.lower()
cliechti89b4af12002-02-12 23:24:41 +000033
cliechti3172d3d2009-07-21 22:33:40 +000034if plat[:5] == 'linux': # Linux (confirmed)
cliechti53c9fd42009-07-23 23:51:51 +000035
cliechti89b4af12002-02-12 23:24:41 +000036 def device(port):
37 return '/dev/ttyS%d' % port
38
cliechti1877c522013-10-11 14:02:26 +000039 TCGETS2 = 0x802C542A
40 TCSETS2 = 0x402C542B
41 BOTHER = 0o010000
cliechti53c9fd42009-07-23 23:51:51 +000042
43 def set_special_baudrate(port, baudrate):
cliechti1877c522013-10-11 14:02:26 +000044 # right size is 44 on x86_64, allow for some growth
cliechti53c9fd42009-07-23 23:51:51 +000045 import array
cliechti1877c522013-10-11 14:02:26 +000046 buf = array.array('i', [0] * 64)
cliechti53c9fd42009-07-23 23:51:51 +000047
cliechti53c9fd42009-07-23 23:51:51 +000048 try:
cliechti95772d32013-10-11 14:38:13 +000049 # get serial_struct
50 FCNTL.ioctl(port.fd, TCGETS2, buf)
51 # set custom speed
52 buf[2] &= ~TERMIOS.CBAUD
53 buf[2] |= BOTHER
54 buf[9] = buf[10] = baudrate
55
56 # set serial_struct
cliechti1877c522013-10-11 14:02:26 +000057 res = FCNTL.ioctl(port.fd, TCSETS2, buf)
Chris Liechti68340d72015-08-03 14:15:48 +020058 except IOError as e:
cliechti95772d32013-10-11 14:38:13 +000059 raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e))
cliechti53c9fd42009-07-23 23:51:51 +000060
cliechti99220a02009-08-14 00:21:25 +000061 baudrate_constants = {
Chris Liechti68340d72015-08-03 14:15:48 +020062 0: 0o000000, # hang up
63 50: 0o000001,
64 75: 0o000002,
65 110: 0o000003,
66 134: 0o000004,
67 150: 0o000005,
68 200: 0o000006,
69 300: 0o000007,
70 600: 0o000010,
71 1200: 0o000011,
72 1800: 0o000012,
73 2400: 0o000013,
74 4800: 0o000014,
75 9600: 0o000015,
76 19200: 0o000016,
77 38400: 0o000017,
78 57600: 0o010001,
79 115200: 0o010002,
80 230400: 0o010003,
81 460800: 0o010004,
82 500000: 0o010005,
83 576000: 0o010006,
84 921600: 0o010007,
85 1000000: 0o010010,
86 1152000: 0o010011,
87 1500000: 0o010012,
88 2000000: 0o010013,
89 2500000: 0o010014,
90 3000000: 0o010015,
91 3500000: 0o010016,
92 4000000: 0o010017
cliechti99220a02009-08-14 00:21:25 +000093 }
94
cliechti53c9fd42009-07-23 23:51:51 +000095elif plat == 'cygwin': # cygwin/win32 (confirmed)
96
cliechtif281fde2002-06-07 21:53:40 +000097 def device(port):
cliechtif5831e02002-12-05 23:15:27 +000098 return '/dev/com%d' % (port + 1)
cliechtif281fde2002-06-07 21:53:40 +000099
cliechti53c9fd42009-07-23 23:51:51 +0000100 def set_special_baudrate(port, baudrate):
cliechti99220a02009-08-14 00:21:25 +0000101 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
cliechti53c9fd42009-07-23 23:51:51 +0000102
cliechtia505de32013-05-31 01:27:25 +0000103 baudrate_constants = {
104 128000: 0x01003,
105 256000: 0x01005,
106 500000: 0x01007,
107 576000: 0x01008,
108 921600: 0x01009,
109 1000000: 0x0100a,
110 1152000: 0x0100b,
111 1500000: 0x0100c,
112 2000000: 0x0100d,
113 2500000: 0x0100e,
114 3000000: 0x0100f
115 }
cliechti53c9fd42009-07-23 23:51:51 +0000116
cliechti4a601342011-12-29 02:22:17 +0000117elif plat[:7] == 'openbsd': # OpenBSD
cliechti53c9fd42009-07-23 23:51:51 +0000118
cliechti89b4af12002-02-12 23:24:41 +0000119 def device(port):
cliechti4a601342011-12-29 02:22:17 +0000120 return '/dev/cua%02d' % port
cliechti89b4af12002-02-12 23:24:41 +0000121
cliechti53c9fd42009-07-23 23:51:51 +0000122 def set_special_baudrate(port, baudrate):
123 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
124
cliechti99220a02009-08-14 00:21:25 +0000125 baudrate_constants = {}
126
cliechti89b4af12002-02-12 23:24:41 +0000127elif plat[:3] == 'bsd' or \
cliechti4a601342011-12-29 02:22:17 +0000128 plat[:7] == 'freebsd':
cliechti53c9fd42009-07-23 23:51:51 +0000129
cliechti89b4af12002-02-12 23:24:41 +0000130 def device(port):
cliechtieaa96882008-06-16 22:59:20 +0000131 return '/dev/cuad%d' % port
cliechti89b4af12002-02-12 23:24:41 +0000132
cliechti53c9fd42009-07-23 23:51:51 +0000133 def set_special_baudrate(port, baudrate):
134 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
135
cliechti99220a02009-08-14 00:21:25 +0000136 baudrate_constants = {}
137
cliechti53c9fd42009-07-23 23:51:51 +0000138elif plat[:6] == 'darwin': # OS X
139
140 version = os.uname()[2].split('.')
141 # Tiger or above can support arbitrary serial speeds
142 if int(version[0]) >= 8:
cliechti53c9fd42009-07-23 23:51:51 +0000143 def set_special_baudrate(port, baudrate):
144 # use IOKit-specific call to set up high speeds
145 import array, fcntl
146 buf = array.array('i', [baudrate])
147 IOSSIOSPEED = 0x80045402 #_IOW('T', 2, speed_t)
148 fcntl.ioctl(port.fd, IOSSIOSPEED, buf, 1)
149 else: # version < 8
150 def set_special_baudrate(port, baudrate):
151 raise ValueError("baud rate not supported")
152
153 def device(port):
154 return '/dev/cuad%d' % port
155
cliechti99220a02009-08-14 00:21:25 +0000156 baudrate_constants = {}
157
cliechti53c9fd42009-07-23 23:51:51 +0000158
cliechti3172d3d2009-07-21 22:33:40 +0000159elif plat[:6] == 'netbsd': # NetBSD 1.6 testing by Erk
cliechti53c9fd42009-07-23 23:51:51 +0000160
cliechti835996a2004-06-02 19:45:07 +0000161 def device(port):
162 return '/dev/dty%02d' % port
163
cliechti53c9fd42009-07-23 23:51:51 +0000164 def set_special_baudrate(port, baudrate):
165 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
166
cliechti99220a02009-08-14 00:21:25 +0000167 baudrate_constants = {}
168
cliechti3172d3d2009-07-21 22:33:40 +0000169elif plat[:4] == 'irix': # IRIX (partially tested)
cliechti53c9fd42009-07-23 23:51:51 +0000170
cliechti89b4af12002-02-12 23:24:41 +0000171 def device(port):
cliechtie2418e92006-06-05 20:03:17 +0000172 return '/dev/ttyf%d' % (port+1) #XXX different device names depending on flow control
cliechti89b4af12002-02-12 23:24:41 +0000173
cliechti53c9fd42009-07-23 23:51:51 +0000174 def set_special_baudrate(port, baudrate):
175 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
176
cliechti99220a02009-08-14 00:21:25 +0000177 baudrate_constants = {}
178
cliechti3172d3d2009-07-21 22:33:40 +0000179elif plat[:2] == 'hp': # HP-UX (not tested)
cliechti53c9fd42009-07-23 23:51:51 +0000180
cliechti89b4af12002-02-12 23:24:41 +0000181 def device(port):
182 return '/dev/tty%dp0' % (port+1)
183
cliechti53c9fd42009-07-23 23:51:51 +0000184 def set_special_baudrate(port, baudrate):
185 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
186
cliechti99220a02009-08-14 00:21:25 +0000187 baudrate_constants = {}
188
cliechti3172d3d2009-07-21 22:33:40 +0000189elif plat[:5] == 'sunos': # Solaris/SunOS (confirmed)
cliechti53c9fd42009-07-23 23:51:51 +0000190
cliechti89b4af12002-02-12 23:24:41 +0000191 def device(port):
192 return '/dev/tty%c' % (ord('a')+port)
cliechti58b481c2009-02-16 20:42:32 +0000193
cliechti53c9fd42009-07-23 23:51:51 +0000194 def set_special_baudrate(port, baudrate):
195 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
196
cliechti99220a02009-08-14 00:21:25 +0000197 baudrate_constants = {}
198
cliechti3172d3d2009-07-21 22:33:40 +0000199elif plat[:3] == 'aix': # AIX
cliechti53c9fd42009-07-23 23:51:51 +0000200
cliechti40e1b072005-03-12 12:05:26 +0000201 def device(port):
202 return '/dev/tty%d' % (port)
cliechti89b4af12002-02-12 23:24:41 +0000203
cliechti53c9fd42009-07-23 23:51:51 +0000204 def set_special_baudrate(port, baudrate):
205 raise ValueError("sorry don't know how to handle non standard baud rate on this platform")
206
cliechti99220a02009-08-14 00:21:25 +0000207 baudrate_constants = {}
208
cliechti89b4af12002-02-12 23:24:41 +0000209else:
cliechtia9a093e2010-01-02 03:05:08 +0000210 # platform detection has failed...
cliechti7aaead32009-07-23 14:02:41 +0000211 sys.stderr.write("""\
212don't know how to number ttys on this system.
cliechti895e8302004-04-20 02:40:28 +0000213! Use an explicit path (eg /dev/ttyS1) or send this information to
214! the author of this module:
cliechti89b4af12002-02-12 23:24:41 +0000215
cliechti895e8302004-04-20 02:40:28 +0000216sys.platform = %r
217os.name = %r
218serialposix.py version = %s
cliechti89b4af12002-02-12 23:24:41 +0000219
220also add the device name of the serial port and where the
221counting starts for the first serial port.
222e.g. 'first serial port: /dev/ttyS0'
223and with a bit luck you can get this module running...
cliechti7aaead32009-07-23 14:02:41 +0000224""" % (sys.platform, os.name, VERSION))
cliechti58b481c2009-02-16 20:42:32 +0000225 # no exception, just continue with a brave attempt to build a device name
226 # even if the device name is not correct for the platform it has chances
cliechti3172d3d2009-07-21 22:33:40 +0000227 # to work using a string with the real device name as port parameter.
cliechtid6bf52c2003-10-01 02:28:12 +0000228 def device(portum):
229 return '/dev/ttyS%d' % portnum
cliechti53c9fd42009-07-23 23:51:51 +0000230 def set_special_baudrate(port, baudrate):
231 raise SerialException("sorry don't know how to handle non standard baud rate on this platform")
cliechti99220a02009-08-14 00:21:25 +0000232 baudrate_constants = {}
cliechtid6bf52c2003-10-01 02:28:12 +0000233 #~ raise Exception, "this module does not run on this platform, sorry."
cliechti89b4af12002-02-12 23:24:41 +0000234
cliechti58b481c2009-02-16 20:42:32 +0000235# whats up with "aix", "beos", ....
236# they should work, just need to know the device names.
cliechti89b4af12002-02-12 23:24:41 +0000237
238
cliechti58b481c2009-02-16 20:42:32 +0000239# load some constants for later use.
240# try to use values from TERMIOS, use defaults from linux otherwise
cliechti8901aef2002-11-19 01:15:05 +0000241TIOCMGET = hasattr(TERMIOS, 'TIOCMGET') and TERMIOS.TIOCMGET or 0x5415
242TIOCMBIS = hasattr(TERMIOS, 'TIOCMBIS') and TERMIOS.TIOCMBIS or 0x5416
243TIOCMBIC = hasattr(TERMIOS, 'TIOCMBIC') and TERMIOS.TIOCMBIC or 0x5417
244TIOCMSET = hasattr(TERMIOS, 'TIOCMSET') and TERMIOS.TIOCMSET or 0x5418
cliechti89b4af12002-02-12 23:24:41 +0000245
cliechti8901aef2002-11-19 01:15:05 +0000246#TIOCM_LE = hasattr(TERMIOS, 'TIOCM_LE') and TERMIOS.TIOCM_LE or 0x001
247TIOCM_DTR = hasattr(TERMIOS, 'TIOCM_DTR') and TERMIOS.TIOCM_DTR or 0x002
248TIOCM_RTS = hasattr(TERMIOS, 'TIOCM_RTS') and TERMIOS.TIOCM_RTS or 0x004
249#TIOCM_ST = hasattr(TERMIOS, 'TIOCM_ST') and TERMIOS.TIOCM_ST or 0x008
250#TIOCM_SR = hasattr(TERMIOS, 'TIOCM_SR') and TERMIOS.TIOCM_SR or 0x010
cliechti89b4af12002-02-12 23:24:41 +0000251
cliechti8901aef2002-11-19 01:15:05 +0000252TIOCM_CTS = hasattr(TERMIOS, 'TIOCM_CTS') and TERMIOS.TIOCM_CTS or 0x020
253TIOCM_CAR = hasattr(TERMIOS, 'TIOCM_CAR') and TERMIOS.TIOCM_CAR or 0x040
254TIOCM_RNG = hasattr(TERMIOS, 'TIOCM_RNG') and TERMIOS.TIOCM_RNG or 0x080
255TIOCM_DSR = hasattr(TERMIOS, 'TIOCM_DSR') and TERMIOS.TIOCM_DSR or 0x100
256TIOCM_CD = hasattr(TERMIOS, 'TIOCM_CD') and TERMIOS.TIOCM_CD or TIOCM_CAR
257TIOCM_RI = hasattr(TERMIOS, 'TIOCM_RI') and TERMIOS.TIOCM_RI or TIOCM_RNG
258#TIOCM_OUT1 = hasattr(TERMIOS, 'TIOCM_OUT1') and TERMIOS.TIOCM_OUT1 or 0x2000
259#TIOCM_OUT2 = hasattr(TERMIOS, 'TIOCM_OUT2') and TERMIOS.TIOCM_OUT2 or 0x4000
cliechti28b8fd02011-12-28 21:39:42 +0000260if hasattr(TERMIOS, 'TIOCINQ'):
261 TIOCINQ = TERMIOS.TIOCINQ
262else:
263 TIOCINQ = hasattr(TERMIOS, 'FIONREAD') and TERMIOS.FIONREAD or 0x541B
264TIOCOUTQ = hasattr(TERMIOS, 'TIOCOUTQ') and TERMIOS.TIOCOUTQ or 0x5411
cliechti89b4af12002-02-12 23:24:41 +0000265
266TIOCM_zero_str = struct.pack('I', 0)
267TIOCM_RTS_str = struct.pack('I', TIOCM_RTS)
268TIOCM_DTR_str = struct.pack('I', TIOCM_DTR)
269
cliechti997b63c2008-06-21 00:09:31 +0000270TIOCSBRK = hasattr(TERMIOS, 'TIOCSBRK') and TERMIOS.TIOCSBRK or 0x5427
271TIOCCBRK = hasattr(TERMIOS, 'TIOCCBRK') and TERMIOS.TIOCCBRK or 0x5428
272
Chris Liechti68340d72015-08-03 14:15:48 +0200273CMSPAR = 0o10000000000 # Use "stick" (mark/space) parity
cliechtiaec27ab2014-07-31 22:21:24 +0000274
cliechti89b4af12002-02-12 23:24:41 +0000275
cliechtif81362e2009-07-25 03:44:33 +0000276class PosixSerial(SerialBase):
cliechti7d448562014-08-03 21:57:45 +0000277 """\
278 Serial port class POSIX implementation. Serial port configuration is
cliechtid6bf52c2003-10-01 02:28:12 +0000279 done with termios and fcntl. Runs on Linux and many other Un*x like
cliechtif0a81d42014-08-04 14:03:53 +0000280 systems.
281 """
cliechtid6bf52c2003-10-01 02:28:12 +0000282
283 def open(self):
cliechti7d448562014-08-03 21:57:45 +0000284 """\
285 Open port with current settings. This may throw a SerialException
286 if the port cannot be opened."""
cliechtid6bf52c2003-10-01 02:28:12 +0000287 if self._port is None:
288 raise SerialException("Port must be configured before it can be used.")
cliechti02ef43a2011-03-24 23:33:12 +0000289 if self._isOpen:
290 raise SerialException("Port is already open.")
291 self.fd = None
cliechti58b481c2009-02-16 20:42:32 +0000292 # open
cliechti4616bf12002-04-08 23:13:14 +0000293 try:
294 self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK)
Chris Liechti68340d72015-08-03 14:15:48 +0200295 except OSError as msg:
cliechti4616bf12002-04-08 23:13:14 +0000296 self.fd = None
cliechtiaf84daa2013-10-10 23:57:00 +0000297 raise SerialException(msg.errno, "could not open port %s: %s" % (self._port, msg))
cliechti2750b832009-07-28 00:13:52 +0000298 #~ fcntl.fcntl(self.fd, FCNTL.F_SETFL, 0) # set blocking
cliechti58b481c2009-02-16 20:42:32 +0000299
cliechtib2f5fc82006-10-20 00:09:07 +0000300 try:
301 self._reconfigurePort()
302 except:
cliechti2750b832009-07-28 00:13:52 +0000303 try:
304 os.close(self.fd)
305 except:
306 # ignore any exception when closing the port
307 # also to keep original exception that happened when setting up
308 pass
cliechtib2f5fc82006-10-20 00:09:07 +0000309 self.fd = None
cliechtif0a4f0f2009-07-21 21:12:37 +0000310 raise
cliechtib2f5fc82006-10-20 00:09:07 +0000311 else:
312 self._isOpen = True
cliechti5c9b0722013-10-17 03:19:39 +0000313 self.flushInput()
cliechti58b481c2009-02-16 20:42:32 +0000314
315
cliechtid6bf52c2003-10-01 02:28:12 +0000316 def _reconfigurePort(self):
cliechtib2f5fc82006-10-20 00:09:07 +0000317 """Set communication parameters on opened port."""
cliechtic6178262004-03-22 22:04:52 +0000318 if self.fd is None:
cliechtia9a093e2010-01-02 03:05:08 +0000319 raise SerialException("Can only operate on a valid file descriptor")
cliechtie8c45422008-06-20 23:23:14 +0000320 custom_baud = None
cliechti58b481c2009-02-16 20:42:32 +0000321
cliechti2750b832009-07-28 00:13:52 +0000322 vmin = vtime = 0 # timeout is done via select
cliechti679bfa62008-06-20 23:58:15 +0000323 if self._interCharTimeout is not None:
324 vmin = 1
325 vtime = int(self._interCharTimeout * 10)
cliechti6ce7ab12002-11-07 02:15:00 +0000326 try:
cliechti4d0af5e2011-08-05 02:18:16 +0000327 orig_attr = termios.tcgetattr(self.fd)
328 iflag, oflag, cflag, lflag, ispeed, ospeed, cc = orig_attr
Chris Liechti68340d72015-08-03 14:15:48 +0200329 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 +0000330 raise SerialException("Could not configure port: %s" % msg)
cliechti58b481c2009-02-16 20:42:32 +0000331 # set up raw mode / no echo / binary
cliechtid6bf52c2003-10-01 02:28:12 +0000332 cflag |= (TERMIOS.CLOCAL|TERMIOS.CREAD)
333 lflag &= ~(TERMIOS.ICANON|TERMIOS.ECHO|TERMIOS.ECHOE|TERMIOS.ECHOK|TERMIOS.ECHONL|
cliechti835996a2004-06-02 19:45:07 +0000334 TERMIOS.ISIG|TERMIOS.IEXTEN) #|TERMIOS.ECHOPRT
cliechti2750b832009-07-28 00:13:52 +0000335 for flag in ('ECHOCTL', 'ECHOKE'): # netbsd workaround for Erk
cliechti835996a2004-06-02 19:45:07 +0000336 if hasattr(TERMIOS, flag):
337 lflag &= ~getattr(TERMIOS, flag)
cliechti58b481c2009-02-16 20:42:32 +0000338
cliechtid6bf52c2003-10-01 02:28:12 +0000339 oflag &= ~(TERMIOS.OPOST)
cliechti895e8302004-04-20 02:40:28 +0000340 iflag &= ~(TERMIOS.INLCR|TERMIOS.IGNCR|TERMIOS.ICRNL|TERMIOS.IGNBRK)
cliechti89b4af12002-02-12 23:24:41 +0000341 if hasattr(TERMIOS, 'IUCLC'):
cliechti895e8302004-04-20 02:40:28 +0000342 iflag &= ~TERMIOS.IUCLC
cliechti3e57b3d2005-08-12 21:04:44 +0000343 if hasattr(TERMIOS, 'PARMRK'):
344 iflag &= ~TERMIOS.PARMRK
cliechti58b481c2009-02-16 20:42:32 +0000345
cliechtif0a4f0f2009-07-21 21:12:37 +0000346 # setup baud rate
cliechti89b4af12002-02-12 23:24:41 +0000347 try:
cliechti2750b832009-07-28 00:13:52 +0000348 ispeed = ospeed = getattr(TERMIOS, 'B%s' % (self._baudrate))
cliechti895e8302004-04-20 02:40:28 +0000349 except AttributeError:
cliechtif1559d02007-11-08 23:43:58 +0000350 try:
351 ispeed = ospeed = baudrate_constants[self._baudrate]
352 except KeyError:
cliechtie8c45422008-06-20 23:23:14 +0000353 #~ raise ValueError('Invalid baud rate: %r' % self._baudrate)
cliechtif0a4f0f2009-07-21 21:12:37 +0000354 # may need custom baud rate, it isn't in our list.
cliechtie8c45422008-06-20 23:23:14 +0000355 ispeed = ospeed = getattr(TERMIOS, 'B38400')
cliechtif0a4f0f2009-07-21 21:12:37 +0000356 try:
357 custom_baud = int(self._baudrate) # store for later
358 except ValueError:
359 raise ValueError('Invalid baud rate: %r' % self._baudrate)
360 else:
361 if custom_baud < 0:
362 raise ValueError('Invalid baud rate: %r' % self._baudrate)
cliechti58b481c2009-02-16 20:42:32 +0000363
364 # setup char len
cliechtid6bf52c2003-10-01 02:28:12 +0000365 cflag &= ~TERMIOS.CSIZE
366 if self._bytesize == 8:
367 cflag |= TERMIOS.CS8
368 elif self._bytesize == 7:
369 cflag |= TERMIOS.CS7
370 elif self._bytesize == 6:
371 cflag |= TERMIOS.CS6
372 elif self._bytesize == 5:
373 cflag |= TERMIOS.CS5
cliechti89b4af12002-02-12 23:24:41 +0000374 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000375 raise ValueError('Invalid char len: %r' % self._bytesize)
cliechtif0a81d42014-08-04 14:03:53 +0000376 # setup stop bits
cliechtid6bf52c2003-10-01 02:28:12 +0000377 if self._stopbits == STOPBITS_ONE:
378 cflag &= ~(TERMIOS.CSTOPB)
cliechti58b481c2009-02-16 20:42:32 +0000379 elif self._stopbits == STOPBITS_ONE_POINT_FIVE:
380 cflag |= (TERMIOS.CSTOPB) # XXX same as TWO.. there is no POSIX support for 1.5
cliechtid6bf52c2003-10-01 02:28:12 +0000381 elif self._stopbits == STOPBITS_TWO:
382 cflag |= (TERMIOS.CSTOPB)
cliechti89b4af12002-02-12 23:24:41 +0000383 else:
cliechti3172d3d2009-07-21 22:33:40 +0000384 raise ValueError('Invalid stop bit specification: %r' % self._stopbits)
cliechti58b481c2009-02-16 20:42:32 +0000385 # setup parity
cliechtid6bf52c2003-10-01 02:28:12 +0000386 iflag &= ~(TERMIOS.INPCK|TERMIOS.ISTRIP)
387 if self._parity == PARITY_NONE:
388 cflag &= ~(TERMIOS.PARENB|TERMIOS.PARODD)
389 elif self._parity == PARITY_EVEN:
390 cflag &= ~(TERMIOS.PARODD)
391 cflag |= (TERMIOS.PARENB)
392 elif self._parity == PARITY_ODD:
393 cflag |= (TERMIOS.PARENB|TERMIOS.PARODD)
cliechtiaec27ab2014-07-31 22:21:24 +0000394 elif self._parity == PARITY_MARK and plat[:5] == 'linux':
395 cflag |= (TERMIOS.PARENB|CMSPAR|TERMIOS.PARODD)
396 elif self._parity == PARITY_SPACE and plat[:5] == 'linux':
397 cflag |= (TERMIOS.PARENB|CMSPAR)
398 cflag &= ~(TERMIOS.PARODD)
cliechti89b4af12002-02-12 23:24:41 +0000399 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000400 raise ValueError('Invalid parity: %r' % self._parity)
cliechti58b481c2009-02-16 20:42:32 +0000401 # setup flow control
402 # xonxoff
cliechti89b4af12002-02-12 23:24:41 +0000403 if hasattr(TERMIOS, 'IXANY'):
cliechtid6bf52c2003-10-01 02:28:12 +0000404 if self._xonxoff:
cliechti62611612004-04-20 01:55:43 +0000405 iflag |= (TERMIOS.IXON|TERMIOS.IXOFF) #|TERMIOS.IXANY)
cliechti89b4af12002-02-12 23:24:41 +0000406 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000407 iflag &= ~(TERMIOS.IXON|TERMIOS.IXOFF|TERMIOS.IXANY)
cliechti89b4af12002-02-12 23:24:41 +0000408 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000409 if self._xonxoff:
410 iflag |= (TERMIOS.IXON|TERMIOS.IXOFF)
cliechti89b4af12002-02-12 23:24:41 +0000411 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000412 iflag &= ~(TERMIOS.IXON|TERMIOS.IXOFF)
cliechti58b481c2009-02-16 20:42:32 +0000413 # rtscts
cliechti89b4af12002-02-12 23:24:41 +0000414 if hasattr(TERMIOS, 'CRTSCTS'):
cliechtid6bf52c2003-10-01 02:28:12 +0000415 if self._rtscts:
416 cflag |= (TERMIOS.CRTSCTS)
cliechti89b4af12002-02-12 23:24:41 +0000417 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000418 cflag &= ~(TERMIOS.CRTSCTS)
cliechti2750b832009-07-28 00:13:52 +0000419 elif hasattr(TERMIOS, 'CNEW_RTSCTS'): # try it with alternate constant name
cliechtid6bf52c2003-10-01 02:28:12 +0000420 if self._rtscts:
421 cflag |= (TERMIOS.CNEW_RTSCTS)
cliechtid4743692002-04-08 22:39:53 +0000422 else:
cliechtid6bf52c2003-10-01 02:28:12 +0000423 cflag &= ~(TERMIOS.CNEW_RTSCTS)
cliechti2750b832009-07-28 00:13:52 +0000424 # XXX should there be a warning if setting up rtscts (and xonxoff etc) fails??
cliechti58b481c2009-02-16 20:42:32 +0000425
426 # buffer
cliechtif0a81d42014-08-04 14:03:53 +0000427 # vmin "minimal number of characters to be read. 0 for non blocking"
cliechtid6bf52c2003-10-01 02:28:12 +0000428 if vmin < 0 or vmin > 255:
429 raise ValueError('Invalid vmin: %r ' % vmin)
430 cc[TERMIOS.VMIN] = vmin
cliechti58b481c2009-02-16 20:42:32 +0000431 # vtime
cliechtid6bf52c2003-10-01 02:28:12 +0000432 if vtime < 0 or vtime > 255:
433 raise ValueError('Invalid vtime: %r' % vtime)
434 cc[TERMIOS.VTIME] = vtime
cliechti58b481c2009-02-16 20:42:32 +0000435 # activate settings
cliechti4d0af5e2011-08-05 02:18:16 +0000436 if [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] != orig_attr:
437 termios.tcsetattr(self.fd, TERMIOS.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
cliechti58b481c2009-02-16 20:42:32 +0000438
cliechtie8c45422008-06-20 23:23:14 +0000439 # apply custom baud rate, if any
440 if custom_baud is not None:
cliechti53c9fd42009-07-23 23:51:51 +0000441 set_special_baudrate(self, custom_baud)
cliechti89b4af12002-02-12 23:24:41 +0000442
443 def close(self):
cliechtid6bf52c2003-10-01 02:28:12 +0000444 """Close port"""
445 if self._isOpen:
cliechtic6178262004-03-22 22:04:52 +0000446 if self.fd is not None:
cliechtid6bf52c2003-10-01 02:28:12 +0000447 os.close(self.fd)
448 self.fd = None
449 self._isOpen = False
cliechti89b4af12002-02-12 23:24:41 +0000450
cliechtid6bf52c2003-10-01 02:28:12 +0000451 def makeDeviceName(self, port):
452 return device(port)
453
454 # - - - - - - - - - - - - - - - - - - - - - - - -
cliechti95c62212002-03-04 22:17:53 +0000455
cliechti89b4af12002-02-12 23:24:41 +0000456 def inWaiting(self):
cliechtid6bf52c2003-10-01 02:28:12 +0000457 """Return the number of characters currently in the input buffer."""
cliechtif5831e02002-12-05 23:15:27 +0000458 #~ s = fcntl.ioctl(self.fd, TERMIOS.FIONREAD, TIOCM_zero_str)
459 s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
cliechti89b4af12002-02-12 23:24:41 +0000460 return struct.unpack('I',s)[0]
461
cliechtia9a093e2010-01-02 03:05:08 +0000462 # select based implementation, proved to work on many systems
463 def read(self, size=1):
cliechti7d448562014-08-03 21:57:45 +0000464 """\
465 Read size bytes from the serial port. If a timeout is set it may
466 return less characters as requested. With no timeout it will block
467 until the requested number of bytes is read.
468 """
cliechti899c9c42011-06-14 23:01:23 +0000469 if not self._isOpen: raise portNotOpenError
cliechtia9a093e2010-01-02 03:05:08 +0000470 read = bytearray()
471 while len(read) < size:
cliechti8d744de2013-10-11 14:31:13 +0000472 try:
473 ready,_,_ = select.select([self.fd],[],[], self._timeout)
474 # If select was used with a timeout, and the timeout occurs, it
475 # returns with empty lists -> thus abort read operation.
476 # For timeout == 0 (non-blocking operation) also abort when there
477 # is nothing to read.
478 if not ready:
479 break # timeout
480 buf = os.read(self.fd, size-len(read))
481 # read should always return some data as select reported it was
482 # ready to read when we get to this point.
483 if not buf:
484 # Disconnected devices, at least on Linux, show the
485 # behavior that they are always ready to read immediately
486 # but reading returns nothing.
487 raise SerialException('device reports readiness to read but returned no data (device disconnected or multiple access on port?)')
488 read.extend(buf)
Chris Liechti68340d72015-08-03 14:15:48 +0200489 except OSError as e:
cliechtic7cd7212014-08-03 21:34:38 +0000490 # this is for Python 3.x where select.error is a subclass of OSError
491 # ignore EAGAIN errors. all other errors are shown
492 if e.errno != errno.EAGAIN:
493 raise SerialException('read failed: %s' % (e,))
Chris Liechti68340d72015-08-03 14:15:48 +0200494 except select.error as e:
cliechtic7cd7212014-08-03 21:34:38 +0000495 # this is for Python 2.x
cliechti8d744de2013-10-11 14:31:13 +0000496 # ignore EAGAIN errors. all other errors are shown
497 # see also http://www.python.org/dev/peps/pep-3151/#select
498 if e[0] != errno.EAGAIN:
499 raise SerialException('read failed: %s' % (e,))
cliechtia9a093e2010-01-02 03:05:08 +0000500 return bytes(read)
cliechti89b4af12002-02-12 23:24:41 +0000501
cliechti4a567a02009-07-27 22:09:31 +0000502 def write(self, data):
cliechtid6bf52c2003-10-01 02:28:12 +0000503 """Output the given string over the serial port."""
cliechti899c9c42011-06-14 23:01:23 +0000504 if not self._isOpen: raise portNotOpenError
cliechti38077122013-10-16 02:57:27 +0000505 d = to_bytes(data)
506 tx_len = len(d)
cliechti3cf46d62009-08-07 00:19:57 +0000507 if self._writeTimeout is not None and self._writeTimeout > 0:
508 timeout = time.time() + self._writeTimeout
509 else:
510 timeout = None
cliechti9f7c2352013-10-11 01:13:46 +0000511 while tx_len > 0:
cliechti5d4d0bd2004-11-13 03:27:39 +0000512 try:
cliechti5d4d0bd2004-11-13 03:27:39 +0000513 n = os.write(self.fd, d)
cliechti3cf46d62009-08-07 00:19:57 +0000514 if timeout:
515 # when timeout is set, use select to wait for being ready
516 # with the time left as timeout
517 timeleft = timeout - time.time()
518 if timeleft < 0:
519 raise writeTimeoutError
520 _, ready, _ = select.select([], [self.fd], [], timeleft)
cliechti5d4d0bd2004-11-13 03:27:39 +0000521 if not ready:
522 raise writeTimeoutError
cliechti88c62442013-10-12 04:03:16 +0000523 else:
524 # wait for write operation
525 _, ready, _ = select.select([], [self.fd], [], None)
526 if not ready:
527 raise SerialException('write failed (select)')
cliechti5d4d0bd2004-11-13 03:27:39 +0000528 d = d[n:]
cliechti9f7c2352013-10-11 01:13:46 +0000529 tx_len -= n
Chris Liechti68340d72015-08-03 14:15:48 +0200530 except OSError as v:
cliechti5d4d0bd2004-11-13 03:27:39 +0000531 if v.errno != errno.EAGAIN:
cliechti65722c92009-08-07 00:48:53 +0000532 raise SerialException('write failed: %s' % (v,))
cliechtif81362e2009-07-25 03:44:33 +0000533 return len(data)
cliechtid6bf52c2003-10-01 02:28:12 +0000534
cliechtia30a8a02003-10-05 12:28:13 +0000535 def flush(self):
cliechti7d448562014-08-03 21:57:45 +0000536 """\
537 Flush of file like objects. In this case, wait until all data
538 is written.
539 """
cliechtia30a8a02003-10-05 12:28:13 +0000540 self.drainOutput()
541
cliechti89b4af12002-02-12 23:24:41 +0000542 def flushInput(self):
cliechtid6bf52c2003-10-01 02:28:12 +0000543 """Clear input buffer, discarding all that is in the buffer."""
cliechti899c9c42011-06-14 23:01:23 +0000544 if not self._isOpen: raise portNotOpenError
cliechti89b4af12002-02-12 23:24:41 +0000545 termios.tcflush(self.fd, TERMIOS.TCIFLUSH)
546
547 def flushOutput(self):
cliechti7d448562014-08-03 21:57:45 +0000548 """\
549 Clear output buffer, aborting the current output and discarding all
550 that is in the buffer.
551 """
cliechti899c9c42011-06-14 23:01:23 +0000552 if not self._isOpen: raise portNotOpenError
cliechti89b4af12002-02-12 23:24:41 +0000553 termios.tcflush(self.fd, TERMIOS.TCOFLUSH)
554
cliechtiaaa04602006-02-05 23:02:46 +0000555 def sendBreak(self, duration=0.25):
cliechti7d448562014-08-03 21:57:45 +0000556 """\
557 Send break condition. Timed, returns to idle state after given
558 duration.
559 """
cliechti899c9c42011-06-14 23:01:23 +0000560 if not self._isOpen: raise portNotOpenError
cliechtiaaa04602006-02-05 23:02:46 +0000561 termios.tcsendbreak(self.fd, int(duration/0.25))
cliechti89b4af12002-02-12 23:24:41 +0000562
cliechti997b63c2008-06-21 00:09:31 +0000563 def setBreak(self, level=1):
cliechti7d448562014-08-03 21:57:45 +0000564 """\
565 Set break: Controls TXD. When active, no transmitting is possible.
566 """
cliechti997b63c2008-06-21 00:09:31 +0000567 if self.fd is None: raise portNotOpenError
568 if level:
569 fcntl.ioctl(self.fd, TIOCSBRK)
570 else:
571 fcntl.ioctl(self.fd, TIOCCBRK)
572
cliechti93db61b2006-08-26 19:16:18 +0000573 def setRTS(self, level=1):
cliechtid6bf52c2003-10-01 02:28:12 +0000574 """Set terminal status line: Request To Send"""
cliechti899c9c42011-06-14 23:01:23 +0000575 if not self._isOpen: raise portNotOpenError
cliechtib2f5fc82006-10-20 00:09:07 +0000576 if level:
cliechtid6bf52c2003-10-01 02:28:12 +0000577 fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_RTS_str)
578 else:
579 fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_RTS_str)
580
cliechti93db61b2006-08-26 19:16:18 +0000581 def setDTR(self, level=1):
cliechtid6bf52c2003-10-01 02:28:12 +0000582 """Set terminal status line: Data Terminal Ready"""
cliechti899c9c42011-06-14 23:01:23 +0000583 if not self._isOpen: raise portNotOpenError
cliechtib2f5fc82006-10-20 00:09:07 +0000584 if level:
cliechtid6bf52c2003-10-01 02:28:12 +0000585 fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
586 else:
587 fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str)
588
589 def getCTS(self):
590 """Read terminal status line: Clear To Send"""
cliechti899c9c42011-06-14 23:01:23 +0000591 if not self._isOpen: raise portNotOpenError
cliechtid6bf52c2003-10-01 02:28:12 +0000592 s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
593 return struct.unpack('I',s)[0] & TIOCM_CTS != 0
594
595 def getDSR(self):
596 """Read terminal status line: Data Set Ready"""
cliechti899c9c42011-06-14 23:01:23 +0000597 if not self._isOpen: raise portNotOpenError
cliechtid6bf52c2003-10-01 02:28:12 +0000598 s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
599 return struct.unpack('I',s)[0] & TIOCM_DSR != 0
600
cliechtid6bf52c2003-10-01 02:28:12 +0000601 def getRI(self):
602 """Read terminal status line: Ring Indicator"""
cliechti899c9c42011-06-14 23:01:23 +0000603 if not self._isOpen: raise portNotOpenError
cliechti89b4af12002-02-12 23:24:41 +0000604 s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
cliechtid6bf52c2003-10-01 02:28:12 +0000605 return struct.unpack('I',s)[0] & TIOCM_RI != 0
cliechti89b4af12002-02-12 23:24:41 +0000606
607 def getCD(self):
cliechtid6bf52c2003-10-01 02:28:12 +0000608 """Read terminal status line: Carrier Detect"""
cliechti899c9c42011-06-14 23:01:23 +0000609 if not self._isOpen: raise portNotOpenError
cliechti89b4af12002-02-12 23:24:41 +0000610 s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
cliechtid6bf52c2003-10-01 02:28:12 +0000611 return struct.unpack('I',s)[0] & TIOCM_CD != 0
cliechti89b4af12002-02-12 23:24:41 +0000612
cliechtia30a8a02003-10-05 12:28:13 +0000613 # - - platform specific - - - -
614
cliechti28b8fd02011-12-28 21:39:42 +0000615 def outWaiting(self):
616 """Return the number of characters currently in the output buffer."""
617 #~ s = fcntl.ioctl(self.fd, TERMIOS.FIONREAD, TIOCM_zero_str)
618 s = fcntl.ioctl(self.fd, TIOCOUTQ, TIOCM_zero_str)
619 return struct.unpack('I',s)[0]
620
cliechtia30a8a02003-10-05 12:28:13 +0000621 def drainOutput(self):
622 """internal - not portable!"""
cliechti899c9c42011-06-14 23:01:23 +0000623 if not self._isOpen: raise portNotOpenError
cliechtia30a8a02003-10-05 12:28:13 +0000624 termios.tcdrain(self.fd)
625
626 def nonblocking(self):
627 """internal - not portable!"""
cliechti899c9c42011-06-14 23:01:23 +0000628 if not self._isOpen: raise portNotOpenError
cliechti198b7e72010-05-25 00:38:23 +0000629 fcntl.fcntl(self.fd, FCNTL.F_SETFL, os.O_NONBLOCK)
cliechtia30a8a02003-10-05 12:28:13 +0000630
cliechti8753bbc2005-01-15 20:32:51 +0000631 def fileno(self):
cliechti2f0f8a32011-12-28 22:10:00 +0000632 """\
633 For easier use of the serial port instance with select.
634 WARNING: this function is not portable to different platforms!
635 """
cliechti899c9c42011-06-14 23:01:23 +0000636 if not self._isOpen: raise portNotOpenError
cliechti8753bbc2005-01-15 20:32:51 +0000637 return self.fd
cliechti89b4af12002-02-12 23:24:41 +0000638
cliechti2f0f8a32011-12-28 22:10:00 +0000639 def setXON(self, level=True):
640 """\
641 Manually control flow - when software flow control is enabled.
642 This will send XON (true) and XOFF (false) to the other device.
643 WARNING: this function is not portable to different platforms!
644 """
645 if not self.hComPort: raise portNotOpenError
cliechti4a601342011-12-29 02:22:17 +0000646 if enable:
cliechti57e48a62009-08-03 22:29:58 +0000647 termios.tcflow(self.fd, TERMIOS.TCION)
648 else:
649 termios.tcflow(self.fd, TERMIOS.TCIOFF)
650
cliechti2f0f8a32011-12-28 22:10:00 +0000651 def flowControlOut(self, enable):
652 """\
653 Manually control flow of outgoing data - when hardware or software flow
654 control is enabled.
655 WARNING: this function is not portable to different platforms!
656 """
657 if not self._isOpen: raise portNotOpenError
658 if enable:
659 termios.tcflow(self.fd, TERMIOS.TCOON)
660 else:
661 termios.tcflow(self.fd, TERMIOS.TCOOFF)
662
cliechtif81362e2009-07-25 03:44:33 +0000663
cliechtif0a81d42014-08-04 14:03:53 +0000664# assemble Serial class with the platform specific implementation and the base
cliechti4a567a02009-07-27 22:09:31 +0000665# for file-like behavior. for Python 2.6 and newer, that provide the new I/O
cliechtif0a81d42014-08-04 14:03:53 +0000666# library, derive from io.RawIOBase
cliechti4a567a02009-07-27 22:09:31 +0000667try:
668 import io
669except ImportError:
670 # classic version with our own file-like emulation
671 class Serial(PosixSerial, FileLike):
672 pass
673else:
674 # io library present
675 class Serial(PosixSerial, io.RawIOBase):
cliechtif81362e2009-07-25 03:44:33 +0000676 pass
677
cliechtia9a093e2010-01-02 03:05:08 +0000678class PosixPollSerial(Serial):
cliechti7d448562014-08-03 21:57:45 +0000679 """\
cliechtif0a81d42014-08-04 14:03:53 +0000680 Poll based read implementation. Not all systems support poll properly.
681 However this one has better handling of errors, such as a device
cliechti7d448562014-08-03 21:57:45 +0000682 disconnecting while it's in use (e.g. USB-serial unplugged).
683 """
cliechtia9a093e2010-01-02 03:05:08 +0000684
685 def read(self, size=1):
cliechti7d448562014-08-03 21:57:45 +0000686 """\
687 Read size bytes from the serial port. If a timeout is set it may
688 return less characters as requested. With no timeout it will block
689 until the requested number of bytes is read.
690 """
cliechtia9a093e2010-01-02 03:05:08 +0000691 if self.fd is None: raise portNotOpenError
692 read = bytearray()
693 poll = select.poll()
694 poll.register(self.fd, select.POLLIN|select.POLLERR|select.POLLHUP|select.POLLNVAL)
695 if size > 0:
696 while len(read) < size:
697 # print "\tread(): size",size, "have", len(read) #debug
698 # wait until device becomes ready to read (or something fails)
cliechti4cd0d2e2010-07-21 01:15:25 +0000699 for fd, event in poll.poll(self._timeout*1000):
cliechtia9a093e2010-01-02 03:05:08 +0000700 if event & (select.POLLERR|select.POLLHUP|select.POLLNVAL):
701 raise SerialException('device reports error (poll)')
702 # we don't care if it is select.POLLIN or timeout, that's
703 # handled below
704 buf = os.read(self.fd, size - len(read))
705 read.extend(buf)
706 if ((self._timeout is not None and self._timeout >= 0) or
707 (self._interCharTimeout is not None and self._interCharTimeout > 0)) and not buf:
708 break # early abort on timeout
709 return bytes(read)
710
cliechtif81362e2009-07-25 03:44:33 +0000711
cliechti89b4af12002-02-12 23:24:41 +0000712if __name__ == '__main__':
713 s = Serial(0,
cliechti53c9fd42009-07-23 23:51:51 +0000714 baudrate=19200, # baud rate
715 bytesize=EIGHTBITS, # number of data bits
cliechti3172d3d2009-07-21 22:33:40 +0000716 parity=PARITY_EVEN, # enable parity checking
cliechti53c9fd42009-07-23 23:51:51 +0000717 stopbits=STOPBITS_ONE, # number of stop bits
cliechti3172d3d2009-07-21 22:33:40 +0000718 timeout=3, # set a timeout value, None for waiting forever
719 xonxoff=0, # enable software flow control
720 rtscts=0, # enable RTS/CTS flow control
cliechti89b4af12002-02-12 23:24:41 +0000721 )
722 s.setRTS(1)
723 s.setDTR(1)
724 s.flushInput()
725 s.flushOutput()
726 s.write('hello')
cliechti109486b2009-08-02 00:00:11 +0000727 sys.stdout.write('%r\n' % s.read(5))
728 sys.stdout.write('%s\n' % s.inWaiting())
cliechti89b4af12002-02-12 23:24:41 +0000729 del s
cliechti4569bac2007-11-08 21:57:19 +0000730