我有这个gps代码,它获取gps数据并写入日志:
#!/usr/bin/python
from systemd import journal
import gps
import time
import threading
import datetime
# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
while True:
try:
report = session.next() # Wait for a 'TPV' report and display
the current time
# To see all report data, uncomment the line below
#print report
if report['class'] == 'TPV':
if hasattr(report, 'time'):
timestamp = (time.time()*1000)
#print timestamp
if hasattr(report, 'lat'):
latitude = report.lat
#print latitude
if hasattr(report, 'lon'):
longitude = report.lon
#print longitude
if hasattr(report, 'alt'):
altitude = report.alt
#print altitude
else:
timestamp = (time.time()*1000)
latitude = 0
longitude = 0
altitude = 0
journal.send(
channel = 'gps',
priority = journal.Priority.INFO,
timestamp = "%f" % (timestamp),
latitude = "%f" % (latitude),
longitude = "%f" % (longitude),
altitude = "%f" % (altitude),
)
except KeyError:
pass
except KeyboardInterrupt:
quit()
except StopIteration:
session = None
print "GPSD has terminated"我得到了这个错误:
Traceback (most recent call last):
File "gps-messi.py", line 57, in <module>
altitude = "%f" % (altitude),
NameError: name 'altitude' is not defined有趣的是,代码有时工作得很好,有时会给我这个错误。我不明白我应该怎么做才能让它一直正确地工作。是不是跟她的大爆炸有关?
发布于 2018-08-29 00:13:36
非常感谢大家。这就是我的代码现在的样子。我测试了一下,它看起来没问题。但是,如果我仍然滑到某个地方,有人能检查一下行journal.send的缩进吗?如果我所做的是正确的(我已经修改了一些变量的名称并增加了速度):
#!/usr/bin/python
from systemd import journal
import gps
import time
import threading
import datetime
# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
while True:
try:
report = session.next() # Wait for a 'TPV' report and display the current time
# To see all report data, uncomment the line below
#print report
timestamp = 0
latitude = 0
longitude = 0
altitude = 0
speed = 0
if report['class'] == 'TPV':
if hasattr(report, 'time'):
timestamp = (time.time()*1000)
#print timestamp
if hasattr(report, 'lat'):
latitude = report.lat
#print latitude
if hasattr(report, 'lon'):
longitude = report.lon
#print longitude
if hasattr(report, 'alt'):
altitude = report.alt
#print altitude
if hasattr(report, 'speed'):
speed = report.speed
#print speed
journal.send(
channel = 'gps',
priority = journal.Priority.INFO,
timestamp = "%f" % (timestamp),
lat_deg = "%f" % (latitude),
lon_deg = "%f" % (longitude),
alt_m = "%f" % (altitude),
speed_mps = "%f" % (speed),
)
except KeyError:
pass
except KeyboardInterrupt:
quit()
except StopIteration:
session = None
print "GPSD has terminated"https://stackoverflow.com/questions/52060580
复制相似问题