Accessing Windows Serial Port
Roel Schroeven
rschroev_nospam_ml at fastmail.fm
Mon Feb 6 15:47:10 EST 2006
George T. schreef:
> I need to access the serial port via python on a windows machine.
> Reading on the web, there are three solutions: pyserial, siomodule and
> USPP. pyserial seems to be the best option since the other two are
> tested with Windows 95 and older versions of python. Would you agree
> with this or have I missed an option?
I hadn't even heard of siomodule and USPP; pyserial is what I use when I
need to read/write from/to the serial port.
> Can anyone provide me an example of how to access the serial port with
> pyserial?
It's quite simple; there are a number of examples on pyserial's website.
Here's a small quick and dirty script that reads data from a serial port
and broadcasts it as UDP over the network to make the incoming data
available to other computers in the office:
import socket
import sys
import serial
ser = serial.Serial('COM1', 38400, timeout=1)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
while True:
msg = ser.readline()
sock.sendto(msg, ('<broadcast>', 5000))
sys.stdout.write(msg)
Basically you can use the objects like other file-like objects.
--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton
Roel Schroeven
More information about the Python-list
mailing list