【问题标题】:Why is the frequency of this Python-CAN script is shifted?为什么这个 Python-CAN 脚本的频率发生了变化?
【发布时间】:2021-03-22 10:16:15
【问题描述】:

在下面的脚本中,我将数据 [0x01, 0x0a] 发送到地址 0x0050000b,然后在地址 0x0790000b 处搜索答案“01 0a”。消息需要每 100 毫秒发送一次。

问题是由于某种原因,我发送的消息偏移了约 1.3 毫秒,这导致我的 CAN 设备无法接收它。

我在下面添加了 CAN 消息的屏幕截图,

感谢任何帮助!

import can
import time
enable_use_server = True

bus = can.interface.Bus(bustype='neovi', channel='1', bitrate=500000)

# Set Control Mode = Buck and Control Type = V_LOW_I_LIM (new mode)
#send(can_id=0x00500000, ext_id=1, dlc=2, one_shot=1, rtr=0, data=[0x01, 0x0A], wait=100)

msg = can.Message(arbitration_id=0x0050000b, data=[0x01, 0x0a], extended_id=True)
bus.send(msg)
time.sleep(.1)
print('Enter Vlow Ilim Mode sent')

#Verifv
SearchID = str(hex(0x0790000b))
SearchID = SearchID.strip('0x')
found = False

while not found:
    mg = str(bus.recv(0.05))
    if re.search(SearchID, mg):
        print('rcv:  ' + mg)
        if re.search("01 0a", mg):
            print(msg)
            print("Vlow Ilim Mode was Successful")
            found = True
        else:
            print("Vlow Ilim Mode was NOT Successful")
            msg = can.Message(arbitration_id=0x0050000b, data=[0x01, 0x0a], extended_id=True)
            bus.send(msg)
            time.sleep(0.1)

here is the CAN log screenshot

【问题讨论】:

    标签: python can-bus python-can


    【解决方案1】:

    代码本身也需要一些时间来执行。最好在循环中编写 sleep ,例如:

    t = time.time_ns()
    while True:
        ...
        time.sleep((100000 - (time.time_ns() - t)) / 1000000)
        t = time.time_ns()
    

    也许还需要一些调整。

    【讨论】:

      猜你喜欢
      • 2015-08-19
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      • 2021-11-21
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多