10 years, 6 months ago.

Accessing files on mbed drive

I just began playing with file I/O on mbed and we rather shocked to discover that using LocalFileSystem does not write a file on the drive that is visible to the PC connected to the mbed. It seems that there is some sort of file system created with the binary because when I re-compiled and ran the program again the contents of the file were erased.

Is there any way to write files on the drive that is visible to the PC? If yes can someone please point me at an example.

Thanks!

2 Answers

10 years, 6 months ago.

LocalFileSystem writes a file to the drive that is visible on your PC. Only the drive will be closed while you have open files.

Just try this example: http://mbed.org/users/mbed_official/code/LocalFileSystem_HelloWorld/

Tim Borland
poster
10 years, 6 months ago.

My code follows a similar pattern to the one in the example except that my program does not exit, it sits in a while loop blinking the LED. I do close the file when I am done with it, so there must be some clean up that needs to happen on the local variable "local" to have the file completely closed and available to the PC.

Just did a quick test and exiting the program does not help either. However I know the file is working because I keep incrementing a number in the file and displaying it on an LCD. Every time I reset the mbed the number on the display increments, but I can never see the text file on the drive.

posted by Tim Borland 11 Oct 2013

Can you post the code you are using? Just tested it with:

#include "mbed.h"
LocalFileSystem randomname("local");
DigitalOut myled(LED1);

int main() {
    FILE *p = fopen("/local/woop.txt","w");
    fprintf(p, "Sound of da police");
    fclose(p);
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

And that works fine for me.

posted by Erik - 11 Oct 2013

Here is my code minus the LCD library:

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);
#define COMPARESIZE 8192
unsigned char compareBytes[COMPARESIZE];
SainSmart2004LCD lcd(0x7e, p9, p10, &pc);
LocalFileSystem fs("local");
char line[80];

void GetString()
{
	FILE * fp;
	fp = fopen("/local/data.txt", "r");
	if(fp != NULL)
	{
		fgets(line, 80, fp);
		fclose(fp);
	}
	else
	{
		line[0] = '0';
		line[1] = 0;
	}
}

void SetString(void)
{
	FILE * fp;
	fp = fopen("/local/data.txt", "w");
	if(fp != NULL)
	{
		fputs(line, fp);
		fclose(fp);
	}
}

int main()
{
    pc.baud(115200);
    pc.format(8, Serial::None, 1);
    pc.printf("Testing!\r\n");
 
    lcd.init();
    pc.printf("After Init\r\n");
    wait(1);
    lcd.backlight();
    pc.printf("After backlight\r\n");
    lcd.clear();
    //wait(1);
    lcd.setCursor(0,0);
    GetString();
    pc.printf("%s\r\n", line);
    lcd.printstr(line);
    int num = atoi(line);
    num++;
    sprintf(line, "%d", num);
    SetString();
    //lcd.printstr("Hello", LCD_LINE1);
    //lcd.printstr("Cruel", LCD_LINE2);
    //lcd.printstr("World", LCD_LINE3);
    //lcd.printstr("!!!!!!!!!!!!!", LCD_LINE4);
    //while(1)
    {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
    
}
posted by Tim Borland 12 Oct 2013

Just ran your code without the remaining lcd stuff: Worked fine for me. Are you sure you are checking your mbed drive correctly.

posted by Erik - 12 Oct 2013

Well, it's really strange. I had to unplug the mbed USB cable and re plug it before Windows would recognize the file. Now the file is there but in Windows I don't see the contents changing after every run of the program! There must be some strange buffering that is going on with Windows and the mbed drive.

posted by Tim Borland 12 Oct 2013

Tim, What firmware do you have? I posted this observation a few days back. Firmware 141212 does not release local file system on last fclose()

If you open the mbed.htm file in an editor, you can find the firmware version.

posted by David Smart 12 Oct 2013

Just upgraded to 141212! That explains it!

Is there any way to make it release the file system?

posted by Tim Borland 12 Oct 2013