i see and tested some nice code http://atlas-scientific.com/_files/code/Arduino-sample-code-EZ-COM-MEGA.pdf
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring3 = ""; //a string to hold the data
boolean input_stringcomplete = false; //have we received all the data from the PC
boolean sensor_stringcomplete3 = false; //have we received all the data
void setup(){ //set up the hardware
Serial.begin(115200); //set baud rate for the hardware serial port_0
Serial3.begin(38400); //set baud rate for software serial port_3
inputstring.reserve(5); //set aside some bytes for receiving data from the PC
sensorstring3.reserve(30); //set aside some bytes for receiving data
}
void serialEvent() { //if the hardware serial port_0 receives a char
char inchar = (char)Serial.read(); //get the char we just received
inputstring += inchar; //add it to the input String
if(inchar == '\r') {input_stringcomplete = true;} //if the incoming character is a <CR>, set the flag
}
void serialEvent3(){ //if the hardware serial port_3 receives a char
char inchar = (char)Serial3.read(); //get the char we just received
//sensorstring3 += inchar; //kll add it to the input String
if(inchar == '\r') {sensor_stringcomplete3 = true;} //if the incoming character is a <CR>, set the flag
else { sensorstring3 += inchar; } //kll
}
void loop(){ //here we go....
if (input_stringcomplete){ //if a string from the PC has been received fully
Serial3.print(inputstring); //send that string
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flag used to tell if we have received a completed string from the PC
}
if (sensor_stringcomplete3){ //if a string has been received fully
Serial.println(sensorstring3); //send that string to the PC's serial monitor
sensorstring3 = ""; //clear the string:
sensor_stringcomplete3 = false; //reset the flag used to tell if we have received a completed string
}
}
like for a terminal emulation ...
but when i tried to use the event in a code
like question and answers in a subroutine it did not work.
can you pls try that following code and tell me what you think?
/*
KLL serial event test
*/
String inputstring = ""; //a string to hold incoming data from the PC
boolean input_stringcomplete = false; //have we received all the data from the PC
void setup(){ //set up the hardware
Serial.begin(115200); //set baud rate for the hardware serial port_0
inputstring.reserve(5); //set aside some bytes for receiving data from the PC KLL but seems to work also for longer strings??
}
void serialEvent() { //if the hardware serial port_0 receives a char
char inchar = (char)Serial.read(); //get the char we just received
inputstring += inchar; //add it to the inputString
Serial.print(" S0: ");
Serial.println(inchar,HEX);
if(inchar == '\r') {input_stringcomplete = true;} //if the incoming character is a <CR>, set the flag
}
void askquestion() {
Serial.println("Question: do you love me? ");
int var = 0;
while(var < 20){ // do something repetitive times
delay(200);
var++; Serial.print(".");
if (input_stringcomplete){
Serial.print(" askquestion loop got: ");
Serial.print(inputstring); //send that string back
Serial.println();
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
break;
}
} // end while
Serial.println(" timeout ");
} // ask question
void loop(){ //here we go....
if (input_stringcomplete){ //if a string from the PC has been recived in its entierty
Serial.print(" main loop got: "); Serial.println(inputstring);
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
askquestion();
}
looks like a character is only catched in a main loop, ( and only one per loop).
so question - answer will never work?
any ideas what i do wrong?