Python-CAN 脚本接收了一半的预期 CAN 消息

Python-CAN script receiving half of the expected CAN messages

我已经使用 Python-CAN 库编写了一个 Python 脚本,它以 1 秒的速率记录接收到的 CAN 消息 5 分钟,然后将所有消息记录到一个文件中并退出。计算机有一个连接到 CAN 总线的 CAN 模块。 (总线上的另一个设备是引擎)我使用SocketCAN接口与它通信。

这台计算机所连接的测试引擎系统正在以我认为是 250kb 的波特率发送大约 114 条消息。我希望看到文件中每 1 秒记录 114 条消息,但我看到的是这个数字的一​​半左右。 (~65 条消息)

有没有可能是引擎的 ECU 设置了 500kb 的波特率,这就是为什么我没有得到我期望的计数的原因?我认为如果波特率不匹配,将无法进行通信,但我没有对系统的物理访问权限,因为我是通过 OTA 更新远程发送脚本,而不是 运行 亲自发送脚本。 (该设备是无头的,但在启动时设置为 运行 脚本)我只看到生成的日志文件。

这里是 python 代码:

(注意,我有将接收到的消息解析为包含的信号的代码,但我没有在此处包含此代码,因为它发生在最后,并且不相关)

class logging:

    def __init__(self):
        #Dictionary to hold received CAN messages
        self.message_Dict = {}

        #List to hold queued dictionaries
        self.message_Queue = []

        #A "filters" object is also created here, but I did not include it
        #I have verified the filters are correct on my test system

    def main(self):

        #Record the current time
        currentTime = datetime.datetime.now()

        #Record the overall start time
        startTime = datetime.datetime.now()

        #Record the iteration start time
        lastIterationStartTime = currentTime

        #Create the CanBus that will be used to send and receive CAN msgs from the MCU
        canbus = can.interfaces.socketcan.SocketcanBus(channel='can0', bitrate=250000)
        #These filters are setup correctly, because all the messages come through
        #on my test system, but I did not include them here
        canbus.set_filters(self.Filters)

        # Creating Listener filters and notifier
        listener = can.Listener()

        #Main loop
        while 1:        

            #create a variable to hold received data
            msg2 = canbus.recv()

            #Record the current time
            currentTime = datetime.datetime.now()

            #If a valid message is detected
            if(msg2 != None):
               if(len(msg2.data) > 0):
                   try:                                
                       #Save the message data into a queue (will be processed later)
                       self.message_Dict[msg2.arbitration_id] = msg2.data

                   except:
                       print("Error in storing CAN message")

            #If 1 second has passed since the last iteration, 
            #add the dictionary to a new spot in the queue
            if((currentTime - lastIterationStartTime) >= datetime.timedelta(seconds=1)):

                #Add the dictionary with messages into the queue for later processing
                messageDict_Copy = self.message_Dict.copy()
                self.message_Queue.append(messageDict_Copy)

                print("Number of messages in dictionary: " + str(len(self.message_Dict)) + " 
                   Number of reports in queue: " + str(len(self.message_Queue)))

                #Clear the dictionary for new messages for every iteration
                self.message_Dict.clear()

                #Record the reset time
                lastIterationStartTime = datetime.datetime.now()
            
            #Once 5 minutes of data has been recorded, write to the file
            if((currentTime - startTime) > datetime.timedelta(minutes=5)):
                
                #Here is where I write the data to a file. This is too long to include

                #Clear the queue
                self.message_Queue = []

                #Clear the dictionary for new messages for every iteration
                self.message_Dict.clear()

#When the script is run, execute the Main method
if __name__ == '__main__':  
    mainClass = logging()
    mainClass.main()

感谢您的任何想法或意见。谢谢

根据我的经验,大多数引擎的ECU通常使用250kb,但最新的使用500kb。我建议你也尝试两者。

另外,消息只有在发送后才会出现在公交车上,这看起来很傻,但是例如卡车,如果您不踩油门,则不会出现涉及油门的消息。因此,您可能需要检查是否所有组件都按预期使用。 can-utils的lib有个'Can sniffer'也可以帮到你

我建议您使用 'can-utils' 来帮助您。这是一个强大的罐头分析工具。

你试过循环波特率吗?也许也可以帮助找到另一个。