2

I am using QtSerialPort to send and receive data from Arduino. I am sending more than 64 bytes (125, 220, more), I receive the data with Arduino, and trying to sending back the same data, but Arduino sending only 64 bytes in its response. Then I am thinking that the problem is with Arduino buffer. I am trying to clear the data but without result.

Here is my Arduino code:

void loop() {    
    if (Serial.available() > 0) {
        int i = 0;
        delay(500);
        int size = Serial.available();
        while (i < size)
            buffer[i++] = Serial.read();

        Serial.print(buffer);
        //delay(500);
        int j = 0;
        while (j < size)
            buffer[j++] = '\0';
        //while (Serial.available()) Serial.read(); I tried it
        //Serial.flush();   Also tried it, but nothing.             
    }
}
2
  • You are probably also sending the zeros back, hard to guess how that will confuse QT. It will. Just no point to using a buffer, just read one byte and send it back with Serial.write(). – Hans Passant Jun 5 '15 at 16:57
  • 1
    Remove the delay(500): during this delay, you are letting the internal buffer of Serial overflow. Instead of just wasting 500 ms, you should be copying the incoming data into your own buffer array as it comes. – Edgar Bonet Jun 10 '15 at 15:07
3

Instead of blocking the loop for 500 ms, you should be actively reading the data as it comes. Otherwise the internal buffer of the Serial object will overflow after only 64 bytes are received.

Here is an example that copies the incoming bytes into a larger buffer (256 bytes) and, upon receiving an end-of-line character, echoes back the whole line.

void loop() {
    static char buffer[256];
    static size_t pos;              // position of next write

    while (Serial.available() && pos < sizeof buffer - 1) {

        // Read incoming byte.
        char c = Serial.read();
        buffer[pos++] = c;

        // Echo received message.
        if (c == '\n') {            // \n means "end of message"
            buffer[pos] = '\0';     // terminate the buffer
            Serial.print(buffer);   // send echo
            pos = 0;                // reset to start of buffer
        }
    }
}
3
  • after every read action the buffer consume one character and the buffer size is menor? So if I constantly send data to Arduino and constantly read from buffer, the buffer never going to be on overloaded? – Robert Jun 10 '15 at 16:40
  • Did not really understand your question. There are two buffers: the one from the Serial object (64 bytes), and your application's buffer. Which one are you talking about? At 9600 bps, each byte takes about one millisecond, which is plenty. As long as loop() runs often enough (with no delay()), the Serial buffer is not going to overflow. – Edgar Bonet Jun 10 '15 at 17:49
  • The final result was receive each byte one by one and removed any delay in the code. Thank you @Edgar Bonet – Robert Jun 18 '15 at 15:39

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.