首页 > 编程知识 正文

串口通信和can通信,树莓派can通信

时间:2023-05-05 00:02:50 阅读:172562 作者:1549

项目原件管理从Adruino过渡到CAN,基础部署流程。

硬件

数量

草莓馅饼3

2

杜邦线

6 * 2

MCP2515

2

可以使用MCP2515与任何具有SPI端口的设备进行通信

软件

备注

can-utils

apt/can端口测试

python-can

py2 py3/python脚本can收发

1连接

用MCP2515端子连接后,需要注意图2点

1 . CAN总线可以直接通过1连接

2. 1对1 CAN网测试必须佩戴2所示的(CAN网需要2个120的总线电阻)才能进行通信) ) ) ) ) ) )。

配置CAN

2.1打开SPI

sudo raspi-config

界面选项- SPI

打开SPI

2.2can配置

sudo vim /boot/config.txt

dtparam=spi=on添加到下一行

dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=25,spimaxfrequency=1000000

sudo reboot

dmesg | grep -i '(can|spi) '

将产生以下结果

配置网络can端口(仅一个can端口) )。

sdoiplinksetcan 0类型500000 restart-ms100

sudo ifconfig can0 up

添加启动项目

sudo vim /etc/network/interfaces

追加

allow-hotplug can0

iface can0can静态

bitrate 500000

restart-ms 100

3测试

3.1使用can-utils

sudo apt install can-utils

发送

cansend can0 111#00000000

接收

candump can0

3.2使用python-can

pip 3安装python-can

import can

bus=can.interface.bus (bustype=' socket can ',channel='can0',bitrate=500000 )

msg _ snd=can.message (arbitration _ id=0xc 0f fee,

data=[ 0,25,0,1,3,1,4,1 ],

is_extended_id=True )

#发送消息

try:

是bus.send(msg_snd )

print(messagesenton(}.format ) bus.Channel_info ) )

except can.CanError:

打印(消息不正确) )。

#接收消息

MSG_recv=bus.recv(0.0 ) )。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。