Go Down

Topic: problem about serial (Read 383 times) previous topic - next topic

jimidini

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

AWOL

#1
Apr 06, 2011, 11:41 am Last Edit: Apr 06, 2011, 11:45 am by AWOL Reason: 1
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: [Select]
for (int i = 0; i < 4; i++)
"car" is only declared with three elements.
"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.

jimidini


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

if (char(var) == 'X' && char(var) =='1')

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


AWOL

Code: [Select]
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]));
}
"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.

Go Up