当前位置:   article > 正文

python-can_python kvadblib

python kvadblib

使用python-can发送信号和获取信号

  1. pip install python-can
  2. 安装usb-can驱动 https://peak-system.com.cn/driver/
  3. dbc 文件解析使用 cantools 库

接收信息

# -*- coding: utf-8 -*-
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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

发送信息

# -*- coding: utf-8 -*-
import time

import can
from can.interfaces.pcan.pcan import PcanBus


def bus_send():
    msg = can.Message(arbitration_id=0x181DFF00, data=[0xEE, 0xFE, 0xFE, 0xFF, 0xFE, 0xFF, 0xFF, 0xFE],
                      is_extended_id=True)  # 报文
    """can消息发送"""
    while True:
        time.sleep(0.02)
        try:
            bus.send(msg)
            print("消息发送 {}".format(bus.channel_info))
            msg = bus.recv(timeout=100)
        except can.CanError:
            print("消息未发送")


if __name__ == '__main__':
    bus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)
    bus_send()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

使用canlib库读取dbc文件并组数据包

  1. 安装canlib.exe https://www.kvaser.com/downloads-kvaser/?utm_source=software&utm_ean=7330130980150&utm_status=latest
  2. 设置环境变量 os.environ.setdefault(“KVDLLPATH”, r"canlib\bin_x64")
  3. 安装canlib库,路径:canlib\python\pycanlib, 命令 pip install canlib-1.24.935-py3-none-any.whl
# -*- coding: utf-8 -*-
import cantools
import os
import binascii

dbc_path = r"xxx.dbc"
# dbc_data = cantools.db.load_file(dbc_path)
os.environ.setdefault("KVDLLPATH", r"D:\software_install\canlib\bin_x64")
from canlib import kvadblib
dbc_data = kvadblib.Dbc(filename=dbc_path)
box = kvadblib.FrameBox(dbc_data)

box.add_message("报文名称")
box.signal("信号名称").phys = int(-100)
box.signal("信号名称").phys = int(200)
frame = box.frames().__next__()
hex_str = binascii.hexlify(frame.data)
print(f"{hex_str}".upper())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/1011027
推荐阅读
相关标签
  

闽ICP备14008679号