5 # 3rd September 2002 Dave Haynes
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
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,
35 # curdir, pardir, sep, altsep, pathsep, defpath, linesep.
41 import serial
.serialutil
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')
64 return 'COM%d' % (portnum
+1)
66 class Serial(serialutil
.FileLike
):
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
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
86 stopbits
= STOPBITS_ONE
,
93 if type(port
) == type(''):
94 # strings are taken directly
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
:
105 elif parity
== PARITY_EVEN
:
107 elif parity
== PARITY_ODD
:
109 elif parity
== PARITY_MARK
:
111 elif parity
== PARITY_SPACE
:
114 self
.stop
= str(stopbits
)
116 self
.filename
= "sermsdos.tmp"
118 self
._config
(self
.portstr
, self
.baud
, self
.parity
,
119 self
.bytesize
, self
.stop
, self
.retry
, self
.filename
)
127 def _config(self
, port
, baud
, parity
, data
, stop
, retry
,
129 comString
= string
.join(("MODE ", port
, ":"
130 , " BAUD= ", baud
, " PARITY= ", parity
131 , " DATA= ", data
, " STOP= ", stop
, " RETRY= ",
132 retry
, " > ", filename
), '')
135 def setBaudrate(self
, baudrate
):
136 self
._config
(self
.portstr
, BAUD_RATES
[baudrate
],
137 self
.parity
, self
.bytesize
, self
.stop
, self
.retry
,
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
)
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
)
160 def flushInput(self
):
161 raise NotImplementedError
163 def flushOutput(self
):
164 raise NotImplementedError
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
178 """Eead terminal status line"""
179 raise NotImplementedError
182 """Eead terminal status line"""
183 raise NotImplementedError
186 """Eead terminal status line"""
187 raise NotImplementedError
190 """Eead terminal status line"""
191 raise NotImplementedError
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__':
200 sys
.stdio
.write('%s %s\n' % (__name__
, s
))