0

I am trying to read long strings from the Serial, using arduino. In order to spare some RAM I don’t use Serial.readString(). I use Serial.read() instead.

During the reading, I also print some debug info to the same Serial port.

Let's say I send 90 characters to the Serial from the PC. I read them with Serial.read(). If I call a few times Serial.println() between two calls of Serial.read(), I get only 67 character of 90 (Serial.available()>0 is false).

I read all the 90 character when I don’t call Serial.println() at all.

Why? I was sure that it is possible to write to the serial and read from it simultaneously, that these are independent processes.

Am I wrong?

7
  • I don't understand, what you mean by "as much I print ... as shorter read string is". Please explain more and more clear – chrisl Feb 4 '20 at 19:54
  • Please show us the sketch you're using. – VE7JRO Feb 4 '20 at 19:56
  • I send 90 characters to the serial. I read them with Serial.read(). If between to calls of Serial.read() I call Serial.println() a few times, I read only 67 character of 90. I read all the 90 character when I don’t call Serial.println at all. – zhekaus Feb 4 '20 at 19:57
  • @VE7JRO ,I believe I described my issue well enough. The sketch is quite long and it will take some time to cut it out. – zhekaus Feb 4 '20 at 20:03
  • 2
    The Serial buffer size is only 64 bytes. If you take too long for printing and your PC sends the data too fast, you will loose data. But that depends on your sketch, how much data you are printing and what baudrate you use – chrisl Feb 4 '20 at 20:21
2

You can indeed simultaneously read and write through the serial port. At the hardware level, the serial port (the UART) is a transmitter and a receiver, which are almost independent. At the software level, they are both handled through interrupts that read from / write to a software buffer.

There is, however, a limitation you should be aware of: the software buffers are not large, typically 64 bytes on AVR-based boards. If you try to write too much too often, the output buffer will fill out, and then all subsequent writes become blocking, and run as slow as the serial port itself. If you receive lots of data and do not read it soon enough, the input buffer will overflow, and you loose some data.

From your description of your issue, it would seem you are hitting the limits of both buffers. You Serial.println() too much, too often. Then these write operations take too long and, in the meanwhile, you do not Serial.read(). Since your PC is not waiting for you to Serial.read(), your input buffer eventually overflows, and you only get slightly more than a buffer worth of characters.

1
  • I'm completely satisfied with your answer. Thank you so much indeed! – zhekaus Feb 4 '20 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.