it's won't miss the data received in hardware rx .
if i have wrong understanding correct me . One more doubt is
String in_chars = ""; without reserve exactly how much byte it's take RAM memory as default .
String inputString = ""; inputString.reserve(200); - 200 bytes is understandable
without serial event & with Serial event :
String in_chars = "";
void setup() {
Serial.begin(9600);
}
void loop() {
char in_char = ' ';
while (Serial.available()){
in_char = Serial.read();
if (int(in_char)!=-1){
in_chars+=in_char;
}
}
if (in_char=='\n'){
Serial.print("Text Entered: ");
Serial.print(in_chars);
in_chars = "";
}
}
///////////////////////////////////////////////////////////////
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Unfortunately that's wrong. SerialEvent() executes just before loop().
If your loop() takes too much time you still can miss characters. Else if you read multiple characters in serialEvent() you can block loop() until all characters have been transmitted.
I have always regarded the SerialEvent() function as a waste of time, preferring to choose when and where in the code I check for serial input. If it were interrupt driven it would probably be more useful
Why? That function will only consume sizeof(s) bytes from Serial and nothing more. Anything greater than that will be left in the buffer, available to read later
You are correct. If you get exactly the size of the buffer, there will not be room to terminate it. Also, the comment replace \n is not correct since that character will be removed from the Serial input but not transferred to the return buffer s
Probably best to
int n = Serial.readBytesUntil ('\n', s, sizeof(s)-1);
s[n] = 0;
int main(void) {
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
As you can see, this main function initializes, calls setup() and then enters an infinite loop where it repeatedly calls loop() and serialEventRun() (if serialEventRun is true). All of the serial stuff is found in HardwareSerial.cpp. Here you can see that serialEventRun() is more conditional checks on the hardware, and then a call to serialEvent(), which is weakly defined in this same file, which you redefine in your INO file. This is the magic and mystery of what exactly happens.
If you have to write a function with a 'magic' name to process serial data when there is some available then you might just as well write your own function with a name of your choice and call it at the end of loop() or anywhere else that is convenient