user10295: 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.             
    }
}


user7508: 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
        }
    }
}


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

<p>Here is my Arduino code:</p>

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

        Serial.print(buffer);
        //delay(500);
        int j = 0;
        while (j &lt; size)
            buffer[j++] = '\0';
        //while (Serial.available()) Serial.read(); I tried it
        //Serial.flush();   Also tried it, but nothing.             
    }
}
</code></pre>
 7508: <p>Instead of blocking the loop for 500&nbsp;ms, you should be actively
reading the data as it comes. Otherwise the internal buffer of the
<code>Serial</code> object will overflow after only 64&nbsp;bytes are received.</p>

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

<pre class="lang-c++ prettyprint-override"><code>void loop() {
    static char buffer[256];
    static size_t pos;              // position of next write

    while (Serial.available() &amp;&amp; pos &lt; 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
        }
    }
}
</code></pre>
