Hi,
I am using arduino mega2560 R3 with 3 hardware serial inputs and the serialEvent() function. This seems to work fine when the input serial streams only occur on one serial input at a time. When serial input is occurring on all three hardware serial ports at the same time i get slightly garbled output e.g serial streams will be put on the end of the serial stream from a previous read etc.
I have included the code excerpt below and some example output when two of the three hardware serial ports are receiving at the same time.
Any help is much appreciated.
Cheers.
void serialEvent1() {
if (Serial1.available() > 0) {
inString =+ "1,";
// get the new byte:
char inChar = (char)Serial1.read();
while (inChar != 'L') {inChar = (char)Serial1.read();}
// add it to the inputString:
while (inChar != '\r') {
while (!Serial1.available()) delay(1);
inString += inChar;
inChar = (char)Serial1.read();
}
Serial1.flush(); // stops multiple reads
if (inString.length() > 20) {
logTag();
chkIfNewTag();
inString = "";
}
else {
inString = "";
}
}
}
void serialEvent2() {
if (Serial2.available() > 0) {
inString += "2,";
// get the new byte:
char inChar = (char)Serial2.read();
while (inChar != 'L') {inChar = (char)Serial2.read();}
// add it to the inputString:
while (inChar != '\r') {
while (!Serial2.available()) delay(1);
inString += inChar;
inChar = (char)Serial2.read();
}
Serial2.flush(); // stops multiple reads
if (inString.length() > 20) {
logTag();
chkIfNewTag();
inString = "";
}
else {
inString = "";
}
}
}
void serialEvent3() {
if (Serial3.available() > 0) {
inString += "3,";
// get the new byte:
char inChar = (char)Serial3.read();
while (inChar != 'L') {inChar = (char)Serial3.read();}
// add it to the inputString:
while (inChar != '\r') {
while (!Serial3.available()) delay(1);
inString += inChar;
inChar = (char)Serial3.read();
}
Serial3.flush(); // stops multiple reads
if (inString.length() > 20) {
logTag();
chkIfNewTag();
inString = "";
}
else {
inString = "";
}
}
}
void logTag() {
tagCnt++;
//Serial.println(tagCnt,DEC); //testing
DateTime now = RTC.now();
sprintf( timeString, "%d:%d:%d", now.hour(), now.minute(), now.second());
// if the file is available, write to it:
//tagString = "LR 0000 0000001234567890";
//dataFile = SD.open(dateString,FILE_WRITE);
//Serial.println(dateString);
if (dataFile) {
dataFile.print(timeString);
dataFile.print(",");
dataFile.println(inString);
dataFile.flush();
// print to the serial port too:
Serial.println(inString);
//inString = "";
}
// if the file isn't open, pop up an error:
else {
Serial.println(F("data logging error"));
//for (;;){}
}
delay(20);
}
Example output from serial terminal
3,LA 00000 LA 00000 0 900 226000106155
1,LR 0000 0000000174825LR 0000 0000000174825496
3,LA 00000 0 900 226000106LA 00000 0 900 226000106155
1,LR 0000 0000000LR 0000 0000000174825496
3,LA 00000 LA 00000 0 900 226000106155
1,LR 0000 0000000174825LR 0000 0000000174825496
3,LA 00000 0 900 226000106LA 00000 0 900 226000106155
1,LR 0000 0000000LR 0000 0000000174825496
1,LR 0000 000000017482596
3,LA 00000 0 900 226000106155
1,LR 0000 0000000174825496
1,LR 0000 0000000174825496
3,LA 00000 0 900 226000106155
1,LR 0000 0000000174825496
3,LA 00000 0 900 226000106155
3,LA LA 00000 0 900 226000106155
1,LR 0000 000000017482549 0000000174825496
3,LA 00000 0 900 226000106155
1,LR 0000LR 0000 00000LR 0000 0000000174825496
3,LA 00000 0 900 226000106155
1,LR 0000 00000001LR 0000 0000000174825496
3,LA 00000 0 900 226000106155
1,LR 0000 000000017482LR 0000 0000000174825496
1,LR 0000 0000000174825496
3,LA 00000 0 900 22LA 00000 0 900 226000106155