Hello,
I am trying to develop a simple interface that receives 'commands' from a host over the serial port. I am trying to receive bytes which I parse and interpret as commands at the Arduino.
I have an issue with the demonstration code below in which I am looking for waiting serial data that needs to be read.
// Test of Serial.available()
int bytesWaiting;
char rxString[64];
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
void setup() {
// Open the serial port connection at 115,200 baud
Serial.begin(115200, SERIAL_8N1);
Serial.println("\nInitializing");
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void readSerialData() {
// Check for pending serial data; if it exists, read it in.
Serial.println("\nEntered readSerialData()...");
Serial.print("* I see bytesWaiting = ");
Serial.println(bytesWaiting);
while(1);
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
void loop() {
bytesWaiting = Serial.available(); // Will be > 0 if there is unread serial data in the buffer
if (bytesWaiting > 0) { // If there are more than none, then loop through and receive them
Serial.println(bytesWaiting);
// readSerialData();
}
}
If I leave the call to readSerialData() commented out so the function is not called, upload and run the code, and enter some characters at the IDE terminal, bytesWaiting will correctly report the number of characters I typed - so far so good.
If I uncomment the function call, then irrespective of the number of characters I enter into the terminal, bytesWaiting is suddenly now reported as 1, both inside and outside the function.
I'm confused. Any insight appreciated.