我想使用Python套接字将GPS数据(longitude, latitude)
发送给另一个。每次运行它都会出错,我的代码是否正确?谢谢。我想将GPS数据发送到另一台服务器,因为数据将被处理为距离。
import serial
import math
import socket
gps = serial.Serial("/dev/ttyUSB0", baudrate=4800)
HOST = '192.168.10.1'
PORT = 4444
BUFFER_SIZE=1024
def longlat():
global lon
global lat
while True:
line = gps.readline()
data = line.split(",")
if data[0] == "$GPRMC":
if data[2]== 'A':
latgps = float(data[3])
if data[4]=='S':
latgps = -latgps
latdeg = int(latgps/100)
latmin = latgps - latdeg*100
lat = latdeg+(latmin/60)
longps = float(data[5])
if data[6]=='W':
longps = -longps
londeg = int(longps/100)
lonmin = longps - londeg*100
lon = londeg+(lonmin/60)
longlat()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST,PORT))
s.send(lon,lat)
data = s.recv(BUFFER_SIZE)
答案 0 :(得分:0)
我使用此代码更改我的代码
import time
from socket import *
import serial
host = '192.168.11.11'
gps = serial.Serial("/dev/ttyUSB0",baudrate=4800)
port = 4444
bufsize = 1024
addr = (host, port)
client = socket(AF_INET, SOCK_STREAM)
client.connect(addr)
while True:
global lon
global lat
line = gps.readline()
data = line.split(",")
if data[0] == "$GPRMC":
if data[2] == 'A':
latgps = float(data[3])
if data[4] == 'S':
latgps = -latgps
latdeg = int(latgps / 100)
latmin = latgps - latdeg * 100
lat = latdeg + (latmin / 60)
longps = float(data[5])
if data[6] == 'W':
longps = -longps
londeg = int(longps / 100)
lonmin = longps - londeg * 100
lon = londeg + (lonmin / 60)
# for i in range(1, 5):
data = ""
for j in range(1, 20000):
rlt = lon
data = rlt
client.sendall(data[:-1])
print ("client send data to server")
time.sleep(5)
# break
client.close()
我收到错误NameError: global name 'lon' is not defined
也许你有解决方案?