历程上说CAN的初始化有两种办法。
第一种:can = CAN(2, CAN.NORMAL, baudrate=250_000, sampling_point=75
第二种:can.init(pyb.CAN.NORMAL, prescaler=16, sjw=2, bs1=7, bs2=3) #250kb/s
但是我用第一种方法进行初始化后可以正常通讯,用第二种办法的时候无法进行通讯,想问问是怎么回事呢?(因为第一种办法没办法设置波特率为800kBps,所以我需要第二种方法自己设置参数使得波特率为800
# CAN Shield Example
#
# This example demonstrates CAN communications between two cameras.
# NOTE: you need two CAN transceiver shields and DB9 cable to run this example.
import pyb
import time, omv
from pyb import CAN
# NOTE: Set to False on receiving node.
TRANSMITTER = False
#can = pyb.CAN(2)
can = CAN(2, CAN.NORMAL, baudrate=250_000, sampling_point=75)
#can.init(pyb.CAN.NORMAL, prescaler=16, sjw=2, bs1=7, bs2=3) #250kb/s
# NOTE: uncomment to set bit timing manually, for example:
#can.init(CAN.NORMAL,prescaler=18, sjw=1, bs1=8, bs2=3)
can.restart()
if (TRANSMITTER):
while (True):
# Send message with id 1
can.send('Hello', 1)
time.sleep_ms(1000)
else:
# Runs on the receiving node.
if (omv.board_type() == 'H7'): # FDCAN
# Set a filter to receive messages with id=1 -> 4
# Filter index, mode (RANGE, DUAL or MASK), FIFO (0 or 1), params
can.setfilter(0, CAN.RANGE, 0, (1, 1638))
else:
# Set a filter to receive messages with id=1, 2, 3 and 4
# Filter index, mode (LIST16, etc..), FIFO (0 or 1), params
can.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4))
while (True):
# Receive messages on FIFO 0
#print(can.any(0))
message =can.recv(0, timeout=10000)
messagetex = message[3]
if (messagetex[0] == 0x01 ):
print(123)
else:
print(message)
# print(can.any(0)))

用第二种办法没有办法通讯的
can = CAN(2, CAN.NORMAL, baudrate=250_000, sampling_point=75
baudrate直接改为800_000不行吗?

你用的是什么硬件?我没报错。

@kidswong999 openmv4连接了一个CAN收发器,接到can转USB上
我使用OpenMV4 固件版本3.9.4也不会报错。