[英]Python3: can.Notifier, can.Logger doesn't write to file
在 python 3.9.2 上使用 can.Logger 我無法用 can bus 上的所有內容編寫日志文件
我的 python 腳本:
#! /usr/bin/python3
import can
import time
bus = can.Bus(interface='socketcan',
channel='can2')
fileName = "test.asc"
notifier = can.Notifier(bus, [can.Printer(), can.Logger(fileName, 'a')])
tm1 = time.perf_counter()
while True:
tm2 = time.perf_counter()
if tm2 >= tm1 + 5:
notifier.stop()
tm1=time.perf_counter()
notifier = can.Notifier(bus, [can.Printer(), can.Logger(fileName, 'a')])
當我運行腳本時,“test.asc”文件已正確創建,但里面什么也沒有。
腳本給我這樣的 output:
2
Timestamp: 1647881138.770576 ID: 0302b0a0 X DLC: 8 00 00 55 00 8d 00 00 aa Channel: can2
Timestamp: 1647881139.770898 ID: 0302b0a0 X DLC: 8 00 00 55 00 8d 00 00 aa Channel: can2
Timestamp: 1647881140.770732 ID: 0302b0a0 X DLC: 8 00 00 55 00 8d 00 00 aa Channel: can2
Timestamp: 1647881141.770435 ID: 0302b0a0 X DLC: 8 00 00 55 00 8d 00 00 aa Channel: can2
T
我想要獲得的結果是記錄在 can 總線上出現的所有內容,直到腳本被中斷
編輯
我已經看到問題可能出在我第二次聲明通知程序的地方。 但我有必要每 5 秒阻止一次通知程序。 我怎樣才能重新啟動它?
無法重新啟動 can.Notifier。
但是要寫一個日志文件,其中包含可以總線上出現的所有內容,您可以使用 bus.recv()
#! /usr/bin/python3
import can
import time
bus = can.Bus(interface='socketcan', channel='can2')
fileName = "test.log"
while True:
with open("/home/pi/"+fileName, "a+") as f1:
message = bus.recv()
f1.write(str(message)+'\n')
time.sleep(0.05)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.