首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-CAN脚本接收一半预期的CAN消息

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

Stack Overflow用户
提问于 2021-11-10 18:10:03
回答 1查看 822关注 0票数 1

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

这台计算机连接的测试引擎系统以250 at的波特率发送了大约114条消息。我期望看到每1秒记录在文件中的114条消息,但是我却看到了大约一半的记录。(~65条信息)

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

下面是python代码:

(注意,我有将接收到的消息解析为包含的信号的代码,但是我在这里没有包含这段代码,因为它发生在末尾,而且与此无关)

代码语言:javascript
复制
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()

我很感激你的任何想法或意见。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-09 12:05:44

根据我的经验,大多数发动机的ECU通常使用250 my,但最新的使用500 my。我建议你也试试这两种。

此外,消息只有在被发送时才会到达总线上,这似乎很愚蠢,但是,例如,一辆卡车,如果您不踩上加速器,就不会出现涉及加速器的消息。因此,您可能需要检查是否所有组件都按照您的预期使用。罐用库有一个“can嗅探器”,它也可以帮助您。

我建议你用“可以-利用”来帮助你。它是一个强大的工具,可以进行分析。

你有没有试着循环波德率?也许也能帮你找到另一个。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69918185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档