Go Down

Topic: Arduino UART to File Issue (Read 97 times) previous topic - next topic

kas_lewis

I'm trying to take an input from the Arduino serial connection and write that to a file on the IoT2020. The code I'm using testing with is bellow

The issue I'm having is that the code does not appear to recognize that there is an input and nothing imputed from the serial appears in the file. I even added an LED "notification" to turn on when a new line is detected in the interrupt but it never turns on. This would mean there is something intrinsically flawed in my code. Any help would be great.

Thanks

K

Code: [Select]
//File handle creation
FILE *testFile;

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
char buff[400];

void setup()
{
    //  sleep(3);
    Serial.begin(115200);

    pinMode(6, OUTPUT);
    pinMode(LED_BUILTIN, OUTPUT);

    // reserve 200 bytes for the inputString:
    inputString.reserve(400);
    // Create files
    system("touch /media/test.txt");
}

void loop()
{
    digitalWrite(6, LOW);
    delay(1000);

    testFile = fopen("/media/test.txt", "a+");
    fprintf(testFile,"This is a great test that has just started \n\r");
    fclose(testFile);

    while(1)
    {

        digitalWrite(6, HIGH);
        //    digitalWrite(LED_BUILTIN, HIGH);
        delay(1000);
        digitalWrite(6, LOW);
        //    digitalWrite(LED_BUILTIN, LOW);
        delay(5000);

        if (stringComplete) {
            // clear the string:
            inputString = "";
            stringComplete = false;
            testFile = fopen("/media/test.txt", "a+");
            //if the file opened okay, write to it:
            if (testFile)
            {
               fprintf(testFile,"This is a great test");
               Serial.println(inputString);
               inputString.toCharArray(buff, inputString.length());
               fprintf(testFile,buff);
               //        digitalWrite(6, LOW);
               // close the file
               fclose(testFile);
           }
           else
           {
                // if the file didn't open, print an error
                Serial.println("error opening test.txt");
            }
        }
    }
}


/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX.  This routine is run between each
time loop() runs, so using delay inside loop can delay
response.  Multiple bytes of data may be available.
*/

    void serialEvent() {
     while (Serial.available()) {
        // get the new byte:
        char inChar = (char)Serial.read();
        // add it to the inputString:
        inputString += inChar;
        // if the incoming character is a newline, set a flag
        // so the main loop can do something about it:
        if (inChar == '\n') {
            digitalWrite(6, HIGH);
            stringComplete = true;
        }
    }
}

Lucario448

Why you clear out the string before actually writing it to the file?

Go Up