473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 13136
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
1
by: sinan . | last post by:
hi i have a program that listens socket connection and serial device. in main program i am creating a socket then calling a class to listen socket under while 1: , class soket(Thread): def...
0
by: elcinturapartida | last post by:
Hello all, I am not sure if this question is about threading or serial i/o - it has elements of both. I'm on WinXP (desktop) and WinNT (labtop), when I run miniterm.py there is no problem both...
13
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
15
by: aikwee | last post by:
hi all, i have a software written in vb.net which has many thread in it. The software basically use about 8 threads to monitor in coming data from serial port, and some other thread that...
4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
5
by: LongBow | last post by:
Hello, Is there a way, in .NET, to determine what are the avialable Serial (Communications) Ports on a Windows OS and is there a way to determine that port isn't being use other than attempting...
2
by: colin | last post by:
Hi, Im having a tiresome amount of trouble with using a bluetooth serial link. The receiving end is a bluetooth-rs232 module conected to my embeded system. The PC has a little usb bluetooth...
5
by: agloth | last post by:
Hi, I have a serial port reader application that uses datareceived event to read incoming data. The application also send response some of the messages. The problem is the application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.