python版本:3.8
linux系统
项目需求:需要在ok3568平台上面接收振动传感器传来的数据,通讯方式为Can。
1.Can传感器配置
在控制台输入以下命令:
ifconfig can0 down #关闭can0口
ip link set can0 type can bitrate 500000 #配置can0的波特率为500k
ip link set can0 up type can #设置can0口类别为can
ifconfig can0 up #打开can口
candump can0& #监听Can0
cansend can0 00000000#0101 #发送的数据根据自己的传感器通讯协议
配置好之后,Can通讯传感器不断在控制台上打印数据,证明Can通讯传感器配置成功。需要注意的是断电之后需要重新对传感器进行配置。
![](https://img-blog.csdnimg.cn/f7567286ea3344e5be2a2d82c9db26a0.png)
2.Python第三方包安装
pip install python-can
接收CAN通讯数据的Python代码:
import can
can_bus = can.interface.Bus(channel='can0', bustyp='socketcan_ctype')
msg = can_bus.recv()
# print(msg)
get_data = msg.data
id = msg.arbitration_id
# print(id)
运行代码结果报错:
AttributeError when using python-can (module 'can' has no attribute 'interface')
解决方法:
pip install python-can==2
pip install python-can --upgrade
仍然报错,报错内容如下:
出现报错:can.exceptions.CanInterfaceNotImplementedError: Unknown interface type "None"
博主参照这篇文章解决了问题,附上链接