Reading long strings on Arduio Nano 33 BLE

Hello everyone,

I'm currently trying to wrap my head around an issue I'm experiencing with the Arduino Nano 33 BLE I'm working on.

My goal is to receive quite long strings from Serial and store them into array but I'm currently having some problem with the reading part of my program.

For now I'm using the Receiving several characters from the Serial Monitor example from this topic
https://forum.arduino.cc/index.php?topic=288234.0

I'm sending this type of data to the serial monitor (either 40 different values or 20) :

<0,0,0,1,0,2,0,2,0,0,2,2,0,1,55,134,169,188,202,213,217,215,228,238,234,223,238,238,240,239,241,241,241,241,233,241,243,240,255,249>

When trying with an Arduino Uno WIFI I have absolutely no issues and the string is perfectly printed to the serial monitor but with the Arduino Nano 33 BLE I'm getting either two possible output : the full one or an incomplete version (as seen in the picture attached)

Here is the code :

const byte numChars = 200;
char receivedChars[numChars];

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithStartEndMarkers();
    showNewData();
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
 // if (Serial.available() > 0) {
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

Do you have any suggestions on things to change in the code to make sure I'm receiving the complete line each time ?

Thanks for your help !!

It looks as if when the problem occurs, it's just after the 64 character mark which implicates the buffering that the serial port does - if the buffer is full, anything more that's sent is discarded.

There have been a couple of threads recently about the Nano 33 BLE and timing and it appears that there are background tasks that it runs in addition to the code in your sketch. If one of those is using the CPU while serial data is in flight, it might cause enough delay to cause buffer overflow.

Try some experiments with strings with less than 64 chars to see if it works consistently.

Edit: typo.

wildbill:
It looks as if when the problem occurs, it's just after the 64 character mark which implicates the buffering that the serial port does - it the buffer is full, anything more that's sent is discarded.

I didn't even think of the fact that the serial buffer could be full, thanks for the suggestion ! I'll look into splitting the data in parts and see if I'm still getting the issue

wildbill:
There have been a couple of threads recently about the Nano 33 BLE and timing and it appears that there are background tasks that it runs in addition to the code in your sketch. If one of those is using the CPU while serial data is in flight, it might cause enough delay to cause buffer overflow.

Interesting, I guess we can't do anything to avoid this, it could lead me to changing the board I'm using for the project ! I'll keep an eye on the threads !
Thanks so much wildbill, got a few things to try now ! I'll report back if it changes anything

I believe this is the same as this issue I opened on GitHub: Nano 33 BLE unreliable USB communication · Issue #129 · arduino/ArduinoCore-mbed · GitHub

Unfortunately, I haven't received any replies from the Arduino devs yet :frowning:

Pieter

PieterP:
I believe this is the same as this issue I opened on GitHub: Nano 33 BLE unreliable USB communication · Issue #129 · arduino/ArduinoCore-mbed · GitHub

Unfortunately, I haven't received any replies from the Arduino devs yet :frowning:

Pieter

Hey Pieter,
Ironically I'm glad I'm not the only one in this situation, but still that situation is unfortunate. I'm indeed in a similar case based on what I can tell from your github thread :confused:
I did a few more tests just now and looks like anything above 128 char (which is apprarently the limit size of the Hardware serial) so I will definetely have to split the string in two or more parts before sending it to the Nano !

You could post a link to this thread in the GitHub issue thread, so the Arduino developers know that multiple people have this problem.

Hopefully it gets fixed soon. The Nano 33 BLE has some great capabilities, but not being able to reliably receive data over USB is a deal breaker in many applications.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.