处理发送至 URL/IP 的 NMEA 数据

working with NMEA data sent to URL/IP

我有一堆设备可以将 NMEA 语句发送到 URL/ip。 看起来像这样

"$GPGGA,200130.0,3447.854659,N,11014.636735,W,1,11,0.8,41.4,M,-24.0,M,*53"

我想读入这些数据,解析它并将关键部分上传到数据库。我知道如何解析它并将其上传到数据库,但我完全不知道如何将数据 "read"/accept/get 数据转换为 python 程序以便我可以解析和上传。

我的第一个想法是将它指向 Django 页面,然后让 Djanog 解析它并上传到数据库(数据将从 Django 站点访问)但是它是 NMEA 语句而不是 HTTP 请求所以 Django 拒绝它作为"message Bad request syntax"

阅读发送至 url/IP 的 NMEA 句子的最佳 (python) 方式是什么?

谢谢

我假设您有一些具有以太网连接的硬件,它通过以太网连接输出 NMEA 字符串。这可能默认有一些随机的 192.168.0.x ip 地址并通过端口 12002 或其他东西吐出数据

您通常会创建一个套接字来侦听此传入数据

server.py

import socket
host = "" #Localhost
port = 12002 
PACKET_SIZE=1024 # how many characters to read at a time
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((host,port))
sock.listen(5) # we should never have more than one client
def work_thread(client):
    while True: #continuously read and handle data
      data = client.recv(PACKET_SIZE)
      if not data:
          break # done with this client
      processData(data)

while True:
     client,addr = sock.accept() # your script will block here waiting for a connection
     t = threading.Thread(target=work_thread,args=(client,))
     t.start()

有时您需要 ping 设备以获取数据

client.py

import socket
host = "192.168.0.204" #Device IP
port = 12002 
PACKET_SIZE=1024 # how many characters to read at a time
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((host,port)) #connect to the device
while True: #continuously read and handle data
    data = sock.recv(PACKET_SIZE)
    processData(data)