using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace getgpsdata
{
public partial class Form1 : Form
{
private delegate void FlushClient();//
public Form1()
{
InitializeComponent();
}
Thread Online;
private void btbegin_Click(object sender, EventArgs e)
{
if (!spgps.IsOpen)
spgps.Open();
spgps.DiscardInBuffer();//
spgps.DiscardOutBuffer();//
Online = new Thread(CrossThreadFlush);
Online.IsBackground = true;
Online.Start();
}
private void CrossThreadFlush()
{
while (true)
{
ThreadFunction();
// sleep
//Thread.Sleep(7500); //30
//Thread.Sleep(3750); //15
Thread.Sleep(1250); //5
// spgps.DiscardInBuffer();
}
}
private void ThreadFunction()
{
if (this.txtdata.InvokeRequired)//
{
FlushClient fc = new FlushClient(ThreadFunction);
this.Invoke(fc);//
}
else
{
string record = spgps.ReadLine();
if (record.StartsWith("$GPRMC"))
{
txtdata.Text += record+"
";
//
txtdata.SelectionStart = txtdata.TextLength;
//
txtdata.ScrollToCaret();
spgps.DiscardInBuffer();
}
}
}
private void btstop_Click(object sender, EventArgs e)
{
//string gpsFile = "1 " + DateTime.Now.ToString().Replace(@"/", @"-").Replace(@"\", @"-").Replace(@":", @"-") + ".txt";
string gpsFile = "5 " + DateTime.Now.ToString().Replace(@"/", @"-").Replace(@"\", @"-").Replace(@":", @"-") + ".txt";
//string gpsFile = "15 " + DateTime.Now.ToString().Replace(@"/", @"-").Replace(@"\", @"-").Replace(@":", @"-") + ".txt";
//string gpsFile = "30 " + DateTime.Now.ToString().Replace(@"/", @"-").Replace(@"\", @"-").Replace(@":", @"-") + ".txt";
StreamWriter swFromFile = new StreamWriter("\\ResidentFlash\\" + gpsFile);
swFromFile.Write(txtdata.Text);
swFromFile.Flush();
swFromFile.Close();
this.Close();
try
{
Online.Abort(Online);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
if (spgps.IsOpen)
{
spgps.Close();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}