pyserial and com port interrupts
benn at cenix-bioscience.com
Thu Jan 13 07:45:29 EST 2005
>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.
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 :
intNoChars = self.__objSerialPort.inWaiting()
if (intNoChars > 0):
#or you could fire an event
#put any clean up code in here.
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!!!
Senior Automation Engineer
Tel : +49 (0)351 4173 154
e-mail : benn at cenix-bioscience.com
Cenix Website : http://www.cenix-bioscience.com
More information about the Python-list