Problems with serial.read()

I read out the serial port via individual char variables and collect them in a string.

This here is my code:

while(Serial.available()) {
char bufferBit = Serial.read();
bufferMessage += bufferBit;
bufferMessage.replace(" ", "");
}
if (bufferMessage != "") {
Serial.println(bufferMessage);
}

I entered the following text into the serial monitor:

all1000000000000000000000000000000MKE_1_0<Synchron_1_0>28490037070.11170. : 810.4294962926.37070. 42250.31890. 810.810.37070. 810.4294962926.810. 37070.11170. 810.810.26710. 42250.31890. 810.810.16350. 810.810.26710. 810.4294962926.810. 810.4294962926.4294962926 .: 16350.4294962926.:16350.42250.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070.:16350.37070. : 16350.37070.: 16350.37070.: 16350.37070.: 16350.37070.: 16350.37070.: 16350.37070.: 16350.37070.: 16350.37070.: 16350.37070.: 16350.37070.: 16350.37070.: 16350.37070.370707070.: 16350.37070.: 16350.37070.: 16350.37070.: 16350.810.: < /M>

The variable bufferMessage came out like this:

all1000000000000000000000000000000MKE_1_0<Synchron_1_0>28490037070.11170. : 810.4294962926.37070. 42250.31890. 810810300: 14992280: 70110808.61: 2538.8.1.30801.708029268.1.969.999.154462: 3.201537: 30701537: 3.0.607015371537: 3.7: 3.0: 3.0: 3.0: 3.7: 3371530107.6.0: 3371570600 :330100.337100.330600:5706.7100:57.3306..ME

The clock speed is: 115200

Can you please help me to fix the problem?

Which problem ?

Posting snippet of code will get you snippets of answ

Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

The serial input basics tutorial may be of interest.

Use of the String class can cause memory problems.

My first guess is that you are taking too long to process each character and the input buffer is overflowing. Try changing:

    bufferMessage += bufferBit;
    bufferMessage.replace(" ", "");

to

    if (bufferBit != ' ') // Add only non-spaces
      bufferMessage += bufferBit;

My first thought (guess) was serial console "eating" some characters. I have really never thrown the full ASCII set at serial console.

I would try reading a single character (c) and immediately doing a serial.Print(c);

I got frustrated many years back with serial poopoo and just built my own test device:
The QBF - "The Quick Brown Fox..." For Serial Diags - Community / Exhibition / Gallery - Arduino Forum

PS... darn thing is still on the bench - I need to upgrade it to a BluePill and make provisions for all standard BAUDs.

Hi there,
I want to read the following text on the serial port:
all1000000000000000000000000000000MKE_1_0<Synchron_1_0>275330636.636. 25666.:636.4294962926.636.:636.636.20660.:636.636.25666.:

This here is my code:

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

void loop() {
  while(Serial.available()) {
    test = Serial.readString(); 
    Serial.print("test: ");Serial.println(test);
  }
  delay(1000);
}

Exactly this text comes out without delay, but with delay only the following text comes out:
all1000000000000000000000000000000

Can someone please help me how to combine a delay and serial read?

Please, clearly mention what string you want to transmit from the InputBox (Fig-1) of the Serial Monitor to UNO? UNO will receive the charcaters of the string as they are arriving and will show them on the OutputBox of the Serial monitor. Correct?

I enter the string in the input box. The serial monitor just prints it out through my function in the loop.

Hi @mario_kuebler_mke. Since your two topics seem too closely related, and thus likely to result wasting the time of the helpers through the creation of parallel discussions, I have merged them into a single topic.

@mario_kuebler_mke

1. Without delay(1000), the following event occurs:

At bd = 500000, one charcaters takes about 20 us time (1*10/500000). readString() is a blcoking codes and it keeps receiving upto 64 charcaters from the Serial Buffer. Those 64 charcaters are printed. After that the remaining charcaters enter into the Serial Buffer, the readString() function reads/prints them correctly and you see them as concatenated with the previous partial string.

2. With delay(1000), the following event occurs:

After receiving/printing the first 64 charcaters, the MCU enters into 1-sec delay(1000) loop which is a blocking code and keeps the interrupt logic disabled/inactive. On the other hand, Serial Port operation requires "active condition of the interrupt logic"; so, no characters have been accumulated in the Serial Buffer though they have been arriving. As a result, you loose the remaining characters.

Hope, the explanations make sense.

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