Bob Greschke wrote: > "Peter Hansen" <[EMAIL PROTECTED]> wrote in message > >>I'd call it very unusual >>(in my experience) to have a program open and close a serial port >>repeatedly. > > 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.
Generally, yes, you just dump data received up to the point you are about to send a new request (if this is a request/response type of thing). PySerial has a .flush() method of some kind I believe, or you can just loop as long as inWaiting() isn't False. If you aren't using hardware or software handshaking, then the buffer filling up is a non-issue for you or the sender. If you're using handshaking, then you do have to keep the buffer from filling. Pretty much all my interesting code runs the PySerial stuff in a separate thread, reading whenever data is available, and (this is a simplification) posting it to a Queue which the main thread can read from as required. In that scenario, you could just have a flag that causes the receive thread to stop posting stuff to the Queue where you currently have close the port to prevent data being seen. I don't recall: is NMEA 0183 asynchronous? Messages can be sent by the GPS even when you didn't ask for one explicitly? If so, then it's possible there are problems even with your current approach: what if you open the port halfway through a packet, and receive only the last few bytes? If it's synchronous (data sent only in response to your requests), then none of this is an issue since there will be no traffic unless you ask for it, so the serial port will be idle between uses. -Peter -- http://mail.python.org/mailman/listinfo/python-list