ethierlab / Motopya

MotoTrak python Version
MIT License
0 stars 0 forks source link

Serial connection Optimizations #4

Open siamakhaz opened 1 month ago

siamakhaz commented 1 month ago
  1. First thing, define debug mode for debugging serial communication. In actual usage, you will send only the necessary data.

    const bool debug=true;
    if (debug)  Serial.println("Some Message");
  2. Remember that each character you add will introduce a delay to the system. Be mindful of adding characters in serial communication; avoid adding extra characters to keep the system running smoothly.

  3. Use flash memory space instead of RAM for fixed messages. For example:

    SerialUSB.print(F("constant string"));
  4. Accumulate data in a local buffer and send it in larger chunks. Here’s an example:

    const int data = 230;
    const float fdata = 203.5;
    const char sdata[] = "Hello";
    char buffer[100]; // Buffer to hold the formatted string
    int index = 0;
    for (int i = 0; i < 100; i++) {
     index += snprintf(buffer + index, sizeof(buffer) - index, "Motopya! data %d float %.2f String %s index %d", data, fdata, sdata, i);
     if (index >= sizeof(buffer) - 50) {  // Ensure there's enough space for the remaining data
       Serial.println(buffer);
       index = 0;
     }
    }
  5. Minimize Serial Flushes. Use SerialUSB.flush() sparingly, as it can block program execution.