User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Finding a Line of GPS data

Fri Nov 10, 2017 3:33 am

Hi, I'm able to read all the lines of data from a GPS module connected via usb, but I only want to see one or two of the lines..

Code: Select all

 import serial
 ser=serial.Serial('dev/ttyUSB0',9600)
 textln=ser.readline()
 while 1:
 	print (textln )
 	
All lines ( and there are many :o ) start with "$GP". How do I pick out the particular line I require ( $GPGGA) and print it? :?
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
rpdom
Posts: 15606
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Finding a Line of GPS data

Fri Nov 10, 2017 6:32 am

That code is wrong. It will read a line of text from the serial port, then print the same line over and over again for ever.

Try putting the ser.readline inside the while loop

Code: Select all

import serial
ser=serial.Serial('dev/ttyUSB0',9600)
while 1:
    textln=ser.readline()
    print (textln)
That's still a bit messy as it will keep trying to read lines forever. I don't use python much, it may be possible to do a "while(textln = ser.readline()):"
Last edited by rpdom on Fri Nov 10, 2017 10:16 pm, edited 1 time in total.

ghp
Posts: 1420
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Finding a Line of GPS data

Fri Nov 10, 2017 6:39 am

Hello,
you can check a line of code with string method startswith:

Code: Select all

import serial
ser=serial.Serial('dev/ttyUSB0',9600)
while 1:
    textln=ser.readline()
    if textln.startswith( '$GPGGA'):
        print (textln)
Good luck with your project !

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Finding a Line of GPS data

Fri Nov 10, 2017 10:11 pm

Thanks for the replies,
Long time no "see" Dom. ;)
ghp - I'll try that code, it looks like it might do what I want. I'll let you know. :)
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
rpdom
Posts: 15606
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Finding a Line of GPS data

Fri Nov 10, 2017 10:17 pm

Hi Ace :-)

We have more spiders now. Some gone, some new ones.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Finding a Line of GPS data

Fri Nov 10, 2017 11:17 pm

Thanks ghp that code does just what I wanted it to. I was a bit worried at first as I ran it from the Python shell and it returned an error (something to do with tuples or bytes) however when the py file was run from the command line it worked 8-)

So tell me Dom, what is the count now - reply in octal.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
rpdom
Posts: 15606
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Finding a Line of GPS data

Sat Nov 11, 2017 5:31 am

Ace Rimmer wrote:
Fri Nov 10, 2017 11:17 pm
So tell me Dom, what is the count now - reply in octal.
In octal :lol: the current count is 50. We're not adding to the collection at the moment, just concentrating on looking after the current ones.

We've got two old men who must be on their last (many) legs. They've lived a lot longer than expected for a mature male of those types.
Typically they get 6 to 18 months after reaching adulthood. These two have both survived over 3 years as adults so far.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Finding a Line of GPS data

Sat Nov 11, 2017 6:06 am

That's a lot of legs Dom. I think I know how the old ones feel! Just had great success with the above line of code that ghp supplied. I'm now able to pick out whichever NMEA sentence I require, turn it into a list and then pick out different elements (altitude etc).

Thanks again ghp
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Finding a Line of GPS data

Sun Nov 12, 2017 11:16 pm

OK, my next problem, this code works great

Code: Select all

 import serial

ser=serial.Serial(' /dev/ttyUSB0',9600)
while 1:
    textln=ser.readline()
    if ( '$GPGGA' ) in textln:
        listba=textln.split( ' , ')
        print ' your altitude is ',listba[9], ' metres above sealevel'
but instead of printing line after line I would like it to erase the line just printed and print it again with the updated value on the same line.
I'm using Python 2.7.* Any ideas?
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Finding a Line of GPS data

Mon Nov 13, 2017 2:27 am

Try this technique to overwriting python output

Code: Select all

#!/usr/bin/python

import sys
import time

for i in range(0,101,10):
 sys.stdout.write("\r[%3d%%]" % i)
 sys.stdout.flush()
 time.sleep(1)

sys.stdout.write("\n")
sys.stdout.flush()

for i in range(0,10):
 sys.stdout.write("\rBlinking ...")
 sys.stdout.flush()
 time.sleep(0.5)
 sys.stdout.write("\r         ...")
 sys.stdout.flush()
 time.sleep(0.5)

sys.stdout.write("\n")
sys.stdout.flush()
The \r returns to the start of the line, the flush ensures the line is written to stdout.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Finding a Line of GPS data

Mon Nov 13, 2017 3:42 am

Thanks Dougie, I'll give it a try this arvo if the boss doesn't catch me. I gather I just use "sys.stdout.write" instead of "print", I haven't used that method before. Cheers.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Finding a Line of GPS data

Tue Nov 14, 2017 4:58 am

Yep, that worked - once I'd figured out how to concanate the components of the string - "print" is much easier! But oh for days of Qbasic with -

Code: Select all

locate (row,column)
:roll:
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

Return to “Python”