i have an ESP32 and i want to read data from an serial connection, but it seems to be that the Serial2 is to slow because the Serial2.available() always overflows. So I´m losing some bytes while reading.
Is there a way to increase the Serial2 buffer? For now its 256 Bytes big. My receiving message is around 1200 Bytes.
You are slowing things down by printing multiple bytes for every byte you receive. Serial.println() is asynchronous, but only when there is space in the transmit buffer. Once the transmit buffer is full, Serial.println() blocks until enough data has been transmitted out of the buffer for there to be space for the new data.
I think client.write() is slow. Although the network communication speed is fast, there is overhead to initiate the communication. It is much faster to store the incoming data in a buffer, then write it all at once. If you can't spare the memory for the full 1200 bytes, you could use a smaller buffer and then write whenever the buffer is full.
Thanks, that was the problem, now its also working with 115k. For now everything is working because i´m always expecting 1208 Bytes, but in the future it could be that the response from the serial has a different length. There is also no character which can show me the end of the telegram. So is there a way to speed up the client.write? Or maybe should i just timeout the serial.read ( because now its too fast ) ?
you can receive any amount of serial input. It will client.write() the data whenever the buffer fills up. This also allows you to use a smaller buffer if you are short on memory (though you do need a large enough buffer that the client.write() overhead doesn't make your while loop too slow to catch all the serial input).
Note that you should only call client.stop() at the end of your while (client.connected()) loop.
I seem to remember that I found in an ESP8266 project I did years ago that there is some maximum number of characters you can send at once, but I don't remember what the exact number was.
It comes down to whether you know the maximum possible amount of serial input, and whether you have enough memory to hold all that in a buffer. If that's not possible, then you need to send it out in chunks as the buffer fills up. Fortunately, the ESP32 has quite a bit of memory, so that makes the option of sending it all at once more feasible.
I'm glad if I was able to be of assistance. Enjoy!
Per