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. 
 }
}
Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
asked Jun 5, 2015 at 15:48
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(). Commented Jun 5, 2015 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. Commented Jun 10, 2015 at 15:07

1 Answer 1

2

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
 }
 }
}
answered Jun 10, 2015 at 15:23
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? Commented Jun 10, 2015 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. Commented Jun 10, 2015 at 17:49
  • The final result was receive each byte one by one and removed any delay in the code. Thank you @Edgar Bonet Commented Jun 18, 2015 at 15:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.