How to use python-can to connect the vector virtual bus

139 views Asked by At

How to use python-can to connect to the vector virtual bus. I ran XCPsim(the software provided by Vector to simulate XCP slave) on my computer and wanted to use a python script to connect to the XCPsim bus and use pyxcp, but I don’t know how to connect. so, i need some help, thanks

  1. how to connect to the vector virtual(python-can connect XCPsim)

  2. how to use pyxcp? The documentation for this library ignores tutorials https://pyxcp.readthedocs.io/en/latest/

pyxcp:https://github.com/christoph2/pyxcp python-can: https://github.com/hardbyte/python-can

this is my code:

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)

1

There are 1 answers

0
Sam Yuan On

ok.i can answer my question.

  1. Download the Vecter Hardware Configuration

  2. Edit your python code, add the app_name

    can.interface.Bus(bustype='vector', app_name='my_app', channel=0)

  3. Configration the "Vecter Hardware Configuration"

  4. Add 'my_app' application with 'Application Channels Configuration'

  5. Set CAN Channels with 'my_app',set Virtual CAN Bus and Channel

  6. switch to run status and deploy ('Vecter Hardware Configuration')

  7. run XCPsim.exe and run your python code

    with can.interface.Bus(bustype='vector', app_name='my_app',channel=0) as bus:
        print('bus', bus)
        message = can.Message(arbitration_id=0x1, data=[253, 0, 0, 0, 0, 0, 0, 0], is_extended_id=False, is_fd=False)
        res = bus.send(message)
        print('send', res)
        res_msg = bus.recv()
        print('res',res_msg)