一:通过终端实现can数据的收发
将MCP2515与SN65HVD230双芯片组合模块的CAN0_H与CAN1_H,CAN0_L与CAN1_L相连
1:安装can-utils:
sudo apt-get install can-utils
2:打开一个终端,用来接收数据
candump can0
3:打开另一个终端,用来发送数据
cansend can1 000#11.22.33.44
如图所示
二:通过代码实现can数据的收发
注意:1,bitrate 100000 通信速率一定要设置你所用的,不然数据接发都会失败,2,每次程序运行完,记得关闭can,如果每次运行都打开一次,会崩的,3,arbitration_id这里设置成自己的报文id
发送端:
import time import os import can import threading #连接can0 can0 = can.interface.Bus(channel = 'can0', bustyp = 'socketcan_ctypes') msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False) #一直循环发送数据 a = 1 while a == 1 : # 设置CAN0通信扩展板 os.system('sudo ip link set can0 type can bitrate 100000') os.system('sudo ifconfig can0 up') can0.send(msg) print(msg) time.sleep(3) #关闭can os.system('sudo ifconfig can0 down')
接收端:
import time import os import can import threading #连接can1 can1 = can.interface.Bus(channel = 'can1', bustyp = 'socketcan_ctypes') #一直循环接收数据 a = 1 while a == 1 : # 设置CAN1通信扩展板 os.system('sudo ip link set can1 type can bitrate 100000') os.system('sudo ifconfig can1 up') msg = can1.recv(3.0) print(msg) #关闭can os.system('sudo ifconfig can1 down')