A limited, but simple and self-contained module giving basic access to the PC Com1 port. Useful for prototyping as it stands, but easily modified for other com ports and needs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | ###
simplecom1: i/o through the com1 port whose transmission parameters have already been
configured through the Windows Device Manager; limited, but requires no special support
beyond the win32file module. If you'd rather use ReadFile() directly to do your own
protocol and ignore readcom1line(), feel free! My use of this is limited to reading,
but also feel free to try win32file.WriteFile with the comhandle below.
readcom1line() accumulates received data until it gets a carriage return. No carriage
return, no readcom1line() return! Similar to keyboard prompting, but may not meet your
needs except for prototyping.
If you use this and get an Access Denied exception, it will probably be because some
other process has the com1 port open already, or maybe your own process (possibly
Pythonwin or some other development environment) opened it and hasn't closed it,
which is just as bad. Don't let go of the open comhandle until you've closed the port!
Also, if you have the Palm Pilot True Sync manager running on com1, you'll need to Exit it
to release the com1 port. Ditto for HyperTerm or any other process that has com1 open.
###
import win32file
def clearcom1receivebuf(comhandle):
# clear out unwanted data from previous transmissions to com1
win32file.PurgeComm(comhandle, win32file.PURGE_RXCLEAR)
def closecom1(comhandle):
comhandle.Close()
def opencom1():
# returns a handle to the Windows file
return win32file.CreateFile(u'COM1:', win32file.GENERIC_READ, \
0, None, win32file.OPEN_EXISTING, 0, None)
def readcom1line(comhandle):
err = 0
char = None
line = ''
while err == 0 and char != '\r':
err, char = win32file.ReadFile(comhandle, 1, None)
if err == 0:
if char == '\n':
pass # eat newlines
else:
line = line + char
else:
break
return line
if __name__ == '__main__':
# demo
comhandle = opencom1()
print readcom1line(comhandle)
closecom1(comhandle)
|
I needed a simple, self-contained module to do some data input through the Com1 port for some quick-and-dirty prototyping, but searching only turned up the sio-151 module which wrapped a commercial DLL and had a lot of moving parts. I dimly remembered that you could open a file called "com1" and get the serial port, tried it out and voila!, it works with some of the win32file module routines. It's primitive (you have to set up the com1 port parameters like baud rate, parity etc. in Device Manager) but effective. I'll probably switch over to the sio module later if this prototype takes off, but otherwise I'll be content with this.
What about the serial module. I'm using the "serial" module, which is supposed to have the same interface on *NIX, and Windows at least.
Even though it is amazingly easy to use, it's possibly more work to install than your example. On Debian you just install the package however.