473,785 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pyserial and com port interrupts

Has anyone done a script that will rspond to the serial com port(s)
receive buffer interrupt, as opposed to polling and timeouts?
Win2000 is the main interest right now.
Thanks
Norm B
Jul 18 '05 #1
8 15911
engsol wrote:
Has anyone done a script that will rspond to the serial com port(s)
receive buffer interrupt, as opposed to polling and timeouts?
Win2000 is the main interest right now.


What problem do you hope to avoid by not using "polling
and timeouts"? (Note that if you specify a sizable
read timeout, you're closer to being interrupt-driven
than you are to what is traditionally called "polling".)

-Peter
Jul 18 '05 #2
On Wed, 12 Jan 2005 17:45:48 -0500, Peter Hansen <pe***@engcorp. com> wrote:
engsol wrote:
Has anyone done a script that will rspond to the serial com port(s)
receive buffer interrupt, as opposed to polling and timeouts?
Win2000 is the main interest right now.


What problem do you hope to avoid by not using "polling
and timeouts"? (Note that if you specify a sizable
read timeout, you're closer to being interrupt-driven
than you are to what is traditionally called "polling".)

-Peter


Peter,
Thanks for the reply. I'm working on a s/w test program using python
code. Com1 and com2 play a part. The problem is that the python code
has a lot of work to do...and the results from the hardware under test can
come from either com1 or com2...at any time. It may be a few milliseconds,
or several seconds, sometimes minutes, before a response is expected.
I'm not sure what timeout value I'd use. Using threads, and re-loading the
timeout values on the fly may be a solution, but I'm not experienced with
threads....and was hoping to avoid them.
Norm B
Jul 18 '05 #3
On Wed, 12 Jan 2005 14:24:48 -0800, engsol <en********@pea k.org> wrote:
Has anyone done a script that will rspond to the serial com port(s)
receive buffer interrupt, as opposed to polling and timeouts?
Win2000 is the main interest right now.


Have you looked into letting the OS do it? I.e., reading from COM4:
or whatever port in a thread maybe one byte at a time? Maybe you can
get your end functionality without writing low levels stuff, depending ;-)

I haven't done this, but it seems an easy thing to try a few experiments with.
The control panel should let you set baud rates and handshaking etc. I would think.

Regards,
Bengt Richter
Jul 18 '05 #4
On Thu, 13 Jan 2005 03:11:23 GMT, Bengt Richter <bo**@oz.net> wrote:
On Wed, 12 Jan 2005 14:24:48 -0800, engsol <en********@pea k.org> wrote:
Has anyone done a script that will rspond to the serial com port(s)
receive buffer interrupt, as opposed to polling and timeouts?
Win2000 is the main interest right now.


Have you looked into letting the OS do it? I.e., reading from COM4:
or whatever port in a thread maybe one byte at a time? Maybe you can
get your end functionality without writing low levels stuff, depending ;-)

I haven't done this, but it seems an easy thing to try a few experiments with.
The control panel should let you set baud rates and handshaking etc. I would think.


PySerial[1] is more than capable of handling all the low level
operations with little more than 's.baud = 9600'. The OP was more
interested in how to write his program so he could react to com port
input in a timely manner in the face of having blocking procedures
elsewhere in his code.

Regards,
Stephen Thorne

[1] http://pyserial.sourceforge.net/
Jul 18 '05 #5
On 2005-01-13, engsol <en********@pea k.org> wrote:
I'm not sure what timeout value I'd use. Using threads, and re-loading the
timeout values on the fly may be a solution, but I'm not experienced with
threads....and was hoping to avoid them.


Using threads in Python is really, really painless.

--
Grant Edwards grante Yow! Someone is DROOLING
at on my collar!!
visi.com
Jul 18 '05 #6
engsol wrote:
Has anyone done a script that will rspond to the serial com port(s)
receive buffer interrupt, as opposed to polling and timeouts?
Win2000 is the main interest right now.
Thanks
Norm B

Hello,

I came across this problem as when I first used PySerial, I
came from a java background which has the ability to register listeners
to a serial comm instance and receive interrupts (although some
implementations use polling behind the scenes anyway). I don't think
that pyserial has this kind of thing so I used the following :

def __checkSerial(s elf):

self.__objLock. acquire()
try:
try:
intNoChars = self.__objSeria lPort.inWaiting ()
if (intNoChars > 0):
strReceivedStri ng =
self.__objSeria lPort.read(intN oChars)
#or you could fire an event
self.newMessage (strReceivedStr ing)
except:
#put any clean up code in here.
raise
finally:
self.__objLock. release()

You can then wrap this in a thread which runs the method every x
milliseconds (the code to ensure that I have all the information is
elsewhere in a superclass which deals with this use case across
communication implementations ). However you will have to be aware that
you will need to lock around groups of calls to ensure you are receiving
the correct information (as above).
Therefore, although threading in Python is 'easy' you would still
have to think about this issue, it is unavoidable in communications I'm
afraid. Even if you could receive interrupts then you will likely be
receiving the interrupt on a different thread of execution than the main
anyways so you'll still have threading problems and need to lock around
calls. This is the problem which I usually express as '/RS232C is not
really a standard, more of a rumour/'.

Interestingly, if we take the 'python in threading is easy'
discussion (partially due to the GIL), then for this use case we could
also say that threading in VB6 is even easier than python - I'll leave
you to guess why!!!

Cheers,

Neil

--

Neil Benn
Senior Automation Engineer
Cenix BioScience
BioInnovations Zentrum
Tatzberg 47
D-01307
Dresden
Germany

Tel : +49 (0)351 4173 154
e-mail : be**@cenix-bioscience.com
Cenix Website : http://www.cenix-bioscience.com

Jul 18 '05 #7
Neil Benn wrote:
engsol wrote:
Has anyone done a script that will rspond to the serial com port(s)
receive buffer interrupt, as opposed to polling and timeouts? Win2000
is the main interest right now.
Thanks
Norm B

Hello,

I came across this problem as when I first used PySerial, I
came from a java background which has the ability to register
listeners to a serial comm instance and receive interrupts (although
some implementations use polling behind the scenes anyway). I don't
think that pyserial has this kind of thing so I used the following :


<snip>

Tabs got screwed up here is teh code again :

def __checkSerial(s elf):

self.__objLock. acquire()
try:
try:
intNoChars = self.__objSeria lPort.inWaiting ()
if (intNoChars > 0):
strReceivedStri ng =
self.__objSeria lPort.read(intN oChars)
self.newMessage (strReceivedStr ing)
except:
raise
finally:
self.__objLock. release()

Cheers,

Neil

--

Neil Benn
Senior Automation Engineer
Cenix BioScience
BioInnovations Zentrum
Tatzberg 47
D-01307
Dresden
Germany

Tel : +49 (0)351 4173 154
e-mail : be**@cenix-bioscience.com
Cenix Website : http://www.cenix-bioscience.com

Jul 18 '05 #8
engsol wrote:
I'm working on a s/w test program using python
code. Com1 and com2 play a part. The problem is that the python code
has a lot of work to do...and the results from the hardware under test can
come from either com1 or com2...at any time. It may be a few milliseconds,
or several seconds, sometimes minutes, before a response is expected.
I'm not sure what timeout value I'd use. Using threads, and re-loading the
timeout values on the fly may be a solution, but I'm not experienced with
threads....and was hoping to avoid them.


I'm with Grant on this: threads in Python are really easy. If
you can describe a little more about what you need/would like
to do with the serial data when it *does* arrive, and how that
relates to the "lot of work" that the rest of the code is doing,
I'm sure there are a number of helpful replies just waiting to
be written to convince you how easy it really is.

For example, if you need to retrieve the data from the serial
ports right away, so the sending device doesn't block or lose
data, but can afford to just store it for later processing
when the main thread has time, that's only a few lines of
code.

If you actually want to reply immediately, then the only
real question is how you come up with the reply. If it needs
data from the main thread, we can show you several easy and
appropriate ways to do that with no thread safety issues.

If you're not experienced with threads, there's almost no place
better to start learning than with Python and c.l.p. :-)

-Peter
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
11040
by: Bob Greschke | last post by:
We have some equipment that communicates at 57600 baud RS232. The path from the PC is USB to a Phillips USB hub, then off of that a TUSB3410 USB/Serial converter. The driver for the 3410 chip creates a "normal" comm port (like COM3). There is a C++ program that has no problem talking to the equipment, but two Python programs have trouble. One is a big, scary, controller program, and the other, since that is the one I'm having trouble...
6
17694
by: googlinggoogler | last post by:
Hiya, I've got a PIC microcontroller reading me humidity data via rs232, this is in ASCII format. I can view this data easily using hyperterminal or pyserial and convert it to its value (relative humidty with ord(input)) But what im trying to do is plot the data in real time, ideally with pylab - as it looks simple to use and simple is the way i want to go! My code is below, it doesnt show a graph, I was wondering whether
5
9963
by: Mimi | last post by:
Hi, I use the pyserial to read data from a serial port. My code is in window Xp and python 2.4. when I use Hyperteminal I can read data without try and try again that it is not the case with pyserial library. anyone can help me ? this is a part of my code: self.ser = serial.Serial() self.ser.baudrate = 9600
2
3741
by: Jon | last post by:
Hi, I wrote some code to read in info from a port using pyserial. the code reads info sent by a box that is connected to my computer by an rs232-to usb adapter. When I was writing the code and testing it on my computer it worked fine. I ran py2exe on the program (which uses wxpython for its gui) and sent the output from the py2exe to another computer. now when I try to run it on this other computer it fails to open the port. it gives...
0
596
by: pauland80 | last post by:
<snip> <snip> Late thanks for your both answers! (Please excuse me for that) The problem was a bug in the device firmware. But before finding this, I dugg lightly in the pyserial source and found (to take with care!) :
0
2079
by: [david] | last post by:
http://pyserial.sourceforge.net/ "port numbering starts at zero, no need to know the port name in the user program" But the implementation in SerialWin32 is just (Portnum +1)
3
11634
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to be working fine when I test it with Hyperterminal . I am using the example program that comes with pyserial, as below. --------------- import serial
6
6669
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send '%' to serial port and make sure it reached the serial port. 2. Once confirmed, send another character.
2
4160
by: bryanleo | last post by:
We are trying to read data from a microcontroller and interface it through serial port. The output is then displayed in Python using Pyserial or the hyperterminal, the former is more important When you touch the input pins of the microcontroller the value changes real time in hyper terminal. But In the case of the pyserial module, even though you touch the input pins, the value does not change. We have actually noticed that python records and...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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
9947
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...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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
2877
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.