pythonuartmicropythonrs485raspberry-pi-pico

RS485 Board starts losing numerous packets Raspberry Pico


I've built a data logger with a Raspberry Pico that sniffs data from an RS485 and saved them into an SD Card (see image). Everything works correctly with the only issue that over time the Raspberry seems to read incorrect data more and more frequently.

pico

A standard packet is composed by 80 bytes as below (it always starts with 0x02 and ends with 0x03.

[2, 140, 130, 128, 137, 128, 0, 0, 0, 128, 130, 135, 130, 173, 178, 128, 178, 128, 178, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 194, 188, 128, 230, 129, 184, 128, 128, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 56, 3]

Now the issue is that over time it seems packets overlap each other like

[2, 140, 128, 128, 137, 128, 0, 0, 0, 160, 136, 157, 136, 173, 167, 129, 181, 129, 181, 129, 128, 133, 128, 129, 128, 128, 128, 175, 155, 147, 160, 128, 128, 128, 128, 221, 255, 249, 255, 133, 188, 128, 230, 129, 192, 128, 192, 128, 128, 128, 152, 0, 2, 140, 128, 128, 137, 128, 0, 0, 0, 168, 136, 165, 136, 173, 167, 129, 181, 129, 181, 129, 128, 133, 128, 129, 128, 128, 128, 153, 156, 253, 159, 128, 128, 249, 255, 242, 255, 149, 128, 133, 188, 128, 230, 129, 192, 128, 192, 128, 128, 128, 152, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 67, 3]

In this istance you can see at a certain point (after the sequence 128, 152, 0) a new packet correctly starts and ends, interrupting the first one.

I'm using a simple Python code to read the UART on my Raspberry Pico. Basically I read every byte and wait for 0x02. As soon as I see it, I start reading the next bytes until I find 0x03, the EOF.

uart1 = UART(0, baudrate=38400, tx=Pin(0), rx=Pin(1))
led = Pin(25, Pin.OUT)

while True:
    while uart1.any() > 0:
        data_raw = uart1.read(1)
        if data_raw == b'\x02':
            line = bytearray()
            led.toggle()
            line += data_raw
            R_EOF = False
            while not R_EOF:
                data_raw = uart1.read(1)
                if data_raw == b'\x03':
                    line += data_raw
                    R_EOF = True
                    led.toggle()
                elif data_raw == None:
                    pass
                else:
                    line += data_raw

I can't understand why my packets overlap each other as, it seems a sistematic error. I thought it was a problem of hardware, like the connections are not soldered in the best way but, I'm not understanding why it happens more often the more time passes.


Solution

  • So the issue seems linked to the fact that the Python code needs to write on the same file at every cycle. This is very fast at the beginning because the file has limited size.

    This time increases when the file starts to occupy several hundreds of Mb and slow down the writing on file.

    The code used for write the file is

    with open("/sd/LOG.txt", "a") as file:
        file.write(stringa_logging + '\r\n')