/*@!Encoding:1252*/
includes
{
  
}

variables
{
  int lastUsedAddress;
}

on preStart
{
  J1939ILControlInit();
}

on start
{
  lastUsedAddress = J1939ILGetAddress();
  J1939ILControlStart(lastUsedAddress);
}

void J1939ILOnChangedState( LONG state )
{
  if((state == 3) && (J1939ILGetAddress() == 254)) //new state is "Stopped", and the node lost its address
  {
    //Here you have to compute a new free address
    // to make it easier, we just increase our old address and try to claim this address to claim 
    //But the addresses 0xFE and 0xFF are not allowed. Therefore we should stop with the address 0xFD
    if(lastUsedAddress < 0xFE) 
    {
      lastUsedAddress++;
      J1939ILControlStart(lastUsedAddress);
    }
  }
}

on sysvar svChangeAddress_Node1
{
  if(@this == 1)
  {
    @sysvar::J1939::Node1::ILAddress = @svNewAddress_Node1;
  }
}
