Leonardo very slow after stopping pythonprog/serialmonitor

Hello,

and sorry but my problem is a bit hard to explain:

My Leonardo sends a lot of data to my pythonprogram via the serial connection over USB. Baudrate is set to 9600 but it seems to be that i receive a lot more characters than possible with that speed. As far as i understood, that normally would be around 1,2 bytes per ms but my logger in python shows me 30-40 characters arriving each ms. That is something, i dont understand here. (The mass of data send for the time is only for debugging)

Anyway, the main problem is as follows: I reset the Leonardo and the program runs without a problem sending lots of (and maybe way too much) data via the Serial wire. At this time, the Leonardo is connected to the computer but no program is listening on the serialport yet. Then i start my pythonprogram, Leonardo continues to run normal sending mass of data that i do log in my python program. The pythonstuff does not send anything to the leonardo while running. Then i exit the pythonprogram and exactly from this moment, the Leonardo is still working but very slow. In normal operation, the main loop takes about 12-13 ms with my code but now this could use up to 40 seconds ! But it still runs... As soon, as i start my pythonprogram again , the Leonardo goes back to normal speed. This is all the same with opening and closing the SerialMonitor from the Arduinosoftware.

I have no idea where to look for the problem, maybe one of you.

ippurk:
I have no idea where to look for the problem, maybe one of you.

I hope it is not too much of a disappointment when I say “neither do I”.

If you need help you need to post the code you are using. And please uses code tags - the # button - so your code looks like this

It would also be a good idea if you give a sample of the data sent by Python and the data that the Arduino appears to receive.

Have you a suitable waiting / checking process in your Python code to allow time for the Leonardo to establish a serial connection? Your Python code should only transmit once it is sure the Leonardo is ready.

…R

thanks for your answer, i try to post something out of my code, that matters.

For sending data to the computer, i created the following function:

struct Message
{
  int type;
  int data[16];
};

void send_message(struct Message m)
{
  Serial.print(m.type);
  for ( int i = 0; i < 16; i++)
  {
    Serial.print(m.data[i]);
    if (i != 15){Serial.print(";");}
  }
  Serial.println();
 }

what finally sends something like this

"23;3;2;4;45;...98\n"

the function for reading “Messages” from the Serial is something like that:

Message get_message()
{
  Message m;
  if (Serial.available())
  {
    int pos = 1;
    while (Serial.available())
    {
      int inByte = Serial.parseInt();
      // Get  type of message (first int)
      if (pos == 1)
        {
          m.type = inByte;
        }
      // Get attached data
      if (pos > 1)
      {
        m.data[pos - 2] = inByte;
      }
      // split data on occurence of ";"
      char inChar = Serial.read();
      if (inChar == ';')
      {
        pos += 1;
        continue;
      }
      // on occurence of "\n" message is complete
      if (inChar == '\n')
      {
        return m;
      }
    }
  }
  // if someting went wrong, return a "0" message
  m.type = 0;
  return m;
}
function is called from void loop() and loop contains a delay(10);

As i mentioned, the pythonpart does not send any bit of data to the arduino when the described problem occurs. Also it is the same problem, when i just start and then stop the “SerialMonitor” from the Arduino-programming-software.

For debuggingpurposes i now have many lines in between the code of the arduino that send something like this:
Serial.println("this is a text used for debuggingpurpose"); But that leads only to the fact, that the problem occurs earlier, as to say, immediately, when i close my pythonprog or serialmonitor. When i dont send such an amount of messages, the problem might occur not before an hour or maybe before 4 hours or something like that.

P.S.: Iam not disappointed at all, thank you :slight_smile:

I don't have enough information from you yet.

For example, does your Python program correctly receive the data sent by the Arduino?

You may have a problem with while (Serial.available() ) because the Arduino works very much faster than the serial port so it will probably terminate the WHILE after one or two bytes are received.

The demo Python and Arduino code in Reply #4 here may give you an idea of how to get things to work.

If you are posting code again, please post a complete sketch.

...R

Hello Robin2,

For example, does your Python program correctly receive the data sent by the Arduino?

Interesting point, but for the time i experienced no such problem.

I read through your code and its quite interesting. Maybe iam going to include something of that. But for the time i can not see, whether it deals with my problem, in fact, i believe, it doesnt.

I didn’t post my entire sketch because of its length, but if you want to see it, you get it. The main purpose of the sketch is controlling a LCD, sensors and some relays connected via I2C.

There are two versions attached, one is the original (WIP) and the other one includes lots of serial messages and lcd.print messages for debugging.
The one without debugging starts to get slow after some hours, the one including debugging slows down nearly immedeatly.

I figured out, that it slows down on execution of the function “send_message”.

EDIT: just a small addition: On another debugging try it slowed down on execution of the function “write_pin”.

What i really dont get here, is the fact, that the occurence of the error depends on whether the com port IN THE PC ! is opened or not, because the serial connection works asynchronously. And the problem does not occur, until i open and then close the SerialMonitor.

Maybe the problem is caused by the special serial port the Leonardo is using…

withDebugging.ino (46.6 KB)

withoutDebugging.ino (43.2 KB)

ippurk: What i really dont get here, is the fact, that the occurence of the error depends on whether the com port IN THE PC ! is opened or not, because the serial connection works asynchronously. And the problem does not occur, until i open and then close the SerialMonitor.

Your code is very long and I'm afraid I am too lazy to study it sufficiently.

The quote above is short but in my mind it contains a huge amount of underlying information that needs to be brought to the surface.

As a small example, why would you open and close the serial monitor - especially if doing so causes a problem?

I think you will find it difficult to figure out the problem with such a very large program. If it was my project I would write a very short sketch to communicate without a problem and then add extra bits step by step ensuring that each step works before going on to the next.

One thing to be very conscious of with a large sketch is that you may be running out of SRAM memory. That can have all sorts of strange consequences.

...R