By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

Threading and serial port access

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.