[SD] Invalid filename

Everything about programming the platform and using the Waspmote API
Post Reply
ashrafelmiligy
Posts: 16
Joined: Mon Aug 13, 2018 5:04 pm
Company: TH Köln

[SD] Invalid filename

Post by ashrafelmiligy » Wed Jun 26, 2019 6:04 pm

Greetings,

My question would not be "How to fix this ?" but rather "Why when I applied this fix, it worked ?!" :D
I would like some explanation regarding the following issue since I am fairly a beginner in C++

I had a simple code which worked perfectly fine with the old IDE/API release (I do not not remember which version exactly) on Waspmote v1.5

The code scans through the SD card and lists the available files, then asks the user for the name of the file they desire to show and it shows it.
It acts a bit similarly to SD_13_SD_card_menu example code.

It was as the following:

Code: Select all

char filename[200];
int val;

void setup()
  {
    USB.flush();
    SD.ON();
    delay(100);
    SD.goRoot();
    delay(100);
    SD.ls( LS_R );  
    USB.println(F("---------------------------"));
    delay(1000);
    
  }
  


void loop()
  {
    USB.println();
    USB.println(F("Enter the name of the file you would like to read at the text entry area at the top ... "));
    USB.println();
    USB.flush();
    
    while(!USB.available())
      {
        //wait for available
      }
    strcpy( filename, "" );
    while (USB.available() > 0)
      {
        val = USB.read();
        USB.print(val,BYTE);
        snprintf(filename, sizeof(filename),"%s%c", filename,val);
      }
  
    USB.println();
    

    if(SD.isFile(filename)==1)
      {
        USB.println();
        USB.print(F("You have chosen to display the contents of "));
        USB.println(filename);
        USB.println();
        delay(1000);
  
        USB.println(F("Here are the chosen recorded data "));
        delay(1000);
        USB.println();
        SD.showFile(filename);

        USB.println();
        USB.println(F("------------------------- END OF DATA -------------------------"));
        USB.println();
        delay(1000);
      }
    else
      {
        USB.println();
        USB.println(F("You have typed an invalid file name, please recheck your spelling or copy the name exactly from the above list without any spaces ... "));
        USB.println();
      }

    
  }
Then I had some problems regarding some radio modules in a different post, so when I asked for Libelium's team help, they asked me to use the latest IDE/API, so I did and after that, this code stopped working properly.

It can only list the files on the SD card but when I enter the file I want to show, it gives "[SD] Invalid filename"

I tried running the "SD_13_SD_card_menu" example code and the same error showed up.
However, after some trial and error, I managed to make the following edits in the WaspSD.cpp file to make it work.

starting Line 3030 "Case : 2", I removed the below lines which clears the memory of filename and takes an input from the user

Code: Select all

						memset(filename, 0x00, sizeof(filename));
						USB.print(F("==> Enter name of file to read:"));

						// wait for incoming data from keyboard
						while (!USB.available() && (millis()-previous < timeout));
						while (USB.available() > 0)
						{
							filename[i] = USB.read();
							i++;
							if (i >= sizeof(filename))
							{
								break;
							}
						}
and replaced them with:

Code: Select all

						for( int i = 0; i < sizeof(filename);  ++i ) //clear old contents of fileName
						{
							filename[i] = (char)0;    
						}
						USB.print(F("==> Enter name of file to read:"));
						
						// wait for incoming data from keyboard
						while (!USB.available() && (millis()-previous < timeout));
						while (USB.available() > 0)
						{
							val = USB.read();
							USB.print(val,BYTE);
							snprintf(filename, sizeof(filename),"%s%c", filename,val);
						}
						filename[strlen(filename)-1] = '\0';
and added under line 2981

Code: Select all

	char filename[20];
	int val;
And this seems to take care of it and allows me to properly show the files requested.

My question would be why did this happen ? and why would my adjustments fix it ?

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: [SD] Invalid filename

Post by libelium-dev » Thu Jun 27, 2019 11:14 am

Hi,

We have checked your code without the modifications and it works selecting No line ending at the bottom part of the serial monitor. Maybe you had that options selected in your previous IDE but not in the current.

It seems that the original code reads the file name with the carriage return or line end characters and your modifications discard those characters.

Regards

ashrafelmiligy
Posts: 16
Joined: Mon Aug 13, 2018 5:04 pm
Company: TH Köln

Re: [SD] Invalid filename

Post by ashrafelmiligy » Thu Jun 27, 2019 3:35 pm

I see !!! This makes much more sense now :D

Thank you very much for the explanation.

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: [SD] Invalid filename

Post by libelium-dev » Fri Jun 28, 2019 8:44 am

You're welcome! :D

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest