Thanks for all your responses - there is still a problem. The RF data packet is 18 ASCII chars "boxed" in with STX & ETB, so 20 chars total, arriving about every 100 mS.
If the "Leg Action" delay is less than 100 mS all is fine, If delay is greater then there are multiple poor reads, of 13 chars, 26 chars, etc, with the odd good one, but not really usable.
As far as I can see, once "GetAllRFText" has seen an STX, all activity should be local to this routine until the ETB is seen and the routine Returns to the main Loop, so it should get a good Data Packet.
It looks to me as if the Serial1 buffer is getting corrupted, overwritten or suchlike.
My "minimal code" trial is below:-
// 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 = 1000;
//--------------------- 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 (Serial.available() > 0)
{
Serial.read();
}
// 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 ======================================================