Python-can库的使用(2)——配置

🙆‍♂️我是纯良思安,爱技术、爱分享,更爱生活🙆‍♂️

💖喜欢的朋友可以关注一下,下次更新不迷路💖


目录

在代码中配置

配置文件

环境变量

接口名称表


前言

通常,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中配置,步骤如下:

  1. 打开Vector Hardware Config
  2. 点击左上角Application
  3. 点击Add
  4. 在弹出的Application settings中输入你想要创建的Application name
  5. 创建成功后在Hardware列表中任意硬件通道右键可以看到新的应用名称

配置文件

在Linux和macOS系统上,会按照以下路径搜索配置文件:

  1. ~/can.conf
  2. /etc/can.conf
  3. $HOME/.can
  4. $HOME/.canrc

在Windows系统中,会在下述路径中搜索配置文件:

  1.  %USERPROFILE%/can.conf
  2. can.ini(当前工作目录)
  3. %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

"canalystii"

CANalyst-II

"cantact"

CANtact CAN Interface

"etas"

ETAS

"gs_usb"

Geschwister Schneider and candleLight

"iscan"

isCAN

"ixxat"

IXXAT Virtual Communication Interface

"kvaser"

Kvaser’s CANLIB

"neousys"

Neousys CAN Interface

"neovi"

Intrepid Control Systems neoVI

"nican"

National Instruments NI-CAN

"nixnet"

National Instruments NI-XNET

"pcan"

PCAN Basic API

"robotell"

Robotell CAN-USB interface

"seeedstudio"

Seeed Studio USB-CAN Analyzer

"serial"

CAN over Serial

"slcan"

CAN over Serial / SLCAN

"socketcan"

SocketCAN

"socketcand"

socketcand Interface

"systec"

SYSTEC interface

"udp_multicast"

Multicast IP Interface

"usb2can"

USB2CAN Interface

"vector"

Vector

"virtual"

Virtual

 可以通过Plugin Interface.添加其他接口类型


🎈如果文章对您有帮助,您可以“点赞、收藏、关注”,这也是我创作动力的源泉🎈
💘感谢支持💘

  • 12
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值