Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

reading serial values from the Serial Monitor
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

reading serial values from the Serial Monitor

by Matty on Tue Oct 26, 2021 3:53 pm

As part of a larger program, I am trying to read 2 values sent from the Serial Monitor (for example "0, 3") using parseInt(). Often the code stops after reading the first value, but it is not consistent. sometimes it reads both, sometimes neither. I have tried adding delays, but that doesn't seem to help. If it hangs and I input another number from the SM, then it reads stand moves on, but Serial.parseInt() never times out?

Here is an example

I type: 0,1 (return)
serial characters Detected: got first number. got second number. .0, 1

That is what it SHOULD do, and sometimes does. then..

I type: 0,1 (return)
serial characters Detected: got first number.
then I type: 2 (return)
got second number. .0, 2

so it lost the "1" somehow

Here is the area of code I am dealing with:

Code: Select all | TOGGLE FULL SIZE
if (Serial.available() > 0) {
  Serial.print (" serial characters Detected: ");
  delay(50);  //to give time for the serial buffer to fill
 int fromFl = Serial.parseInt();
 Serial.print ("got first number. ");
   delay(50);  //to give time for the serial buffer to fill

 int toFl = Serial.parseInt();
  Serial.print ("got second number. ");

 while (Serial.available() > 0) {
  Serial.print (".");
byte b  = 0;
b = Serial.read();
 }//MATT: flush the incomming serial buffer
 
Serial.print (fromFl);
 Serial.print (", ");
 Serial.println (toFl);
 
 frame = fromFl;
 stopFlag = false;
 //set up floor chaning parameters here

  break;
  //}
  }//end of serial available


Any help would be appreciated!
Matty
 
Posts: 8
Joined: Fri Aug 17, 2012 8:26 pm

Re: reading serial values from the Serial Monitor

by mikeysklar on Thu Oct 28, 2021 3:15 pm

Two things come to mind.

1) Make sure the Serial baud rate you are setting in your code (not shown in your example) matches the bottom right corner of the Arduino IDE baud rate setting.

2) The serial console is likely adding add CR & NL to strings. You will want to monitor for those.

I'd experiment with the Serial Input Basics from the Arduino Forum.

Code: Select all | TOGGLE FULL SIZE
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;

void setup() {
 Serial.begin(9600);
 Serial.println("<Arduino is ready>");
}

void loop() {
 recvWithEndMarker();
 showNewData();
}

void recvWithEndMarker() {
 static byte ndx = 0;
 char endMarker = '\n';
 char rc;
 
 // if (Serial.available() > 0) {
           while (Serial.available() > 0 && newData == false) {
 rc = Serial.read();

 if (rc != endMarker) {
 receivedChars[ndx] = rc;
 ndx++;
 if (ndx >= numChars) {
 ndx = numChars - 1;
 }
 }
 else {
 receivedChars[ndx] = '\0'; // terminate the string
 ndx = 0;
 newData = true;
 }
 }
}

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChars);
 newData = false;
 }
}


https://forum.arduino.cc/t/serial-input-basics/278284

mikeysklar
 
Posts: 6184
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.