Serial Read Problem

Hello,

I am trying to read values coming in from the serial port of a ESP8266. The values can be 2, 3, or 4 digit values.

Everything works as long as the data is more than 2 digits. But if the data is 2 digits, then most of
the time a random value is added to its LSB.

For example if the data is 35. Then it shows me a value of 351, or 350, or 358.
I used to terminate the data with “\n” sending out of the ESP8266 to the MEGA2560. I then
changed it to a value of “Z”. But it did not make any difference.

Any ideas. Here is my code

void Rcv_Temp()         // This routine updates the Cooling Menu Temperature every time Cooling Web Page is called
{
char  buff[6];           // This is used for reading data coming in - 6 bytes
char  buffer[4];        // Serial read only reads one byte at a time. So we need
int   index, n, m, i;         // to create a buffer to store each there. Once we receive
                        // a 'Z' from the serial port, then we
                        // put bytes together to get the integer value received
                        // First data coming in is FWD Temperture, the second is AFT temp
                        // I tried ending the DATA with carriage return('\n'). But if the Data
                        // coming in is less than 3 digits, it gets corrupted and add a digit
                        // to the LSB. 
    for (i=0; i<50; i++)  // Loop 50 times to make sure that you receive the Temp coming in
    {
      
    server.handleClient();

   
       if (Serial.available()) 
      {
    
      // Store the incoming data one byte at a time in the buffer
       index = Serial.readBytesUntil('Z', buffer, 4); // new line every 4 chars. 

        if (buffer[0] == 'F')
        {

          int n = atoi(buffer + 1);       // exclude the first char 'F' and convert the rest to integer
          
          Serial.println(n);          // For trouble shooting only. We dont want to send it to MEGA2560
          buffer[0] = '\0';             // end Terminate the buffer
        
     
          Temperature = String(n);      // pass the value to the Temperature to send out to the website
        }

        if (buffer[0] == 'A')
        {

          int m = atoi(buffer + 1);       // exclude the first char 'A' and convert the rest to integer
          
          Serial.println(n);          // For trouble shooting only. We dont want to send it to MEGA2560
          buffer[0] = '\0';             // end Terminate the buffer
        
    
          Temperature1 = String(m);      // pass the value to the Temperature to send out to the website
        }

        

         
       }
    }

}

Thanks

It makes no sense to me what you are doing.

You don't need the for loop.

You keep reading until you get all 4 bytes.

     index = Serial.readBytesUntil('Z', buffer, 4); // new line every 4 chars.

The newline character is not 'Z'.

Look for the "serial input basics" thread.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

Hello,

Example #3 worked for me.

Thank you all. :slight_smile: