7 Replies Latest reply on Jun 28, 2017 3:09 PM by retrorocket1

    Intel Edison, Arduino IDE - File Write

    retrorocket1

      Not sure this is the right place for this but here goes,

       

      So I recently got the Intel Edison arduino board and have been running into issue after issue. I think I have finally resolved all the hardware issues.I am using the Arduino IDE on Windows 10.

       

      What I am trying to do is have live reporting from a plotting software to the Intel Edison as well as respond to previously stored values. These values are stored until the algorithm updates to include them. Now I can write to a file without any issues if I am in append mode but if I open a file in write mode it won't write to the file at all.

       

      This is a modified version of blink just to test the file writing functionality of the Edison.

       

       FILE* fp;
      // the setup function runs once when you press reset or power the board
      void setup() {
        // initialize digital pin LED_BUILTIN as an output.
        pinMode(LED_BUILTIN, OUTPUT);
      
      }
      
      // the loop function runs over and over again forever
      void loop() {
        fp=fopen("rslt.txt", "w");
      
        for(int i = 0; i < 10; i++)
        {
        q = 1;
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        fprintf(fp,"%d " ,q);
        delay(1000);                       // wait for a second
        digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
        fprintf(fp,"0 ");
        delay(1000);                       // wait for a second
        }
        
        fclose(fp);
        
      }
      
        • 1. Re: Intel Edison, Arduino IDE - File Write
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi Retrorocket1,
           
          Thanks for your interest in the Intel® Edison platform.
           
          When you use the append mode “a”, it will append data at the end of the file, for example, using that mode in the code you shared, it will open and write the data to the file, when the for loop is completed, the file will be closed with the data saved, then it continues with the next loop, so your file will have the previous data and the new one will be added and the end.
           
          On the other hand, when you use the writing mode “w”, it creates an empty file for writing, and in case there already exist another file with the same name, its content is erased and the file is considered as a new empty file. Therefore, using that mode in your code, it will open and write data to the file, when the for loop is completed, the file will be closed with the data saved, and then continues immediately with the next loop, that will erase the data and consider the file as a new empty one, that’s why you are seeing your file empty.
           
          Hope you find this information useful.
           
          Regards,
          -Yermi A.
           

          • 2. Re: Intel Edison, Arduino IDE - File Write
            retrorocket1

            Okay so if I understand what you are saying, in write mode it only saves once the file is closed, not when it recieves the next line of data?

             

            What I want Is I open the file, It overwrites everything, the program writes to the file for x number of runs, It reads the file, then erases all contents and starts again. From my understanding of the write mode the code I put in the initial post should write in that manor. Now I haven't added any code that reads it but If i open the file while it is running shouldn't I be able to read it?

            • 3. Re: Intel Edison, Arduino IDE - File Write
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              Hi Retrorocket1,

              According to the code you posted, once you have the data stored, it immediately opens the file again (erasing the old data) and stores new data, then repeats it again and again, so if you want to read that data you should read it after the file is closed.  Please look at comment in the code below.

               

              1. void loop() { 
              2.   fp=fopen("rslt.txt", "w"); 
              3. for(int i = 0; i < 10; i++) 
              4.   { 
              5.   q = 1
              6.   digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
              7.   fprintf(fp,"%d " ,q); 
              8.   delay(1000);                       // wait for a second
              9.   digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
              10.   fprintf(fp,"0 "); 
              11.   delay(1000);                       // wait for a second
              12.   } 
              13.   fclose(fp); 
              14. // Here you should open the file in reading mode to read the data before the loop runs again and overwrite the file
              15. }

               

              Note: when you open the file in write mode, once you have written the data, you should close it, then, to read the file you should open it in read mode and once the read is done the file should be closed again.

              Here you can find examples to write and read to a file C Programming Files I/O

               

              Hope that information helps, however, let me know if you have any questions.

              Regards,
              -Yermi A. 

              • 4. Re: Intel Edison, Arduino IDE - File Write
                retrorocket1

                so I added the following lines to the program where you said but it still doesn't work.

                 

                  fp=fopen("rslt.txt", "rb");
                  fscanf(fp, "%d", &a);
                  Serial.println(a);
                  fclose(fp);
                

                 

                It is able to upload just fine though. And the LED blinks meaning there isn't any hangs waiting for input. Just nothing is printed to serial.

                • 5. Re: Intel Edison, Arduino IDE - File Write
                  Intel Corporation
                  This message was posted on behalf of Intel Corporation

                  Please take a look at the following code, in which I’m able to read the file and print the characters in the Serial monitor:

                   

                   

                  void loop() { 
                  
                    fp=fopen("/home/root/test.txt", "w"); 
                  
                    for(int i = 0; i < 5; i++) 
                    { 
                    int q = 1; 
                    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level) 
                    fprintf(fp,"%d " ,q); 
                    delay(1000);                       // wait for a second 
                    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW 
                    fprintf(fp,"0 "); 
                    delay(1000);                       // wait for a second 
                    }
                  
                    fclose(fp);
                  
                     char a;
                     fp = fopen("/home/root/test.txt","r");
                     while(1)
                     {
                        a = fgetc(fp);      //it will return a single character from the file
                        if( feof(fp) )      // check for the EOF (end-of-the line)
                        {
                           break ;
                        }
                        Serial.println(a);  // print the characters
                     }
                     fclose(fp);   
                  }
                  

                   

                  This is the output of the Serial Monitor.
                  Serial Monitor.png

                   

                  You can use that code a reference and modify it according to your needs.

                   

                  Rregards,
                  -Yermi A.

                  1 of 1 people found this helpful
                  • 6. Re: Intel Edison, Arduino IDE - File Write
                    retrorocket1

                    hmm, that code isn't working even in its native form. It will upload but it isn't sending anything to the serial monitor. I might have to reflash my edison.

                    • 7. Re: Intel Edison, Arduino IDE - File Write
                      retrorocket1

                      reflashing did not fix it. Hmm. Does the regular usb plug need to be plugged in or just the two micro-usb cables?

                       

                      also relaized that I didn't have a serial.begin command specifying the baud rate. unfortunately, I am getting the random symbols you get when you try to read at the wrong baudrate.

                       

                      here is my revised code and serial monitor.

                       

                       

                       

                      int q;
                      
                       FILE* fp;
                      // the setup function runs once when you press reset or power the board
                      void setup() {
                        // initialize digital pin LED_BUILTIN as an output.
                        pinMode(LED_BUILTIN, OUTPUT);
                        Serial.begin(9600);
                      }
                      
                      
                      void loop() {  
                        Serial.println("1");
                        
                       // fp=fopen("test.txt", "a");  
                        
                        for(int i = 0; i < 5; i++)  
                        {  
                        int q = 1;  
                        digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)  
                        fprintf(fp,"%d " ,q);  
                        delay(1000);                      // wait for a second  
                        digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW  
                        fprintf(fp,"0 ");  
                        delay(1000);                      // wait for a second  
                        }  
                        
                        fclose(fp);  
                        
                        char a;  
                        fp = fopen("test.txt","r");  
                        while(1)  
                        {  
                            a = fgetc(fp);      //it will return a single character from the file  
                            if( feof(fp) )      // check for the EOF (end-of-the line)  
                            {  
                              break ;  
                            }  
                            Serial.println(a);  // print the characters  
                            Serial.println("1");
                        }  
                        fclose(fp);    
                      } 
                      

                       

                      Serial Monitor.JPG

                       

                      edit: nvm, realized a line was still commented, it appears to be working at least in append mode

                       

                      edit2: write mode not so much, In write mode I get basically the above image without the random symbol line. Meaning the file acts like it is empty.

                       

                      edit3: nvm apparently the I t just wasn't updating immediately. Thank you so much.