0

I have Python code from source which let to treat GPRMC format. I modified the code where data is from "gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC" and is a list like this:

$GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W*70
$GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W*70

So when i launch it i got this error :

data = ser.readline()
AttributeError: 'str' object has no attribute 'readline

Below is a part of the code:

port = "gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC"

print("Receiving GPS data")
ser = port

find = False
while find == False:
    print("Waiting for GPRMC data")
    data = ser.readline()
if data[0:6] == "$GPRMC":
    parsingData = data.split(",")
    print(parsingData)
if parsingData[2] == "A":
    parseGPS(data)
    find = True

Please help me to fix this. ps: i'm not python coder it's just several days i begin this language and sorry for my bad english

jww
  • 97,681
  • 90
  • 411
  • 885
google
  • 1
  • 1
  • 1
    i do see that you call a variable twice which is `port` and `ser` ! which you calling it via `data = ser.readline()` ! and then you call `string method` which is faulty. also for `parseGPS` it's not defined ! – αԋɱҽԃ αмєяιcαη Nov 16 '19 at 15:34
  • This is just a part of the code where the function is defined in the link – google Nov 16 '19 at 15:41

2 Answers2

0

From what i read: you run

gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC

in the os and it returns lines like

$GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W*70

This is my solution. Hope that it helps:

from subprocess import Popen, PIPE
from re import search 

process = Popen("gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC", stdout=PIPE, shell=True, universal_newlines=True) 
while True: 
    line = str(process.stdout.readline())
    if not line: 
        break

    print("Receiving GPS data")
    find = False
    print(line)

    while find == False:  
        if search(r'$GPRMC', line):
            print("Waiting for GPRMC data")
            parsingData = line.strip().split(",")
            print(parsingData)
            if parsingData[2] == "A":
                parseGPS(data)
                find = True
Razvan I.
  • 239
  • 1
  • 5
  • i tried your code and only the message "Receiving GPS data" is displayed. And when i press Ctrl +D i have : ser = os.popen("gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC").read().split('\n') KeyboardInterrupt It looks like that it's blocked in this – google Nov 16 '19 at 16:36
  • Run this command manualy on your os till it works: gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' etc. Then come back to the code and edit the ser variable. – Razvan I. Nov 16 '19 at 17:21
  • gpsd -d | grep "$GPRMC" | awk -F ':' '{print $2}' ==> blank screen and for gpsd -d | awk -F ':' 'BEGIN{OFS=":"}{print $2}' | grep GPRMC it is OK. – google Nov 16 '19 at 17:23
  • Then it is a command that output values in an infinite loop ? In this case my solution won t work. Or you have just to wait till it processes all the lines if the command has an end. – Razvan I. Nov 16 '19 at 17:25
  • So i can't predict how many lines will be in the output.It should process each line from the output gpsd -d | awk -F ':' 'BEGIN{OFS=\":\"}{print $2}' | grep GPRMC . – google Nov 16 '19 at 17:33
  • Even though the 'parsingData' is defined, i got this one : Receiving GPS data Receiving GPS data Waiting for GPRMC data Traceback (most recent call last): File "gps.py", line 119, in if parsingData[2] == "A": NameError: name 'parsingData' is not defined – google Nov 16 '19 at 18:12
  • infinite display of(Waiting for GPRMC data) until i cancel it. It seems Popen handles badly the command. ^CTraceback (most recent call last): File "gps.py", line 114, in print("Waiting for GPRMC data") KeyboardInterrupt – google Nov 16 '19 at 19:01
  • Try again. If this doesn't work try to debug, because is hard for me to see if it is working or not because i can't test it. – Razvan I. Nov 16 '19 at 19:13
  • It stucks displaying one line a several time and i cancel it and got: ^CTraceback (most recent call last): File "gps.py", line 116, in if search(r'$GPRMC', line): File "/usr/lib/python2.7/re.py", line 146, in search return _compile(pattern, flags).search(string) File "/usr/lib/python2.7/re.py", line 230, in _compile def _compile(*key): KeyboardInterrupt – google Nov 16 '19 at 20:35
0

There are two problems here.

(1) ser is a string. You need to run the commands in that string before there is any output to get.

(2) once there is output, you won’t use readline — that’s how you read lines from an open file, and your output wont be in a file.

Familiarize yourself with how to run a OS command via python. There are many ways to do that. If you don’t know where to start, here’s a great overview: https://stackoverflow.com/a/92395

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80