serial and threads

Neil Benn benn at cenix-bioscience.com
Tue Aug 17 13:10:14 CEST 2004


Silke wrote:

>Hi all!
>
>I'm trying to write a program in python using the modules
>'serialwin32' and 'thread' to create one thread that writes to a
>
>  
>
<snip>
Hello,

          I've done a similar implementation, I always assume that - 
unless the documentation for a class specifically states that it is 
thread-safe then I assume it isn't.  The way I get around the issue you 
have is to lock access to the serial object (s in your case).  The way I 
get around this is to use the InWaiting method (or whatever the 
equivalent will be in your serial module) which will return how many 
bytes are waiting, then I can read that number of bytes from the serial 
port, store the information and release the lock on that port. :

        self.__objLock.acquire()
        try:
            intNoChars = self.__objSerialPort.inWaiting()
            if (intNoChars > 0):
                strReceivedString = self.__objSerialPort.read(intNoChars)
                self.newMessage(strReceivedString)
            self.__objLock.release()
        except:
            self.__objLock.release()
            raise   

    Obviously you will also want to lock on all other access to the 
port, close, open, write, etc.

    P.S. Please don't start ranting about my use of Hungarian!!

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 : benn at cenix-bioscience.com
Cenix Website : http://www.cenix-bioscience.com




More information about the Python-list mailing list