Why is my serial data ending to soon

I'm trying to read from a BT serial port and write what I receive to a txt file, the code works fine for short strings (Ie: 1234) but cuts off with anything long (ie 12345676890 becomes 12345678) Here is the relevant part of my code:

void WriteConfigFile(String ConfigFile) {
    char receivedChar;
    Serial2.println("Waiting for New File");
    while (Serial2.available() < 1) { //wait for serial data and save to data file
         // wait
     }
    Serial2.println("Data Recived");
    SD.remove(ConfigFile.c_str()); //Remove Old File
    myFile = SD.open(ConfigFile.c_str(), FILE_WRITE);
    while (Serial2.available()) {
        receivedChar = Serial2.read();
            myFile.print(receivedChar);
            Serial2.print(receivedChar);
    }
    myFile.close();
    Serial2.println("New Config Writen");
}

any ideas?

Thanks for the help, I added a 5 second delay after reading the first byte from the buffer but same result. any more ideas?

Read the serial input basics tutorial. Those are proven methods for reading serial input.

It is preferred that you post the whole code. It may be OK in this instance, but often there is information missing when snippets are posted.

Good work posting in code tags on your first post. Not many new members bother reading the guide lines. Thank you and welcome.

1 Like

consider using Serial.readBytesUntil() to read a complete line of input terminated with a specific character (e.g. linefeed)

1 Like

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