Oops - in last posted code I missed the "1" in Serial1 for the Flush operation, but the code I tested was correct and failed as I noted. Further experiment has given an "acceptable" performance with a delay of 95 mS after the Flush operation.
I may explore UART Interrupts to improve things. Final code below.
t
// for ProMicro Leonardo & HC-12 RF link
// RF Serial port uses default Serial1 pins ( Monitor uses USB serial port )
byte RF_CMD = 21; // For RF Mode Changes, Normal HIGH, take LOW for commands
boolean GOT_RF_DATA, READING_DATA, GOOD_DATA; // flags RF data
// following are RF Data Block delimiters
char RFStart = 0x02; // "STX" = Start of RF data block
char RFEnd = 0x17; // "ETB" = End of RF data block
char RFText[32];
byte NumChars, MaxChars = 31;
byte RF_MessageLength = 18; // "Normal" RF message data length
// Simulated Leg Movement delay before next search for RF data
int LegDelay = 2000;
//--------------------- INITIAL SETUP
void setup()
{
Serial.begin(115200); // start Leonardo Monitor Serial Port
while (!Serial && millis() < 5000) // wait up to 5 seconds for Serial Port
// Initialise HC-12 RF link for Serial Data reception via Serial1 port
Serial1.begin(9600);
pinMode(RF_CMD, OUTPUT);
digitalWrite(RF_CMD, HIGH); // HC-12 normal, transparent mode
GOT_RF_DATA = false; // clear flag for more data
NumChars = 0; // Empty the RF Character Array
} //------- END OF INITIAL SET UP
void loop()
{
GetAllRFText(); // if any RF data available, read it
if (GOT_RF_DATA == true) // when we have a Data Packet from Base Station
{
PrintRFText(); // show RF Data Packet on Monitor
// now finished with this data, clear Flags for next transmission
GOT_RF_DATA = false; // clear flag for more data
NumChars = 0; // Empty the Character Buffer
}
// now waste some time, e.g. moving our Legs
Serial.print(" .... Start Delay ... ");
delay(LegDelay);
Serial.println(" ##### end delay ###");
}
//======== end of MAIN loop ===================================================
//##############################################################################
// Read whole RF character string, "boxed" by RFStart, RFEnd codes.
void GetAllRFText(void)
{
byte RF_Char;
// first FLUSH the Input Buffer to clear "old" data
while (Serial1.available() > 0) {
Serial1.read();
}
delay(95);
// if any RF data now available,look for start character, then stay here until
// whole packet is read
if (Serial1.available())
{
RF_Char = Serial1.read();
if (RF_Char == RFStart)
{ // Start of data seen, Flag this, then read till end of data
READING_DATA = true;
NumChars = 0;
while (GOT_RF_DATA == false) // now read rest of packet
{
if (Serial1.available())
{
RF_Char = Serial1.read();
if (RF_Char != RFEnd) // this char is content of packet
{
RFText[NumChars] = RF_Char;
NumChars++;
if (NumChars >= MaxChars) NumChars = MaxChars - 1;
}
else // indicates end of data packet
{
RFText[NumChars] = '\0'; // terminate the data string
READING_DATA = false;
GOT_RF_DATA = true;
}
}
} // end of while(GOT_RF_DATA == false)
}
}
}
//##############################################################################
// print the RF text on Monitor
//##############################################################################
void PrintRFText(void)
{
if (GOT_RF_DATA == true)
{
// Display on Local Monitor
Serial.print(" Received Text >");
Serial.print(RFText);
Serial.print("< ");
Serial.print(" > "); Serial.print(NumChars);
if (NumChars != RF_MessageLength)
{
Serial.println(" <!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
Serial.println();
}
}
//===== END OF ALL CODE ======================================================ype or paste code here