Search code examples
capl

How to increment Hex Value of a message in CAPL


I am trying to read data from my Vehicle ECU. My plan is to send IDs from 0x700 to 0x7FF and check against which cases a response is received from the ECU. My code goes as follows. But upon compiling, it shows an error on the line: msg++ saying Operand types are incompatible. Which part of the code do i need to modify to get the desired output?

variables
    {
      message 0x700 msg;
      msTimer t1;
      int i=0;
      long j;
      byte check_byte0;
    }

on key 'a'
{
    message 0x700 msg;
      for(j=0;j<256; j++)
     {
         msg.byte(0)=0x01;
         msg.byte(1)=0x22;
         output(msg);
         check_byte0 = this.byte(0);
         if(check_byte0 == 62)
         {
            write("output recieved");
            i+=1;
         }
        setTimer(t1,20);
        msg++;
      }
      write("%d",i);
    }

Solution

  • variables
    {
        message 0x700 msg;
        msTimer t1;
    }
    
    on start
    {
        // this part is run only after start
        msg.dlc = 2; //set msg length
        msg.byte(0)=0x01; //set first data byte
        msg.byte(1)=0x22; //set second data byte
        write("Help: Press key \'a\' to start the testing.");
    }
    
    on key 'a'
    {
        msg.id = 0x700; //reset ID
        setTimerCyclic(t1, 20); //start cyclic timer for 20 ms
    }
    
    on timer t1
    {
        output(msg); //sendig message
        write("Msg id: 0x%02X transmitted", msg.id);
        if (msg.id++ >= 0x7FF) cancelTimer(t1); //stop timer on the last ID
    }
    
    on message *
    {
        if (this.dir == Rx) // Filter to received messages. Its posible to filter some IDs.
        {
            write("Msg id: 0x%02X recieved with length: %d and first byte: 0x%02X", this.id, this.DataLength, this.byte(0));
        }
    }