GPS Logger for FEZ Panda II - Logs all GPS data to SD card gps.txt file

If somebody are interested:

GPS Logger v1.0

Features:

  • Logging GPS data to SD card.
  • Starts when LDR button is pressed.
  • Stops when LDR button is pressed.

Issues:

  • Needs to be restarted to start logging again, is not good to use Thread.Abort() I know but idea
    is that when you click button to stop logging you will want to take card out and check the file
    for this you need to unmount card, so thread.suspend() is not helping here, anybody have better ideas?
  • Always writes in the same gps.txt file and deletes all previous records,
    was thinking to use time and data as name but for some reason my Panda’s clock
    always starts from 00:00 so every time I start program time resets and file stays
    with the same name…

If you have any ideas how to fix issues let me know and sorry for my english…

Code itself:


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

namespace SD
{
    public class Program
    {
        public static void Main()
        {

            // Create new Thread that runs the ExampleThreadFunction
            Thread ExampleThread = new Thread(new ThreadStart(ExampleThreadFunction));

            // SD stuff is in
            PersistentStorage sdPS = new PersistentStorage("SD");

            // Led stuff is in 
            OutputPort LED;
            LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, true);

            // Button stuff in
            InputPort Button;
            Button = new InputPort((Cpu.Pin)FEZ_Pin.Digital.LDR, false,
                                   Port.ResistorMode.PullUp);
            

            while (true)
            {
                //Led status at the beginning is off
                LED.Write(false);

                if (Button.Read())
                {

                    while (Button.Read()) ;   // wait while busy

                            //Led is on
                            LED.Write(true);

                            // Mount
                            sdPS.MountFileSystem();

                            // Start our new Thread
                            ExampleThread.Start();

                    while (Button.Read()) ;   // wait while busy

                            //Led is off
                            LED.Write(true);

                            // Abort our new Thread
                            ExampleThread.Abort();

                            // Unmount
                            sdPS.UnmountFileSystem();
                }

            }

            

        }


        public static void ExampleThreadFunction()
        {

            // bit rate change acording your GPS
            SerialPort serialPort = new SerialPort("COM3", 4800);
            serialPort.Open();

            //here we create file in SD card main folder
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @ "\gps.txt", FileMode.Create);


                        while (true)
            {
            
                        int bytesToRead = serialPort.BytesToRead;
                        if (bytesToRead > 0)
                        {

                            // all struff from GPS streams in to file
                            byte[] buffer = new byte[bytesToRead];
                            serialPort.Read(buffer, 0, buffer.Length);
                            Debug.Print(new String(System.Text.Encoding.UTF8.GetChars(buffer)) + "\n");
                            FileHandle.Write(buffer, 0, buffer.Length);
                            Thread.Sleep(500);
                        }
                        // Cleaning 
                        Debug.GC(true);
                        Debug.EnableGCMessages(false);

                        }

            }
        }

    }

Welcome to the community and thanks for sharing.

Please use code tags so the code is colored and formatted correctly. I will add them for you.

Thank you. I just started so did’n know how…

Welcome!

Is this for the GPS Logger from Adafruit? What FEZ board do you run it on? You’ll notice there is a sub-site for posting code like this. It tends to eventually get lost here, i.e. hard to search for.

No, I created myself the code mixing with some tutorial code.
This is FEZ Panda II board but this code should work for most of FEZ boards and also Netduino boards.

Just one question / tip.
Wouldnt it be better to have an interrupt driven event on the button instead of the while(true) loop and put the main thread to sleep?

A couple items.

Firstly, I really don’t like to see any code that has a built-in endless loop:

While (true)
{
// Do something useful
}

Consider implementing a way to control the thread to permit a graceful exit, as in:

private static bool myCondition = true;
.
.
//Thread entry point

While (myCondition)
{
// Do something useful
}

This way, you can set myCondition in another method in response to a necessary halt condition.
Threads can always be restarted. Secondly, try stepping back from your experimentation and learning
and take a little time to study the problem more. Coding and learning is fun, but you’ll end up
with a real tangled mess if you’re not careful.

In addition to having the COM port run in its own thread, I might suggest:

        * Create a class that handles reading & writing to the micro SD card so it is independent of                         what's going on in the main program loop. This way, the logger can handle multiple write requests asynchronously.

          - Create a public delegate in the class to handle receiving data from other threads
          - Create an event in the class so that it can raise the event and send responses to callers

       * Create another thread to manipulate the on-board LED.
           - Rapid blink LED when sdCard has successfully unmounted 
             (Hint: The LED thread method should also use events and delegates)
           - Slow, long blink when GPS logging is active
           - LED is OFF when program threads have all halted
           (All you'd need to do here is pass the LED thread an int representing which state it should flash.)
         
       * Similarly, create an thread/event when the button is pressed

Once you have these objects designed and running, you’ll find that getting them to talk to each other is
the key to making your application work flawlessly and will permit adding features with ease.