python-can库基于PCAN-USB使用方法
一、概述
1.介绍
python-can库为Python提供了控制器局域网的支持,为不同的硬件设备提供了通用的抽象,并提供了一套实用程序,用于在CAN总线上发送和接收消息。
支持硬件接口:
|
Name |
Documentation |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2.环境搭建
Python安装:https://www.python.org/ftp/python/3.7.9/python-3.7.9-amd64.exe
PCAN-USB驱动:https://www.peak-system.com/fileadmin/media/files/pcan-basic.zip
库:pip install python-can
3.参考文档
https://python-can.readthedocs.io/en/master/#
二、常用方法
1.接收报文
from can.interfaces.pcan.pcan import PcanBus def bus_recv():
"""轮询接收消息"""
try:
while True:
msg = bus.recv(timeout=100)
print(msg)
except KeyboardInterrupt:
pass if __name__ == '__main__':
bus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)
bus_recv()
2.发送报文
from can.interfaces.pcan.pcan import PcanBus def bus_send():
"""can消息发送"""
while True:
time.sleep(0.02)
try:
bus.send(msg)
print("消息发送 {}".format(bus.channel_info))
except can.CanError:
print("消息未发送") if __name__ == '__main__':
msg = can.Message(arbitration_id=0x181DFF00, data=[0xEE, 0xFE, 0xFE, 0xFF, 0xFE, 0xFF, 0xFF, 0xFE],
is_extended_id=True) # 报文
bus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)
bus_send()
3.定期发送报文
def bus_send_periodic():
"""周期发送报文"""
print("开始每200毫秒发送一条消息。持续时间10s")
task = bus.send_periodic(msg, 1.5) # 定期发送
if not isinstance(task, can.ModifiableCyclicTaskABC): # 断言task类型
print("此接口似乎不支持")
task.stop()
return
time.sleep(5) # 持续时间
print("发送完成")
print("更改运行任务的数据以99开头")
msg.data[0] = 0x99
task.modify_data(msg) # 修改data首字节
time.sleep(10) task.stop()
print("停止循环发送")
print("将停止任务的数据更改为单个 ff 字节")
msg.data = bytearray([0xff]) # 重新定向data
msg.dlc = 1 # 定义data长度
task.modify_data(msg) # 修改data
time.sleep(10)
print("重新开始")
task.start() # 重新启动已停止的周期性任务
time.sleep(10)
task.stop()
print("完毕") if __name__ == '__main__':
msg = can.Message(arbitration_id=0x181DFF00, data=[0xEE, 0xFE, 0xFE, 0xFF, 0xFE, 0xFF, 0xFF, 0xFE],
is_extended_id=True) # 报文
bus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)
bus_send_periodic()
4.循环收发消息
from can.interfaces.pcan.pcan import PcanBus def send_cyclic(stop_event):
"""循环发送消息"""
print("开始每1秒发送1条消息")
start_time = time.time()
while not stop_event.is_set():
msg.timestamp = time.time() - start_time
bus.send(msg)
print(f"tx: {msg}")
time.sleep(1)
print("停止发送消息") def receive(stop_event):
"""循环接收消息"""
print("开始接收消息")
while not stop_event.is_set():
rx_msg = bus.recv(1)
if rx_msg is not None:
print(f"rx: {rx_msg}")
print("停止接收消息") def send_and_recv_msg():
"""发送一个消息并接收一个消息,需要双通道CAN"""
stop_event = threading.Event()
t_send_cyclic = threading.Thread(target=send_cyclic, args=(stop_event,))
t_receive = threading.Thread(target=receive, args=(stop_event,))
t_receive.start()
t_send_cyclic.start()
try:
while True:
time.sleep(0) # yield
except KeyboardInterrupt:
pass # 正常退出
stop_event.set()
time.sleep(0.5) if __name__ == '__main__':
msg = can.Message(arbitration_id=0x181DFF00, data=[0xEE, 0xFE, 0xFE, 0xFF, 0xFE, 0xFF, 0xFF, 0xFE],
is_extended_id=True) # 报文
bus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)
send_and_recv_msg()
python-can库基于PCAN-USB使用方法的更多相关文章
- python安装库报错的处理方法
在安装python map库时遇到了还多问题,找了好的方法都没有安装成功,最后改安装basemap库参考了了:https://www.jb51.net/article/147780.htm一文操作,最 ...
- python第三方库的四种安装方法
1,直接pip install安装 2,在python-->default setting-->project interprer-->add 3,在这个链接里找到需要的包,下载 h ...
- python常用库
本文由 伯乐在线 - 艾凌风 翻译,Namco 校稿.未经许可,禁止转载!英文出处:vinta.欢迎加入翻译组. Awesome Python ,这又是一个 Awesome XXX 系列的资源整理,由 ...
- Python机器学习库scikit-learn实践
原文:http://blog.csdn.net/zouxy09/article/details/48903179 一.概述 机器学习算法在近几年大数据点燃的热火熏陶下已经变得被人所“熟知”,就算不懂得 ...
- Python常用库大全
环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. v ...
- python的库小全
环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. v ...
- python 三方库
---------------- 这又是一个 Awesome XXX 系列的资源整理,由 vinta 发起和维护.内容包括:Web框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理. ...
- Python常用库大全,看看有没有你需要的
作者:史豹链接:https://www.zhihu.com/question/20501628/answer/223340838来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Python第三方库资源
[转载]Python第三方库资源 转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github ...
- 【转载】Python第三方库资源
转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github.com/jobbole/awesom ...
随机推荐
- 事务与一致性:刚性or柔性
转发自 https://cloud.tencent.com/developer/article/1038871 在高并发场景下,分布式储存和处理已经是常用手段.但分布式的结构势必会带来"不一 ...
- vue学习4-class和sytle绑定
#### Class绑定: 1. 通过数组的方式来实现: 2. 通过对象的方式来实现: 通过对象: 通过数组,通过数组是把多个style样式对象添加进去:
- 运维利器-ClusterShell集群管理
在运维实战中,如果有若干台数据库服务器,想对这些服务器进行同等动作,比如查看它们当前的即时负载情况,查看它们的主机名,分发文件等等,这个时候该怎么办?一个个登陆服务器去操作,太傻帽了!写个shell去 ...
- 单例模式的各种实现方式(Java)
单例模式的基础实现方式 手写普通的单例模式要点有三个: 将构造函数私有化 利用静态变量来保存全局唯一的单例对象 使用静态方法 getInstance() 获取单例对象 懒汉模式 懒汉模式指的是单例对象 ...
- Arduino+ESP32 之 SD卡读写
背景知识: ESP32有两种使用SD卡的方法,一种是使用SPI接口访问SD卡,另一种是使用SDMMC接口访问SD卡 . Arduino core for the ESP32中SPI方式占用4个IO口, ...
- Javascript之递归求裴波那契数
一.遍历的方式性能更加,递归的方式代码利于阅读.简短,性能略差 二.裴波那契数定义: · 位置0的裴波那契数为0 · 1和2的裴波那契数为1 · n(n > 2)裴波那契数为 (n-1)的裴波那 ...
- linux计划任务之cron
目录 cron计划任务之用户级 cron计划任务之系统级 cron计划任务之用户级 1.安装crond centos7 执行命令: # yum install -y crontabs /bin/sys ...
- JMeter压力测试简单使用
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11915535.html JMeter压力测试简单使用: 我们可以使用JMeter来测试一下自己 ...
- 鸟哥的Linux学习笔记-bash
1. /bin/bash是linux预设的shell,也是Linux发行版的标准shell,它兼容sh,可以看作是sh的功能加强. 2. bash具有命令记录功能,在bash中通过上下键就可以翻找之前 ...
- xss标签和属性爆破
当网站过滤了大部分的HTML标签和属性,我们就尝试爆破一下,看哪些标签和属性没有没过滤. 爆破的步骤: 1. 首先在测试点输入我们正常的exp,并抓包发送到Intruder模块. 2. 将exp改为 ...