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);
}
}
}
}