This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C129EXL troubles using data file - code is stuck at fopen()



Hello,

I recently purchase the TM4C129EXL board. In my application, I need to read a file to set up several variables.

I tried to use the function fopen() but it seems to stuck the code when it's called.

Is there a way to read a file with TM4C129EXL?

I want to use fopen(), fgets(), strtok() and strlen() so I include <stdio.h> and <string.h>.

Here is a part of my code (sorry for the french comments).

void lireFichier(void)
{
	UARTprintf("1\n");
	FILE * fichier = NULL;

	UARTprintf("2\n");
	fichier = fopen("tableauxCodeC.txt","r"); // UART print 2 but not 3 nor 4

	UARTprintf("3\n");

	if (fichier != NULL) //ouverture réussie
	{
		UARTprintf("4\n");
		chaine ligne = {0};
		int den = 0, nbLigne = 0;

		while ( fgets ( ligne, sizeof ligne, fichier ) != NULL ) //tant qu'on trouve une nouvelle ligne
		{
			nbLigne++; // on compte les lignes

			int i = 0, taille = 1;

			strtok(ligne, " "); // strtok "découpe" ligne au niveau des espaces entre les chaînes

			while(strtok(NULL, " ") != NULL) taille++; // on compte le nombre de chaines par ligne

			if(nbLigne < 21) // partie des transformées en Z du fichier
			{
				if( den == 0) //on a un numérateur
				{
					int idata = (int)((nbLigne-1)/2);
					data[idata].dimNum = taille;

					int compteur = 0; // compteur de chaines

					while( i <= (sizeof ligne) && ligne[i])
					{
						data[idata].num[compteur]=atof(ligne+i); // transforme la chaine en float et l'enregistre à l'adresse du pointeur
						i += strlen(ligne+i) + 1; //chaine suivante
						compteur += 1; //float suivant
					}
					den = 1;
				}
				else //on a un dénominateur
				{
					int idata = (int)((nbLigne-2)/2);
					data[idata].dimDen = taille;

					int compteur = 0; // compteur de chaines

					while( i <= (sizeof ligne) && ligne[i])
					{
						data[idata].den[compteur]=atof(ligne+i); // transforme la chaine en float et l'enregistre à l'adresse du pointeur
						i += strlen(ligne+i) + 1; //chaine suivante
						compteur += 1; //float suivant
					}
					den = 0;
				}
			}
			else //polynome
			{
				pFF.dim = taille;

				int compteur = 0; // compteur de chaines

				while( i <= (sizeof ligne) && ligne[i])
				{
					pFF.coeff[compteur] = atof(ligne+i); // transforme la chaine en float et l'enregistre à l'adresse du pointeur
					i += strlen(ligne+i) + 1; //chaine suivante
					compteur += 1; //float suivant
				}
			}
		}
		fclose(fichier);
	}
}

Here is the kind of structure I have in my file:

1.1     2
1     2     3.2
4     5     6.2
7     8     9.2e-5
1     2
1     2     3
4     5     6
7     8     9
1     2
1.001     2     3     10

It is always 10 lines of real numbers. There is 10 number per line max, I don't know how many number there is (it may vary).

These datas are generated with Matlab.

If there is no way to read a *.txt file, what do you suggest? Basicaly I need to set up some variables with datas that I generate with my PC and transmit it to the TM4C129EXL.

I am considering using UART, but this code works with a console application with code::blocks

If you have any questions please let me know.

Thank you.

  • I just read "Optimizing C Compiler User's Guide" section I/O. It seems that it can't work without a debugger. I have an embedded application so I'm going to use UART
  • Hello Fabien,

    You will need to use the FatFs for opening a file. From the information provided so far, I don't see if you are using FatFS.

    How are you inputting the files to you application?

    If you want to use a USB drive as the source for the files, then the example "usb_host_msc" can be used.

    There is an example called "sd_card" in the folder "examples/boards/dk-tm4c129x/" that can be used if you desire to use an "sd_card" as a way to provide the files to your application. But the DK-TM4C129X board will have to be used, as the launchpads don't have the hardware support for using an SD card.

    Thanks,
    Sai
  • Stellaris Sai said:
     You will need to use the FatFs for opening a file. From the information provided so far, I don't see if you are using FatFS.

    No I don't

    Stellaris Sai said:
    How are you inputting the files to you application?

    My code in Matlab generate the file and save it with the right name directly in the CCS project folder. It not on USB stick but on my HDD.

    However, I'm going to try that, thank you!!