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

发布于 2025-01-15 23:33:18 字数 1251 浏览 0 评论 0原文

在 python 3.9.2 上使用 can.Logger 我无法编写一个日志文件,其中包含所有可以总线的

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总线上到达我的所有内容,直到脚本被中断

编辑

i'我发现问题可能出在我第二次声明通知者的地方。 但我有必要每 5 秒阻止一次通知程序。我怎样才能重新启动它?

using can.Logger on python 3.9.2 i'm not able to write a log file with everything that comes on can bus

my python script:

#! /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.

the script give me an output like this:

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

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. how can I restart it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2025-01-22 23:33:18

无法重新启动 can.Notifier。

但要写入包含 CAN 总线上所有内容的日志文件,您可以使用 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)

it's not possible to restart can.Notifier.

but to write a log file with everything that comes on can bus you can use 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文