Data Logger

I am attempting to create a temperature data logger that will write to an excel file on a usb flash drive. I am needing some guidance as to where to even begin with writing to a .csv file on a usb flash drive using usb host module.

Thanks in Advance

Take a look at the System.IO namespace. You need to reference System.IO.dll… There are File.Write methods there you can use

After some searches on Google, I have found some code using System.IO similar to this…



void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice)
        {
            stream = storageDevice.Open("tempFile\\Data.csv", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            writer = new StreamWriter(stream);
            writer.Write("Time,Temp");
                       
        }


I am fairly new to c# programming and have only had a brief introduction to the System.IO class.

Any help in this area would be greatly appreciated

This should get you going… (code is not tested in real life…)

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.Presentation.Shapes;
using Microsoft.SPOT.Touch;

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

namespace GadgeteerApp3SdCardWriteTest
{
    public partial class Program
    {
        // thread that does the logging
        private Thread _loggerThread;

        // separator used to separate all the logged elements on one line
        private readonly char _separator = ';';

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            InitSensors();

            _loggerThread = new Thread(new ThreadStart(DoLogging));

            sdCard.SDCardMounted += sdCard_SDCardMounted;
            sdCard.SDCardUnmounted += sdCard_SDCardUnmounted;

            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }

        private void InitSensors()
        {
            // init your sensors
        }

        private void DoLogging()
        {
            // get sensor data ..
            var fakeSensorData = new object[] { 1.0, 5, true, false, "GOOD", "BAD", "OFFLINE" };
            var timestamp = DateTime.Now;

            // log to file
            WriteToFile(timestamp, fakeSensorData);
        }

        void WriteToFile(DateTime timestamp, params object[] sensorValues)
        {
            if (sdCard.IsCardInserted && sdCard.IsCardMounted)
            {
                using (var fileStream = new FileStream("data.csv", FileMode.OpenOrCreate))
                {
                    var line = new StringBuilder();

                    line.Append(timestamp.ToUniversalTime());

                    for (var i = 0; i != sensorValues.Length; i++)
                    {
                        // add separator
                        line.Append(_separator);

                        // and sensor value
                        line.Append(sensorValues[i].ToString());
                    }

                    // add line break 
                    line.Append('\n');

                    // get 
                    byte[] data = Encoding.UTF8.GetBytes(line.ToString());

                    // write the data and close the file
                    fileStream.Write(data, 0, data.Length);
                    fileStream.Close();
                }
            }
        }

        void sdCard_SDCardUnmounted(SDCard sender)
        {
            // stop logging
            _loggerThread.Start();
        }

        void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
        {
            // start logging 
            _loggerThread.Suspend();
        }

    }
}