I am sending large texts from PC to Arduino over Serial and I cannot simply copy-paste it, as Arduino is not fast enough to process it on the fly.
I need some flow control (probabely XON/XOFF) so Arduino would automatically send "I am bussy, stop sending" when its input buffer is about overflow and then "send more data" when the buffer is empty again.
(The Arduino program need to process the received text and it may take a time and there is not enought RAM to buffer all input. And on the other hand it should be responsive, so it should not wait for full buffer.)
(I had more projects with similar problems. The current is FORTH interpret on Arduino Uno (atmega328p) / Arduino Mega (atmega2560), where I want to copy-paste some programs first (say 1-5kB of text) and then continue with interactive communication (run some words, see results, play games ...)
What is the best solution?
-
examine avrdudejsotola– jsotola2025年06月24日 18:06:12 +00:00Commented Jun 24 at 18:06
-
@jsotola - avrdude is used for "burning" binary code into flash via bootloader. I need to communicate with running program. How is avrdude suposed to help?gilhad– gilhad2025年06月24日 20:43:53 +00:00Commented Jun 24 at 20:43
-
it sends large amount of data without error ... see how it is accomplished ... examine the source code for avrdude and for the bootloaderjsotola– jsotola2025年06月24日 22:44:15 +00:00Commented Jun 24 at 22:44
-
It uses internal protocol with known buffer size (256) and synchronisation messages dialog style ( such this ww1.microchip.com/downloads/aemDocuments/documents/OTH/… ). I would prefere something supproted by existing terminal programs if possible.gilhad– gilhad2025年06月25日 02:50:30 +00:00Commented Jun 25 at 2:50
-
How often do you need this data transfer? And what is on the other side of transmission? Who is transmitting?halfbit– halfbit2025年07月23日 00:30:32 +00:00Commented Jul 23 at 0:30
1 Answer 1
The Arduino Serial
has no support for flow control, and neither does
the serial port on the AVR microcontrollers. Unless you find a library
that handles this for you, you will have to manage it on your own.
A typical way to handle serial input is to copy the incoming
bytes from the Serial
buffer into your own buffer and, when you see a
message terminator (typically some end-of-line), you handle the whole
message. Maybe something like this:
void loop() {
/* Process incoming commands. */
while (Serial.available()) {
static char buffer[BUFFER_LENGTH];
static int length = 0; // length received so far
int data = Serial.read();
if (data == '\r') { // on end-of-line:
buffer[length] = '0円'; // terminate the string
if (length) exec(buffer); // call the interpreter
length = 0; // reset the buffer
} else if (length < BUFFER_LENGTH - 1) {
buffer[length++] = data; // buffer the incoming byte
}
}
}
If you are following this scheme, you could modify it to add your own
XON/OFF support. Assuming the interpreter is the slow bit, you may
simply replace exec(buffer);
with:
{
Serial.write(0x13); // send XOFF
exec(buffer); // call the interpreter
Serial.write(0x11); // send XON
}