Using while in CAPL causes software to freeze at the beginning of simulation runtime

79 Views Asked by At

When simulating running in Canoe, the software freezes, which I believe is the reason for the while. However, in the while code segment, I have written the logic to stop the loop, and I don't know why.........................................................................................................................................................

this is my code.

/*@!Encoding:936*/
includes
{
  
}

variables
{
   timer time1;
   int status;
   int idx;
   message 0x1 msg;
}

on preStart
{
    sysSetVariableInt(sysvar::Mynamespace::LockSign,0);
    write("begin");
    write("sysvar1: %d",sysGetVariableInt(sysvar::Mynamespace::LockSign));
    status = 1;
    idx=0x741;
    setTimer(time1,2);
}


on timer time1
{
  write("trigger time");
  if(sysGetVariableInt(sysvar::Mynamespace::LockSign)==0)
  {
    sysSetVariableInt(sysvar::Mynamespace::LockSign,1);
  }
  status=0;
}

on message *
{
    if(this.byte(1)==0x51 & this.byte(2))
    {
      write("found: 0x%x",this.id);
      sysSetVariableInt(sysvar::Mynamespace::LockSign,0);
      cancelTimer(time1);
    }
}

on start
{
    msg.dlc = 3;
    msg.byte(0) = 2;
    msg.byte(1) = 0x11;
    msg.byte(2) = 0x1;
    write("begin while\n");
    while(status==1)
    {
        if(sysGetVariableInt(sysvar::Mynamespace::LockSign)==0)
        {
          write("send %d meg\n",idx);
          msg.id=idx;
          output(msg);
          idx++;
          sysSetVariableInt(sysvar::Mynamespace::LockSign,1);
          setTimer(time1,2);
        }
        else{
          write("LockSign\n");
        }
        
        if(idx==0x744)
        {
          status=0;
        }
      write("test");
    }  
}

I tried putting the while code in "on key", but it still didn't work

1

There are 1 best solutions below

1
On

The working model of CANoe can be described as cooperative multitasking. There is only one "thread" running, which executes the whole simulation.

For example, when a message is received, all associated event handlers, like for example on message ... are executed. While these handlers are executed, the simulation is blocked.

In your case, your code in on start never finishes, which will cause the simulation to stall.

You have to divide your code and logic into smaller pieces which are triggered by events. These pieces have to finish as fast as possible.

I did not fully get your use case, but your code currently in on start could simply be put into a timer

variables
{
...
msTimer time2;
}

on timer time2
{
  msg.dlc = 3;
  msg.byte(0) = 2;
  msg.byte(1) = 0x11;
  msg.byte(2) = 0x1;
  if(sysGetVariableInt(sysvar::Mynamespace::LockSign)==0)
  {
    write("send %d meg\n",idx);
    msg.id=idx;
    output(msg);
    idx++;
    sysSetVariableInt(sysvar::Mynamespace::LockSign,1);
    setTimer(time1,2);
  }
  else{
    write("LockSign\n");
  }
        
  if(idx==0x744)
  {
    status=0;
  }
  setTimer(time2,10);
}

What was in your while-loop before will now be executed every 10 milliseconds.

Most likely to code could be optimized further by using on sysvar Mynamespace::LockSign for reacting only when the sysvar has changed instead of polling. But that depends on your requirements.