采集WINCE设备GPS数据

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+"\n";
                    // 设置光标位置到文本最后 
                    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());
            }
        }
    }
}
 


 

你可能感兴趣的:(thread,object,String,Class,WinCE)