472,119 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Threading and serial port access

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

Jul 19 '05 #1
2 12873
wi****@macleod-group.com 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
Jul 19 '05 #2
Diez wrote:
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

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ^CeFoS^ | last post: by
1 post views Thread by sinan . | last post: by
reply views Thread by elcinturapartida | last post: by
13 posts views Thread by Al the programmer | last post: by
15 posts views Thread by aikwee | last post: by
5 posts views Thread by agloth | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.