linux CAN协议最大速率



我正在编写一个程序,该程序每秒必须处理20000条CAN消息。当使用socketcan在linux下用python编写代码时,当msg_per_second超过200时,我似乎开始丢失消息。

限制因素是什么?我如何修改程序或操作系统设置以避免丢失can消息?

当msg_per_second为1000时,下面的代码显示了大约990的计数。他们应该是平等的。

#!/usr/bin/env python3
import time
import can 
import datetime
count = 0
count_print_time = datetime.datetime.now()
bus = can.Bus(interface='socketcan', channel='vcan0', bitrate=500000)
msg = can.Message(arbitration_id=0x123, data=[1, 2, 3, 4, 5, 6], is_extended_id=True)
msg_per_second = 1000
period = 1 / msg_per_second
bus.send_periodic(msg, period)
while(True):
bus.recv(timeout=None)
count = count + 1
time_elapsed = datetime.datetime.now() - count_print_time
if time_elapsed.total_seconds() >= 1:
count_print_time = datetime.datetime.now()
print(f"count : {count}")
count = 0

输出:

计数:989计数:988计数:988计数:990计数:990计数:990计数:990

我的问题可能与接收缓冲区大小有关。当我运行以下代码时,我总是能读取278条消息:

import can
count = 0
bus1 = can.Bus(interface='socketcan', channel='vcan0', bitrate=500000)
bus2 = can.Bus(interface='socketcan', channel='vcan0', bitrate=500000)
for i in range(1000):
msg = can.Message(arbitration_id=i, data=[1, 2, 3, 4, 5, 6], is_extended_id=True)
bus1.send(msg)
while(True):
msg = bus2.recv(timeout=None)
count = count + 1
print(f"count: {count}") 

输出:计数:1计数:2。。。计数:277计数:278

我查看了socketcan文档,找不到有关缓冲区大小的信息。

为recv((函数计时,需要800到1200个us才能执行。对于1200us,这提供了每秒833条消息的最大接收速率。

如果接收处理时间超过发送处理时间,消息可能会在缓冲区中累积,最终在缓冲区满时丢弃。

我一直找不到增加缓冲区大小的方法。第二个代码片段总是给我最后278条消息。其余的消息将被丢弃。

最新更新