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();
inString = "";
stringComplete = false;
}
}
void serialEvent(){
while(Serial.available() && stringComplete == false){
char inChar = Serial.read();
inData[index] = inChar;
index++;
if (inChar == '\n'){
index = 0;
stringComplete = true;
}else{
inString += inChar;
}
}
}
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, ).