Threading and serial port access

willie at macleod-group.com willie at macleod-group.com
Sat Jun 18 09:09:40 EDT 2005


Hi,

I'm writing a program which requires the use of three serial ports and
one parallel port.  My application has a scanning devices on each port,
which I can access fine with pyserial.  However, I'm unsure of how
exactly I should be designing the program, I thought I could use
threading to start class:

class scanner(Thread):
        def __init__(self,port):
                Thread.__init__(self)
                self.port = port
        def scan(self):
                ser = serial.Serial(port)
                print ser.portstr
                id = ser.read(12)
                ser.close

But this doesn't work as I thought when I call it like:

for port in range(0,totalserialports):  # loop through all serial ports
        print "starting thread for port %d" %(port)
        NewThread = scanner(port)
        NewThread.scan()
        NewThread.start()

I get:

starting thread for port 0
/dev/ttyS0

Now, I know that I haven't specified any port timeouts, but I don't
want it to timeout, I want to open each port and keep it open
indefinately.  Threading seems to block waiting for the read from the
serial port.  How can I open every serial port at the same time, read
from it, do an action and then go back to it?  Anyone got any good
documentation sources for threading that explain things clearly and
gives examples? (I'm running python 2.3.4)

What's the most python like way of achieving my end goal?

Thanks

Regards

William MacLeod




More information about the Python-list mailing list