Serial.read() and Serial.available() are working not as expected in the code

Hello, community.

I had an idea to use Serial I/O connection for communication between PC and Arduino Due in a way that from the computer I can send commands which can or do not have to return some values back to PC.
My code did not work as expected. Step by step I started to remove line by line to verify where the problem appeared. Beneath you can find the tests and results.

Test1:

char input;

void setup() {
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() > 0) {
    input = (char)Serial.read();
    Serial.print("We read:");
    Serial.println((byte)input, BIN);
  }   
  delay(1000);
}

Serial port monitoring (with timestamps):

0:12:07.437 -> We read:11011111
0:12:09.439 -> We read:11011111
0:12:11.437 -> We read:11111111
0:12:11.437 -> We read:11111111
0:12:13.415 -> We read:11110111
0:12:15.425 -> We read:11011111
0:12:17.446 -> We read:1111111
0:12:19.442 -> We read:10111111
0:12:21.427 -> We read:11111111
0:12:21.427 -> We read:11111111
0:12:23.431 -> We read:11011111
0:12:25.446 -> We read:10111111
0:12:27.415 -> We read:11111111
0:12:29.423 -> We read:11111111
0:12:29.466 -> We read:11111111
0:12:31.446 -> We read:1111111
0:12:33.424 -> We read:11111111
0:12:33.424 -> We read:11111111

You may think that everything is okay. The trick is that I do not type anything for sending to Arduino. Those bytes seem to be sent somehow automatically being filled by 1s in each bit. And sometimes 0 is set at some random bit.

Do you want to be amazed more? I was wondering how we could drop in the while loop if I did not type anything?! Therefore I added one more line to the code for debugging.

Test2:

char input;

void setup() {
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() > 0) {
    input = (char)Serial.read();
    Serial.print("We read:");
    Serial.println((byte)input, BIN);
  }   
  delay(1000);
  Serial.println(Serial.available()); // THIS IS A LINE I ADDED
}

Serial port monitoring (with timestamps):

0:13:36.388 -> 0
0:13:37.390 -> 0
0:13:38.372 -> 0
0:13:39.387 -> 0
0:13:40.395 -> 0
0:13:41.382 -> 0
0:13:42.384 -> 0
0:13:43.397 -> 0
0:13:44.378 -> 0
0:13:45.356 -> 0
0:13:46.397 -> 0
0:13:47.383 -> 0
0:13:48.393 -> 0
0:13:49.372 -> 0
0:13:50.399 -> 0
0:13:51.384 -> 0

Honestly, I am very confused. When I force to print if there is something in buffer, mistakenly random readings disappear! Is it not a great proof of quantum physics and observer effect?!
Okay, jokes apart.

What can cause this behaviour? How to avoid this issue?

I thought that the problem is in damaged USB-cable. But one small repair in code changes the behaviour as you have seen. Additionally I tried a new cable. The problem remained.
Reprograming the Arduino board constantly reproduces the results.

The board I am using: Arduino Due, programming port.
I used Arudino IDE 2.0 for programming..

Please, let me know if I am missing to tell you some other important information.
And I am sorry if I posted the topic in a wrong category. Answer to my question was not found by me here.

Thank you in advance!

Are you SURE your PC is configured for the same bit rate, parity, and number of start bits?

You may be encountering a bug with the Serial monitor with ide 2.0. Try entering from the monitor with Ctrl+Enter

https://github.com/arduino/arduino-ide/issues/572

I am not sure. I am going to investigate this. I was thinking about problem with configuration, but in Serial monitor there is only setting for the rate (at least, the most obvious button).

Are you really using a Serial port or are you using USB to do the puter to puter thingy?

The Arduino Due is connected to PC via USB 2.0 cable.
This is what I see in properties for the port which was assigned to Arduino USB connection (COM5).
COM5 configuration Windows

I have checked the properties of assigned COM5 for USB connection with Arduino.
You can find the in the reply to another comment.

I tried to explicitly say Serial.begin(9600, SERIAL_8N1); for the code. The situation is the same.
Do you know where I can check the settings for serial monitor in Arduino IDE 2.0?

Probably, it is not a bug but rather another way of sending the data. I will call it messenger-like confirmation.

But this is not related to the problem I have.

When using the Arduino Due's Native USB port, use the following Setup() code to initialize the port:

SerialUSB.begin(2000000); // Initialize Native USB port
while(!SerialUSB); // Wait until connection is established

An internet search on your part will reveal more information.

I can try to use Arduino Due's Native USB port later. At least in case if I cannot resolve the issue.
However, right now I am using programming port (title near it -> ATMEGA16U2).
Thank you for the suggestion.

Seems to be a problem with Arduino IDE 2.0 as supposed by @cattledog in #3.
I tried with 1.8.15 and it works as expected.

What can I say? Using Native USB port does not have this problem.

char input;

void setup() {
  SerialUSB.begin(9600); // Initialize Native USB port
  while(!SerialUSB) {}
}

void loop() {
  while (SerialUSB.available() > 0) {
    input = (char)(SerialUSB.read());
    SerialUSB.print("We read:");
    SerialUSB.println(input);
  }   
  delay(1000);
}

I typed "arduino" and this what I saw in the serial monitor tool:

21:36:30.984 -> We read:a
21:36:30.984 -> We read:r
21:36:30.984 -> We read:d
21:36:30.984 -> We read:u
21:36:30.984 -> We read:i
21:36:30.984 -> We read:n
21:36:30.984 -> We read:o

What I am looking for now is how can I check settings for Serial Port monitor in Adruino IDE 2.0.

Probably, the problem is because of beta version.
I have also just tried to upload the code via Arduino IDE 1.8.16. Everything works as expected. The Test1 from my original message does not send me back some random data as it was with IDE 2.0.

Should I report it as a bug?

And still one question remains for me. How can I change setting of Serial monitor tool in Arduino except baud rate?

It sounds like this bug:

Don't worry about it. That was a "red herring".

1 Like

To send receive words and or sentences I use start and stop characters like so:

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  sSerial.reserve ( StringBufferSize300 );
  char OneChar;
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == ‘>’)
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
              xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          sSerial.concat ( OneChar );
        }
        else
        {
          if ( OneChar == ‘<’ )
          {
            sSerial = “”; // clear string buffer
            BeginSentence = true; // found begining of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
  }
  vTaskDelete( NULL );
} //void fReceiveSerial_LIDAR( void * parameters  )

You are right. Seems to be exactly the same. And, as mentioned, happens when using IDE 2.0.
Thank you for the link.

About 'red herring'.. I agree that that was not the solution but I still wonder. We cannot change parity or stop bits etc. in Serial Monitor, can we?

Oi! I got an idea.
How about taking a look at this:
Serial - Arduino Reference

And the thingies about serial.begin and serial.end ? Might be onto something?

I would stay on the line, what in0 mentioned Serial.read() and Serial.available() are working not as expected in the code - #14 by in0 .
It must be some weird bug with Serial Monitor in the new IDE with some type of boards.

So, to clarify what we have, I did the next steps.
I uploaded this code via Arduino IDE 2.0:

char input;

void setup() {
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() > 0) {
    input = (char)Serial.read();
    Serial.print("We read:");
    Serial.println((byte)input, BIN);
  }   
  delay(1000);
}

In the Serial Monitor Tool of Arduino IDE 2.0 (baud rate was set to 9600) I was receiving this (once again I emphasize that I did not send anything by myself)

22:46:13.818 -> We read:11111111
22:46:15.788 -> We read:11011111
22:46:17.814 -> We read:11011111
22:46:19.798 -> We read:11101111
22:46:21.797 -> We read:11111111
22:46:23.822 -> We read:11011111
22:46:25.819 -> We read:11101111
22:46:27.798 -> We read:11111111
22:46:29.788 -> We read:11011111

I closed the Serial Monitor and opened Arduino IDE 1.8.16 Serial Monitor Tool (settings of baud rate 9600). I have seen clear screen (as expected). That was why I typed several space bars. This is the result:

22:49:49.645 -> We read:100000
22:49:50.637 -> We read:100000
22:49:52.602 -> We read:100000
22:49:52.648 -> We read:100000
22:49:52.648 -> We read:100000
22:49:53.603 -> We read:100000
22:49:53.650 -> We read:100000

Nice bit representation of the space bar character (decimal code: 32; binary code: 00100000).

I closed the Serial Monitor.
Then I installed Tera Term terminal, set the settings for the Serial Port (9600, 8 bit, no parity, 1 stop bit, no flow control), and opened the terminal. No wonder, but I saw nothing. Therefore, I typed several space bars again. And expected messages appeared:

We read:100000
We read:100000
We read:100000
We read:100000
We read:100000
We read:100000
We read:100000

As we can see, nothing was changed in/with the Arduino DUE. Yet, different serial monitors provided different results.

My verdict: I face the bug with new Arduino IDE 2.0.
No direct evidence that the bug is the same or at least similar to the reported one is the period of getting some weird characters in IDE 2.0 Serial Monitor Tool. In the bug report per1234 mentioned that:

On my Leonardo, the RX LED flashes at 1 Hz while the Serial Monitor is connected

If we check the timestamps for IDE 2.0 in my post, you can see that the period between weird characters receiving is around 2 s (I have rounded to the tenth of second in the timestamp sequence and kept only seconds): 13.8, 15.8, 17.8, 19.8, 21.8, 23.8, 25.8, 27.8, 29.8.
It is in a good agreement with reported 1 Hz (or 1 s period) because I additionally have a delay in my code for 1000 ms.
1 s + 1000 ms = 2 s.

Removing the delay(1000) function creates not only similar frequency of receiving false data, but sometimes they become distorted (actually, this was the reason why I placed delay in the code):

23:13:24.906 -> We read:11111111
23:13:25.959 -> We read:1111111
23:13:26.966 -> We read:1111111
23:13:27.998 -> We read:11111111
23:13:29.051 -> U�����11111111
23:13:29.051 -> We read:11111111
23:13:30.104 -> U�����11111111
23:13:30.104 -> We read:11111111
23:13:31.140 -> We read:11111111

Thanks everybody for the help and reading.

I wasn't able to reproduce that with your sketch sans delay, my Due, and Arduino IDE 2.x, but it does look similar to this: