DataLogger Demo SD Card Fix
Answer
8/11/17 7:19 PM

Hello Community,

The Datalogger Demo has an issue with the SD Card.  If there are any folders on the SD Card it will not run, as it gets stuck in an infinite loop due to the scan_files function. 

The first fix to try is to reformat the SD Card and to remove all files from it from the SD Card other than the logger.ini file.  It is recommended not to do a quick format on the SD Card either.

This still might not fix the issue as Windows Machines will automatically put on a System Volume Information folder as soon as you try to access the SD Card (so when you drop the logger.ini file from the file explorer, for example, this often gets added).  So the next fix is to open up the command prompt and remove this hidden folder and add the logger.ini file through the command prompt.  This should fix the issue.

The real root of the issue is with the scan_files function.  It tries to look at all the files on the SD Card so that it can find any files on the SD Card of certain extensions with a number attached to it and then will update the log file name accordingly.  This deep dive into the sub-folders is unecessary when trying to determine the name for a file in the root folder (yes perhaps there is a desire in how you organize your files for this deep dive to be necessary, but as the code works now you can't do that, so I did not spend time on the real fix of this function).  I have, however, updated my scan_files function to ignore all folders and it works just fine, even with the the System Volume Information folder.

Here is the fix, look in your code for the first two lines within the scan_files function and edit the if statement accordingly:

fn = fno.fname;
if (fno.fattrib & AM_DIR) { /* It is a directory */
    continue;
}

Thank you Christopher Koch

0 (0 Votes)
RE: DataLogger Demo SD Card Fix
Answer
8/11/17 9:42 PM as a reply to BCDS Tester.
I decided I HAD to fix the scan_files function.  There will still be an issue with the numbering of the new log file, so perhaps that fix is to come, but for now this fix will allow the scan_files function to actually scan all the files and directories on the SD Card.
FRESULT scan_files(void) {
	FRESULT res;
	FILINFO fno;
	DIR dir;
	int i = 0;
	char *fn, *p;                                   /* This function assumes non-Unicode configuration */
	TCHAR newestfile[MAX_FILE_NAME_LENGTH] = { 0 }; /* variable to store the filename of the newest file on SD-Card*/
	static char path[MAX_PATH_LENGTH] = "";         /* variable to store rootpath to SD-Card*/
	long temp_filenumber = 0;
	long filenumber = 0;

	printf("Open Directory\r\n");
	res = f_opendir(&dir, path); /* Open the directory */
	if (res == FR_OK) {
		i = strlen(path);
		printf("Path %s\r\n", path);
		for (;;) {
			printf("Read Directory\r\n");
			res = f_readdir(&dir, &fno); /* Read a directory item */
			if (res != FR_OK || fno.fname[0] == 0) {
				printf("End Directory\r\n");
				break; /* Break on error or end of dir */
			}
			if (fno.fname[0] == '.') {
				printf("Ignore dot entry\r\n");
				continue; /* Ignore dot entry */
			}

			fn = fno.fname;
			if (fno.fattrib & AM_DIR) { /* It is a directory */
				printf("It is a Directory\r\n");
				sprintf(path + i, "/%s", fn);
				res = scan_files();
				printf("Scan Complete\r\n");
				path[i] = 0;
				if (res != FR_OK) {
					printf("Something went wrong\r\n");
					break;
				}
			} else { /* It is a file. */
				printf("It is a file\r\n");
				/**< check for newer timestamp and a valid fileformat*/
				if (((strstr(fno.fname, "CSV") != NULL)
						|| (strstr(fno.fname, "CST") != NULL)
						|| (strstr(fno.fname, "JSN") != NULL))) {
					printf("Check Extension\r\n");
					strcpy(newestfile, fn);
					p = newestfile;
					printf("Process File Name\r\n");
					while (*p) { // While there are more characters to process...

						if (isdigit((int) *p)) { // Upon finding a digit, ...
							temp_filenumber = strtol(p, &p, 10); // Read a number, ...
						} else { // Otherwise, move on to the next character.
							p++;
						}
					}
					printf("Process Done\r\n");
					if (temp_filenumber >= filenumber) {
						filenumber = temp_filenumber;
						buttoncount = filenumber + 1;
					}
				}
			}
		}
	}
	return res;
}

 

0 (0 Votes)
RE: DataLogger Demo SD Card Fix
Answer
8/14/17 2:02 PM as a reply to BCDS Tester.

Hello Chris,

thank you for proposing a fix to the Data Logger's issue with additional directories on the SD card. Unfortunately, you left out two constant definitions in the code. Could you please provide the values for  MAX_FILE_NAME_LENGTH and MAX_PATH_LENGTH that you used? I tested your code with both defined as 100 and the Data Logger demo is now working without getting stuck.

Kind regards,
Franjo

0 (0 Votes)
RE: DataLogger Demo SD Card Fix
Answer
8/14/17 3:18 PM as a reply to Franjo Stjepandic.

Sorry, I was getting rid of some "magic" numbers with these. 

#define MAX_FILE_NAME_LENGTH  13
#define MAX_PATH_LENGTH       256

The MAX_FILE_NAME_LENGTH is a direct replacement of the number that is in the old code.  This is limited by the fname variable in the FILINFO struct.  I extended the length of MAX_PATH_LENGTH from the original code, but it is totally arbitrary.  The user should extend this, if they know they have a path > 256 characters long.

Christopher Koch

0 (0 Votes)
RE: DataLogger Demo SD Card Fix
Answer
8/15/17 1:04 PM as a reply to BCDS Tester.

Hello Chris,

thank you for providing the values for MAX_FILE_NAME_LENGTH and MAX_PATH_LENGTH. I agree that they  depend on the developer's requirements, but it's useful to have a default value.

Kind regards,
Franjo

0 (0 Votes)