[英]Why is the frequency of this Python-CAN script is shifted?
在下面的脚本中,我将数据 [0x01, 0x0a] 发送到地址 0x0050000b,然后在地址 0x0790000b 处搜索答案“01 0a”。 消息需要每 100 毫秒发送一次。
问题是由于某种原因,我发送的消息被移动了~1.3ms,这导致我的 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)
代码本身也需要一些时间来执行。 最好在循环中编写 sleep ,例如:
t = time.time_ns()
while True:
...
time.sleep((100000 - (time.time_ns() - t)) / 1000000)
t = time.time_ns()
也许还需要一些调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.