Pages: [1]   Go Down
Author Topic: problem about serial  (Read 356 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HI, I'm trying to send data between two arduino but I've a little problem.
The TX/RX modules are rf 443MHz. the TX print via serial two message "X1234" and "X4321"
The rx module read the buffer but when I try to put an IF there's a problem:
Code:
#include <SoftwareSerial.h>

int  car[3] ;
int carattere;

void setup(){
Serial.begin(2400);

}


void loop(){
  riprova:
  for (int i = 0; i < 4; i++) {
      car[i] = 0;
  }
  if (Serial.available()){
    if (char(Serial.read()) != 'X'){
        goto riprova;
    }

if (char(Serial.read()) != '1'){
        goto riprova;
    }
    car[0] = Serial.read(); // Then: Get them.
    car[1] = Serial.read(); // Then: Get them.
    car[2] = Serial.read(); // Then: Get them.
    Serial.print(char(car[0]));
    Serial.print(char(car[1]));
    Serial.println(char(car[2]));
    }
  }

  


If I delete the second IF it's ok but with do nothing....
Any suggestion??.
Many thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26326
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

goto's are rarely needed in C.
Try using "while" or "do..while" constructs.

You have three consecutive "Serial.read"s, but no guarantee that there is data for them to read.

Code:
for (int i = 0; i < 4; i++)
"car" is only declared with three elements.
« Last Edit: April 06, 2011, 04:45:15 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

goto's are rarely needed in C.
Try using "while" or "do..while" constructs.

I understand this, but Isn't the problem... the goto it's ok...

You have three consecutive "Serial.read"s, but no guarantee that there is data for them to read.

ok, but if I delete the second IF it's ok... I've tryed also this
Code:
if (char(var) == 'X' && char(var) =='1')

:
 but same result... nothing..
thank you

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26326
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()
{
  for (int i = 0; i < 3; i++) {
      car[i] = 0;
  }

  do {
   while (Serial.available() == 0);
  } while (Serial.read()) != 'X');

  for (int i = 0; i < 3; ++i) {
    while (Serial.available() == 0);
    car[i] = Serial.read();
  }
  Serial.print(char(car[0]));
  Serial.print(char(car[1]));
  Serial.println(char(car[2]));
}
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: