Receiving CAN frame

In this tutorial we will learn how to receive a CAN frame using neoVI hardware. We are going to assume the script is connected to a HW that receiving CAN frames.

1. Initialize Vehicle Spy X

First step we need to initialize a new instance of the vspyx.Core.Application.

import vspyx

if 'app' not in globals():
        global app
        app = vspyx.Core.Application.New()
        app.Initialize(loadAllModules=True)

2. Setup the Hardware

To use the hardware, first we need to activate it, create a controller and connect the desired hardware channel to the controller. vspyx.VehicleSpy.Module.AddSource()

device = app.VehicleSpy.AddSource("hardware sn") # Activates HW
channel = app.Resolver[f'{device.Source.Identifier} {"Channel Name"} Discovery Channel']

Now we need to create a controller and connect it to a channel: vspyx.Communication.Channel.NewAttachedController()

[controller, connector] = channel.NewAttachedController('controller name', listenOnly=False) # creates controller and connects it to HW channel

3. Create a Report

Next we need to create a function that accesses and logs the incoming CAN traffic vspyx.Communication.DataLinkPDUPoint

def report(point: vspyx.Runtime.Point):
        if not isinstance(point, vspyx.Communication.DataLinkPDUPoint):
                return

        print(f"{point.GetAttribute('ShortName')} {point.GetAttribute('ArbID')}\
                        {point.GetAttribute('Length')} {point.GetAttribute('Payload')}\
                        {point.GetAttribute('ChannelName')} {point.GetAttribute('WireType')}\
                        {point.GetAttribute('DLC')} {point.GetAttribute('Timestamp')}")

4. Go Online with Hardware

We’re going to use report function made in step3 to log and monitor the incoming data.

observer = app.VehicleSpy.PrepareForStart(analysisMode=False)
observer.OnPoint.Add(report)
app.VehicleSpy.Start()
app.VehicleSpy.Scheduler.Wait(vspyx.Core.Event())

Complete Example:

import vspyx

hardware_sn = 'icsneo **1' # wildcard to connect to first device
channel_name = 'HSCAN'

def setup_hw():

        device = app.VehicleSpy.AddSource(hardware_sn) #activate hw
        channel = app.Resolver[f'{device.Source.Identifier} {channel_name} Discovery Channel']
        return channel

def report(point: vspyx.Runtime.Point):
        if not isinstance(point, vspyx.Communication.DataLinkPDUPoint):
                return

        print(f"{point.GetAttribute('ShortName')} {point.GetAttribute('ArbID')}\
                        {point.GetAttribute('Length')} {point.GetAttribute('Payload')}\
                        {point.GetAttribute('ChannelName')} {point.GetAttribute('WireType')}\
                        {point.GetAttribute('DLC')} {point.GetAttribute('Timestamp')}")

def connect_to_controller(channel):
        [controller, connector] = channel.NewAttachedController('my_setup', listenOnly=False)
        return controller

def go_online():
        observer = app.VehicleSpy.PrepareForStart(analysisMode=False)
        observer.OnPoint.Add(report)
        app.VehicleSpy.Start()
        app.VehicleSpy.Scheduler.Wait(vspyx.Core.Event())

def main():

        if 'app' not in globals():
                global app
                app = vspyx.Core.Application.New()
                app.Initialize(loadAllModules=True)

        print(f'Version: {app.Version}')

        channel = setup_hw()
        controller = connect_to_controller(channel)
        go_online()

if __name__ == '__main__':
        main()