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 Logging: Is there a netduino version of this lib?


  • Please log in to reply
3 replies to this topic

#1 samjones

samjones

    Advanced Member

  • Members
  • PipPipPip
  • 105 posts

Posted 01 February 2012 - 05:06 AM

I am looking to do something like this: http://code.tinyclr....gram-logging-2/ On the netduino, is there such/similar? (It does not just compile...) Thanks!

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 01 February 2012 - 12:14 PM

Hi samjones,

I am looking to do something like this:
http://code.tinyclr....gram-logging-2/

On the netduino, is there such/similar? (It does not just compile...)

I'd recommend removing the vendor-specific commands and just using the built-in .NET System.IO classes.

On your Netduino Plus, the SD card is mounted at @"\SD\" automatically when inserted. For a classic Netduino, follow the mounting instructions provided in the firmware download thread for v4.1.1 or v4.2 beta.

Chris

#3 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 01 February 2012 - 10:21 PM

Try this. I replaced the mounting code with a new method to check if the SD volume exists. Did a few tests and seems to work great.

using System;
using System.IO;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;

namespace SDLogging
{
	public class SDLogger : IDisposable
	{
    	bool cardLoaded = true;
    	FileStream fileStream;

    	string path;
    	bool FileOpen = false;

    	public bool DebugMirror = true;

    	public SDLogger()
    	{
        	try
        	{
            	// try to access the microSD
            	if (VolumeExist() == false)
            	{
                	throw new Exception("microSD not found");
            	}

            	// get the path
            	string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            	int idx;
            	try
            	{
                	// get the next number for the log, to try to make 'em unique so not overwritten
                	idx = getIndex(rootDirectory);
                	if (DebugMirror) Debug.Print("Try : " + idx.ToString());
            	}
            	catch
            	{
                	// if can't get number, use 0;              	
                	idx = 0;
                	if (DebugMirror) Debug.Print("Catch : " + idx.ToString());
            	}
            	// open file
            	if (DebugMirror) Debug.Print("Open file : " + idx.ToString());
            	path = rootDirectory + @"\log " + idx + ".csv";
            	if (DebugMirror) Debug.Print(path.ToString());
            	//fileStream = new FileStream(rootDirectory + @"\log " + idx + ".csv", FileMode.Create, FileAccess.Write);
            	fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
            	FileOpen = true;
        	}
        	catch
        	{
            	// if failed, assume no card in slot
            	cardLoaded = false;
        	}
    	}

    	// get the next number from the index.txt file.
    	int getIndex(string rootDirectory)
    	{
        	// open the index file to get the next number
        	FileStream fileHandle = new FileStream(rootDirectory + @"\index.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        	byte[] data = new byte[100];

        	// read the data
        	int readCount = fileHandle.Read(data, 0, data.Length);
        	int idx;
        	if (readCount != 0)
        	{
            	// convert string data to int
            	var str = new string(Encoding.UTF8.GetChars(data), 0, readCount);
            	try
            	{
                	// increment last log #
                	idx = int.Parse(str) + 1;
            	}
            	catch
            	{
                	idx = 1;
            	}
        	}
        	else
            	idx = 1;

        	// close the file so that it can be overwritten
        	fileHandle.SetLength(0);

        	// store this number
        	byte[] data2 = Encoding.UTF8.GetBytes(idx.ToString());
        	fileHandle.Write(data2, 0, data2.Length);

        	// close handle
        	fileHandle.Close();

        	if (DebugMirror) Debug.Print("getIndex = " + idx.ToString());

        	return idx;
    	}

    	// If you want to do some customized logging, just create a new method similar to the ones below      	

    	// just write out a string
    	public void LogInfo(string Info)
    	{
        	// if card present, then write
        	if (cardLoaded)
        	{
            	Debug.Print("Going into LogInfo");
            	byte[] data = Encoding.UTF8.GetBytes(getTicks() + "," + "\"C\"" + ",\"" + Info + "\"\r\n");
            	// write the data and close the file
            	try
            	{
                	if (FileOpen) // if file is open then write to it
                	{ fileStream.Write(data, 0, data.Length); }
                	else // if file is not open, then open it to append and write to the end
                	{
                    	fileStream = new FileStream(path, FileMode.Append);
                    	fileStream.Write(data, 0, data.Length);
                	}
            	}
            	catch { }
            	finally { Close(); }

            	if (DebugMirror) Debug.Print(getTicks() + "," + "\"C\"" + ",\"" + Info);
        	}
    	}

    	// write out the data from the Sharp IRs
    	public void LogReading(double leftReading, double rightReading)
    	{
        	if (cardLoaded)
        	{
            	Debug.Print("Going into LogReading");
            	// write CSV data line
            	byte[] data = Encoding.UTF8.GetBytes(getTicks() + "," + "\"R\"" + "," + leftReading.ToString("F2") + "," + rightReading.ToString("F2") + "\r\n");
            	// write the data and close the file
            	try
            	{
                	if (FileOpen) // if file is open then write to it
                	{ fileStream.Write(data, 0, data.Length); }
                	else // if file is not open, then open it to append and write to the end
                	{
                    	fileStream = new FileStream(path, FileMode.Append);
                    	fileStream.Write(data, 0, data.Length);
                	}
            	}
            	catch { }
            	finally { Close(); }

            	if (DebugMirror) Debug.Print(getTicks() + "," + "\"R\"" + "," + leftReading.ToString("F2") + "," + rightReading.ToString("F2"));
        	}

    	}

    	// write out the data from the Sharp IRs and the corresponding motor speeds
    	public void LogReadingAndSpeed(double leftReading, int leftSpeed, double rightReading, int rightSpeed)
    	{
        	if (cardLoaded)
        	{
            	Debug.Print("Going into LogReadingAndSpeed");
            	// write CSV data line
            	byte[] data = Encoding.UTF8.GetBytes(getTicks() + "," + "\"S\"" + "," + leftReading.ToString("F2") + "," + leftSpeed + "," + rightReading.ToString("F2") + "," + rightSpeed + "\r\n");
            	// write the data and close the file
            	try
            	{
                	if (FileOpen) // if file is open then write to it
                	{ fileStream.Write(data, 0, data.Length); }
                	else // if file is not open, then open it to append and write to the end
                	{
                    	fileStream = new FileStream(path, FileMode.Append);
                    	fileStream.Write(data, 0, data.Length);
                	}
            	}
            	catch { }
            	finally { Close(); }

            	if (DebugMirror) Debug.Print(getTicks() + "," + "\"S\"" + "," + leftReading.ToString("F2") + "," + leftSpeed + "," + rightReading.ToString("F2") + "," + rightSpeed);
        	}
    	}

    	public void Close()
    	{
        	fileStream.Close();
        	fileStream = null;
        	FileOpen = false;
    	}

    	public void Dispose()
    	{
        	// likely never called, as the log will be open as long as system is running.
        	fileStream.Close();
        	fileStream = null;
    	}

    	long getTicks()
    	{
        	// change this if you want a different timestamp marker
        	return (DateTime.Now.Ticks / 1000) - 128752416000000;
    	}

    	// Check if the SD card is mounted
    	private static bool VolumeExist()
    	{
        	VolumeInfo[] volumes = VolumeInfo.GetVolumes();
        	foreach (VolumeInfo volumeInfo in volumes)
        	{
            	if (volumeInfo.Name.Equals("SD"))
                	return true;
        	}

        	return false;
    	}


	}
}


#4 samjones

samjones

    Advanced Member

  • Members
  • PipPipPip
  • 105 posts

Posted 01 February 2012 - 11:02 PM

@ Chris Walker: I tried, but failed... there was more to it than my abilities could figure out. @ dvanderwekke: Thank you! I will try this over the weekend, or as soon as I can!




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.