首页 > 解决方案 > Python 使用 gps 和 gps3 库的问题

问题描述

我正在尝试使用 python (3.8.2) 中的 GPS 库获取 gps 数据,但一直抛出相同的错误,即

[WinError 10061] 由于目标机器主动拒绝,无法建立连接

还尝试了 gps3 库并不断收到相同的错误。这是我的代码:

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)
       except StopIteration:
            pass

if __name__ == '__main__':

   gpsp = GpsPoller()
   gpsp.start()

   while 1:
       time.sleep(5)
       print(gpsp.get_current_value()) 

这是错误的回溯,以防万一

Traceback (most recent call last):
  File "test_gps_poller.py", line 27, in <module>
    gpsp = GpsPoller()
  File "test_gps_poller.py", line 11, in __init__
    self.session = gps(mode=WATCH_ENABLE)
  File "C:\Users\user\AppData\Roaming\Python\Python37\site-packages\gps\gps.py", line 197, in __init__
    gpscommon.__init__(self, host, port, verbose, reconnect)
  File "C:\Users\user\AppData\Roaming\Python\Python37\site-packages\gps\client.py", line 43, in __init__
    self.connect(self.host, self.port)
  File "C:\Users\user\AppData\Roaming\Python\Python37\site-packages\gps\client.py", line 69, in connect
    self.sock.connect(sa)
  ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

任何帮助将不胜感激,谢谢!

标签: pythongps

解决方案


推荐阅读