简体   繁体   English

Python3: can.Notifier, can.Logger 不写入文件

[英]Python3: can.Notifier, can.Logger doesn't write to file

using can.Logger on python 3.9.2 i'm not able to write a log file with everything that comes on can bus在 python 3.9.2 上使用 can.Logger 我无法用 can bus 上的所有内容编写日志文件

my python script:我的 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')])

when I run my script, the "test.asc" file is correctly created but there's nothing inside.当我运行脚本时,“test.asc”文件已正确创建,但里面什么也没有。

the script give me an output like this:脚本给我这样的 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

the result I would like to obtain is to log everything that comes to me on the can bus until the script is interrupted我想要获得的结果是记录在 can 总线上出现的所有内容,直到脚本被中断

EDIT编辑

i've seen that the problem probaly is where i declare notifier for the second time.我已经看到问题可能出在我第二次声明通知程序的地方。 but it is necessary for me to block the notifier every 5 seconds.但我有必要每 5 秒阻止一次通知程序。 how can I restart it?我怎样才能重新启动它?

it's not possible to restart can.Notifier.无法重新启动 can.Notifier。

but to write a log file with everything that comes on can bus you can use bus.recv()但是要写一个日志文件,其中包含可以总线上出现的所有内容,您可以使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM