0

I am sending a short 12-char control string over serial with Python and Debian Jessie.

It works, but sometimes chars are added or skipped randomly. Here it is an extract of the Arduino code:

void setup() {
  matrix.begin();
  matrix.setTextWrap(true);
  matrix.setFont(&TomThumb);
  matrix.fillScreen(matrix.Color333(0, 1, 1));
  matrix.swapBuffers(false);
  Serial.begin(115200);
}

void loop() {
  char c = 0; // serial char buffer
  byte i = 0; // string counter (reused multiple times)
  // read the equalizer control string
  delay(1);
  if (Serial.available() > 0) {
    turnmatrixoff();
    String control_str = Serial.readStringUntil('\n');
    Serial.println((control_str));
    draw_equalizer(control_str.c_str());
  }

Here is the log of the strings echoed back from the Arduino:

10563942332
00863931110
00773940100
0000974940100
00763940100
10693931211
00593950100
00544840100
00464950100
1050000
004549310744940100
00664961211
00694941111
10784951100
0
00593931100
00373931100
00483920100
00583720110
10473510000
000332710000
00252710000
00142510000
00122620000
002510000
003310100
00334520110
00343520100
00244720100
00355820110
00343611110
10144621210
00424921210
10343921210
00233811310

As you can see, some strings are truncated or padded (they all should be of the same size).

I've tried coding a char-by-char read function but the results were even worse:

char c = 0; // serial char buffer
byte i = 0;

while (Serial.available() > 0) {
  delay(1);  //small delay to allow input buffer to fill
  c = Serial.read();
  if (c == '\n' || i >= EQUALIZER_STR_SIZE - 1) {
    // finished reading the string
    equalizer_str[i] = 0; // terminate the string
    Serial.println(equalizer_str); // debug echo
    draw_equalizer(equalizer_str);
    break;
  } else {
    // append to the string
    equalizer_str[i] = c;
    i += 1;
  }
}

EDIT: here it is the full python script.

Decreasing the baud rate to 9600 seems to fix the issue, but the matrix updates too slowly this way.

Maybe I could use some other serial port initialization trick to prevent buffer overflows?

UPDATE:

I've found another way to reduce the serial traffic by increasing the audio capture chunk. For some reason this way the errors are gone completely. Here it is a video of the code finally running fine.

  • 2
    Change delay(1); //small delay to allow input buffer to fill to delay(1); //small delay to allow input buffer to overflow. There is never any excuse for using a delay while reading serial. – Majenko May 13 '17 at 12:48
  • do you mean i should remove the delay? – eadmaster May 13 '17 at 12:50
  • 1
    Absolutely. You might like to read this too: hackingmajenkoblog.wordpress.com/2016/02/01/… – Majenko May 13 '17 at 12:55
  • just tried removing the delay, i still get the truncated strings... – eadmaster May 13 '17 at 12:56
  • Also you should be aware that there is no handshaking on Arduino serial. If you don't read the data faster than you send it you will overflow. If you need to, slow down your sending with a delay in the python code. – Majenko May 13 '17 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.