Search code examples
c#canoeautomotive

Adding event procedures using CANoe.NET API in VS studio


I tried to used the following procedure: enter image description here

My problem is that it did not worked until I've created an observer and activated it before expecting the message to be seen. I'm not sure it is the proper way to use this kind of 'on event' procedure.

My main purpose for now it's to create a test case that sends a message (with a specific id, DLC, raw data..) and to make sure the message was sent correctly, having the expected DLC. I want to use the specified procedure to check OnCANFrame(channel, myID) that the DLC is the correct one. So I send a message for 3 times using my test case (LL2015), and expect to be seen by on event procedure and check the DLC length, very simple.

Here is my code:

public override void Main()
{
    ICheck check = new MyUserCheck("MyUser check", "Check DLC of message XCP");
    check.Activate();
    LL2015.LL2015(); -> test method
    check.Deactivate();
}
public class Test_LL2015
{
    [TestCase("LL2015")]
    public void LL2015()
    {

        byte[] data = new byte[] { 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        CANFrame msg = new CANFrame(0x1BFDEF00, 12);  -> defined CANframe
        msg.SetRawData(data);
        msg.Send(); ->send the frame
        Execution.Wait(3000);
        msg.Send(); -> send the frame again
        Execution.Wait(3000);
        msg.Send(); -> send the frame again
        Execution.Wait(3000);
    }
}
public class MyUserCheck : UserCheck
{
    public MyUserCheck(string title, string description)
    : base(title, description)
    {
    }
    [OnCANFrame(2, 0x1BFDEF00)]
    public void FrameReceived1(CANFrame frame)
    {
        if (frame.DLC != 12)
        Report Violation("DLC check for frame 0x1BFDEF00 failed");
        else
        {
            Report.TestStepPass("[passed]");
            Report.TestStep(frame.TimeNS.ToString());
        }
    }
}

This code version it's working, but I'm not sure if it's a correct way to do this. I only have the example from https://cdn.vector.com/cms/content/know-how/_application-notes/AN-IND-1-011_Using_CANoe_NET_API.pdf Is there any other way for doing something like this? Thank you!

!EDIT!

Based on the response, there are 2 options:

  1. I've declared the method in the main class: public class MainClass : TestModule { ....

     public override void Main()
     {       
    
         LL2015.LL2015(); -> test method
    
     }
    
     [OnCANFrame(2, 0x1BFDEF00)]
     public void FrameReceived1(CANFrame frame)
     {
         if (frame.DLC != 12){
             Report.TestStepFail("[failed]");
         }     
         else
         {
             Report.TestStepPass("[passed]");
             Report.TestStep(frame.TimeNS.ToString());
         }
     }
    

    }

  2. It is described in the answer from MSpiller


Solution

  • There are basically two options for implementing event procedures in .NET and have them called by CANoe.

    1. You add your handler as a method to your main-Class. These handlers are registered and activated automatically.

    2. You add your handler in your own class and register it by calling Vector.CANoe.Runtime.Runtime.RegisterHandlers. E.g.:

    public class HandlerClass
    {
        ...
    
        [OnCANFrame(2, 0x1BFDEF00)]
        public void FrameReceived1(CANFrame frame)
        {
            if (frame.DLC != 12)
            Report Violation("DLC check for frame 0x1BFDEF00 failed");
            else
            {
                Report.TestStepPass("[passed]");
                Report.TestStep(frame.TimeNS.ToString());
            }
        }
    }
    
    ...
    
    var myHandler = new HandlerClass(...);
    Runtime.RegisterHandlers(myHandler);