Reference to a String Array:

HI,

I have this pseudo/test code:

(Sorry for NOT Marking the code below, I have seearched for the CTL ??? (mark key(s)) in help, but I Just can’t find it. Deeply sorry )

String Destin = {“WR”,“RD”,“DIR”,“ANA”,“PULL”};
String Command = {“GO”,“STOP”,“HELP”};

void setup()
{
Serial.begin(9600);
}

void loop()
{

String Str = “GO”;

Str.toUpperCase();

for (int n = 0; n <= 3; ++n)
{
if (Command[n] == Str)
{
Serial.println('Command found ');

}
else
{
Serial.println('Command not found ');

}
}

It does not work as I hoped. Instead I get a random number (error number (?)) written 3 times (Serial.println(…)) .

Question:

Is it possible in ARD C to do something as shown (modifying my code) or how should I do IT ?

Kris AKA Snestrup2016

Please read this before posting a programming question and take the advice regarding the use of code tags then post a complete program that can be compiled that demonstrates the problem.

 for (int n = 0; n <= 3; ++n)
 {
     if (Command[n] == Str)

Command has only three elements; don’t try retrieving the fourth.

HI,

#UKHeliBob : Thanks for the advice. Actually I knew it. MY fault. I apologize.

#AWOL: Correct. A big, big TYPO. Sorry, I should have discovered that - before I send the code. I was a bit frustrated, working on the code since early morning.

Here is the code (total):

String Istr;
byte _comma_count = 0;
String msg_fields[20] = “”;
int Antal; /* danish for NUMBER */
String Command = {“WR”,“RD”,“HELP”};

void setup()
{
Serial.begin(9600);
}

void loop()
{
String Str = “WR”;

Istr = readline();
if (Istr != “”)
{
Istr.toUpperCase();
Antal = getCSVfields(Istr);

//
// this is a test , trying to make it easier to find and manipulate string(s).
//

for (int n = 0; n <= 2; n++)
{
if (Command[n] == Str)
{
Serial.println('Command found ');

}
else
{
Serial.println(‘Command not found’);

}
}

/*
if (Antal != 0 )
{
Serial.print("antal = ");
Serial.println(Antal);
Serial.print("comm[0] = ");
Serial.println(msg_fields[0]);
}

String Comm = msg_fields[0];

Serial.print(“NY comm = “);
Serial.print(msg_fields[0]);
Serial.print(” '”);
Serial.print(Comm);
Serial.print("’ ‘");
Serial.print(Comm[0]);
Serial.println("’ ");
*/

//
// Tried if (msg_fields[0] = “RD”) { } … does not work
//
// this works:

if ((0x52 == msg_fields[0][0]) && (0x44 == msg_fields[0][1]))
{
RDCommand();
}
else if ((0x57 == msg_fields[0][0]) && (0x52 == msg_fields[0][1]))
{
WRCommand();
}
Antal = 0;
Istr = “”;
}
}

String readline()
{
if (Serial.available() == 0) return ("");
Istr = “”;
Istr = Serial.readStringUntil(’\n’); /* \r == CR \n = LF */
return (Istr);
}

int getCSVfields( String input_buffer)
{
byte _sentencePos = 0;

_comma_count = 0;
for (int n = 0; n <= 20; ++n)
{
msg_fields[n] = “”;
}
while (1)
{
if (input_buffer[_sentencePos] == NULL) break;
if (input_buffer[_sentencePos] == 44) /* , */
{
_comma_count++;
msg_fields[_comma_count] = “”;
_sentencePos++;
}
else
{
msg_fields[_comma_count] += input_buffer[_sentencePos];
_sentencePos++;
}
}
return _comma_count + 1;
}

void RDCommand()
{
Serial.println("test RD 1 ");
Serial.print("test COMM 2 ");
Serial.println(msg_fields[1]);

return;
}

void WRCommand()
{
Serial.println("test WR 1 ");
Serial.print("test COMM 2 ");
Serial.println(msg_fields[1]);
return;
}

Why do I have to use Integers as chars in

else if ((0x57 == msg_fields[0][0]) && (0x52 == msg_fields[0][1])) — this ?

UKHeliBob : Thanks for the advice. Actually I knew it. MY fault. I apologize.

Then why did you decide to ignore the advice again ?

HI,

Yes, You may say i am a fool, and In some ways you're right. (Please don't say it loudly !).

I have printed the approx. 15 pages and I can assure you, that I WILL read it again. Carefully and concentrated.

Why do I have to use Integers as chars in

else if ((0x57 == msg_fields[0][0]) && (0x52 == msg_fields[0][1])) — this ?

You don’t. 0x57 is the same as ‘W’ and 0x52 is the same as ‘R’.

  for (int n = 0; n <= 20; ++n)
  {
    msg_fields[n] = "";
  }

Crapping on element 21 of a 20 element array is still NOT a good idea.