Main Site Documentation

Problem with sd card


#1

Hello,

i wrote a small programm to messuare the temperature und the humid of the air with my panda II board,


using System;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.Threading;
using System.IO;
using Microsoft.SPOT.IO;


namespace MFConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            AnalogIn temp, feucht;
            double x, y;
            int n=0;

           

            temp = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An1);
            temp.SetLinearScale(0, 5000);

            feucht = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An3);
            feucht.SetLinearScale(0, 5000);

            //DateTime datum = new DateTime(2012, 07, 25, 11, 46, 11);
            //DateTime datum = DateTime.Now;
            //Utility.SetLocalTime(datum);

            if (PersistentStorage.DetectSDCard())
            {
                Debug.Print("Sd Karte erkannt");
            }
            else
            {
                Debug.Print("nicht erkannt");
            }

            PersistentStorage sd;
            sd = new PersistentStorage("SD");
            sd.MountFileSystem();
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream stream = new FileStream(rootDirectory + @ "\werte.txt", FileMode.Create);

            StreamWriter writer = new StreamWriter(stream);

            while (true)
            {
                n++;

                x = temp.Read();
                x = x / 14.601;
                string stringt;
                stringt = x.ToString();
                stringt = stringt.Substring(0, 5);
                

                y = feucht.Read();
                y = y / 61.79;

                string stringf;
                stringf  = y.ToString();
                stringf = stringf.Substring(0, 5);


                Debug.Print(DateTime.Now.ToString());

                Debug.Print(n.ToString());
                Debug.Print("Temperatur: " + stringt + " °C");
                Debug.Print("Rel. Luftfeuchte: "+stringf+" %");
                Debug.Print("________________________________________");



                writer.WriteLine("Temperatur: " + stringt + " °C");

                /*
                textFile.WriteLine("Temperatur: " + stringt + " °C");
                textFile.WriteLine("Rel. Luftfeuchte: " + stringf + " %");
                textFile.WriteLine("________________________________________");

                textFile.Close();

                */
                Thread.Sleep(2000);


            }

            
 
        }

    }
}

Sd card has the format FAT.
The problem is that no file is created on the sd card.
What is my fault?

if theres anything you dont understand, i will try to translate (german).

greetings
kevin


#2

What andre said. You need to flush your streamwriter…

See the flush and close methods here: http://msdn.microsoft.com/en-us/library/dd187978.aspx


#3

hi, thanks for the response,
i changed my code into

using System;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.Threading;
using System.IO;
using Microsoft.SPOT.IO;


namespace MFConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            AnalogIn temp, feucht;
            double x, y;
            int n=0;

           

            temp = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An1);
            temp.SetLinearScale(0, 5000);

            feucht = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An3);
            feucht.SetLinearScale(0, 5000);

            //DateTime datum = new DateTime(2012, 07, 25, 11, 46, 11);
            //DateTime datum = DateTime.Now;
            //Utility.SetLocalTime(datum);

            if (PersistentStorage.DetectSDCard())
            {
                Debug.Print("Sd Karte erkannt");
            }
            else
            {
                Debug.Print("nicht erkannt");
            }

            PersistentStorage sd;
            sd = new PersistentStorage("SD");
            sd.MountFileSystem();
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;

            Debug.Print(rootDirectory);

            

            while (PersistentStorage.DetectSDCard())
            {
                n++;

                FileStream stream = new FileStream(rootDirectory + @ "\werte.txt", FileMode.Create);
                StreamWriter writer = new StreamWriter(stream);


                x = temp.Read();
                x = x / 14.601;
                string stringt;
                stringt = x.ToString();
                stringt = stringt.Substring(0, 5);
                

                y = feucht.Read();
                y = y / 61.79;

                string stringf;
                stringf  = y.ToString();
                stringf = stringf.Substring(0, 5);


                Debug.Print(DateTime.Now.ToString());

                Debug.Print(n.ToString());
                Debug.Print("Temperatur: " + stringt + " °C");
                Debug.Print("Rel. Luftfeuchte: "+stringf+" %");
                Debug.Print("________________________________________");



                writer.WriteLine("Temperatur: " + stringt + " °C");

                /*
                textFile.WriteLine("Temperatur: " + stringt + " °C");
                textFile.WriteLine("Rel. Luftfeuchte: " + stringf + " %");
                textFile.WriteLine("________________________________________");

                textFile.Close();

                */
                writer.Flush();
                writer.Close();

                Thread.Sleep(2000);

            }
         
        }

    }
}

but still doesent work :-(.

i think i found the fault. i changed paarts in the programm and wondered why the output dosent changes.

[quote]Incrementally deploying assemblies to device
Deploying assemblies for a total size of 173920 bytes
Assemblies not successfully deployed to device.
Deployment to the device was not successful.[/quote]
but i can ping the divice via MFDeploy


#4

You have only ~148KB available for your application on Panda II.

You need to clean-up your project and remove anything that you don’t need.

Please show a screenshot of your solution tree with all the nodes open. (It is a “Solution View” inside Visual Studio that shows all files/resource/references etc. that are included in your project)


#5

i removed the gadeteer assambly an it works, but not right :-(.
i made some changes to the code:

using System;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.Threading;
using System.IO;
using Microsoft.SPOT.IO;


namespace MFConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            AnalogIn temp, feucht;
            double x, y;
            int n=0;
            bool h;


            temp = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An1);
            temp.SetLinearScale(0, 5000);

            feucht = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An3);
            feucht.SetLinearScale(0, 5000);

            //DateTime datum = new DateTime(2012, 07, 25, 11, 46, 11);
            //DateTime datum = DateTime.Now;
            //Utility.SetLocalTime(datum);

            PersistentStorage sd;
            sd = new PersistentStorage("SD");
            sd.MountFileSystem();
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;

          
             Debug.Print(rootDirectory);
             h = PersistentStorage.DetectSDCard();
             if(h)
             {
                 Debug.Print("SD detected");
             }
             Thread.Sleep(2000);

            
            
            while (true)
            {
                n++;

                FileStream stream = new FileStream(rootDirectory + @ "\values.txt", FileMode.Create);
                StreamWriter writer = new StreamWriter(stream);


                x = temp.Read();
                x = x / 14.601;
                string stringt;
                stringt = x.ToString();
                stringt = stringt.Substring(0, 5);
                

                y = feucht.Read();
                y = y / 61.79;

                string stringf;
                stringf  = y.ToString();
                stringf = stringf.Substring(0, 5);


                Debug.Print(DateTime.Now.ToString());

                Debug.Print(n.ToString());
                Debug.Print("Temperatur: " + stringt + " °C");
                Debug.Print("Rel. Luftfeuchte: "+stringf+" %");
                Debug.Print("________________________________________");


                writer.WriteLine(n);
                writer.WriteLine("Temperatur: " + stringt + " °C");
                writer.WriteLine("Rel. Luftfeuchte: " + stringf + " %");
                writer.WriteLine("________________________________");
               
                writer.Flush();
                writer.Close();

                Thread.Sleep(2000);

            }

        }

    }
}

sd card is deteced and a file is created but the programm writes only 4 lines.
e.g.


#6

if you want to accumulate all lines of your readings in the file you need to use FileMode.Append.


#7

Avoid using file extensions is probably what you Have to do. I has the same issue and solved it using single filenames such as ipcfg instead of ipcfg.txt for exemple…


#8

Jeff answered the question for the 4 lines, file is overwritten every time


#9

filemode append works, thank you guys :slight_smile: