赞
踩
使用python-can发送信号和获取信号
# -*- 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()
# -*- 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()
使用canlib库读取dbc文件并组数据包
# -*- 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())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。