Threading and serial port access

W

willie

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
 
D

Diez B. Roggisch

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
 
W

willie

Diez said:
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".

Thanks for the help, the code you previously posted worked, but I can
see it could get very messy if the number of ports increased...

I'm going to look at twisted python. Thanks again for the pointers,
much appreciated.

Regards

William MacLeod
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top