File read/write is always corrupted on USB

Hi! Just want to clarify something about Reading/Writing of bytes in the FEZ.

I’m not sure if it’s only a .NETMF or a Fez issue so I’ll post here also.

Problem:

When I read and write a File(.exe, .doc) in a USB flash drive connected to the Fez ( to duplicate the File), the copied File is corrupt(MD5 check fail). But the File is in correct File Size as the original.

I’ve tried the SAME code on .NET C# and .NETMF C#.

C# .NET

using System;
using System.Windows.Forms;
using System.IO;

class MainClass
{
  public static void Main() 
  {
    OpenFileDialog dlgOpen = new OpenFileDialog();
    dlgOpen.Title="Select file to back up";

    if (dlgOpen.ShowDialog() == DialogResult.OK)
    {
      FileStream inStream = File.OpenRead(dlgOpen.FileName);
      FileStream outStream =  File.OpenWrite(dlgOpen.FileName + ".bak");
      int b;

      while ((b = inStream.ReadByte()) > -1)
        outStream.WriteByte( (byte) b);

      outStream.Flush();
      outStream.Close();
      inStream.Close();
    }
  }
}

C# .NETMF

using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
using System.IO;
using System;
namespace MFConsoleApplication1
{
    class Program
    {
        static PersistentStorage ps;
        static void DeviceConnectedEvent(USBH_Device device)
        {
            if (device.TYPE == USBH_DeviceType.MassStorage)
            {
                Debug.Print("USB Mass Storage detected...");
                ps = new PersistentStorage(device);
                ps.MountFileSystem();

                string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
                 FileStream inStream = new FileStream(rootDirectory + @ "\sample.docx",FileMode.Open, FileAccess.Read);
                    

                        FileStream outStream = new FileStream(rootDirectory + @ "\5kbcopy.docx", FileMode.OpenOrCreate, FileAccess.Write);
                        int b;
                        while ((b = inStream.ReadByte()) > -1)
                            outStream.WriteByte((byte)b);
                        outStream.Flush();
                        outStream.Close();
                        inStream.Close();
                        Thread.Sleep(System.Threading.Timeout.Infinite);
                              
            }           
        }
        public static void Main()
        {
            USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
            Thread.Sleep(System.Threading.Timeout.Infinite);
        }
    }
}

Another code

string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;

                try
                {
                    using (FileStream fsSource = new FileStream(rootDirectory + @ "\longtext.txt",
                        FileMode.Open, FileAccess.Read))
                    {
                        // Read the source file into a byte array.            
                        byte[] bytes = new byte[fsSource.Length];
                        int numBytesToRead = (int)fsSource.Length;
                        int numBytesRead = 0;
                        while (numBytesToRead > 0)
                        {
                            // Read may return anything from 0 to numBytesToRead.
                            int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
                            // Break when the end of the file is reached.              
                            if (n == 0)
                                break;
                            numBytesRead += n;
                            numBytesToRead -= n;
                        }
                        numBytesToRead = bytes.Length;
                        // Write the byte array to the other FileStream.            
                        using (FileStream fsNew = new FileStream(rootDirectory + @ "\superlong.txt",
                            FileMode.Create, FileAccess.Write))
                        {
                            fsNew.Write(bytes, 0, numBytesToRead);
                        }
                    }
                }
                catch
                {
                }

I receive no errors when running it on the FEZ. Also, I thing I’ve noticed on the FEZ, when I ADD a code to display the READ BYTES on the debug console for the FEZ(.NETMF) and also on a textbox for the .NET, the displayed BYTES from the FEZ are different from the .NET.

Many people are using the file system, not sure why there is a problem on your end.
Is the problem in reading the file? Writing the file?..
Try something simple first, like reading a few bytes from a file. Then write them and look if you see any problems.

Also, check the power source maybe it is no sufficient. Try powering the board from a power pack instead of relying on USB power.

My problem seems to be on reading the file using the .NETMF. The .NETMF reads my file differently unlike the .NET.

I tried reading a file from my PC then sending it via serial connection to the Fez to write the bytes, and it had no problems except when sending large files which I’m still working on.

Can you try my codes and see if you could reproduce my problem? The codes there are the whole program with only the references to be added left.

Also, I’m already using a 7V power supply.

The Fez can only read the files it wrote/created…

Ex. Using the Fez .NETMF, I created a .txt file with some random strings. That .txt file can be copied/read/write perfectly again using the Fez.NETMF.

But when I create a new .txt file using the PC, and copied the file using the Fez .NETMF. The duplicate file is now corrupted.

Can somebody test the codes above if they’ll have the same result.

Could it be a problem with encoding character set ?

That’s possible because when I modify the above .NETMF code to:


                        int b;
                        while ((b = inStream.ReadByte()) > -1)
                        {
                            outStream.WriteByte((byte)b);
                            Debug.Print( b.toString() );
                        }

And the .NET code to:

      
      int b;
      while ((b = inStream.ReadByte()) > -1)
       {
        outStream.WriteByte( (byte) b);
         textbox1.Appendtext( b.toString() );
        }

To show how .NET and .NETMF read the same file. And the bytes read of .NETMF are different from .NET’s read bytes.

But how is reading bytes affected by character encoding set.

Before trying anything. Did you test this on a binary file? Raw data no encoding

We tried this code:

using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
using System.IO;
using System;
namespace MFConsoleApplication1
{
    class Program
    {
        static PersistentStorage ps;
        static void DeviceConnectedEvent(USBH_Device device)
        {
            if (device.TYPE == USBH_DeviceType.MassStorage)
            {
                Debug.Print("USB Mass Storage detected...");
                ps = new PersistentStorage(device);
                ps.MountFileSystem();
 
                string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
                 FileStream inStream = new FileStream(rootDirectory + @ "\sample.docx",FileMode.Open, FileAccess.Read);
 
 
                        FileStream outStream = new FileStream(rootDirectory + @ "\5kbcopy.docx", FileMode.OpenOrCreate, FileAccess.Write);
                        int b;
                        while ((b = inStream.ReadByte()) > -1)
                            outStream.WriteByte((byte)b);
                        outStream.Flush();
                        outStream.Close();
                        inStream.Close();
                        Thread.Sleep(System.Threading.Timeout.Infinite);
 
            }           
        }
        public static void Main()
        {
            USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
            Thread.Sleep(System.Threading.Timeout.Infinite);
        }
    }
}

It is working fine. Do this:
-Create a text file on PC with several strings, so you can open it and compare it later.
-Format your USB thumb driver and then put the file on it.
-run your program on FEZ and make sure it runs to then end after closing the streams.
-The files should be identical.

It’s now working. :stuck_out_tongue: Thank you very much Chimp and Supa.

I think this did the trick.

[quote]
Format your USB thumb driver and then put the file on it.[/quote]

Because I was using my college USB(w/ many important files), I did not reformat it because it was already FAT32.
But Fez didn’t read it correctly maybe because the USB drive was formatted with CUSTOM ALLOCATION UNIT SIZE.

Now what is left is to Optimize the writing/reading to reduce memory consumption. I am currently restricted to 20-30kb files due to bad memory management. :frowning: