我目前正在一个项目的数据记录器上工作。数据记录器应从车载访问平台上的两个不同CAN总线系统读取数据,解码原始数据并定期将其存储在json文件中,该文件将在机组停机前上传到blob存储器。我正在使用一个基于raspberry pi的硬件,并用Python 3.7编程。 除了定期存储数据外,一切正常。在json文件中创建和存储数据的工作开始得很好,并在100毫秒内完成。一分钟后,您开始看到两个数据点之间的时间漂移。如果代码运行数小时,则两个数据点之间的时间间隔可达分钟
我知道Linux不是RTOS,但应该可以以100ms的速度保存数据。那么,我的方法是错误的吗
我尝试使用threading.Event而不是sleep,我还尝试将其作为多线程进程而不是线程运行
下面是我遇到问题的函数代码:
def create_json_file():
global FILE_INITIALIZED
global FILE_NAME
once = bool
try:
while not stop_create.is_set():
if not store_data:
next_call = time.time()
if store_data == True:
if not once:
next_call = time.time()
once = True
# Calculate the offset taking into account daylight saving time
utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone
utc_offset = datetime.timedelta(seconds=-utc_offset_sec)
timestamp = datetime.datetime.now().replace(tzinfo=datetime.timezone(offset=utc_offset)).isoformat()
tmp_json_dict = {}
tmp_json_dict['deviceId'] = DEVICE_ID
tmp_json_dict['timestamp'] = timestamp
tmp_json_dict.update(processed_data)
if FILE_INITIALIZED == False:
first_json_dict = []
first_json_dict.append(tmp_json_dict)
FILE_NAME = './storage/' + DEVICE_ID + datetime.datetime.now().strftime("_%Y%m%d_%H%M.json")
#/home/pi/Test
if FILE_INITIALIZED == False:
with open(FILE_NAME,"w+") as outputfile:
json.dump(first_json_dict,outputfile,indent=4,sort_keys=False)
FILE_INITIALIZED = True
first_json_dict.clear()
else:
with open(FILE_NAME, "r+") as file:
file_data = json.load(file)
file_data.append(tmp_json_dict)
file.seek(0)
json.dump(file_data, file, indent=4)
next_call = next_call + INTERVAL_TIME
time.sleep(abs(next_call - time.time()))
except Exception as err:
logging.exception(err)
if not stop_create.is_set():
# Only log exception if we were not going to stop the thread
# When quitting, the main thread calls close() on the serial device
# and read() may throw an exception. We don't want to display it as
# we're stopping the script anyway
global thread_exception_file
thread_exception_file = sys.exc_info()
以下是一段时间后带漂移的输出:
[
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:24:13.480148+01:00"
},
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:24:13.580144+01:00"
},
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:24:13.679995+01:00"
},
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:24:13.780075+01:00"
},
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:24:13.880101+01:00"
},
...
...
...
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:25:20.078147+01:00"
},
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:25:20.316632+01:00"
},
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:25:20.556145+01:00"
},
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:25:20.797479+01:00"
},
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:25:21.041794+01:00"
},
{
"deviceId": "Prototype_FieldTest_P300KS_Facelift",
"timestamp": "2020-11-28T22:25:21.288214+01:00"
}
]
目前没有回答
相关问题 更多 >
编程相关推荐