Python3: can.Notifier, can.Logger 不写入文件
Python3: can.Notifier, can.Logger doesn't write to file
在 python 3.9.2 上使用 can.Logger 我无法将 can 总线上出现的所有内容写入日志文件
我的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”文件已正确创建,但里面没有任何内容。
脚本给我这样的输出:
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。
但是要将所有出现在 can bus 上的内容写入日志文件,您可以使用 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)
在 python 3.9.2 上使用 can.Logger 我无法将 can 总线上出现的所有内容写入日志文件
我的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”文件已正确创建,但里面没有任何内容。
脚本给我这样的输出:
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。
但是要将所有出现在 can bus 上的内容写入日志文件,您可以使用 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)