7 years, 6 months ago.

Problem communication between MBED and USB key

Hello,

I have a problem with USB communication. I can read the content on the USB key and the content of a file only one time. The second time, the program stops at the message : Connect a Mass Storage device Thank you for your help. i am a beginner in the programmation with the MBED.

Below my program:

include "main.h"

#define FSNAME "msc"
DigitalIn interFA(p27);		   

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

Serial debug(USBTX, USBRX); 

InterruptIn ComutFA(p27); interruption

int valeur,i,j,k,Code,CodeImp,Loop,CodeCW, select,first,prime,adr_sw1,val_sw1,adr_sw2,val_sw2;
int data[50];

int val_int[150];

char ligne[150];
char cmd[2];

float val_float[20];
FILE* fp; 

void CONF_MOST() {
debug.printf("Configuration\r\n");
led1 = 0;
led2 = 1;

	DIR *d;
	struct dirent *p;
    
    printf("\n\r=================================================\n\r");
    printf("USB Mass storage demo program for mbed LPC1768\n\r");
    printf("=================================================\n\r");
   
	d = opendir("/msc");
    
    printf("\n\rList of files on the flash drive:\n\r");
    if ( d != NULL )
    {
        while ( (p = readdir(d)) != NULL )
        {
        	printf(" - %s\n\r", p->d_name);

        }
    }
    else
    {
    	error("Could not open directory!");
    }
led2 = 0;

led3 = 1;
	debug.printf("\r\n Open file MOST.txt\r\n");
	fp = fopen( "/msc/MOST.txt", "r");	

	   	if ( fp == NULL )
	   	{
       debug.printf("File MOST.txt not found in USB key !!! \r\n");
    	}	

		select = 0x6D;

		while (val_int[0] != select) {
			fgets(ligne, sizeof(ligne), fp);
			sscanf(ligne, "%x", &val_int[0]);
//			debug.printf("Donnees");
//			debug.printf(" = %x \r\n", val_int[0]);
		}

	sscanf(ligne,"%x %x %x %x %x %x %x %x %x %x %f %f %x %f %x %x %x %x %x %x %x %x %x %x %x %x %f",
			&val_int[0], &val_int[1], &val_int[2], &val_int[3], &val_int[4],
			&val_int[5], &val_int[6], &val_int[7], &val_int[8], &val_int[9],
			&val_float[0], &val_float[1], &val_int[10], &val_float[2],
			&val_int[11], &val_int[12], &val_int[13], &val_int[14],
			&val_int[15], &val_int[16], &val_int[17], &val_int[18],
			&val_int[19], &val_int[20], &val_int[21], &val_int[22],
			&val_float[3]);
	debug.printf("\n\rDonnees : %x %x %x %x %x %x %x %x %x %x %1.1f %2.2f %x %1.1f %x %x %x %x %x %x %x %x %x %x %x %x %2.3f\r\n\n",
			val_int[0], val_int[1], val_int[2], val_int[3], val_int[4],
			val_int[5], val_int[6], val_int[7], val_int[8], val_int[9],
			val_float[0], val_float[1], val_int[10], val_float[2], val_int[11],
			val_int[12], val_int[13], val_int[14], val_int[15], val_int[16],
			val_int[17], val_int[18], val_int[19], val_int[20], val_int[21],
			val_int[22], val_float[3]);
	fclose(fp);
led3 = 0;
}


/*--------------------------------------------------------*/
/*Beginning of the program */
/*--------------------------------------------------------*/

int main() {




	MSCFileSystem msc("msc");
	
 	CONF_MOST();


	ComutFA.fall(&CONF_MOST);


	debug.printf("Running \r\n");
	while (FOREVER) {
	       led1 = !led1;
		   wait(0.5);
	
	} // parenthese du While

}   // parenthese du Main

Please edit / repost using

<<code>>
your program
<</code>>

so that the code formatting is correctly preserved.

posted by Andy A 27 Oct 2016
Be the first to answer this question.