[EMAIL PROTECTED] wrote: > 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)
The problem is not your code (as far as I can see that w/o running it), but that you didn't understand the threading API. You have to overload the run-method of a Thread object and then start your thread - which will result in executing the run method in a separate thread. Like this: class Poll(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.setDaemon(True) self.running = True def run(self): while self.running: .... # do whatever you want here for t in [Poll() for i in xrange(3)]: t.start() Apart from that the approach you use is wasting resources - if you are concerned about that (or better style...) use e.g. twisted with the serial and parallel support and its so-called select reactor. The idea behind that concept is that the OS is responsible for scannig IO-Ports. It notifies an application about newly arrived data by the system function select - which means that your program sits and waits not cosuming any resources until actual data arrives. See the module select for an overview, and google for "python twisted serial". regards, Diez -- http://mail.python.org/mailman/listinfo/python-list