如何使用python-can连接向量虚拟总线。我在电脑上运行XCPsim(Vector提供的模拟XCP从站的软件),想用python脚本连接XCPsim总线并使用pyxcp,但我不知道如何连接。 所以,我需要一些帮助,谢谢
如何连接向量虚拟(python-可以连接XCPsim)
如何使用pyxcp?该库的文档忽略教程 https://pyxcp.readthedocs.io/en/latest/
pyxcp:https://github.com/christoph2/pyxcp python-can:https://github.com/hardbyte/python-can
这是我的代码:
from pya2l.parser import A2lParser as Parser
import pyxcp
import can
with open("XCPSIM.a2l", "r") as f:
a2l_string = f.read()
# print('a2l_string', a2l_string)
with Parser() as p:
# get the AST.
ast = p.tree_from_a2l(a2l_string.encode())
# print('ast', ast)
measurements = []
for measurement in ast.PROJECT.MODULE[0].MEASUREMENT:
measurements.append(measurement)
print('measurements', len(measurements))
with can.interface.Bus(channel=0, bustype='vector', bitrate=500000) as can_bus:
message = can.Message(arbitration_id=0x1, data=[255, 0, 0, 0, 0, 0, 0, 0], is_extended_id=False, is_fd=False)
can_bus.send(message)
print('message', message)
# # master = pyxcp.master('CAN', 1, 2)
好的,我可以回答我的问题。
下载 Vecter 硬件配置
编辑你的Python代码,添加app_name
can.interface.Bus(bustype='vector', app_name='my_app',channel=0)
配置“Vector 硬件配置”
使用“应用程序通道配置”添加“my_app”应用程序
使用“my_app”设置CAN通道,设置虚拟CAN总线和通道
运行 XCPsim.exe 并运行您的 python 代码
使用 can.interface.Bus(bustype='vector', app_name='my_app', channel=0) 作为总线: 打印('总线',总线) 消息 = can.Message(仲裁_id=0x1, 数据=[253, 0, 0, 0, 0, 0, 0, 0], is_extended_id=False, is_fd=False) res = 总线.发送(消息) 打印('发送',res) res_msg = 总线.recv() 打印('res',res_msg)