Arduino Mega gets the correct data through Serial 0, but not Serial 1-3

I currently have a HC-06 Bluetooth device connected to my Arduino Mega 2560 to receive strings sent from an Android device. From HC-06 to Serial 0, I get data without errors with the following code:

String inString = "";
int index = 0;
boolean stringComplete = false;

void setup(){
  Serial.begin(9600);
  pinMode(pwmPin, OUTPUT);
}

void loop(){
  if(stringComplete){
    ParseSerialData();       //Parse the received data
    inString = "";           //Reset inString to empty
    stringComplete = false;  //Reset the system for further input of data
  }
}


void serialEvent(){
  while(Serial.available() && stringComplete == false){
    char inChar = Serial.read();
    inData[index] = inChar;       //Store it in char array
    index++;

    if (inChar == '\n'){          //Check for termination character
      index = 0;                  //Reset the index
      stringComplete = true;      //Set completion of read to true
    }else{
      inString += inChar;         //Also store as string
    }
  }
}

When I try to replace "Serial" with "Serial1" and "serialEvent ()" with "serialEvent1 ()" and move the Bluetooth device to TX1 and RX1, this program no longer works.

I read that some people encountered similar problems when using AVR-GCC 4.4.x and solved the problem by downgrading to 4.3.x, but I have 4.3.2 (on Windows 8.1, the same problem arose using Arduino IDE 1.0.3 , 1.0.5-r2 and 1.5.6-r2).

( 0 ) BT, Serial 1:

String inString = "";
int index = 0;
boolean stringComplete = false;

void setup(){
  Serial1.begin(9600);
  Serial.begin(9600);
  pinMode(pwmPin, OUTPUT);
  Serial.println("Setting up...");
}

void loop(){
  if(stringComplete){
    ParseSerialData();
    inString = "";
    stringComplete = false;
  }
}


void serialEvent1(){

  Serial.println("In serialEvent1...");

  while(Serial1.available() && stringComplete == false){

    Serial.println("Char in...");

    char inChar = Serial1.read();

    Serial.println("WTF");
    Serial.println(inChar);
    Serial.println("WTF2");

      inData[index] = inChar;

      index++;

      if (inChar == '\n'){
        Serial.println("Termination char read...");

        index = 0;
        stringComplete = true;
      }else{
        inString += inChar;
      }
   }
}

, :

Setting up...
In serialEvent1...
Char in...
WTF

WTF2

inChar , "@". , Android, "s, 1\n".

, Serial1.available() , 's' ( , Serial), (newline char) , .

- . Arduino, 1 , 0, Serial Serial1 .

- , Serial1 , Serial0?

, .

!

EDIT:

SO, - ( Serial0 Arduino ) ( , , - serialEvent). , Serial1, . - serialEvent1 . , / . , , , .

, Serial1 Arduino, Android , :

   Serial1.print("b,1\n");

   Serial1.print("A long test message. A long test message.\n");

2:

, /. , , . , , HC-06 1, 1. 0, , 1, Bluetooth Android. 1 , 0, HC-06. , stackoverflow. , (, HC-06 1 , , . , Serial0 Serial1, ).

+4
2

, Rx1:

  Serial1.begin(9600);

  pinMode(19, INPUT);  
  digitalWrite(19, HIGH);

, 3V "" arduino 5V HIGH, bluetooth TX LOW.

+5

. , EDIT 2 (bluetooth TX → RX0, TX0 → RX1, TX1 → Bluetooth RX), Bluetooth Arduino 0.

Bluetooth 400 3,68 , Arduino 0 - 0 5 . , Bluetooth 3,3 , Arduino - 3 , (, , Serial 0).

bluetooth HC-06 , , , Serial1, . bluetooth 160 3,3 .

, ( ), , - Serial1 , Serial0. , , StackExchange - . , .

0

Source: https://habr.com/ru/post/1536876/


All Articles