我有一个运行的代码,我在Raspberry pi启动时在crontab中运行(代码在python“idle”中运行正常)。它有一个无限的while循环。一段时间后,python脚本停止运行。我在网上搜索并得到了可能导致他们的以下答案
此时我真的很困惑它背后的原因是什么。我需要帮助来理解背后的原因。还有什么是在启动raspberry pi时运行此代码的最佳方法。 crontab是否需要调整或是否有其他方法?请详细回复,因为我是EE专业,不熟悉linux,raspberry pi(因为我刚开始使用它)和python。代码如下:
import threading
import os
import time
import serial
import httplib
import math
#GPS data
os.chdir('/home/pi/Desktop')
#Distance function
def Distance(lat1, long1,lat2,long2):
degree_to_rad = float(math.pi / 180.0)
d_lat = (lat2 - lat1) * degree_to_rad
d_long = (long2 - long1) * degree_to_rad
a = pow(math.sin(d_lat / 2), 2) + math.cos(lat1 * degree_to_rad) * math.cos(lat2 * degree_to_rad) * pow(math.sin(d_long / 2), 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
km = 6367 * c
mi = 3956 * c
return km
#global variables
global ser
global recieveBuffer
global recieveFlag
global webAddress
global data
#Serial Communication Settings
baud = 9600;
#port = 'Com4'
port ='/dev/ttyAMA0'
#default WebAddress and Data
webAddress = "gpsmaster.eu5.org"
webData = "/post.php?"
#Lat and Lng Coordinates, Initialied at Gate
lat=31.470997
lng=74.411116
lumsLat=31.470997
lumsLng=74.411116
ser = serial.Serial(port, baud)
if not ser.isOpen():
ser.open()
recieveBuffer="NULL"
recieveFlag=0
def shutDown():
os.system("sudo shutdown -h now")
def readSerial():
global recieveBuffer
global recieveFlag
global ser
while True:
recieveBuffer = ser.readline()
recieveFlag=1
# Function to Post Data, Return Y for success, N for failure
def httpGet(webAddress,webData):
try:
#print(webAddress)
#print(webData)
conn = httplib.HTTPConnection(webAddress)
conn.request("GET",webData)
res = conn.getresponse()
#print res.status, res.reason
conn.close()
except:
print("Exceptoion!!")
return ['N','Failed']
else:
return ['Y',res]
def replayMaster(ser,recieved):
global lat
global lng
recieved=recieved.replace("\n", "")
recieved=recieved.replace("\r", "")
tokenized = recieved.split(',');
command = tokenized[0]
if command=='AT':
ser.write('<OK>')
elif command=='POST':
if lat!=0 and lng != 0: # and Distance(lumsLat,lumsLng,lat,lng)<50
lat = float(tokenized[1])
lng = float(tokenized[2])
ans = httpGet(webAddress,"%slat=%f&lng=%f" % (webData, lat,lng))
#with open("logFile.txt","a") as fileStream:
# fileStream.write("%s,%f,%f\r\n" % (ans[0],lat,lng))
#fileStream.close()
if ans[0]=='N':
ser.write('<ERROR>')
else:
ser.write('<'+`ans[1].status`+'>')
else:
ser.write('<Invalid Coordinates>')
print ("Invalid Coordinates")
elif command=='CLOSE':
ser.close()
elif command=='HALT':
ser.write('<Shutting Down>');
shutDown()
else:
ser.write('<Unknown Command>')
serialReadThread = threading.Thread(target=readSerial)
serialReadThread.start()
ser.write('<OK>')
while True:
#p#rint Distance(31.470997,74.411116,31.469723,74.512244)
if recieveFlag:
replayMaster(ser,recieveBuffer)
print(recieveBuffer)
recieveFlag=0
print("waiting: |%f|,|%f|"%(lat,lng))
time.sleep(0.4)
答案 0 :(得分:1)
我也遇到了 crontab 的一些问题。我在重新启动后使用短时间延迟修复了它。
@reboot (sleep 10; python3 ABSOLUTE_PATH/example.py)
也许这会有所帮助