system
January 29, 2011, 6:56pm
1
char str[25];
int i=0;
int str_len;
int inByte;
int last_inByte;
String rezultat;
void setup() {
Serial.begin(9600);
}
void loop() {
inByte = Serial.read();
str_len=0;
if (inByte == '!') read_serial();
}
void read_serial() {
Serial.println ("SOL FOUND and reading");
Serial.print("READ: !\n");
while (inByte!= '*')
if (Serial.available() > 0) {
inByte = Serial.read();
Serial.print("READ: ");
str[str_len] = inByte;
Serial.println(str[str_len]);
str_len++;
} else if (Serial.available() == 0) {
Serial.println("EOL not available, data str invalid");
Serial.println("$N");
break;
}
if (inByte == '*') {
Serial.println ("EOL FOUND");
for (i=0;i<(str_len-1);i++) {
rezultat += str[i];
}
if(rezultat == "YES") {
Serial.write("Yeah");
}
Serial.print ("\n");
}
}
I get 'Yeah' back only if I send !YES* as first send thing from opening serial monitor. Otherwise I don't get 'Yeah' at all. Please help.
rezultat += str*;*
Won't this just keep getting added to with every pass? I don't see anything that resets it.
Try printing it, see if it just gets longer & longer.
system
January 29, 2011, 11:17pm
4
It's inside for loop. It will loop only as much times as there are bytes waiting. I can print it, and everything is normal.
system
January 30, 2011, 9:27pm
6
Re-read CrossRoads question and think about this. Suppose you send to the serial port this data:
!Yes*!No*!Maybe*
What will rezultat contain when the first * is encountered? "Yes".
What will rezultat contain when the next * is encountered? "YesNo"
What will rezultat contain when the next * is encountered? "YesNoMaybe"
At some point, after you use the contents of rezultat, you should reset it to an empty string.
system
January 31, 2011, 8:29pm
7
It's you again. Thank you one more time. It's finally working.
char str[25];
int i=0;
int str_len;
int inByte;
int last_inByte;
String rezultat;
int baud = 9600;
void setup() {
Serial.begin(baud);
}
void loop() {
inByte = Serial.read();
str_len=0;
if (inByte == '!') read_serial();
}
void read_serial() {
Serial.println ("SOL FOUND and reading");
Serial.print("READ: !\n");
while (inByte!= '*')
if (Serial.available() > 0) {
inByte = Serial.read();
Serial.print("READ: ");
str[str_len] = inByte;
Serial.println(str[str_len]);
str_len++;
} else if (Serial.available() == 0) {
Serial.println("EOL not available, data string invalid");
break;
}
if (inByte == '*') {
Serial.println ("EOL FOUND");
rezultat = "";
for (i=0;i<(str_len-1);i++) {
rezultat += str[i];
}
if(rezultat == "YES") {
Serial.println("Yeah");
}
Serial.print ("\n");
}
}