Exemplo n.º 1
0
        public static void Main()
        {
            // some debug output for funsies
            OutputSDInfo();

            var volume = new VolumeInfo("SD");

            // check to see if there's an SD card inserted
            if (volume != null)
            {
                // "SD" is the volume name,
                var path = Path.Combine("SD", "test.txt");

                // write some text to a file
                File.WriteAllBytes(path, Encoding.UTF8.GetBytes("Foooooooo"));

                // Must call flush to write immediately. Otherwise, there's no guarantee
                // as to when the file is written.
                volume.FlushAll();
            }
            else
            {
                Debug.Print("There doesn't appear to be an SD card inserted");
            }
        }
        public static ArrayList Load()
        {
            var volume = new VolumeInfo("SD");

            if (volume != null)
            {
                var    path = Path.Combine("SD", "test.txt");
                string json = new string(Encoding.UTF8.GetChars(File.ReadAllBytes(path)));
                var    logs = Json.NETMF.JsonSerializer.DeserializeString(json);

                var humidityLogs = new ArrayList();
                foreach (Hashtable item in logs as ArrayList)
                {
                    humidityLogs.Add(new HumidityLog()
                    {
                        Date     = item["Date"].ToString(),
                        Humidity = int.Parse(item["Humidity"].ToString())
                    });
                }

                volume.FlushAll();

                return(humidityLogs);
            }
            else
            {
                Debug.Print("There doesn't appear to be an SD card inserted");
            }

            return(new ArrayList());
        }
Exemplo n.º 3
0
        public void LogException(Exception ex, DateTime timestamp, TimeSpan machineTime)
        {
            // Silly hresult isn't public.
            int hresult;

            try
            {
                hresult = (int)ex.GetType().GetField("m_HResult", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(ex);
            }
            catch (Exception)
            {
                hresult = 0;
            }

            using (var stream = new FileStream(Path.Combine(this.Volume.RootDirectory, ExceptionFileName), FileMode.Append, FileAccess.Write, FileShare.None))
                using (var writer = new StreamWriter(stream))
                {
                    writer.WriteLine("WallTime: " + timestamp.ToString("yyyy-MM-dd HH:mm:ss") + "Z");
                    writer.WriteLine("MachineTime: " + machineTime);
                    writer.WriteLine(ex.ToString());
                    writer.WriteLine("HResult: " + hresult.ToString());
                    writer.WriteLine(ex.StackTrace);

                    for (int i = 0; i < 15; i++)
                    {
                        writer.Write('-');
                    }
                    writer.WriteLine();
                }
            Volume.FlushAll();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Write to SD Card and Read from SD Card
        /// </summary>
        /// <param name="relayNo"></param>
        /// <param name="openHour"></param>
        /// <param name="openMinute"></param>
        /// <param name="closeHour"></param>
        /// <param name="closeMinute"></param>
        /// <param name="days">days --just like below monday = 2 tuesday = 3 </param>
        /// <param name="write">true for writing</param>
        /// <returns>readLine</returns>
        static void SDCardWrite(int relayNo, int openHour, int openMinute, int closeHour, int closeMinute, int days)
        {
            var volume = new VolumeInfo("SD");

            //Writing from default ones from Visual Studio
            //Normally you can override it from your SD card

            if (volume != null)
            {
                // "SD" is the volume name,
                var    path      = Path.Combine("SD", "Relay_" + relayNo + ".txt");
                string writingTo = relayNo + ".";
                writingTo += openHour + ".";
                writingTo += openMinute + ".";
                writingTo += closeHour + ".";
                writingTo += closeMinute + ".";
                writingTo += days + ".";

                // File.WriteAllBytes(path, Encoding.UTF8.GetBytes(writingTo));

                StreamWriter myFile = new StreamWriter(path);
                myFile.Write(writingTo);
                myFile.Close();

                volume.FlushAll();
            }
            else
            {
                Debug.Print("Problem");
            }
        }
        public static void Save(ArrayList humidityLogs)
        {
            var volume = new VolumeInfo("SD");

            if (volume != null)
            {
                var    path = Path.Combine("SD", "test.txt");
                string json = Json.NETMF.JsonSerializer.SerializeObject(humidityLogs);
                File.WriteAllBytes(path, Encoding.UTF8.GetBytes(json));
                volume.FlushAll();
            }
            else
            {
                Debug.Print("There doesn't appear to be an SD card inserted");
            }
        }
Exemplo n.º 6
0
        private static void WriteTextToFile(string path, string textValue)
        {
            //more information about FileStream https://docs.microsoft.com/en-us/previous-versions/windows/embedded/y0bs3w9t%28v%3dvs.102%29
            //More information for FileMode Enumeration https://docs.microsoft.com/en-us/previous-versions/windows/embedded/6b40c5ay(v%3dvs.102)

            using (var fileStream = new FileStream(
                       path,
                       FileMode.Append, //Opens the file if it exists and seeks to the end of the file, or creates a new file.
                       FileAccess.Write))
            {
                //Write some text to a file
                //Use StringUtility from NetMf.CommonExtensions to format string
                var byteData = Encoding.UTF8.GetBytes(textValue);
                fileStream.Write(byteData, 0, byteData.Length);

                //Must call flush to write immediately.
                //Otherwise, there's no guarantee as to when the file is written.
                volume.FlushAll();
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Write a string to the  SD card
        /// </summary>
        /// <returns>
        /// True if success
        /// </returns>
        /// <param name="filepath">The path on the SD card</param>
        /// <param name="content">The content to write</param>
        public static bool WriteString(string filepath, string content)
        {
            var fullath = Path.Combine(SD_ROOT, filepath);

            try
            {
                var volume = new VolumeInfo(SD_VOLUME);
                if (volume != null)
                {
                    File.WriteAllBytes(fullath, Encoding.UTF8.GetBytes(content));
                    volume.FlushAll();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 8
0
        public static void Main()
        {
            // some debug output for funsies
            OutputSDInfo();

            var volume = new VolumeInfo("SD");

            // check to see if there's an SD card inserted
            if (volume != null)
            {
                // "SD" is the volume name,
                var path = Path.Combine("SD", "test.txt");

                // write some text to a file
                File.WriteAllBytes(path, Encoding.UTF8.GetBytes("Foooooooo"));

                // Must call flush to write immediately. Otherwise, there's no guarantee
                // as to when the file is written.
                volume.FlushAll();
            }
            else
            {
                Debug.Print("There doesn't appear to be an SD card inserted");
            }

            // 6. Display sensor data on multiline LCD
            //lcd = new Lcd2004(N.Pins.GPIO_PIN_D5, N.Pins.GPIO_PIN_D7, N.Pins.GPIO_PIN_D8, N.Pins.GPIO_PIN_D9, N.Pins.GPIO_PIN_D10, N.Pins.GPIO_PIN_D11);
            //sensor = new BME280(updateInterval: 0);

            //string displayMessageLine1Prior = "";
            //string displayMessageLine1;
            //string displayMessageLine2Prior = "";
            //string displayMessageLine2;
            //string displayMessageLine3Prior = "";
            //string displayMessageLine3;
            ////string displayMessageLine4;
            //while (true)
            //{
            //    //  Make the sensor take new readings.
            //    sensor.Update();

            //    //  Prepare a message for the user and output to the LCD
            //    displayMessageLine1 = "Temperature: " + sensor.Temperature.ToString("F1") + " C";
            //    displayMessageLine2 = "Humidity: " + sensor.Humidity.ToString("F1") + " %";
            //    displayMessageLine3 = "Pressure: " + (sensor.Pressure / 100).ToString("F0") + " hPa";

            //    if (displayMessageLine1 != displayMessageLine1Prior
            //        || displayMessageLine2 != displayMessageLine2Prior
            //        || displayMessageLine3 != displayMessageLine3Prior)
            //    {
            //        // WriteLine does a ClearLine, so no need to clear.
            //        //lcd.Clear();
            //        lcd.WriteLine(displayMessageLine1, 0);
            //        lcd.WriteLine(displayMessageLine2, 1);
            //        lcd.WriteLine(displayMessageLine3, 2);
            //    }

            //    displayMessageLine1Prior = displayMessageLine1;
            //    displayMessageLine2Prior = displayMessageLine2;
            //    displayMessageLine3Prior = displayMessageLine3;

            //    Thread.Sleep(2000);
            //}

            //Debug.Print(DateTime.Now.ToString("yyyyMMdd-HH:mm:ss"));

            // 5: Display stuff on a multi-line display
            //ITextDisplay lcd = new Lcd2004(N.Pins.GPIO_PIN_D5, N.Pins.GPIO_PIN_D7, N.Pins.GPIO_PIN_D8, N.Pins.GPIO_PIN_D9, N.Pins.GPIO_PIN_D10, N.Pins.GPIO_PIN_D11);
            //lcd.WriteLine("This working?", 0);
            //Thread.Sleep(Timeout.Infinite);

            // 4: Get sensor data from BME280
            ////
            ////  Create a new BME280 object and put the sensor into polling
            ////  mode (update intervale set to 0ms).
            ////
            //BME280 sensor = new BME280(updateInterval: 0);

            //string message;
            //while (true)
            //{
            //    //
            //    //  Make the sensor take new readings.
            //    //
            //    sensor.Update();
            //    //
            //    //  Prepare a message for the user and output to the debug console.
            //    //
            //    message = "Temperature: " + sensor.Temperature.ToString("F1") + " C\n";
            //    message += "Humidity: " + sensor.Humidity.ToString("F1") + " %\n";
            //    message += "Pressure: " + (sensor.Pressure / 100).ToString("F0") + " hPa\n\n";
            //    Debug.Print(message);
            //    //
            //    //  Sleep for 1000ms before repeating the process.
            //    //
            //    Thread.Sleep(1000);
            //}

            // 3: Pulse an LED to make sure I can get Netduino.Foundation to work
            //// create a new LED on pin 11
            //var pwmLed = new Netduino.Foundation.LEDs.PwmLed(N.PWMChannels.PWM_PIN_D11,
            //    Netduino.Foundation.LEDs.TypicalForwardVoltage.White);

            //// pulse the LED
            //pwmLed.StartPulse();

            //// keep the app running
            //Thread.Sleep(Timeout.Infinite);

            // 2: Blink and LED to make sure I can deploy
            //// configure an output port for us to "write" to the LED
            //OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            //// note that if we didn't have the SecretLabs.NETMF.Hardware.Netduino DLL, we could also manually access it this way:
            ////OutputPort led = new OutputPort(Cpu.Pin.GPIO_Pin10, false);
            //int i = 0;
            //while (true)
            //{
            //    led.Write(true); // turn on the LED
            //    Thread.Sleep(250); // sleep for 250ms
            //    led.Write(false); // turn off the LED
            //    Thread.Sleep(250); // sleep for 250ms

            //    Debug.Print("Looping" + i);
            //    i++;
            //}

            // 1: Included with File>New
            //Debug.Print(Resources.GetString(Resources.StringResources.String1));
        }