🙆♂️我是纯良思安,爱技术、爱分享,更爱生活🙆♂️
💖喜欢的朋友可以关注一下,下次更新不迷路💖
目录
前言
通常,Python-can库与特定的CAN接口一起使用,这可以在代码中指定,也可以从配置文件或环境变量中读取。
在代码中配置
can对象公开了一个rc字典,可以用来设置接口和通道,以Vector 的VNbox为例(VN1640A、VN1630A、VN5620等均可)
import can
can.rc['interface'] = 'vector'
can.rc['bustype'] = 'vector'
can.rc['channel'] = '0'
can.rc['app_name'] = 'Python_can'
#波特率配置,启用CANFD
can.rc['fd'] = True
can.rc['bitrate'] = 500000
can.rc['data_bitrate'] = 2000000
from can.interface import Bus
bus = Bus()
这里需要注意的是app_name需要自己在Vector Hardware Config中配置,步骤如下:
- 打开Vector Hardware Config
- 点击左上角Application
- 点击Add
- 在弹出的Application settings中输入你想要创建的Application name
- 创建成功后在Hardware列表中任意硬件通道右键可以看到新的应用名称
配置文件
在Linux和macOS系统上,会按照以下路径搜索配置文件:
- ~/can.conf
- /etc/can.conf
- $HOME/.can
- $HOME/.canrc
在Windows系统中,会在下述路径中搜索配置文件:
- %USERPROFILE%/can.conf
- can.ini(当前工作目录)
- %APPDATA%/can.ini
配置文件设置默认接口和通道:
[default]
interface = <the name of the interface to use>
channel = <the channel to use by default>
bitrate = <the bitrate in bits/s to use by default>
配置还可以包含其他部分(或上下文):
[default]
interface = <the name of the interface to use>
channel = <the channel to use by default>
bitrate = <the bitrate in bits/s to use by default>
[HS]
# All the values from the 'default' section are inherited
channel = <the channel to use>
bitrate = <the bitrate in bits/s to use. i.e. 500000>
[MS]
# All the values from the 'default' section are inherited
channel = <the channel to use>
bitrate = <the bitrate in bits/s to use. i.e. 125000>
from can.interface import Bus
hs_bus = Bus(context='HS')
ms_bus = Bus(context='MS')
环境变量
可以从以下环境变量中提取配置:
- CAN_INTERFACE
- CAN_CHANNEL
- CAN_BITRATE
- CAN_CONFIG
CAN_CONFIG环境变量允许使用JSON设置任何总线配置。例如:
CAN_INTERFACE=socketcan CAN_CONFIG={"receive_own_messages": true, "fd": true}
接口名称表
Name | Documentation |
---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
可以通过Plugin Interface.添加其他接口类型
🎈如果文章对您有帮助,您可以“点赞、收藏、关注”,这也是我创作动力的源泉🎈
💘感谢支持💘