Split a String to many chars, and than send it throw Serial

Hello Freinds,

I wanna send a String throuw Serial to another ESP. I know how it works to send it via Serial. But what I do not know, is to split a String, and send it one by one.

In this example, the String Called 'Router':

String Router="Netgear";



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

void loop() {
  delay(5000);
  Serial.write('N');
  Serial.write('e');
  Serial.write('t');
  Serial.write('g');
  Serial.write('e');
  Serial.write('a');
  Serial.write('r');
  delay(2000);
}

Do you mean something like this?

for (char const& c: Router) {
  Serial.write(c);
}
3 Likes

Yes, like that. I will try, wait 10 minutes please. I have to go out for a cigarette :smiley:

Today man, you absolutely saved my day :smiley: it simply works :smiley:

1 Like

Wow, yes, it works too :smiley:

Why do you want to send each character individually?

This code will send each character immediately after the previous character, with no delay between characters. It takes roughly 87uS to send a character at 115200 baud, as soon as you start sending the 'N', the code continues with the next line, putting the next character into the transmit buffer. Before the initial 'N' is completely sent, the code will have reached the delay() and all the characters will be in the transmit buffer. At that point, the characters are being pulled from the transmit buffer and fed into the serial hardware's internal buffer, ready to be send as soon as the previous character completes.

void loop() {
  delay(5000);
  Serial.write('N');
  Serial.write('e');
  Serial.write('t');
  Serial.write('g');
  Serial.write('e');
  Serial.write('a');
  Serial.write('r');
  delay(2000);
}
1 Like

Okay, I am from Germany, therefore, I really do not understand everythink :slight_smile:

Do you wanna say, that this code

  Serial.write('N');
  Serial.write('e');
  Serial.write('t');
  Serial.write('g');
  Serial.write('e');
  Serial.write('a');
  Serial.write('r');

is better than this code

String Router="Netgear";
Serial.print(Router);

because the first one could be breaken up by the delay() function ?

Okay, Thanks man. I will send the String every 2 seconds. Thanks to ya all for your kind help.

Greetings from Germany.

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