Example: 50-Arduino-Boards/Nicla-Vision/50-Board-Control/can.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# CAN示例
#
# 此示例演示了两台相机之间的CAN通信。
# 注意:运行此示例需要两个CAN收发器扩展板和DB9电缆。
import time
import omv
from pyb import CAN
# 注意:在接收节点上设置为False。
TRANSMITTER = True
can = CAN(1, CAN.NORMAL, baudrate=125_000, sample_point=75)
# 注意:取消注释以手动设置位时序,例如:
# can.init(CAN.NORMAL, prescaler=32, sjw=1, bs1=8, bs2=3)
can.restart()
if TRANSMITTER:
while True:
# 发送ID为1的消息
can.send("Hello", 1)
time.sleep_ms(1000)
else:
# 在接收节点上运行。
if omv.board_type() == "H7": # FDCAN
# 设置过滤器以接收ID为1到4的消息
# 过滤器索引,模式(RANGE、DUAL或MASK),FIFO(0或1),参数
can.setfilter(0, CAN.RANGE, 0, (1, 4))
else:
# 设置过滤器以接收ID为1、2、3和4的消息
# 过滤器索引,模式(LIST16等),FIFO(0或1),参数
can.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4))
while True:
# 在FIFO 0上接收消息
print(can.recv(0, timeout=10000))