Python Serial Inwaiting Example
Hi all, Have been attempting to read in NMEA sentences from a GPS unit - unfortunately whilst I am able to do this fine in a HyperTerminal window whenever I try to do this using Vizard & PySerial I just get a load of weird characters with the occasional sentence creeping through! I have set the baud rate (4800) and everything else to be the same as HyperTerm but with no joy. - confusingly however if I choose the plain 'text out' option for the GPS unit then Vizard reads this in no problem at all (with all the same settings on the Vizard side)?? Has anyone got any idea why can Vizard/python read the plain text output but not NMEA sentences?? Peace out Vizard Wizards. Hi there, If you could give us a little more information then I think we can solve this problem for you, I have written a GPS parser myself using PySerial and it works fine for me. You mention that there are a lot of weird characters.
Can you give us a sample so we can see what you are getting? Are you polling the serial port at a fast enough rate? Are you checking for cases where there is no data to be read in? When you initialize pySerial, make sure you set the timeout to 0, and obviously the COM and Baud Rate to the correct values. To read in data: def readSerial(self): buf = self.serial.read(4096) if len(buf) >0: print 'Read serial -%d bytes'% (len(buf), buf) self.incoming += buf Note that you should read in more than one byte at a time, to improve performance. You should call readSerial() every time Vizard refreshes the display.
Also, you need to read everything into a buffer, and keep reading until you have a complete line terminated with a r or n character. Once you have this, process the string up to the line terminator. You may have other characters after the line terminator, so keep them because they will form part of the next NMEA sentence. Please give the above suggestions a try, and if you are still stuck then please send us some code and data samples and we might be able to spot something in there. Regards, Wayne. Code: while 1: ser = serial.Serial(port=0, baudrate=4800, timeout=0) line = ser.readline() print line #DEBUG ser.close()'line' is then passed to a parser which has been developed with the aid of a text file of a GPS output (NMEA). I've included two text files - GPS DUMP is what is received when the GPS is outputting NMEA sentences and the TXT file shows what is received when the GPS unit is outputting text only.
I am using the pyserial module and getting values in. >>>>>>Here are the relevant bits of a serial to tcp/ip app that I use. Most >>>likely you have the wrong baudrate set. Ram Amritwani By Anuradha Paudwal Download. If it is an older GPS device the >>>baudrate will be 4800, newer devices allow you to set it to higher >>>speeds, 9600 being standard.