Clarity on Serial Monitor and Print/Write Commands

Current Solution to While Loop Code: I was able to find a decent solution to my while loop! I used the control function called "break". I used the asterisk symbol as the marker to exit out of the while loop. Unfortunately, I have some issues with it. It is not always consistent when I display the readings on both my Serial Monitor and my Cell Phone.

I am currently out of ideas to fix this issue so as to make the code run smoothly. I would appreciate any ideas on the matter and thank you. Also, before I forget, here is the latest section of code that I am using:

if (INCOMING_DATA == 97)    //a on the keypad
       {                                     
       while (Serial1.available())
           {
           SERIAL1_DATA = Serial1.read();
           if (SERIAL1_DATA != 13)
                                //13 is dec for carriage return. So long
                                //as SERIAL1_DATA does not equal 13 it 
                                //writes to the ESP32.
             {
             ESP_32.write(SERIAL1_DATA);             
             }
           Serial.write(SERIAL1_DATA);
                                //Previous two codes write data from PIC and
                                //display it on CELL PHONE & SERIAL MONITOR.
           if (SERIAL1_DATA == 42)    
                                //42 is decimal for *.                     
                { 
                break;          //The control structure "break" allows an 
                                //exit from a for or while or do loop.          
                }
           }
           ESP_32.println(" "); //Prints a space for better reading.
           Serial.println(" "); //Prints a space for better reading.
        }

Also, from the PIC the statements are created. There are 4 print() commands and the 4th print() command has the asterisk (*) on it, which is used to exit the while loop. Theses statements are sent to the ESP32 Bluetooth Device through Serial Communication. Then from the ESP32 Bluetooth Device is where I print these 4 statements. I appreciate the time and help on this you guys so thank you.

   printf("---------Request of Readings---------\n\r");                                    

   printf("Analog Voltage Value = %f V\n\r", Analog_Value);

   printf("Digital Value = %f\n\r", Digital_Value);
   
   printf("------------End of Readings-------------\n\r\*");

Also, And this is the resulting message that I get on my Cell Phone:

PerryBebbington:
I'm not saying you are wrong, but that seems wrong as serial ports receive incoming date in bytes. I would think the most sensible variable type would be byte, as the serial port does not know what the received data represents.

That’s the whole point of using 2 bytes because any byte value would be legit, form 0x00 to 0xFF, so how would you know otherwise that read has failed?

Sapphire_Dragon:
Also, from the PIC the statements are created. There are 4 print() commands and the 4th print() command has the asterisk (*) on it, which is used to exit the while loop. Theses statements are sent to the ESP32 Bluetooth Device through Serial Communication. Then from the ESP32 Bluetooth Device is where I print these 4 statements. I appreciate the time and help on this you guys so thank you.

can you clarify the ask?

J-M-L, sure, the previous post was to give the remaining details. The 4th print() command had the asterisk (*) on it:

printf("------------End of Readings-------------\n\r*");

I used the asterisk as the final marker as you suggested but I do not always get a clear response when I receive that data. I guess my question would be what is the best way to exit out of the while loop if I am using a single character? Maybe I could use a word? I'm not sure as to how though.

Why don’t you send a non printing character like ASCII 0x04 (End of Transmission) as a marker?

You could also send a key phrase like a dot alone on a line, in which case you need to listen for “\r\n.\r\n”
To do that you need to keep a small buffer of the last 5 bytes received and compare that buffer with the key phrase every time you get a new byte

Hey, J-M-L!

At the time, I didn't think of that, which will be another approach that I'll take so thank you for that! I believe I tried what you mentioned about a key phrase (i.e. a dot). Just for fun I used the capital letter "E".

The data from Serial1 Port would stop at "E" but when I called for the next message the data read right where I was last at.

EX: The End Game ====> The E *when I called the next message it would read:

nd Game *the letters on the left would appear; I'll post what this looks like.

Question: I wonder if I need to "reset" reading the data coming from Serial1 Port and is that possible? I have not found a command/function to "reset" (I have looked at Stream.flush() and bit.clear() but those will not work). I will be working on the dot method with the carriage return and next line notations and will report back to inform you what I got. I appreciate the input and guidance J-M-L!

As promised in my previous post, below is the data that I see on my Cell Phone (the marker I used is the captial letter "E"). Also, lower case "a" is the letter used to obtain data from Serial1 Port:

Well then you need also a start mark :slight_smile:
Otherwise You don’t know up to where you need to discard what’s coming in the serial buffer

Problem: Trying to designate the starting and ending markers. The text that I want printed out will repeat one more time before ending.

Text Code being sent thr. Serial Port: I am using Z as the starting marker while using X as the ending marker. This code is from the PIC microcontroller.

   printf("ZRequest of Readings------------------\n\r");                                    

   printf("Analog Voltage Value = %f V\n\r", Analog_Value);

   printf("Digital Value = %f\n\r", Digital_Value);
   
   printf("End of Readings-------------------------X\n\r\");

Arduino Code for Serial Monitor and Cell Phone: I have yet to figure out how to make this code work effectively. I could be misunderstanding a point from earlier. What concept or principle am I missing? I'd appreciate the help on this.

if (INCOMING_DATA == 97)    //a on the keypad
       {                                     
       while (Serial1.available())
           {
           SERIAL1_DATA = Serial1.read();
           if ((SERIAL1_DATA != 13) && (SERIAL1_DATA != 'Z')) 
                                //prints data as long as 13 or "Z" are skipped.   
                {
                ESP_32.write(SERIAL1_DATA);
                } 

           Serial.write(SERIAL1_DATA);

           if (SERIAL1_DATA == 'X')                        
                { 
                break;          //The control structure "break" allows an 
                                //exit from a for or while or do loop.          
                }

            }//Exit from the while loop.            
           
           ESP_32.println(" "); //Prints a space for better reading.
           Serial.println(" "); //Prints a space for better reading.
       
       }//Exit from the beginning if statement.

Attachment of Results on Cell Phone: I will post the pic up shortly.

**Here is the picture of my results on my Cell Phone of the code from the previous post. **

is your remote device configured to echo what is sent ?

one more time, the way you read the Serial buffer is not fool proof. go back have a look at Serial Input Basics on how to handle this, you'll see proper code to deal with start and end markers

Message: I’m sorry that it’s been a while for the holidays kept me away! I was able to understand and then created the following code that corrected my problem from earlier. The problem was the message would not stop at the desired ending of the statement (I used the asterisk symbol as an end marker of the statement). I want to thank J-M-L, PerryBebbington, david_2018, and those who offered their knowledge on this thread. I appreciate all your help you guys and thank you!

The code created used a while loop that would empty out the buffer until there were no more letters/numbers/symbols, which Serial1.available would become zero. This segment of code was the solution to the problem that I had. I will upload the specific code and the results to see!

I hope that this helps anyone who has a similar problem and for the administrator who monitors this thread can flag this as a completed thread. I appreciate all of the help and thank you!!

Code:

    if (INCOMING_DATA == 97)    //a on the keypad
       {                                     
       while (Serial1.available())           
          {
          SERIAL1_DATA = Serial1.read();
                if (SERIAL1_DATA != 13)
                    {
                    ESP_32.write(SERIAL1_DATA);                      
                    }
                Serial.write(SERIAL1_DATA);

                if (SERIAL1_DATA == '*')                        
                    { 
                    break;      //The control structure "break" allows an 
                                //exit from a for or while or do loop.          
                    }

            }//Exit from the while loop.             
           
           ESP_32.println(" "); //Prints a space for better reading.
           Serial.println(" "); //Prints a space for better reading.

           while (Serial1.available() > 0)
              {
              EMPTY_SERIAL = Serial1.read();  
              }
                                //This while loop is stating while there
                                //are characters in the Serial1 buffer place
                                //next character into variable "EMPTY_SERIAL"
                                //until there are no more characters.
                                 
       }//Exit from the beginning if statement.

Text Message Result: Alright, here is the final text message on my cell phone. And the message works as desired without problem! Thank you again everyone.

Well done !