# Serial.readString trunc long text

**URL:** <https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428>\
**Category:** Programming\
**Created:** [August 18, 2022, 11:22am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428 "2022-08-18T11:22:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 11:22am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/1 "2022-08-18T11:22:04Z")

</div>

Hello,

i don't understand why my text is truncated.  
this is a exemple of my message:

4200;0;100;0|0;0;0;400|0;0;0;-100|0;0;1;0|-247;0;100;0|0;0;0;500|0;0;0;-100|0;0;1;0|-466;0;124;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;124;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;137;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;137;0|0;0;0;500|0;0;0;-100|0;0;1;0|714;0;173;0|0;0;0;500|0;0;0;-100|0;0;1;0|-714;0;149;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;149;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;137;0|0;0;0;500|0;0;0;-100|0;0;1;0|466;0;173;0|0;0;0;500|0;0;0;-100|0;0;1;0|-507;-744;137;0|0;0;0;500|0;0;0;-100|0;0;1;0|97;-441;126;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;147;0|0;0;0;500|0;0;0;-100|0;0;1;0|263;-65;108;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;166;0|0;0;0;500|0;0;0;-100|0;0;1;0|-199;-499;158;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;115;0|0;0;0;500|0;0;0;-100|0;0;1;0|-10;-959;120;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;154;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;-1580;154;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;120;0|0;0;0;500|0;0;0;-100|0;0;1;0|10;-959;158;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;115;0|0;0;0;500|0;0;0;-100|0;0;1;0|199;-499;108;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;166;0|0;0;0;500|0;0;0;-100|0;0;1;0|-263;-65;126;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;147;0|0;0;0;500|0;0;0;-100|0;0;1;0|-97;-441;137;0|0;0;0;500|0;0;0;-100|0;0;1;0|507;-744;100;0|0;0;0;500|0;0;0;-100|0;0;1;0|-466;0;137;0|0;0;0;500|0;0;0;-100|0;0;1;0|466;0;173;0|0;0;0;500|0;0;0;-100|0;0;1;0|-507;13255;137;0|0;0;0;500|0;0;0;-100|0;0;1;0|97;-441;147;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;126;0|0;0;0;500|0;0;0;-100|0;0;1;0|263;-65;166;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;108;0|0;0;0;500|0;0;0;-100|0;0;1;0|-199;-499;158;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;115;0|0;0;0;500|0;0;0;-100|0;0;1;0|-10;-959;120;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;154;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;-1580;120;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;154;0|0;0;0;500|0;0;0;-100|0;0;1;0|10;-959;158;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;115;0|0;0;0;500|0;0;0;-100|0;0;1;0|199;-499;166;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;108;0|0;0;0;500|0;0;0;-100|0;0;1;0|-263;-65;147;0|0;0;0;500|0;0;0;-100|0;0;1;0|0;0;126;0|0;0;0;500|0;0;0;-100|0;0;1;0|-97;-441;137;0|0;0;0;500|0;0;0;-100|0;0;1;0|-6892;0;0;-100|

this text is 2089 characters long

```cpp

int countStep(String *text, int text_length){
  int c=0;
  for (int i=0; i < text_length; i++)
  { 
    if(text->charAt(i) == '|') 
    { 
      c++; 
    }
  }
  return c;
}

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

void loop() {
  while (!Serial.available());
  val_Text = Serial.readString();
  int text_length = val_Text.length();
  Serial.println(text_length);
  Serial.println(val_Text);
  int counterStep_ = countStep(&val_Text, text_length);
  Serial.println(counterStep_);
}

```

but when we print text length with find 1486 for exemple

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 18, 2022, 11:26am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/2 "2022-08-18T11:26:47Z")

</div>

What is arduino board you are use?

---

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 11:28am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/3 "2022-08-18T11:28:37Z")

</div>

arduino UNO

---

<div class="post-metadata">

**Author:** ![foltynfan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/foltynfan/32/793134_2.png) [@foltynfan](https://forum.arduino.cc/u/foltynfan)\
**Post date:** [August 18, 2022, 11:28am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/4 "2022-08-18T11:28:42Z")

</div>

What baud rate is the Serial monitor

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 18, 2022, 11:30am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/5 "2022-08-18T11:30:19Z")

</div>

> [@land95100](#):
>
> this text is 2089 characters long

> [@land95100](#):
>
> arduino UNO

Uno has 2048 bytes of RAM only  
There is no way it can accept a line in 2089 chars

and note that part of RAM is used by your program, so the maximum string will even shorter, like your 1489 bytes...

---

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 11:30am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/6 "2022-08-18T11:30:35Z")

</div>

> [@foltynfan](#):
>
> What baud rate is the Serial monitor

it s a arduino Uno so i use 9600 or 115200

---

<div class="post-metadata">

**Author:** ![foltynfan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/foltynfan/32/793134_2.png) [@foltynfan](https://forum.arduino.cc/u/foltynfan)\
**Post date:** [August 18, 2022, 11:31am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/7 "2022-08-18T11:31:04Z")

</div>

Okay so your using 9600

---

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 11:31am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/8 "2022-08-18T11:31:09Z")

</div>

aie ok thank you

---

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 11:32am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/9 "2022-08-18T11:32:58Z")

</div>

yes i used 9600

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 18, 2022, 11:39am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/10 "2022-08-18T11:39:45Z")

</div>

Tell us about your project. Why do you need to send such long lines?

There are two questions here -

1. Why do you going to transfer this line at once? long strings are usually broken into short parts, say 100 characters each, and transmitted one by one

2. What are you going to do with a string over 2000 characters on a board with 2k memory? even if you can accept this line in parts, you still won’t be able to process it as a whole

---

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 11:44am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/11 "2022-08-18T11:44:36Z")

</div>

it's for a radial printer, it s print point in gel texture

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 18, 2022, 11:48am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/12 "2022-08-18T11:48:00Z")

</div>

> [@land95100](#):
>
> 4200;0;100;0|0;0;0;400|0;0;0;-100|0;0;1;0|-247;0;100;0|0;0;0;500

These parts, separated by '|' stick - looks like separate instructions for a printer?  
so you can transfer and print one instruction at once

---

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 11:50am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/13 "2022-08-18T11:50:34Z")

</div>

yes it was i did in first, but it was complicated with the delay, so that the instructions do not overlap I had to add time and the print was less fluid

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 18, 2022, 11:53am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/14 "2022-08-18T11:53:30Z")

</div>

Do not use readString() because delays. Read chars one by one with read(), separating instructions by '|' as in the big string  
There are lot of string parsing tutorials in the net

---

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 11:55am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/15 "2022-08-18T11:55:36Z")

</div>

it works like a stack ? the texts will be added one after the other ?

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 18, 2022, 11:56am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/16 "2022-08-18T11:56:30Z")

</div>

i do not understand  
Where the texts will be added?

---

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 11:59am UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/17 "2022-08-18T11:59:45Z")

</div>

when we read character by character, if we send several strings, the loop will go to the next text once it has done all the characters of the previous text? or it will overwrite

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 18, 2022, 12:04pm UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/18 "2022-08-18T12:04:11Z")

</div>

Transmitter sends text chars one by one and receiver accepts it in the same sequence.  
The chars can't overlap.  
Once the receiver get the '|', it understands that the instruction is received in full and print this line at the printer.  
The lines are not stored at the receiver.

---

<div class="post-metadata">

**Author:** ![land95100](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@land95100](https://forum.arduino.cc/u/land95100)\
**Post date:** [August 18, 2022, 12:06pm UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/19 "2022-08-18T12:06:21Z")

</div>

ok i understand thank a lot \<3

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 18, 2022, 12:07pm UTC](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428/20 "2022-08-18T12:07:41Z")

</div>

but note, in this case the rest of the program must not contain delays, otherwise characters will be lost.

[Next page](https://forum.arduino.cc/t/serial-readstring-trunc-long-text/1023428.md?page=2)
