Finding character in response code

Hi

I how started to play around with my arduino and im trying to write a protokoll to get data from my heat meter, but i dont have that mutch knowledge of C so i hope some one can help me.

My problem is that i can't get my code right how to find some character i my response code from the meter, the response look like this see below.

What i need is.

I wont to find the hex character i marked with red, and need to know the start position off the first character "00" and the last position of the character "1B", the length can be different so i think i need a funktion that has following parameter like

Input: Wath to find = eg. "001B" // has ma be variable i size also
Output: Where the first carakter is
Output: Where the last carakter is

Response:

Physical Layer:
40 3F 10 00 1B 7F 16 04 11 01 2A F0 24 F3 8A 0D

I hope someone can and will help me

Jesper

Knowing something about the data to be search is critical to writing code to search the data.

Is the data in an array? What kind of array? int? byte? char?

Or, is it in an object, like a String?

but i dont have that mutch knowledge of C

Array traversal is pretty basic.

Hi

Yes you are right Array basic my fault, the response i get from the meter is stored in a array of the type char

char received[100];//! Variable : array for storing the data received from the module
:wink: Jesper

I have some old code i have used before and i have tried to change in it to get the function i wont but with know luck

byte waitForData(char* expectedAnswer, int MAX_TIMEOUT, int timeout, int seconds) {

      for (int i = 0; i < 100; i++) received[i] = ' ';

      char command[50];
      int theLength = 0;
      int it=0;
      bool theSame=false;
      bool errorAnswer=false;
      int inbyte=0;
      byte countLetters = 0;
      char* errorCommand="ERROR";
      uint8_t errorLength=5;
      
      uint8_t first=1;
      uint8_t match=0;
    
      while( expectedAnswer[theLength]!='\0' ) theLength++;
            
      // if there is a heating time, then wait to see if you got
      // any data from the serial port
      while (seconds >0) {
            delay(1000);
            seconds--;
      }
            
      while(timeout < MAX_TIMEOUT) {
           
                while(!Serial.available() && timeout < MAX_TIMEOUT) {
                  timeout++;
                  delay(1000);
            }
            
         
                while( Serial.available() && !match )
            {
                  if( first )
                  {
                        for(it=0;it<theLength;it++)
                        {
                             
                                        received[it]=Serial.read();
                              delay(20);
                              
                        }
                        first=0;
                  }
                  it=0;
                  
                  
                        if( Serial.available() )
                  {
                        theSame=true;
                        for(it=0; it<theLength ; it++)
                        {
                              if(received[it]!=expectedAnswer[it]){
                                    theSame= false;
                                    break;
                              }
                        }
                        if( theSame ){
                              match=1;
                              return 1;
                        }
                        else
                        {
                              for(it=0; it<theLength-1 ; it++)
                              {
                                    received[it]=received[it+1];
                              }
                              //received[it]=Serial.read(); JBM
                                        received[it]=Serial.read();
                              delay(20);
                              
                        }
                  }
            }
      }

So this was the code i used as a way to get started

Something like this should work (not tested, though)

int findInArray(char array[], int aLen, char find[], int fLen)
{
   int fPos = -1;
   if(aLen > fLen || fLen < 1) // The data to be found can not exist
                  // if the array to search is shorter than the array to find
   {
      for(int i=0; i<aLen-fLen; i++)
      {
         boolean match = true;
         for(int j=0; j<fLen; j++)
         {
            if(array[i+j] != find[j]) // If the two characters don't match
            {
               match = false;
               break; // Quit looking
            }
         }

         if(match) // The string to find was found
         {
            fPos = i;
            break; // Only report the first occurrence
         }
      }
   }
   return fPos;
}

Hi PaulS

Tanks alto for you help

I can see the aLen and the fLen are values i have to know and write, but the position of the char in the response from the meter to find can be different, so i need the code to tell me what the aLen is and the fLen.

Jesper

The function reports the position of the string to be found in the array, if the string to be found exists, or -1 if it does not exist.

You have to know how long the string to be found (fLen) is.

You have been reading data from the meter and storing it in the array. You should know the last position you stored data in. That's the "length" of the array (aLen).

Hi

Tanks to PaulS i got what i wont and it seems to work, so i will post the code.

Tanks again to PaulS

char Response[]        ={"3F1080080160411012AF0212AFA"};
char Reg_kW[5]         ={"A2AF"};
int Reg_staPos =0; 
int Reg_endPos =0;


int GetLength(char array[], char endChar)
{
   int aLen =0;
  
    while( array[aLen]!=endChar ) aLen++; // Finding end char of the response array
      if(aLen > 0) return aLen;
      else return -1;
}

int GetRegCode(char array[], char find[])
{
   int aLen =0;
   int fLen =0;
   boolean theSame=false;
   int fPos = -1;
   int match=0;
   int i =0;
   int j =0;
   while( array[aLen]!='\0' ) aLen++; // Finding end char of the response array
   //Serial.println(aLen);              // Debug
   while( find[fLen]!='\0' ) fLen++;  // Finding end char of the finding array
   //Serial.println(fLen);              // Debug
  
   if(aLen > fLen || fLen < 1) // The data to be found can not exist
                               // Quit looking
                                   // if the array to search is shorter than the array to find
   {
          for(i=0; i<aLen-fLen; i++)
      {
           theSame=true;
         for(j=0; j<fLen; j++)
         {
            if(array[i+j] != find[j]) // If the two characters don't match
            {
                   theSame= false;
                   break; // Quit looking
            }
         }
           if( theSame ){
         match=1;
           return i+j;
           }
        }
   }
   if( match ) return i+j;
   else return -1;
}

void setup() {
      Serial.begin(19200);
      Serial.print("Examples of Meter\n\n");
}

void loop()
{
 if(Reg_endPos=GetRegCode(Response,Reg_kW)) 
 Serial.println("Reg Code OK");
 else
 Serial.println("Reg Code Error"); 
 Reg_staPos = Reg_endPos - GetLength(Reg_kW,'\0');
 Serial.print("Start pos of reg ");
 Serial.println(Reg_staPos); 
 Serial.print("End pos of reg ");
 Serial.println(Reg_endPos); 
 delay(10000);
}