Odd behavior of variable that reads Serial.available()

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.

Do you intend to stop and freeze the Arduino?

@jremington - yes, I wanted to make sure updating ceased at that point.

When you uncomment the call to readSerialData, as soon as you type the first character, readSerialData is called. The code then prints some text and the value of bytesWaiting (which will be 1 because the Arduino is a lot faster than you are at typing and so it called readSerialData as soon as you typed the first character). The function then enters a dead spin loop in while(1); and never does anything else.

I was previously unaware of readBytesUntil() which seems to work for what I need. Apparently it can block but for my application this doesn't matter.

Thanks for the above explanations.

// Test of Serial.readBytesUntil()

char inBuffer[64];
char CR = 13;   // ASCII decimal code for Carriage Return
int readStatus;
int i;

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
void setup() {
  Serial.begin(115200, SERIAL_8N1);
  Serial.println("\nInitializing");
}

// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void parseString() {

Serial.println("\nEntered parseString()...\n");

Serial.print(" readStatus was: ");
Serial.println(readStatus);

Serial.print(" inBuffer contains: ");
Serial.println(inBuffer);
Serial.println();

  for(i=0;i<64;i++){
    inBuffer[i] = 0;
  }
}

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
void loop() {

  if (Serial.available()) {
    readStatus = Serial.readBytesUntil(CR, inBuffer, 64);
    parseString();
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.