Receive and order GPS data in Raspberry

1

I am trying to receive some data from a NEO M6 GPS module in my Raspberry Pi 3 and I have already verified that there is communication between them. I have this script in python but it does not work for me. I do not understand python.

import gps

session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
x= 1

while x == 1:
report = session.next()
if report['class'] == 'TPV':
if hasattr(report, 'time'):
print 'Hora: ' + str(report.time)
if hasattr(report, 'lat'):
print 'Latitud: ' + str(report.lat)
if hasattr(report, 'lon'):
print 'Longitud: ' + str(report.lon)
if hasattr(report, 'speed'):
print 'Velocidad: ' + str(report.speed)
if hasattr(report, 'track'):
print 'Rumbo: ' + str(report.track)
if hasattr(report, 'head'):
print report.head
x= 0

But when I created the .py and executed it, my Raspberry told me this

File"testgps.py", line 10 
if hasattr(report, 'time'):
^
IndentationError: expected an indented block

How can I get this program or a similar one to work for me?

The program was taken from the Raspberry forum and it seems that the people did work for it. I have to say that I have installed everything they said was necessary. Thank you very much for your time.

    
asked by wasous 27.05.2017 в 13:36
source

1 answer

2

It is an indentation error since after a if a block of code is expected to be executed if the condition is met. Your code is completely indented, I do not know if you have it or it is a problem when copying it here.

Indenting in Python is crucial , it's not mere aesthetics. It is the way in which this language defines the blocks of code, it does not use keys or reserved words (BEGIN, END, etc) as other languages do.

The code should look something like this:

import gps

session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

x = 1
while x == 1:
    report = session.next()
    if report['class'] == 'TPV':
        if hasattr(report, 'time'):
            print 'Hora: ' + str(report.time)
        if hasattr(report, 'lat'):
            print 'Latitud: ' + str(report.lat)
        if hasattr(report, 'lon'):
            print 'Longitud: ' + str(report.lon)
        if hasattr(report, 'speed'):
            print 'Velocidad: ' + str(report.speed)
        if hasattr(report, 'track'):
            print 'Rumbo: ' + str(report.track)
        if hasattr(report, 'head'):
            print report.head
        x = 0

However, you are not capturing the possible exceptions such as the iterator ends without if report['class'] == 'TPV' being fulfilled, something more general that prints as long as there is available data:

import gps

session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

while True:
    try:
        report = session.next()
        if report['class'] == 'TPV':
            if hasattr(report, 'time'):
                print 'Hora: ' + str(report.time)
            if hasattr(report, 'lat'):
                print 'Latitud: ' + str(report.lat)
            if hasattr(report, 'lon'):
                print 'Longitud: ' + str(report.lon)
            if hasattr(report, 'speed'):
                print 'Velocidad: ' + str(report.speed)
            if hasattr(report, 'track'):
                print 'Rumbo: ' + str(report.track)
            if hasattr(report, 'head'):
               print report.head
            print

    except KeyError:
        pass
    except KeyboardInterrupt:
        quit()
    except StopIteration:
        session = None
        print "GPSD has terminated"

The script will print all the data available in the iterator. You can stop the script by pressing Ctrl + c at any time.

Always use four spaces to devise between levels to follow the recommendations of PEP 8 and do not use tabs (if you used them, never mix with spaces).

The code is valid for Python 2.x.

    
answered by 27.05.2017 / 14:17
source