简体   繁体   English

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

[英]Python-CAN script receiving half of the expected CAN messages

I have written a Python script utilizing the Python-CAN library which records received CAN messages at a 1 second rate for 5 minutes, before logging all the messages into a file and exiting.我已经使用 Python-CAN 库编写了一个 Python 脚本,该脚本以 1 秒的速度记录接收到的 CAN 消息,持续 5 分钟,然后将所有消息记录到文件中并退出。 The computer has a CAN module which is connecting to the CAN bus.计算机有一个连接到 CAN 总线的 CAN 模块。 (The other device on the bus is an engine) I communicate with it using the SocketCAN interface. (总线上的另一个设备是引擎)我使用 SocketCAN 接口与它通信。

The test engine system that this computer is connected to is sending around 114 messages at what I believe is a 250kb baud rate.这台计算机所连接的测试引擎系统正在以我认为 250kb 的波特率发送大约 114 条消息。 I am expecting to see 114 messages recorded in the file for each 1 second period, but instead I'm seeing about half that count.我希望看到文件中每 1 秒记录 114 条消息,但我看到的数量大约是一半。 (~65 messages) (~65 条消息)

Could it be possible that the engine's ECU is set to a 500kb baud rate, and that's why I'm not getting the count I am expecting?发动机的 ECU 是否有可能设置为 500kb 的波特率,这就是为什么我没有得到我期望的计数? I would think there would be no communication if the baud rates do not match, but I do not have physical access to the system because I'm sending the script remotely through an OTA update and not running it myself.如果波特率不匹配,我认为不会有任何通信,但我没有对系统的物理访问权限,因为我通过 OTA 更新远程发送脚本而不是自己运行它。 (The device is headless, but is setup to run the script on startup) I just see the log files that are generated. (设备是无头的,但设置为在启动时运行脚本)我只看到生成的日志文件。

Here is the python code:这是python代码:

(A note, I have code parsing the received messages into the contained signals, but I did not include this code here because it happens at the end, and it is not relevant) (注意,我有代码将接收到的消息解析为包含的信号,但我没有在此处包含此代码,因为它发生在最后,并且不相关)

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()

I appreciate any ideas or input you have.我感谢您的任何想法或意见。 Thank you谢谢

In my experience, most of the engine's ECU usually uses 250kb, but the newest ones are using 500kb.根据我的经验,大多数引擎的ECU通常使用250kb,但最新的使用500kb。 I would suggest you too also try the both.我建议你也尝试两者。

Also, the messages will only come to the bus if they have been sent, it seems silly but for example a truck, if you don't step on the accelerator the messages referring to the accelerator will not appear.此外,如果消息已经发送,消息只会到达公共汽车,这看起来很傻,但例如卡车,如果你不踩油门,则不会出现与油门相关的消息。 So, maybe you need to check if all components are being using as you expect.因此,也许您需要检查是否所有组件都按预期使用。 The lib of can-utils has a 'Can sniffer' that can also help you. can-utils的 lib 有一个“Can嗅探器”,它也可以帮助你。

I suggest you to use 'can-utils' to help you in that.我建议你使用'can-utils'来帮助你。 It is a powerful tool to can analyses.它是一个强大的工具,可以进行分析。

Did you try to loop the baudrate?您是否尝试循环波特率? Maybe can also help to find another.也许还可以帮助找到另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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