⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 蓝牙连接GPS
💻 CS
字号:
#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using OpenNETCF.Net.Bluetooth;
using System.IO;
using System.Text;

#endregion

namespace BluetoothDemo {
    /// <summary>
    /// 窗体的摘要描述。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form {
        private ListBox listBox1;
        private MenuItem menuItem_seek;
        private MenuItem menuItem_close;
        private MenuItem menuItem_connect;
        /// <summary>
        /// 窗体的主菜单。
        /// </summary>
        private System.Windows.Forms.MainMenu mainMenu1;

        BluetoothDeviceInfo[] devices;
        BluetoothClient client;
        BluetoothEndPoint EpGps;
        private MenuItem menuItem_ReadData;
        private Timer timer1;
        private Label label1;
        BluetoothAddress address;
        public Form1() {
            InitializeComponent();
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing) {
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent() {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.menuItem_seek = new System.Windows.Forms.MenuItem();
            this.menuItem_close = new System.Windows.Forms.MenuItem();
            this.menuItem_connect = new System.Windows.Forms.MenuItem();
            this.menuItem_ReadData = new System.Windows.Forms.MenuItem();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.timer1 = new System.Windows.Forms.Timer();
            this.label1 = new System.Windows.Forms.Label();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.menuItem_seek);
            this.mainMenu1.MenuItems.Add(this.menuItem_close);
            this.mainMenu1.MenuItems.Add(this.menuItem_connect);
            this.mainMenu1.MenuItems.Add(this.menuItem_ReadData);
            // 
            // menuItem_seek
            // 
            this.menuItem_seek.Text = "搜索";
            this.menuItem_seek.Click += new System.EventHandler(this.menuItem_seek_Click);
            // 
            // menuItem_close
            // 
            this.menuItem_close.Text = "关闭";
            this.menuItem_close.Click += new System.EventHandler(this.menuItem_close_Click);
            // 
            // menuItem_connect
            // 
            this.menuItem_connect.Text = "连接";
            this.menuItem_connect.Click += new System.EventHandler(this.menuItem_connect_Click);
            // 
            // menuItem_ReadData
            // 
            this.menuItem_ReadData.Text = "读取";
            this.menuItem_ReadData.Click += new System.EventHandler(this.menuItem_ReadData_Click);
            // 
            // listBox1
            // 
            this.listBox1.Location = new System.Drawing.Point(14, 26);
            this.listBox1.Size = new System.Drawing.Size(211, 142);
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
            // 
            // timer1
            // 
            this.timer1.Interval = 2000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(14, 171);
            this.label1.Size = new System.Drawing.Size(211, 46);
            this.label1.Text = "label1";
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.listBox1);
            this.Menu = this.mainMenu1;
            this.Text = "Form1";

        }

        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main() {
            Application.Run(new Form1());
        }

        private void menuItem_seek_Click(object sender, EventArgs e) {
            Cursor.Current = Cursors.WaitCursor;
            if (BluetoothClient.RadioMode == RadioMode.PowerOff) {
                BluetoothClient.RadioMode = RadioMode.Connectable;
            }
            client = new BluetoothClient();
            devices = client.DiscoverDevices();
            Cursor.Current = Cursors.Default;
            this.listBox1.Items.Clear();
            if (devices != null && devices.Length > 0) {
                for (int i = 0; i < devices.Length; i++) {
                    if (devices[i].DeviceName != null) {
                        this.listBox1.Items.Add(devices[i].DeviceName);
                    }
                }
            }
        }

        private void menuItem_close_Click(object sender, EventArgs e) {
            this.timer1.Enabled = false;
            client.Close();
            BluetoothClient.RadioMode = RadioMode.PowerOff;
        }

        private void menuItem_connect_Click(object sender, EventArgs e) {
            this.EpGps = new BluetoothEndPoint(this.address, BluetoothService.SerialPort);
            this.client.Connect(this.EpGps);
        }

        private void menuItem_ReadData_Click(object sender, EventArgs e) {
            this.timer1.Enabled = true;
            timer1_Tick(null, null);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
            if (this.listBox1.SelectedIndex >= 0) {
                int index = this.listBox1.SelectedIndex;
                this.address = new BluetoothAddress(devices[index].DeviceID);
            }
        }

        /// <summary>
        /// 转换NMEA协议的“度分”格式为十进制“度度”格式
        /// </summary>
        /// <param name="DegreeMinutes">3956.9808,N,11621.0352,E</param>
        /// <returns></returns>
        public string DM2DD(string DegreeMinutes) {
            string sDegree;
            string sMinute;
            string sReturn = "";
            if (DegreeMinutes.IndexOf(".") == 4) {
                DegreeMinutes = DegreeMinutes.Replace(".", "");
                double sDegree1 = Convert.ToDouble(DegreeMinutes.Substring(0, 2));
                double sDegree2 = Convert.ToDouble(DegreeMinutes.Substring(2, DegreeMinutes.Length - 2));
                string sTmp = Convert.ToString(sDegree2 / 60);
                sDegree2 = Convert.ToDouble(sTmp.Substring(0, sTmp.Length));
                sDegree2 = sDegree2 / 10000;
                sDegree = Convert.ToString(sDegree1 + sDegree2);
                if (sDegree.Length > 11)
                    sDegree = sDegree.Substring(0, 11);
                sReturn = sDegree;
            }
            else if (DegreeMinutes.IndexOf(".") == 5) {
                DegreeMinutes = DegreeMinutes.Replace(".", "");
                double sMinute1 = Convert.ToDouble(DegreeMinutes.Substring(0, 3));
                double sMinute2 = Convert.ToDouble(DegreeMinutes.Substring(3, DegreeMinutes.Length - 3));
                string sTmp = Convert.ToString(sMinute2 / 60);
                sMinute2 = Convert.ToDouble(sTmp.Substring(0, sTmp.Length));
                sMinute2 = sMinute2 / 10000;
                sMinute = Convert.ToString(sMinute1 + sMinute2);
                if (sMinute.Length > 10)
                    sMinute = sMinute.Substring(0, 10);
                sReturn = sMinute;
            }
            return sReturn;
        }

        private void timer1_Tick(object sender, EventArgs e) {
            string gpsx = "0";
            string gpsy = "0";
            byte[] buffer1 = new byte[0x200];
            Stream GpsStream = this.client.GetStream();
            int num1 = GpsStream.Read(buffer1, 0, 0x200);
            string gpsinfo = Encoding.ASCII.GetString(buffer1, 0, num1);
            if (gpsinfo != "") {
                string text1 = "$GPRMC";
                int index = gpsinfo.IndexOf(text1);
                if (index > -1) {
                    string text2 = gpsinfo.Substring(index);
                    if (text2.Length > 1) {
                        string text3 = text2.Substring(0, 0x2a);
                        string[] gpsdata = text3.Split(',');
                        if (gpsdata[2] == "A") {
                            gpsx = DM2DD(gpsdata[3]);
                            gpsy = DM2DD(gpsdata[5]);
                            this.label1.Text = "经度=" + gpsx.ToString() + "/纬度=" + gpsy.ToString();
                        }
                    }
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -