擅长:python、mysql、java
<p>您需要做的是定期轮询“session.next()”—这里的问题是您正在处理一个串行接口—您将按接收结果的顺序获取结果。由您来维护具有最新检索值的“当前值”。</p>
<p>如果不轮询session对象,最终UART FIFO将填满,并且无论如何也不会得到任何新值。</p>
<p>考虑为此使用一个线程,不要等待用户调用gps_poll(),您应该正在轮询,当用户需要新值时,他们会使用返回当前值的“get_current_value()”。</p>
<p>从我的头顶上看,可能是这样简单的事情:</p>
<pre><code>import threading
import time
from gps import *
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.session = gps(mode=WATCH_ENABLE)
self.current_value = None
def get_current_value(self):
return self.current_value
def run(self):
try:
while True:
self.current_value = self.session.next()
time.sleep(0.2) # tune this, you might not get values that quickly
except StopIteration:
pass
if __name__ == '__main__':
gpsp = GpsPoller()
gpsp.start()
# gpsp now polls every .2 seconds for new data, storing it in self.current_value
while 1:
# In the main thread, every 5 seconds print the current value
time.sleep(5)
print gpsp.get_current_value()
</code></pre>