Go Down

Topic: EOL and SOL, working only first time xD (Read 3067 times) previous topic - next topic

dario111cro

Code: [Select]
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.


CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

dario111cro

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.


PaulS

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.

dario111cro

It's you again. :) Thank you one more time. It's finally working.
Code: [Select]
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");
    }
}


Go Up