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

📄 serialport.cs

📁 c#编写的带控件GPS接收程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
            }
        }

        public void Read()
        {
            Thread readThread = new Thread(new ThreadStart(ReadThread));
            readThread.Start();
        }

        private void ReadThread()
        {
            Byte[] buffer = new Byte[256];
            OVERLAPPED ovlCommPort = new OVERLAPPED();
            int BytesRead = 0;
            while (hPortHandle != INVALID_HANDLE_VALUE)
            {
                BytesRead = 0;
                ReadFile(hPortHandle, buffer, buffer.Length, ref BytesRead, ref ovlCommPort);

                if (this.UpdateDataEventHandler != null)
                {
                    this.UpdateDataEventHandler(this, new NewDataEventArgs(Encoding.ASCII.GetString(buffer, 0, BytesRead)));
                }
            }
        }

        public void Write(ref Byte[] buffer)
        {
            if (hPortHandle != INVALID_HANDLE_VALUE)
            {
                OVERLAPPED ovlCommPort = new OVERLAPPED();
                int BytesWritten = 0;
                WriteFile(hPortHandle, buffer, buffer.Length, ref BytesWritten, ref ovlCommPort);
            }
            else
            {
                throw (new ApplicationException("串口未打开!"));
            }  
        }

        public void Close()
        {
            if (hPortHandle != INVALID_HANDLE_VALUE)
            {
                CloseHandle(hPortHandle);
            }
            opened = false;
        }

        public bool Opened
        {
            get
            {
                return opened;
            }
        }

        public string GetGPS(string strGPS, string strFind)
        {
            ///从GPS中读取的数据中,找出想要的数据
            ///GPSstring原始字符串,
            ///strFind要查找的内容,X:经度,Y:纬度,T:时间,V:速度,是数字从1开始,即以“,”分隔的位置
            ///返回查找到指定位置的字符串
            string handerStr = "$GPRMC";//GPS串头
            int findHander = strGPS.IndexOf(handerStr);//看是否含有GPS串头
            if (findHander < 0)
            {
                return "-1";
            }
            else
            {
                strGPS = strGPS.Substring(findHander, strGPS.Length - findHander);
                string[] ArryTmp = strGPS.Split(",".ToCharArray());
                try
                {
                    if (ArryTmp[2] == "V")
                    {
                        return "V";//没有信号
                    }
                    else
                    {
                        switch (strFind)
                        {
                            case "X":
                                return DM2DD(ArryTmp[5]);

                            case "Y":
                                return DM2DD(ArryTmp[3]);

                            case "T":
                                return T2Time(ArryTmp[9], ArryTmp[1]);

                            case "V":
                                return Convert.ToString(Convert.ToDouble(ArryTmp[7]) * 1.852);

                            default:
                                return "V";

                        }
                    }
                }
                catch
                {
                    return "V";
                }
            }
        }

        public openPortResult ScanPort(string selectedPort,int rate)
        {

            try
            {
                if (opened)
                {
                    Close();
                    Open(selectedPort, rate);
                }
                else
                {
                    Open(selectedPort, rate);//打开串口

                }

                if (opened)
                {
                    Thread.Sleep(3000);
                    byte[] gpsBuffer = new Byte[512];
                    Read(ref gpsBuffer);
                    Close();

                    if (Encoding.ASCII.GetString(gpsBuffer, 0, gpsBuffer.Length).IndexOf("$GP") >= 0)
                    {
                        return openPortResult.FindGps;
                    }
                    else
                    {
                        return openPortResult.None;
                    }
                }
                else
                {
                    return openPortResult.OpenFailed;
                }
            }
            catch
            {
                return openPortResult.OpenFailed;
            }

        }

        private string T2Time(string strDate, string strTime)
        {
            string dT = "20" + strDate.Substring(4, 2) + "-" + strDate.Substring(2, 2) + "-" + strDate.Substring(0, 2);
            string TT = strTime.Substring(0, 2) + ":" + strTime.Substring(2, 2) + ":" + strTime.Substring(4, 2);
            DateTime T = Convert.ToDateTime(dT + " " + TT);
            T = T.AddHours(8);
            return T.ToString();
        }

        private string DM2DD(string DegreeMinutes)
        {
            //转换NMEA协议的“度分”格式为十进制“度度”格式
            string sDegree;
            string sMinute;
            string sReturn = "";
            if (DegreeMinutes.IndexOf(".") == 4)
            {
                //DegreeMinutes = Replace(DegreeMinutes, ".", "")
                //DM2DD = CDbl(Left(DegreeMinutes, 2)) + CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) / 10000
                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 = Replace(DegreeMinutes, ".", "")
                //DM2DD = CDbl(Left(DegreeMinutes, 2)) + CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) / 10000
                DegreeMinutes = DegreeMinutes.Replace(".", "");
                double sMinute1 = Convert.ToDouble(DegreeMinutes.Substring(0, 3));
                double sMinute2 = Convert.ToDouble(DegreeMinutes.Substring(3, DegreeMinutes.Length - 2));
                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;
        }

    }
}

⌨️ 快捷键说明

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