WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Reading form serial port strangeness (https://forum.worldviz.com/showthread.php?t=2789)

nige777 06-16-2010 09:20 AM

Reading form serial port strangeness
 
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 :confused: .

- 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

wayne 06-16-2010 10:13 AM

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

nige777 06-17-2010 07:31 AM

wow, thanks for the reply Wayne, thats great to know

-not at the office today which is where the GPS unit is :D however I have a quick question about implimenting readSerial - I know its my lack of OO know-how but I am a little confused as to how to pass the open serial port to the function :o :o
In my first attempts I did something like this:
Code:

ser = serial.Serial(0, 4800, timeout=1)
- I'm now aware that the timeout parameter was incorrect and neither is this:-
Code:

readSerial(ser)
Sorry for my n00bishness but I'd be very grateful for any pointers you could give me :D
??

nige777 06-17-2010 01:22 PM

I think I should clarify - my last post was as clear as mud! :o - what I meant to say is that I am also aware that I can't just call the function with
Code:

readSerial(ser)

nige777 06-18-2010 06:54 AM

2 Attachment(s)
Ok, I am back in the office.


The code I have been using to connect to the GPS is as follows:
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 really like your buffer method but am at a loss on how to implement it :o
I am off to pour over the documentation for PySerial again as I am sure I'm missing something obvious :confused:

wayne 06-21-2010 04:54 PM

I had a look at your code below. The problem is that you are opening up the serial port, reading a line, and then closing the port. Then it is reopened when you go through the while loop. You should not repeatedly open and close a serial port, it should just be opened once, and then you can call readline() within the while loop.

Also, the readSerial() code I included earlier was cut and pasted out of a class that I had written for myself. So it defined a method called readSerial(), but if you fix up your previous code with the suggestion I made then it should work well.


Quote:

Originally Posted by nige777 (Post 10240)
Ok, I am back in the office.


The code I have been using to connect to the GPS is as follows:
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 really like your buffer method but am at a loss on how to implement it :o
I am off to pour over the documentation for PySerial again as I am sure I'm missing something obvious :confused:


nige777 06-22-2010 12:04 PM

Thanks for the heads up there Wayne, as you suspected my serial settings were wrong :D however readline() would still not work correctly with 'NMEA out' enabled but work fine with 'Text out'! :confused:
However I have ended up using the following approach - Thanks StackOverflow :D
Code:

buffer = ''
while True:
        buffer = buffer + ser.read(ser.inWaiting())
        if '\n' in buffer:
                lines = buffer.split('\n') # Splits buffer block into lines
                last_received = lines[-2]
                buffer = lines[-1]
                print last_received

which is working pretty much how I want it to :)


All times are GMT -7. The time now is 09:37 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC