大家好,欢迎来到IT知识分享网。
摘要:最近用到python-can,发现网上参考资料实在不多,也缺乏一定的系统性,特此进行一番整理。——by catmemo
注:
抱歉一段时间以来没有时间和精力去更新这份博文,在这段时间以来,主要基于python-can这个库,开发了一个CAN节点模拟仿真工具,有需要的可以去下载来用,当然也欢迎二次开发。后面,时间会相对宽裕些,会继续更新这篇博文。
(当然也包括CAN收发、诊断、图形化显示等功能,详细内容可以在抖音搜索:47:/ 复制打开抖音,看看【一夜春风的作品】CAN 仿真神器——QX工具链之UltraSim… ЭmAELbZr5bntmw8ππ )。
1、简述
python-can库为Python提供了控制器局域网的支持,为不同的硬件设备提供了通用的抽象,并提供了一套实用程序,用于在CAN总线上发送和接收消息。
python-can可以在Python运行的任何地方运行; 从具有商用CAN的高功率计算机到USB设备,再到运行Linux的低功率设备(例如BeagleBone或RaspberryPi)。
更具体地说,该库的一些示例用法如下:
- 被动记录CAN总线上发生的情况。 例如,使用其OBD-II端口监视商用车辆。
- 测试通过CAN交互的硬件。在现代汽车,摩托车,轮船甚至轮椅上找到的模块已经使用此库通过Python测试了组件。
- 在回路中对新的硬件模块或软件算法进行原型设计。 轻松与现有总线进行交互。
- 创建虚拟模块以原型化CAN总线通信。
连接到CAN总线,创建和发送消息的简单实例:
#!/usr/bin/env python # coding: utf-8 """ This example shows how sending a single message works. """ from __future__ import print_function import can def send_one(): # this uses the default configuration (for example from the config file) # see https://python-can.readthedocs.io/en/stable/configuration.html bus = can.interface.Bus() # Using specific buses works similar: # bus = can.interface.Bus(bustype='socketcan', channel='vcan0', bitrate=) # bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=) # bus = can.interface.Bus(bustype='ixxat', channel=0, bitrate=) # bus = can.interface.Bus(bustype='vector', app_name='CANalyzer', channel=0, bitrate=) # ... msg = can.Message(arbitration_id=0xc0ffee, data=[0, 25, 0, 1, 3, 1, 4, 1], is_extended_id=True) try: bus.send(msg) print("Message sent on {}".format(bus.channel_info)) except can.CanError: print("Message NOT sent") if __name__ == '__main__': send_one()
2、安装
可以使用pip命令进行安装:
$ pip install python-can
$ pip install python-can[serial]
2.1 GNU/Linux 依赖项
理论上来说,现代Linux内核(2.6.25或更新版本)有一个socketcan的实现。如果用Python3.3或更高版本调用,此版本的python can将直接使用socketcan,否则将通过ctypes的类型来判断。
2.2 Windows 依赖项
2.2.1 Kvaser
2.2.2 PCAN
请注意,PCANBasic API时间戳从系统启动开始计算秒数。要将这些时间转换为纪元时间,请使用uptime库。否则,时间将以系统启动后的秒数返回。要安装uptime库,请运行pip install python can[pcan]。
如果安装了Python for Windows Extensions库,此库将可以很好的被用于获得新消息的通知,而不是不得不使用的CPU密集型轮询。
2.2.3 IXXAT
2.2.4 NI-CAN
2.2.5 neoVI
查看neoVI
2.2.6 Vector
2.2.7 CANtact
python3 -m pip install cantact
(3)更多相关的文档,请查看cantact.io
2.2.8 CanViewer
python -m pip install "python-can[viewer]"
2.2.9 Installing python-can in development mode
此软件包的“开发版本”安装允许您在本地进行更改,或从Git存储库中获取更新并使用它们,而无需重新安装。下载或克隆源存储库,然后:
python setup.py develop
3、配置
通常,此库与特定的CAN接口一起使用,可以在代码中指定,也可以从配置文件或环境变量中读取。
3.1、代码中指定
can对象暴露了一个rc字典,该字典可用于在从can.interfaces导入之前设置接口和通道。
import can can.rc['interface'] = 'socketcan' can.rc['channel'] = 'vcan0' can.rc['bitrate'] = from can.interface import Bus bus = Bus()
您还可以为每个Bus实例指定接口和通道:
import can bus = can.interface.Bus(bustype='socketcan', channel='vcan0', bitrate=)
3.2、配置文件
在Linux系统上,将在以下路径中搜索配置文件:
- 〜/ 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. > [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. >
from can.interfaces.interface import Bus hs_bus = Bus(context='HS') ms_bus = Bus(context='MS')
3.3、环境变量
可以从以下环境变量中获取配置:
- CAN_INTERFACE
- CAN_CHANNEL
- CAN_BITRATE
3.4、接口名称
| Name | Documentation |
|---|---|
| “socketcan” | SocketCAN |
| “kvaser” | Kvaser’s CANLIB |
| “serial” | CAN over Serial |
| “slcan” | CAN over Serial / SLCAN |
| “ixxat” | IXXAT Virtual CAN Interface |
| “pcan” | PCAN Basic API |
| “usb2can” | USB2CAN Interface |
| “nican” | NI-CAN |
| “iscan” | isCAN |
| “neovi” | NEOVI Interface |
| “vector” | Vector |
| “virtual” | Virtual |
| “canalystii” | CANalyst-II |
| “systec” | SYSTEC interface |
4、库API
主要对象是BusABC和Message。
- Bus
- Thread safe bus
- Message
- Listeners
- Asyncio support
- Broadcast Manager
- Bit Timing Configuration
- Internal API
4.1、Bus
vector_bus = can.Bus(interface='vector', ...)
4.1.1、Transmitting
我们可以调用send()方法并传过去一个Message类型的参数将message发送到CAN总线上,如下所示:
with can.Bus() as bus: msg = can.Message( arbitration_id=0xC0FFEE, data=[0, 25, 0, 1, 3, 1, 4, 1], is_extended_id=True ) try: bus.send(msg) print(f"Message sent on {
bus.channel_info}") except can.CanError: print("Message NOT sent")
周期型信号的发送则通过boradcast manager控制。
4.1.2、Receiving
通过调用recv()方法或者直接在bus上进行迭代可以读取总线上的messages:
with can.Bus() as bus: for msg in bus: print(msg.data)
此外,我们也可以通过Listener api来接收和处理来自Notifier的消息。
4.8、Utilities
can.detect_available_configs(interfaces=None)
[source]
- Parameters
interfaces (Union[None, str, Iterable[str]])
- None:在所有已知接口中搜索 - string:在字符串名称的接口中搜索 - 在可迭代的接口名称中搜索
- Return type
list[dict] - Returns
一个可迭代的dicts,每个dict都适合在can.BusABC的构造函数中使用。
4.9、Notifier
Notifier对象用作总线的消息分发器。
classcan.Notifier(bus, listeners, timeout=1.0, loop=None)
管理can.Message实例向侦听器的分发。
支持多个总线和侦听器。
待续…
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/115034.html