Unreliable UART

Hello everyone,

I am having a hard time with communicating between a LoRa e5 mini board and something else (PC, RPi, Arduino) using UART.

Here is an output example :

hello from board
this is a very very very very long string, because i feel like it
well hello too
well helo too
that's a pretty long line too
that's a petty long line too
0123456789876543210
012345679876543210

Two first lines are issued from my board, then it is a line I am sending, and the line echoed back to me. It seems the problem is on the board reading side, but I can’t see what I am missing.

Here is the code that generated said output :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ztimer.h"
#include "stdio_base.h"

int main(void)
{
    stdio_init();

    size_t max_len = 240;
    char msg[max_len];

    stdio_write("hello from board\n", 17);
    stdio_write("this is a very very very very long string, because i feel like it\n", 66);

    while (1) {
        //checking for input data
        if (stdio_available()) {

            //wait a bit, making sure we get everything
            ztimer_sleep(ZTIMER_MSEC, 10);

            //read and print back
            ssize_t count = stdio_read(&msg, max_len);

            for (int i = 0; i < count; i++) {
                printf("%c", msg[i]);
            }
            printf("\n");
        }
        ztimer_sleep(ZTIMER_MSEC, 1);
    }
    return 0;
}

To connect to the board I tried pyterm, my own python script, the Arduino IDE Serial monitor, they all encounter this problem.

Interestingly, when I try tests/drivers/sx126x which includes a shell to try sx126x features, my input is correctly read, so I guess the problem is on some coding on parameters missing rather on RIOT OS itself

Does anybody have a clue ?

Edit : another (smallish) problem I encounter is it seems my data is not sent (from the board) until I send a \n I tried both printf and stdio_write, both seems to need that \n

Something seems off about the example code and output given. Your example code inserts newlines \n, but those are not showing up in the example output.

Also, stdio_read() is blocking. I don’t think you should need the calls to ztimer_sleep() for that reason.

Solved. Pasting my code for anyone that could need it :

#include <stdio.h>
#include "ztimer.h"
#include "stdio_base.h"


int main(void)
{
    stdio_init();

    size_t max_size = 256;
    char buffer[max_size];
    size_t current_size = 0;
    uint32_t timestamp = 0;

    while (1) {

        while (stdio_available()) {

            //read byte after byte, store it in buffer[]
            char c;
            ssize_t count = stdio_read(&c, 1);
            buffer[current_size] = c;
            current_size += count;

            timestamp = ztimer_now(ZTIMER_MSEC);
        }

        //no new data since 10ms, let's do something with it
        if (current_size > 0 && ztimer_now(ZTIMER_MSEC) - timestamp > 10) {
            stdio_write(buffer, current_size);

            //reset buffer
            current_size = 0;
        }

    }
    return 0;
}

There might be a better way to do it, but this works for me.

There is no need to call stdio_init() yourself, it’s already called in early_init(). You can also just use getchar() or shell_readline().