pySerial Windows write problem

Neil Benn benn at cenix-bioscience.com
Fri Jul 29 07:20:52 EDT 2005


Bob Greschke wrote:

>"Peter Hansen" <peter at engcorp.com> wrote in message >
>  
>
>>Actually, I'm curious why you don't do the same.  I'd call it very unusual 
>>(in my experience) to have a program open and close a serial port 
>>repeatedly.  Among other things, this means that the DSR/DTR lines are 
>>toggling high and low repeatedly, and that alone could cause undesirable 
>>behaviour in certain devices.
>>    
>>
>
>I guess I could.  It's just that not releasing the port/not KNOWING that the 
>port has been closed at predictible times is our chief complaint about the 
>C++ program.   As an aside, when I left work I left a version of the 
>while-loop program running, opening the port, writing to the equipment, 
>getting a response, and closing the port.  It was up to about 180 sucessful 
>cycles (in a row -- it will stop if it fails).  I think it's a hardware 
>problem. :)
>
>
>  
>
Hmm, keep the port open.  One bad thing that can happen is that if you 
don;t keep the port open then another program running on the box and nip 
in and claim ownership of the port.  I agree with Peter, I never 
relinquish the port in code unless I have a reason to (ie shutdown or 
chnaging the COM port I'm using).  I doubt that it is a hardware problem 
on your device as the RS232 tandard (I prefer to call it a rumour) 
doesn't have any kind of RRP. Even if you are running RTS/CTS or 
XON/XOFF then you again shouldn't have a problem becuase once the data 
is sent.received then the lines should be back to normal.  If you wish 
to test this and am not happy with writing C code to test it then maybe 
you could try it with something that doens;t use C code such as Java 
(with the javax.comm or rxtx extensions)?

>>In none of my own serial-based programs (perhaps a few dozen such to date) 
>>have I ever opened and closed a port other than at startup and shutdown 
>>(just as your C++ program does).  Unless you've got a good reason to do 
>>otherwise, if this solves your problem it's certainly the most direct 
>>approach to do so.
>>    
>>
>
>One of the serial ports (there are actually two) is used to read some NMEA 
>sentences from a GPS.  It is only rarely read.  If it is also opened when 
>the program starts and kept open would you just dump the buffer and then 
>read to get the most current info?  What happens when the buffer fills up? 
>The "main" port is just commands sent, responses received kind of traffic.
>  
>
PySerial doesn;t have any kind of event firing to notify you when data 
is available.  The way I get round this is to have a loop polling (in a 
seperate thread) to see if any data is available (it's a method on the 
interface), then read all the data in and fire this off to my 
listeners/observers with the read data.  That way your buffers will not 
fill up.  I would also do that on your other port as well to give you a 
common way of receiving data.  I did this and then started downloading 
random stuff of wiki and sending the data from one serial port to 
another in huge chunks with no problems.

>I'd write a C extension to do the serial stuff, but I don't know a thing 
>about Windows programming.
>
>Thanks!
>
>
>  
>
I wouldn't bother as well - PySerial works fine for me, as I mentioned 
the only thing I don;t like is no evetn firing when data is availabel 
but you can code around taht if you wish to.

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