python实现简单can数据的收发

一:通过终端实现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')


  • 2
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路漫漫其远,吾求索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值