[Python] Why do any other baud rate than 9600 corrupt my serial communication from my python...

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a personal project that consists of synchronizing my addressable ws2811 light strip to Spotify. There is no real-time audio analysis. My python program reads a JSON file, and based on the song playback, determines which LEDs should change to the specified color.

    The JSON has this format:

    { "0": [[0,4,0,0,255], [5,6,255,0,0]], "1832": [[0,0,255,0,0]]}

    The keys represent a song playback in ms. For each array, the first 2 values represent a range of LED indexes, and the last 3 are rgb values. So "0":[[0,4,0,0,255], [5,6,255,0,0]] means "at 0ms, change LEDs 0 to 4 to blue and LEDs 5 to 6 to red".

    As I am still developing it, the range of indexes logic is not completed yet, meaning the first 2 values are always equal (in other words, each 5 byte array is currently only responsible for a single LED). While this could be optimized, working with a very small amount of data already introduces noticeable delays. I'm limiting it to 10 LEDs (in what the JSON contains), which means that even with the range logic, my program would scale poorly to 300+ LEDs.

    Python:

    for timestamp in led_data.keys():
    if last_sent_timestamp < timestamp <= elapsed_time:
    message = bytearray([])
    for values in led_data[timestamp]:
    message.append(254) # Start byte
    led_index_1, led_index_2, r, g, b = values
    message.extend([led_index_1, led_index_2, r, g, b])
    message.append(253) # End byte
    ser.write(message)
    ser.flush()
    last_sent_timestamp = timestamp


    Arduino:

    #include <FastLED.h>
    #define NUM_LEDS 60
    #define DATA_PIN 6
    #define START_BYTE 254
    #define END_BYTE 253

    CRGB leds[NUM_LEDS];

    void setup() {
    FastLED.addLeds<WS2811, DATA_PIN, BRG>(leds, NUM_LEDS);
    FastLED.clear();
    FastLED.show();
    Serial.begin(9600);
    }

    void loop() {
    static byte buffer[7]; // Buffer to hold the incoming packet
    static int index = 0;

    while (Serial.available() >= 7) {
    byte incomingByte = Serial.read();

    if (incomingByte == START_BYTE) {
    index = 0; // Reset buffer index
    }

    buffer[index++] = incomingByte;

    if (index == 7 && buffer[6] == END_BYTE) {
    // Complete packet received
    int ledIndex1 = buffer[1];
    int ledIndex2 = buffer[2]; // Useless for now
    int r = buffer[3];
    int g = buffer[4];
    int b = buffer[5];

    if (ledIndex1 < NUM_LEDS) {
    leds[ledIndex1] = CRGB(r, g, b); // Changing a single LED rather than a range for now
    }
    index = 0; // Reset buffer for the next packet
    }
    }
    FastLED.show();
    delay(1);
    }


    When working on 9600 baud rate, there is no data corruption. The significant data corruption only occurs after changing the baud rate, but sending lots of data for a single timestamp or many consecutive timestamps on 9600 introduces too much delay. So I tried any other baud rate, like 57600 or 115200.

    I'm setting ser = serial.Serial('COM9', 115200) and Serial.begin(115200), changed the port's setting to 115200 in my device manager. I also tried other usb cables and ran the code on another computer. On these baud rates, I'm not just missing LED data, they very often show the wrong color (but somehow never the wrong LED).

    I can't find much documentation about this off-brand Arduino (Elegoo).

    What am I doing wrong?

    TLDR: ser.write seems to slow down my program, but changing the baud rate for a faster communication corrupts my data.

    Thanks in advance.

    Continue reading...

Compartilhe esta Página