1

I have a C# application that can control CANoe through its COM interface. I have it working for the most part, except what I'm about to describe below.

From section 2.4 in this document (https://assets.vector.com/cms/content/know-how/_application-notes/AN-AND-1-117_CANoe_CANalyzer_as_a_COM_Server.pdf), I have the following code that gets the current value of an environment variable:

string varName = "Some variable";
CANoe.Environment mEnvironment = (CANoe.Environment)mCANoeApp.Environment;
CANoe.EnvironmentVariable envVar = (CANoe.EnvironmentVariable)mEnvironment.GetVariable(varName);

if (envVar != null)
{
    Console.WriteLine($"Current value of {varName} is {envVar.Value}");
}

Every environment variable I put in there returns the value 0.

The weird thing is, let's say I'm working on environment variable VarA:

varName = "VarA";
if (envVar != null)
{
    Console.WriteLine($"Current value of {varName} is {envVar.Value}");
    envVar.Value = value;
    Console.WriteLine($"Value of {varName} now is {envVar.Value}");
}

I pass in the value of 2, I get:

Current value of VarA is 0
Value of VarA now is 0

Then I pass 3, I get:

Current value of VarA is 2
Value of VarA now is 2

Changing value of a variable works fine everytime, I see the change reflected on CANoe panel right away.

But it seems that in the code, the value I get is the previous value? Why is this the case and how can I get the actual current value of an environment variable?

edit: add reference document

2 Answers2

1

That is a concurrency issue.

With CANoe setting the environment variable to a value will not happen immediately.

As a mental model, you can think of CANoe's on ... blocks as non-interruptible and non-concurrent. No changes to the system will take effect while such a block is executed. Only one of these blocks is executed at the same time.

This removes the need for locking which is typically needed in multi-threaded applications.

In your case, you set - from outside via COM - the environment variable and continue your C# code right away.

CANoe will cache the setting of the environment variable until no other CANoe code is running. I.e. as mentioned above, all message handlers etc. have run through.

While CANoe is waiting to process the setting of the environment variable and finally does so, your code has already gone on and reached and executed the line, where you access the variable of the environment variable. Therefore, you will - or rather might - get the old value there.

If you would add some sleep in your C# code you would get the updated value with a higher probability.

The only way to be absolutely sure that the environment variable has definitely been set, is by creating and registering an event handler, that listens for changes of the environment variable.


Just for your interest, environment variables have been deprectated and will no longer be supported in newer releases of CANoe.

MSpiller
  • 3,500
  • 2
  • 12
  • 24
1

You can access the OnChange event via COM.

string varName = "Some variable";
CANoe.Environment mEnvironment = (CANoe.Environment)mCANoeApp.Environment;
CANoe.EnvironmentVariable envVar = (CANoe.EnvironmentVariable)mEnvironment.GetVariable(varName);
envVar.Onchange += CANoeTransport_COM_OnChange;
void CANoeTransport_COM_OnChange(object Value)
        {
            var envVar = (EnvironmentVariable)Value;
            //do something with envVar.Value
        }

cj.burrow
  • 119
  • 1
  • 7