我无法使简单的脚本在Python2.7中正常工作,而在Python3中无法正常工作。脚本:
enter code here{
#!/usr/bin/env python
# simple test program to test serial I/O data from gps
#
# Dec 2019 HRK
import Adafruit_BBIO.UART as UART
import serial
UART.setup("UART2")
ser2 = serial.Serial('/dev/ttyO2', timeout=2)
def parseGPS(gps_data):
if gps_data[0:6] == "$GNRMC":
sdata=gps_data.split(",")
if sdata[2] == 'V':
print("Sat data not valid")
return
print("Parsing GNRMC data")
time=sdata[1][0:2]+":"+sdata[1][2:4]+":"+sdata[1][4:6]
lat=decode(sdata[3])
dirlat=sdata[4]
lon=decode(sdata[5])
dirlon=sdata[6]
print('GMT: {} lat: {}{} long: {}{}'.format(time, lat, dirlat, lon, dirlon))
def decode(coord):
# converts dddmm.mmmmm > DD deg MM.MMMMM min
x = coord.split(".")
head = x[0]
tail = x[1]
tail = tail[0:2]
deg = head[0:-2]
min = head[-2:]
return deg + ":" + min + "." + tail
print("receiving GPS data")
while True:
data = ser2.readline()
parseGPS(data)
}
Python2提供了预期的响应。 Python3刚在打印后停止(“接收GPS数据”)。我知道两个Python版本在序列上的差异,但是Google搜索没有为我带来解决方案。问题可能出在ser2.readline()语句。请指教。提前致谢,哈克
当您初始化'ser2'时,您会遗漏很多东西。尝试设置更多参数以确保它们正确。到家后,我将尝试重新创建该错误并帮助您获取一些正常工作的代码这篇文章的例子Python Serial: How to use the read or readline function to read more than 1 character at a time
serial.Serial( port='COM5',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)