Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

SD Only Write First File But Not Second


  • Please log in to reply
4 replies to this topic

#1 Evotodi

Evotodi

    New Member

  • Members
  • Pip
  • 2 posts
  • LocationIndiana

Posted 25 September 2012 - 07:49 PM

Hello all,
I have run into a issue I cannot find a way around. My main program calls MakeSD, which checks for an SD card, removes all files on the SD card, creates a file called inVision.cfg, creates another file called config.cfg, this all works on firmware 4.2 RC1. However when I update to 4.2 QFE2 that is when things go bad... MakeSD is called, checks for SD card, removes all files, creates the file inVision.cfg correctly, creates the file config.cfg but does not write anything into it. If i swap the order of operations and create the config.cfg first, then inVision.cfg is created but contains no data.

The only way for it to work is flash 4.2 RC1 onto the Netduino.

Any help or insight would greatly be appreciated.

Justin


Some information:
  • Netduino Plus
  • 2 gig sandisk sd card
  • .NET Micro Framework SDK v4.2 (QFE2)
  • Netduino SDK v4.2: 64-bit
  • Netduino Flashed with 4.2 QFE2

Things I've tried:
  • Add/Remove sw.dispose and file.dispose
  • Make the FileStream and StreamWriter varilbles different between routines
  • Add a delay between routines
  • Flash TinyBooterDecompressor.bin for 4.2 QFE2
  • Different brand 2gig SD cards
  • Beat my head on the desk while uploading the program (didn't work!)


The Code:

        public bool MakeSD()
        {
            if (!VolumeExist())
            {
                Debug.Print("No SD Card");
                return false;
            }
            RemFiles();
            Debug.Print("SD Cleaned");
            if (MakeConfigFile())
            {
                Debug.Print("CFG Conplete");
                if (MakeMacFile())
                {
                    Debug.Print("MAC Complete");
                }
            }
            Debug.Print("SD Complete");
            Thread.Sleep(1000);
            return true;
        }

        private bool VolumeExist()
        {
            VolumeInfo[] volumes = VolumeInfo.GetVolumes();
            foreach (VolumeInfo volumeInfo in volumes)
            {
                if (volumeInfo.Name.Equals("SD"))
                    return true;
            }

            return false;
        }

        public bool RemFiles()
        {
            try
            {
                string[] filePaths = Directory.GetFiles(@"\SD\");
                foreach (string filePath in filePaths)
                {
                    Debug.Print(filePath.ToString());
                    try
                    {
                        File.Delete(filePath);
                    }
                    catch (Exception f)
                    {
                        Debug.Print("File Delete Error");
                        Debug.Print(f.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Print("RemFiles Error");
                Debug.Print(e.ToString());
            }
            return true;
        }

        public bool MakeMacFile()
        {
            string original_string = MAC();
            byte[] original_data = UTF8Encoding.UTF8.GetBytes(original_string);
            int[] new_data = new int[17];
            ushort  a = 0;
            for (int i = 0; i <= original_data.Length - 1 ; i++)
            {
                a = Convert.ToUInt16(original_data[i].ToString());
                new_data[i] = a * 16;
            }
            try
            {
                FileStream file = new FileStream(@"\SD\inVision.cfg", FileMode.Create);
                StreamWriter sw = new StreamWriter(file);
                foreach (int l in new_data)
                {
                    sw.WriteLine(l.ToString());
                }
                sw.WriteLine("EOF");
                sw.Flush();
                sw.Close();
                file.Close();
                file.Dispose();
                sw.Dispose();
                Debug.Print("MAC Saved !");
            }
            catch(Exception e)
            {
                Debug.Print("MakeMacFile");
                Debug.Print(e.ToString());
            }
            return true ;
        }

        public bool MakeConfigFile()
        {
            FileStream file = new FileStream(@"\SD\config.cfg", FileMode.Create);
            StreamWriter sw = new StreamWriter(file);
            string[] config = new string[17];
            config[0] = "192.168.1.100"; //IP
            config[1] = "255.255.255.0"; //Subnet
            config[2] = "192.168.1.1"; //Gateway
            config[3] = "True"; //DHCP enabled
            config[4] = "8.8.4.4"; //DNS Pri
            config[5] = "8.8.8.8"; //DNS Sec
            config[6] = "-39.7"; //tc offset (-41.7)
            config[7] = "0"; //rh offset
            config[8] = "0"; //wtf offset
            config[9] = "0"; //he offset
            config[10] = "0"; //hep offset
            config[11] = "0"; //wf offset
            config[12] = Server; //Server address
            config[13] = pingable.ToString(); //Server pingable
            config[14] = "True"; //FieldOn detect
            config[15] = "True"; //compressor detect
            config[16] = "EOF";
            foreach (string l in config)
            {
                Debug.Print(l);
                sw.WriteLine(l.ToString());
            }
            sw.Flush();
            sw.Close();
            sw.Dispose();
            file.Close();
            file.Dispose();
            Debug.Print("Config Saved !");
            return true;
        }



#2 Coyttl

Coyttl

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts
  • LocationSilver Spring, MD, USA

Posted 26 September 2012 - 07:13 PM

I don't think it will matter, but did you try using "using" statements for the FileStream and StreamWriter's?

#3 Evotodi

Evotodi

    New Member

  • Members
  • Pip
  • 2 posts
  • LocationIndiana

Posted 28 September 2012 - 08:08 PM

coyttl, Adding a using statement around the streamwriter did not help. Another quirk i have found is after flashing the 4.2 QFE2 tiny-boot-decompressor.bin with sam-ba the reset pin will not reset the netduino plus. Works fine with the 4.1 tiny-boot-decompressor.bin

#4 Coyttl

Coyttl

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts
  • LocationSilver Spring, MD, USA

Posted 29 September 2012 - 02:44 AM

Interesting - I think I just ran into the same issue -

On bootup, my code checks to see if the file exists. If it does not exist, it tries to create it:
FileStream fs = File.Create(logFile);
	fs.Close();

Then, I get into my reading of my Analog pin, and saving the values to my 'logFile':
//Write the file.
	using (FileStream fs = new FileStream(logFile, FileMode.Append, FileAccess.Write))
	{
		byte[] msg = UTF8Encoding.UTF8.GetBytes(logmsg);
		fs.Write(msg, 0, msg.Length);
	}
On the first bootup, the file is created and my code executes, reading the pin and saving to the file. No exception is thrown, but after powering off the N+ and reading the card, my log file is 0 bytes - there's nothing in it.

On the second bootup, the file's there so it's not created. In this case, my code then reads the pin and writes to the file - this time, after powering off the N+ and reading the file, it has data in it.

#5 Coyttl

Coyttl

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts
  • LocationSilver Spring, MD, USA

Posted 29 September 2012 - 02:51 AM

I think I got it. Or, at least, got a workaround.

On your first file, before closing it, try doing a
file.Flush();
in the MakeConfigFile function. Adding that to my code made my second write succeed.

Nevermind, my test was wrong. D'oh.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.