473,756 Members | 4,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,p ort):
Thread.__init__ (self)
self.port = port
def scan(self):
ser = serial.Serial(p ort)
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,totalse rialports): # 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 13193
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,p ort):
Thread.__init__ (self)
self.port = port
def scan(self):
ser = serial.Serial(p ort)
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,totalse rialports): # 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.Threa d.__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
9094
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 same port to change the direction of the robot. The trajectory frame is managed by an applet, and the project works good when the applet is called by a html document allocated in the same local machine under W98 where the classes and the serial port...
1
1533
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 __init__(self): Thread.__init__(self) self.host = '127.0.0.1' self.port = 4500 self.data = '' try:
0
697
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 writing, reading, opening the com port, no errors, etc. But if I run wxTerminal.py when read from it nothing happens. It ran without error and it seemed to write ok, If I run the code to do the write, then I connect to the device through...
13
4833
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 been told it is not possible to access the serial ports from asp.net. The application is used to control custom hardware. The hardware is connected to a PC through serial ports. Our customer wants to control the hardware from a remote...
15
3046
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 process those data. What happen is the software will suddenly "dissapear" after running for some time.
4
11206
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. What I would like to do is make the serial port accessible via a web service. The web service and the legacy application would be running on the same machine. The mobile application would access the web service via a network connection. It...
5
9497
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 to opening the Serial Port and catching the associated exception? Thanks If there isn't a way to determine the Serial Port within .NET I would be willing, I guess, to use a WinAPI is that was the only way.
2
5431
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 dongle, ive tried a another dongle with widicom software as I was having problems with the IVT bluesoleil software. that seems to have got rid of some other problems,
5
6796
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 sometimes behaves weird. (My application does not respond some of the messages) And i think its because of the threading. I know that datareceived event uses a
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9689
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.