1

I am a college student in an "introductory" to robotics course. At the beginning of the year we were told to buy a Netburner 5270 board. Throughout the year we learned some basic codes but now our final project is almost due and I am having an error. My code is pretty basic. I want it to first check if the pushbutton is pressed, if it is check if dipswitch1 is high, if it is then turn led1 on, turn servo (open door), and create-write a string- and close a text file onto my SD card. Everything works...70% of the time.

This is the function that should do everything I need for my SD card

    void Enter_Door(char * FileName) {
    iprintf("\r\nCreating file: %s\r\n", FileName);
int i = 0;
F_FILE* fp;
do {
    fp = f_open( FileName, "a+" ); // Creating and Opening File
    i++;
    if (fp == 0) { /// Does NOT open
        OSTimeDly(TICKS_PER_SECOND / 2);
    }
} while (fp == 0 && i < 5);
if (fp == 0) {
    iprintf("\r\n Error in Opening:%s\r\n", FileName);
} else {

    const unsigned int write_buffer_size = 100;
    char write_buf[write_buffer_size];
    sniprintf(write_buf, write_buffer_size, "Person Entered at %ld seconds\r\n", Secs);
    int n = f_write( write_buf, 1, strlen( write_buf ), fp );
    iprintf("Wrote %d bytes: %s", n, write_buf);

    iprintf("Closing file %s\r\n\r\n", FileName);

    int rv = f_close( fp ); // Close a previously opened file of type F_FILE
    do {
            i++;
            if (rv != 0) { /// Does NOT open
                OSTimeDly(TICKS_PER_SECOND / 2);
            }
        } while (rv != 0 && i > 10);
    if (rv != 0) {
        f_close_PrintError(FileName);

        DisplayEffsErrorCode(rv);

    }

** the two do while loops in there I most recently added to try to redo the call, I am not sure if I wrote that write (again this is my first coding class)

My error occurs within the f_close function. I will push the pushbutton 10 times in a row and everything will work, but the 11th time I will get and error, but the 12th time it will work again? My typical errors are: * Error in f_close("Enter.txt") durring task(Main); F_ERR_NOTOPEN

or

* Error in f_close("Enter.txt") durring task(Main); F_ERR_WRITE

or

* Error in f_close("Enter.txt") durring task(Main); F_ERR_NOTFORMATED

Now like I said I do not get an error alot, but when I do it is one of those three. I really do not know whats going on or how to even attempt to fix it. This code is being written on NBeclipse. If anyone has any suggestions or tips I would really appreciate it.

Ben Norris
  • 11
  • 1
  • Why is there a loop and delay after `f_close`. Doesn't it return a status code immediately? – Joe Dec 05 '12 at 19:19
  • f_close(), like iprintf(), is not a C or C++ standard library function. Are these wrappers around fclose() and printf()? Who wrote the wrappers or where did they come from? Could f_close() itself be buggy? – phonetagger Dec 05 '12 at 19:47
  • @Joe - The OP mentions in his post that the loops & delays were added to help debug his problem. While the top do-while loop might possibly help (re-try the f_open() call), the bottom do-while loop obviously won't help even the slightest, since it's not re-trying anything. – phonetagger Dec 05 '12 at 19:49
  • Hey guys, Thanks for your responses. Again i will say I have never coded anything before thats why I am so stressed. @ Phonetagger How would you write a loop to attempt and re-try closing it. Again thanks for responding this is awesome. Our professor gave us a base demo code which wrote to and SD but I had to implement it in my own way and change it all around to work for my project, so if you see any part of the code that is wrong just let me know – Ben Norris Dec 05 '12 at 20:43
  • Is this a C++ or C question? Does your professor know they are not the same language? – Sebastian Mach Dec 27 '12 at 16:40

1 Answers1

0

In the following code you are not properly using f_close:

    int rv = f_close( fp ); // Close a previously opened file of type F_FILE
    do {
            i++;
            if (rv != 0) { /// Does NOT open
                OSTimeDly(TICKS_PER_SECOND / 2);
            }
        } while (rv != 0 && i > 10);

Try adding f_close into the loop instead:

    int rv;
    i = 0;
    do {
            rv = f_close( fp ); // Close a previously opened file of type F_FILE
            if (rv != 0)  /// Does NOT open
                OSTimeDly(TICKS_PER_SECOND / 2);
            i++;
        } while (rv != 0 && i > 10);

I don't know if this will specifically fix your problem, but looking at your code you were calling f_close and setting 'rv' to that functions return value before you looped. Once in the loop you were incrementing 'i' and then checking the value of 'rv'. This repeated 10 times and then exited the loop. You need to call f_close inside of the loop so that 'rv' is set on each iteration. Otherwise you were just using an empty loop.

Additionally, 'i' needs to be re-initialized to 0 before you use it again. Without looking at your code in a full sense, I can see that if there were an error opening the file at the top of your code then 'i' could possibly already be set to a value of 5. Unless your intention was to start out the value at 5 and run through the f_close loop another 5 times it is bad practice.

Andrew Corsini
  • 1,548
  • 3
  • 11
  • 11