目前在将我的两个代码合并为一个代码时遇到了问题。代码1连续打印出全球定位系统数据,但一旦添加代码2,它只打印一次。我怀疑时间循环可能是问题所在,但我不确定。
如能提供任何帮助,将不胜感激:)
组合代码:
import os
import sys
import time
import serial
import datetime
from gps import *
ser = serial.Serial(
port = '/dev/ttyS0',
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1
)
moment = time.strftime("%d-%m-%Y %H:%M",time.localtime())
try:
gpsd = gps(mode=WATCH_ENABLE)
except:
print('ERROR: Cannot set time')
sys.exit()
while True:
gpsd.next()
if gpsd.utc != None and gpsd.utc != '':
gpsutc = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + gpsd.utc[11:19]
os.system('sudo date -u --set="%s"' % gpsutc)
sys.exit()
if ser.in_waiting > 0:
x = ser.readline().decode('utf-8')
file = open('GPS ' + moment + '.txt', "a")
file.write(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f")[:-3] + " ")
file.write(str(x))
file.close()
print(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f ")[:-3])
print(x)代码1(收集全球定位系统数据并存储到文本文件):
import time
import serial
import datetime
ser = serial.Serial(
port = '/dev/ttyS0',
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1
)
moment = time.strftime(%d-%m-%Y %H:%M", time.localtime())
while True:
if ser.in_waiting > 0:
x = ser.readline().decode('utf-8')
file = open('GPS ' + moment + '.txt', "a")
file.write(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f)[:-3] + " ")
file.write(str(x))
file.close()代码2(将树莓Pi时间设置为相等的全球定位系统时间):
import os
import sys
import time
from gps import *
try:
gpsd = gps(mode=WATCH_ENABLE)
except:
print('ERROR: Cannot set time')
sys.exit()
while True:
gpsd.next()
if gpsd.utc != None and gpsd.utc != '':
gpsutc = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + gpsd.utc[11:19]
os.system('sudo date -u --set="%s"' % gpsutc)
sys.exit()发布于 2022-05-01 15:27:57
我不拥有GPS硬件,所以我无法验证,但是合并代码中的第二个sys.exit()让我感到刺痛:我假设代码2中对应的sys.exit()一旦读取成功,脚本就会结束--这是有道理的。但是在组合脚本中,它仍然会结束整个脚本。
我的第一个想法是删除它,但随后您将经常重新设置系统时间,这可能会导致进一步的问题;也许最好在合并的脚本中保留两个循环,只需将一个sys.exit()替换为by。类似于:
import os
import sys
import time
import serial
import datetime
from gps import *
ser = serial.Serial(
port = '/dev/ttyS0',
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1
)
moment = time.strftime("%d-%m-%Y %H:%M",time.localtime())
try:
gpsd = gps(mode=WATCH_ENABLE)
except:
print('ERROR: Cannot set time')
sys.exit()
while True:
gpsd.next()
if gpsd.utc != None and gpsd.utc != '':
gpsutc = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + gpsd.utc[11:19]
os.system('sudo date -u --set="%s"' % gpsutc)
break
while True:
if ser.in_waiting > 0:
x = ser.readline().decode('utf-8')
file = open('GPS ' + moment + '.txt', "a")
file.write(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f")[:-3] + " ")
file.write(str(x))
file.close()
print(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f ")[:-3])
print(x)https://stackoverflow.com/questions/72077931
复制相似问题