Update sdk/platform-tools to version 26.0.0.
[android_tools.git] / sdk / platform-tools / systrace / catapult / telemetry / third_party / pyserial / serial / sermsdos.py
blob09a00177b07a6059c8d6749ccbfa85ecb32a59ac
1 # sermsdos.py
3 # History:
5 # 3rd September 2002 Dave Haynes
6 # 1. First defined
8 # Although this code should run under the latest versions of
9 # Python, on DOS-based platforms such as Windows 95 and 98,
10 # it has been specifically written to be compatible with
11 # PyDOS, available at:
12 # http://www.python.org/ftp/python/wpy/dos.html
14 # PyDOS is a stripped-down version of Python 1.5.2 for
15 # DOS machines. Therefore, in making changes to this file,
16 # please respect Python 1.5.2 syntax. In addition, please
17 # limit the width of this file to 60 characters.
19 # Note also that the modules in PyDOS contain fewer members
20 # than other versions, so we are restricted to using the
21 # following:
23 # In module os:
24 # -------------
25 # environ, chdir, getcwd, getpid, umask, fdopen, close,
26 # dup, dup2, fstat, lseek, open, read, write, O_RDONLY,
27 # O_WRONLY, O_RDWR, O_APPEND, O_CREAT, O_EXCL, O_TRUNC,
28 # access, F_OK, R_OK, W_OK, X_OK, chmod, listdir, mkdir,
29 # remove, rename, renames, rmdir, stat, unlink, utime,
30 # execl, execle, execlp, execlpe, execvp, execvpe, _exit,
31 # system.
33 # In module os.path:
34 # ------------------
35 # curdir, pardir, sep, altsep, pathsep, defpath, linesep.
38 import os
39 import sys
40 import string
41 import serial.serialutil
43 BAUD_RATES = {
44 110: "11",
45 150: "15",
46 300: "30",
47 600: "60",
48 1200: "12",
49 2400: "24",
50 4800: "48",
51 9600: "96",
52 19200: "19"}
54 (PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK,
55 PARITY_SPACE) = (0, 1, 2, 3, 4)
56 (STOPBITS_ONE, STOPBITS_ONEANDAHALF,
57 STOPBITS_TWO) = (1, 1.5, 2)
58 FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5, 6, 7, 8)
59 (RETURN_ERROR, RETURN_BUSY, RETURN_RETRY, RETURN_READY,
60 RETURN_NONE) = ('E', 'B', 'P', 'R', 'N')
61 portNotOpenError = ValueError('port not open')
63 def device(portnum):
64 return 'COM%d' % (portnum+1)
66 class Serial(serialutil.FileLike):
67 """
68 port: number of device; numbering starts at
69 zero. if everything fails, the user can
70 specify a device string, note that this
71 isn't portable any more
72 baudrate: baud rate
73 bytesize: number of databits
74 parity: enable parity checking
75 stopbits: number of stopbits
76 timeout: set a timeout (None for waiting forever)
77 xonxoff: enable software flow control
78 rtscts: enable RTS/CTS flow control
79 retry: DOS retry mode
80 """
81 def __init__(self,
82 port,
83 baudrate = 9600,
84 bytesize = EIGHTBITS,
85 parity = PARITY_NONE,
86 stopbits = STOPBITS_ONE,
87 timeout = None,
88 xonxoff = 0,
89 rtscts = 0,
90 retry = RETURN_RETRY
93 if type(port) == type(''):
94 # strings are taken directly
95 self.portstr = port
96 else:
97 # numbers are transformed to a string
98 self.portstr = device(port+1)
100 self.baud = BAUD_RATES[baudrate]
101 self.bytesize = str(bytesize)
103 if parity == PARITY_NONE:
104 self.parity = 'N'
105 elif parity == PARITY_EVEN:
106 self.parity = 'E'
107 elif parity == PARITY_ODD:
108 self.parity = 'O'
109 elif parity == PARITY_MARK:
110 self.parity = 'M'
111 elif parity == PARITY_SPACE:
112 self.parity = 'S'
114 self.stop = str(stopbits)
115 self.retry = retry
116 self.filename = "sermsdos.tmp"
118 self._config(self.portstr, self.baud, self.parity,
119 self.bytesize, self.stop, self.retry, self.filename)
121 def __del__(self):
122 self.close()
124 def close(self):
125 pass
127 def _config(self, port, baud, parity, data, stop, retry,
128 filename):
129 comString = string.join(("MODE ", port, ":"
130 , " BAUD= ", baud, " PARITY= ", parity
131 , " DATA= ", data, " STOP= ", stop, " RETRY= ",
132 retry, " > ", filename ), '')
133 os.system(comString)
135 def setBaudrate(self, baudrate):
136 self._config(self.portstr, BAUD_RATES[baudrate],
137 self.parity, self.bytesize, self.stop, self.retry,
138 self.filename)
140 def inWaiting(self):
141 """returns the number of bytes waiting to be read"""
142 raise NotImplementedError
144 def read(self, num = 1):
145 """Read num bytes from serial port"""
146 handle = os.open(self.portstr,
147 os.O_RDONLY | os.O_BINARY)
148 rv = os.read(handle, num)
149 os.close(handle)
150 return rv
152 def write(self, s):
153 """Write string to serial port"""
154 handle = os.open(self.portstr,
155 os.O_WRONLY | os.O_BINARY)
156 rv = os.write(handle, s)
157 os.close(handle)
158 return rv
160 def flushInput(self):
161 raise NotImplementedError
163 def flushOutput(self):
164 raise NotImplementedError
166 def sendBreak(self):
167 raise NotImplementedError
169 def setRTS(self,level=1):
170 """Set terminal status line"""
171 raise NotImplementedError
173 def setDTR(self,level=1):
174 """Set terminal status line"""
175 raise NotImplementedError
177 def getCTS(self):
178 """Eead terminal status line"""
179 raise NotImplementedError
181 def getDSR(self):
182 """Eead terminal status line"""
183 raise NotImplementedError
185 def getRI(self):
186 """Eead terminal status line"""
187 raise NotImplementedError
189 def getCD(self):
190 """Eead terminal status line"""
191 raise NotImplementedError
193 def __repr__(self):
194 return string.join(( "<Serial>: ", self.portstr
195 , self.baud, self.parity, self.bytesize, self.stop,
196 self.retry , self.filename), ' ')
198 if __name__ == '__main__':
199 s = Serial(0)
200 sys.stdio.write('%s %s\n' % (__name__, s))