Main Site Documentation

EMX SD Write issue


#1

Just blew the dust off a product from 2010.

Using the EMX10-SM-128, upgraded to .Net Microframework V4.2. from 4.0.

Device is a data logger that is woken up by a micro controller periodically and then when the data logger is finished it instructs the micro to put it back to sleep. Micro controls power and reset line.

The formerly working code runs on the EMX, I can download and debug it, step through etc. For some reason however, it no longer writes the data to a file on the SD card consistently. It seems to work sometimes when I am stepping through but not when running full speed. Using streamwriter class.

Persistent storage mounts, doesn’t appear to be any issue along the way.

I am using streamwriter class. I am flushing and closing. I can post code but It isn’t available to me right now.

I have searched for and read posts I thought were related to this issue and have tried long delays after mounting the storage (3+ seconds) and three different SD cards. SD card is formatted FAT16.

Please help!


#2

Try FAT32 formatting.


#3

Tried FAT32 last night. No change in result.

Wrote the following test program:
public static void Main()
{
Debug.EnableGCMessages(true);

        PersistentStorage ps = new PersistentStorage("SD");

        ps.MountFileSystem();

        int x = 0;

        if (File.Exists(@ "\SD\data.txt"))
        {
            File.Delete(@ "\SD\data.txt");
        }

        do
        {
            
            string filename = @ "\SD\data.txt";

// using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
// {
// byte [] xbytes = Encoding.UTF8.GetBytes(x.ToString() + ‘\n’ );
//
// SourceStream.Seek(0, SeekOrigin.End);
// SourceStream.Write(xbytes, 0, xbytes.Length);
//
// }

            using (StreamWriter streamWriter = new StreamWriter((filename), true))
            {
                streamWriter.WriteLine(x.ToString());

// streamWriter.Flush();
// streamWriter.Close();
}

            // works with these lines
            ps.UnmountFileSystem();
            ps.MountFileSystem();
            // doesn't work without

            using (StreamReader streamReader = new StreamReader(filename))
            {
                string test = streamReader.ReadToEnd();
                
                Debug.Print(test);
            }

            x++;
            
            Thread.Sleep(100);

        } while (x < 20);
    }

The Filestream code doesn’t work at all, doesn’t even create the file. Without the mount/unmount lines the read of the file with stream reader looks like it is working in the debug window but when the program exits and I put the SD card in a pc the file has not been written.

The unmount and mount seems to properly force the write. The flush and close doesn’t appear to do anything.


#4

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

#5

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

#6

N00b mistake.

        public static void Main()
        {
            Debug.EnableGCMessages(true);

            PersistentStorage ps = new PersistentStorage("SD");

            ps.MountFileSystem();

            int x = 0;

            if (File.Exists(@ "\SD\data.txt"))
            {
                File.Delete(@ "\SD\data.txt");
            }

            do
            {
                
                string filename = @ "\SD\data.txt";

//                using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
//                {
//                    byte [] xbytes = Encoding.UTF8.GetBytes(x.ToString() + '\n' );
//
//                    SourceStream.Seek(0, SeekOrigin.End);
//                    SourceStream.Write(xbytes, 0, xbytes.Length);
//
//                }


                using (StreamWriter streamWriter = new StreamWriter((filename), true))
                {
                    streamWriter.WriteLine(x.ToString());
//                    streamWriter.Flush();
//                    streamWriter.Close();
                }


                // works with these lines
                ps.UnmountFileSystem();
                ps.MountFileSystem();
                // doesn't work without

                using (StreamReader streamReader = new StreamReader(filename))
                {
                    string test = streamReader.ReadToEnd();
                    
                    Debug.Print(test);
                }

                x++;
                
                Thread.Sleep(100);

            } while (x < 20);
        }

The Filestream code doesn’t work at all, doesn’t even create the file. Without the mount/unmount lines the read of the file with stream reader looks like it is working in the debug window but when the program exits and I put the SD card in a pc the file has not been written.

The unmount and mount seems to properly force the write. The flush and close doesn’t appear to do anything.


#7

I would try to wait a little bit after mounting the file system (thread.sleep). There is also a bunch of example in this tutorial (in case you haven’t seen it yet)

http://www.ghielectronics.com/docs/51/file-system


#8

I am not a big fan of using delays for such things. Is there some other more positive way to know when the card is ready to be written? I don’t want to waste any power.

I have also tried using a thread sleep for 10 seconds after the mount just to see if it would work. And you can see that I have tried FileStream.

I don’t see anything in the tutorials that are different than what I have attempted.


#9

@ Shawn - http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation%20v4.2/Premium/html/cebcb028-63fe-e138-69c9-cbf36b6d1a21.htm


#10

Those link above are good enough,
Just more clear,

  • Try to add Thread.Sleep(1000) after called ps.MountFileSystem(); Some SD need, some don’t.
  • Try to call Flush/Close after you finished a Stream/FileStream.
  • If you want to put Mounted/Unmounted in a loop, you should do these steps after Unmounted.

ps.Unmount.....
ps.Dispose()
ps = null;

Of course, alway try to create new


ps = new PersistentStorage("SD");

for every loop after you unmounted.


#11

Hello, I do not know if this will help you:

I have been having problems with microSD and either Cerb40 or Cerbuino.

Not all microSD worked the same: some work fine; others could be mounted, and could read files and directories, but could not write nor read; and some of them just block the entire process when trying to mount them.

Today I have reformated one of the microSD wich could be mounted, but the Cerb40 was unabled to read or write. In the format process (FAT32) , I have used a low ‘asignation size’: 2048. And now, this microSD is working fine (writing/reading once a second for serveral hours).

Hope this helps


#12

@ epenciso -

At least, this is a good one for us. Thanks


#13

Dat,

I have just formated another one of the old microSDs which did not work at all. Now it works fine also. :slight_smile:


#14

Thanks Mike the legend. Found the relevant note at that link:

Also, if you create a directory, delete files…etc, you should call yourPersistentStorage.UnmountFileSystem() or yourVolumeInfo.FlushAll().

This test code now works fine, no delays and I have incorporated it into the full project with no problems so far.


        public static void Main()
        {
            Debug.EnableGCMessages(true);

            PersistentStorage ps = new PersistentStorage("SD");

            ps.MountFileSystem();
            VolumeInfo vi = VolumeInfo.GetVolumes()[0];
                 
            int x = 0;

            if (File.Exists(@ "\SD\data.txt"))
            {
                File.Delete(@ "\SD\data.txt");
            }

            do
            {
                
                string filename = @ "\SD\data.txt";

                using (StreamWriter streamWriter = new StreamWriter((filename), true))
                {
                    streamWriter.WriteLine(x.ToString());
                }

                using (StreamReader streamReader = new StreamReader(filename))
                {
                    string test = streamReader.ReadToEnd();
                    
                    Debug.Print(test);
                }

                x++;
                
                Thread.Sleep(100);

            } while (x < 20);

            vi.FlushAll();
            ps.UnmountFileSystem();
        }

[quote][/quote]


#15

Hi,

I can I inquire as to the need to call UnmountFileSystem or FlushAll after creating a directory or deleting files?

My application is constantly writing files and periodically deleting files, so on startup I mount the file system and never unmount it unless errors get thrown.

Now one thing I have noticed is that maybe after a month or so my application starts to throw errors relating to writing to the sd card and no amount of unmounting appears to fix the issue - though once a restart is done of the application everything is fine again and it writes as normal (so it’s not like the SD card has been corrupted).


#16

Chris,
I need to do the un-mount because I am constantly cycling power. Sounds like you have exactly the opposite scenario, your device is on for at least a month at a time?

If it takes a month for this to occur you are going to have fun with your debug cycle.

If I were you (after this experience) I would flush an un-mount periodically, re-instantiate the persistent storage and re-mount. Maybe once a day?

Cheers


#17

Hi Shawn,

Thanks for the suggestion - that is def something I will build into the application and see if it makes a difference. Though like you say, as it seems to run fine for over a month may take a bit of time to know if it makes any difference.

Cheers.