Go Down

Topic: Serial read same byte (Read 548 times) previous topic - next topic

a8365885

I want communicate with smart phone by bluetooth (use Serial). I send mode then send data. But I get same byte.

I get 0xF1 (mode). I expect data are 0x55 and 0x77. But I get 0xF1, too......... (0xF1 and 0x55).

I test it if I read 3 bytes then I get 0xF1 0x55 0x77.

Have someone know this problem???????

Code: [Select]

byte mode;
byte buffer1[2];

void setup() {
  Serial.begin(38400);
  while (!Serial);
}

void loop() {
  Serial.write("READY1\n");
  if (Serial.available() > 0) {
    mode = Serial.read();
    Serial.print("MODE: ");
    Serial.println(mode, HEX);

    switch (mode) {
     
      case 0xF1:      // Serial.println("SIMPLE: ");
        Serial.write("READY2"); Serial.write(0xF1); Serial.println();
        Serial.readBytes(buffer1, 2);
        if (buffer1[0] != 0x55) {     //error
          Serial.print("ERROR: ");
          Serial.print(buffer1[0], HEX);
          Serial.print(", ");
          Serial.println(buffer1[1], HEX);
          break;
        } else {                                  //current data
          Serial.println((char) buffer1[1]);
        }
       
        Serial.write("SUCCESS"); Serial.write(0xF1); Serial.println();
        break;
    }
  }
  delay(500);
}

holmes4

Just because you found that there is at least 1 byte in the buffer DOES NOT mean that there are 3 bytes in the buffer!

Mark

6v6gt

Interesting that you can mix Serial.print debug statements and Serial.write statements to your Bluetooth interface.

Robin2

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Vaclav

I want communicate with smart phone by bluetooth (use Serial). I send mode then send data. But I get same byte.

I get 0xF1 (mode). I expect data are 0x55 and 0x77. But I get 0xF1, too......... (0xF1 and 0x55).

I test it if I read 3 bytes then I get 0xF1 0x55 0x77.

Have someone know this problem???????

Code: [Select]

byte mode;
byte buffer1[2];

void setup() {
  Serial.begin(38400);
  while (!Serial);
}

void loop() {
  Serial.write("READY1\n");
  if (Serial.available() > 0) {
    mode = Serial.read();
    Serial.print("MODE: ");
    Serial.println(mode, HEX);

    switch (mode) {
     
      case 0xF1:      // Serial.println("SIMPLE: ");
        Serial.write("READY2"); Serial.write(0xF1); Serial.println();
        Serial.readBytes(buffer1, 2);
        if (buffer1[0] != 0x55) {     //error
          Serial.print("ERROR: ");
          Serial.print(buffer1[0], HEX);
          Serial.print(", ");
          Serial.println(buffer1[1], HEX);
          break;
        } else {                                  //current data
          Serial.println((char) buffer1[1]);
        }
       
        Serial.write("SUCCESS"); Serial.write(0xF1); Serial.println();
        break;
    }
  }
  delay(500);
}

What is the actual output on your monitor screen?

If you modify the code structure and do readBytes( buffer 3 ) first than analyze your buffer ( mode etc) it will work better.

Go Up