Go Down

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


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.



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);

    pinMode(6, OUTPUT);

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

void loop()
    digitalWrite(6, LOW);

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


        digitalWrite(6, HIGH);
        //    digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(6, LOW);
        //    digitalWrite(LED_BUILTIN, LOW);

        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");
               inputString.toCharArray(buff, inputString.length());
               //        digitalWrite(6, LOW);
               // close the file
                // 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;


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

Go Up