MartijnBraam / gpsd-py3

Python 3 GPSD client
107 stars 45 forks source link

Attribute error in reccomended basic script. #16

Closed Zanati123 closed 6 years ago

Zanati123 commented 6 years ago

Hi there, I'm attempting to attach a GPS hat (successfully receiving data from) for my Rpi running Ubuntu Stretch using the recommended starting script:

#!/usr/bin/env python3
import gpsd

# Connect to the local gpsd
gpsd.connect

#Get gps position
packet = gpsd.get_current()

print(packet.position)

However I receive a attribute error:

Traceback (most recent call last):
  File "/home/pi/gps/gps_v1.py", line 11, in <module>
    packet = gpsd.get_current()
  File "/home/pi/.local/lib/python3.5/site-packages/gpsd/__init__.py", line 278, in get_current
    gpsd_stream.write("?POLL;\n")
AttributeError: 'NoneType' object has no attribute 'write'

I am running python 3.5, and installed correctly via pip3. Would you have any recommendations to fix this?

Thank you, Daniel

MartijnBraam commented 6 years ago

You're not supposed to use the whole example script, you probably want to remove the second gpsd.connect line since you want to connect to the default gpsd daemon.

I don't have a GPS hat for my rpi so I have no idea how the rest is supposed to work, you can check if gpsd is running correctly by starting cgps and checking if it gets a position

Zanati123 commented 6 years ago

Hi, Thank you for the prompt response. I had copied and pasted the sample code from github without realising the two instances of an attempt to connect. I've updated my comment to reflect the attribute error which is caused by the code i did in fact use the first time and now:

#!/usr/bin/env python3
import gpsd

# Connect to the local gpsd
gpsd.connect

#Get gps position
packet = gpsd.get_current()

print(packet.position)

With the same attribute error:

Traceback (most recent call last):
  File "/home/pi/gps/gps_v1.py", line 11, in <module>
    packet = gpsd.get_current()
  File "/home/pi/.local/lib/python3.5/site-packages/gpsd/__init__.py", line 278, in get_current
    gpsd_stream.write("?POLL;\n")
AttributeError: 'NoneType' object has no attribute 'write'

I am using a Adafruit Ultimate GPS HAT and it is pumping out gps data for me. Any recommendations?

Thank you, Dan

MartijnBraam commented 6 years ago

gpsd.connect should be gpsd.connect(). The that block of code seems to be working fine for me with by gps module otherwise.

Zanati123 commented 6 years ago

Thank you! The answer was obviously too simple.