Pages: [1]   Go Down
Author Topic: EOL and SOL, working only first time xD  (Read 2500 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bump?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 439
Posts: 23782
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: