473,406 Members | 2,954 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,406 software developers and data experts.

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 15765
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********@peak.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********@peak.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********@peak.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(self):

self.__objLock.acquire()
try:
try:
intNoChars = self.__objSerialPort.inWaiting()
if (intNoChars > 0):
strReceivedString =
self.__objSerialPort.read(intNoChars)
#or you could fire an event
self.newMessage(strReceivedString)
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(self):

self.__objLock.acquire()
try:
try:
intNoChars = self.__objSerialPort.inWaiting()
if (intNoChars > 0):
strReceivedString =
self.__objSerialPort.read(intNoChars)
self.newMessage(strReceivedString)
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
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...
6
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...
5
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...
2
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...
0
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...
0
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
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...
6
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...
2
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.