Having lots of problems with SD Card reading

Hi all,

I am working on a project that requires an SD card because we have a lot of text files (that can’t be stored in the Cerberus’ memory.

The problem is that sometimes reading a file works and sometimes it doesn’t.

I can’t seem to find a pattern but sometimes the program starts to run fine. Other times I get “memory allocation failure” (with the number of blocks failed) and other times I get an IOException when FileStream.Read(data, offset, length) is called.

I have tried creating a test project which only mounts the SD card, prints out the root files and then reads one of them. This again sometimes works and sometimes doesn’t work.

I am using a powered USB hub and a Cerberus.

I have tried multiple SD cards and SD card modules as well as a couple of different Cerberus’ and microUSB cables.

Here is my (simple) code:

FileStream stream = storageDevice.OpenRead(fileName);

            byte[] data = new byte[stream.Length];

            stream.Read(data, 0, data.Length);

            string returnString = new string(System.Text.Encoding.UTF8.GetChars(data));


I have also tried using stream.ReadByte() thinking Read() might be the problem, but this made no difference.

Any ideas?


Memory allocation failed is possibly showing you the fault, although why that’s not happening in all instances is interesting. Memory is tight on these devices, so opening anything not under your control will require pretty careful attention.

My Cerb observations: I was using my cerb and the SD card module and I found that when I first powered up the device, the SD card couldn’t be read. If I powered up and held the device in reset for a few seconds, the SD card could be read. I used StevePresley’s Gadgeteer code sample https://www.ghielectronics.com/community/codeshare/entry/643 to check the behavior.

It can be that your problems are due to the fact that you read binary from the stream (I mean that the incoming data chunks are not split at the string borders) and than you transform the data to strings but these data are not strings (do not end with e.g. \r\n). You should use something like the class SerialBuffer.cs that I used in my Code Share FEZ/PC Bluetooth File Transfer Server/Client to convert the read data from the byteArray to strings.
I had no problems reading the fileStream from the sd-card on a Cerbuino Bee mainboard.
Regards Roland

Sorry, I was wrong: that the data are read from the stream in chunks is not right for filestreams. But in your case the complete file is read to one ByteArray. I assume, that the contents of your files consists of numerous strings. With your code you get the contents of the whole file in one long string. Is it that what you want?
Perhaps it works with a construct like this, using the class SerialBuffer I mentioned above.

byte[] b = new byte[2048];
FileStream iFile;
long lRemain;
iFile = new FileStream(sFile, FileMode.Open, FileAccess.Read);
lRemain = iFile.Length;
while (lRemain > 0)
         if (lRemain < 2048)
         b = new byte[lRemain];
         iFile.Read(b, 0, b.Length);
         mySerialBuffer.LoadSerial(b, 0, b.Length);
         lRemain -= b.Length;

Then you can read one string after another with

while ((string dataLine = mySerialBuffer.ReadLine()) != null)

             // ... do something with the string dataLine

Ah okay, thanks for the replies.

@ Roschmi - Yes the files are just strings and turning the byte[] into a String works fine. It’s the Read() part that is not working sometimes (or even OpenRead()) sometimes.

@ Brett - As a test I’m only opening one text file which is 65 bytes and so I shouldn’t be having the memory problems you mention.

I’ll try a few of the things suggested.

if the files are small as you say, it could be a power issue.
For me the cerbuino bee worked stabel with 1 GB Sandisk SD-Card. 4 GB Toshiba SD-Cards did not work stable. The older SD-Cards do not need very steep Signal edges…
One time a had a Problem due to a bad ribbon cable between power module and Mainboard.
Soldering a Tantal capacitor of 20 uF at the SD-Card housing between GND and 3.3 V enhanced stability.

Regards Roland