Files written on sdCard unreadable on PC

Hi guys,

I’m pretty new to this and got stuck with this simple thing that i can’t figure it out.

I want to log the temperature value for example in a text file. I can read and write the file on the .NET Gadgeteer program but when I open the file on my computer the information is not human understandable :slight_smile:

For example “Hello SD card” gets stored as “OSJ÷«`zÓ«zc.ƒ” …

I need to read the file on the computer because I’m using the data to plot a graph.

I’m using the Fez Spider mainboard.

This is the Program.cs file:

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using System.Text;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Modules.Seeed;
using System.IO;

namespace HomeSensorize
    public partial class Program
        void ProgramStarted()
            sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);

        void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
            string rootDirectory = sdCard.GetStorageDevice().RootDirectory;
            StreamWriter textFile = new StreamWriter(rootDirectory + @ "\hello.txt", true);
            textFile.WriteLine("Hello SD card");

            FileStream fileStream = new FileStream(rootDirectory + @ "\hello.txt", FileMode.Open);
            byte[] data = new byte[fileStream.Length];
            fileStream.Read(data, 0, data.Length);
            string text = new string(Encoding.UTF8.GetChars(data));


Also, where could I get some extra information on the alternative ways of writing data to the file system ?


I’m not sure why the file is not ‘readable’ in WinOS, but may be it’s something with Encoding.
I did it the oter way around (Created file in WinOS and read int in NETMF) which works grate (UTF8 encoded XML file).

The file API in System.IO sould be quite similar to the Win.NET API. So you could look there.
Also the NETMF API Docs can help (see download section on or online API Reference for .NET Micro Framework | Microsoft Learn)

If not solved by monday I can try your code on my G120.

btw. which Firmware do you use?

Thank you for your fast reply. I think I have version 4.1. I’m opening the file on MacOS but I’m sure that this is not the cause of the problem.

Yep you need to use the other Streamwriter constructor that takes the encoding enum parameter. Best bet will be to use ASCII encoding.

this is what i’m dealing with: (see the screenshot)

instead of that mumbo jumbo in hello.txt there should be a couple of lines with “Hello SD card” (I’m appending to it every time)

i’ll try updating to 4.2 and see if the problem goes away …

this is insane. i formated the sd card, same fat 32 as before, but now it just works …

don’t know why but this is something to remember :slight_smile:

thank you all for your help