获取平板电脑GPS信息的程序。。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SeriaCom
{

    public partial class Form1 : Form
    {
        string GPS_text = "";
        
        private SerialPort myserialPort = new SerialPort(); // 定义SerialPort串口类对象myserialPort
        public Form1()
        {

            InitializeComponent();
            try
            {
                tb1.Text = "";
                myserialPort.PortName = "COM3";               //定义为COM3
                myserialPort.BaudRate = 9600;                 //波特率为9600
                myserialPort.Open();
                myserialPort.DataReceived += new SerialDataReceivedEventHandler(myserialPort_DataReceived);
                // 当串口有数据收到时,启动myserialPort_DataRecieved事件函数
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
              
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }


        // 串口有数据收到
        private void myserialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
           
            GPS_text = GPS_text + myserialPort.ReadExisting();  // 读取串口数据
            MessageBox.Show("获取成功。。。"+GPS_text+"");
            tb1.Text = GPS_text;
            if (!String.IsNullOrEmpty(GPS_text))      // 如果GPS_text字符串最后一个字符是“回车”
            {
                //Invoke(new EventHandler(update_data));    // 通过Invoke方法执行update_data函数
                string[] GPS_info = GPS_text.Split('$'); // 按照逗号分隔把$GPRMC各种信号分割到字符串数组
                MessageBox.Show("value。。。1111" + GPS_info[0] + "22222" + GPS_info [1]+ "");
                this.labjd.Text = GPS_info[1].Split(',')[5].ToString(); //经度
                this.labwd.Text = GPS_info[1].Split(',')[3].ToString();//纬度
                GPS_text = "";
            }
            myserialPort.Close();
        }

        private void update_data(object sender, EventArgs e)
        {
            string[] GPS_info = GPS_text.Split(','); // 按照逗号分隔把$GPRMC各种信号分割到字符串数组

            GPS_text = "";                // 置空GPS_text以便存储新的串口接收到的字符串
            MessageBox.Show("进入后面了,可以赋值了。。");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 form = new Form1();
            form.Refresh();
            

        }
    }
}

然后做成安装项目 ,在平板电脑上 运行局可以了 ,注意:那个COM3口不能被平板上别的程序占用。

posted on 2012-10-19 08:14  小东北  阅读(646)  评论(0编辑  收藏  举报