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
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;
}
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.
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).
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);
}