I'm trying to receive data from serial port, but when the program is run, the serial monitor repeats the data three types between each interval,
below is the code and screenshot of the serial read being repeated and separated by few some dashes ("-----")
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11, true); // RX, TX
String inData;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() {
while (mySerial.available() > 0)
{
char recieved = mySerial.read();
inData += recieved; // Process message when new line character is recieved
if (recieved == '\n')
{
//Serial.print("Arduino Received: ");
Serial.println(inData);
Serial.println ("------------");
inData = """"; // Clear recieved buffer
}
}
delay(800);
}
it turns out i needed to add the serial.flush() function to stop the frequent repetition
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11, true); // RX, TX
String inData;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() {
while (mySerial.available() > 0)
{
char recieved = mySerial.read();
inData += recieved; // Process message when new line character is recieved
if (recieved == '\n')
{
//Serial.print("Arduino Received: ");
//Serial.println(inData);
Serial.println(inData);
inData = ""; // Clear recieved buffer
mySerial.flush();
delay(1000);
}
}
}
Loko:
it turns out i needed to add the serial.flush() function to stop the frequent repetition
That will have different effects depending what IDE version you are using, but it is NOT necessary to call flush() in order to handle serial port input and output correctly.
You haven't clarified what the actual problem is, but I very much doubt that flush() has fixed the underlying problem.