登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

樱之花

叶散的时候,你明白欢聚;花谢的时候,你明白青春.

 
 
 

日志

 
 
关于我

分类中“我的实验室”是我在日常工作中的一些知识总结,有些写的比较匆忙,可能大家在阅读时会产生困扰,后期有时间我会重新整理编辑,谢谢大家的到访,您们的支持是我前进的动力!

从串口读取GPS数据  

2007-09-12 17:49:14|  分类: Windows Mobile |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

网上找来的代码自己修改之后放到这里的。

参考地址:

http://www.pcppc.cn/kaifa/VBjiaocheng/kaifa_18010.html
http://www.itqoo.com/programme/ASPNET/200609/1489.html
http://topic.csdn.net/t/20060812/21/4945572.html
http://bbs.pdafans.com/viewthread.php?tid=154187

Imports System.Runtime.InteropServices
Public Class Form1
    Private StrInfo As String = ""
    Private StrX As String = ""
    Private StrY As String = ""
    Public Class dcb
        Friend DCBlength As UInt32
        Friend BaudRate As UInt32
        Friend fBinary As UInt32
        Friend fParity As UInt32
        Friend fOutxCtsFlow As UInt32
        Friend fOutxDsrFlow As UInt32
        Friend fDtrControl As UInt32
        Friend fDsrSensitivity As UInt32
        Friend fTXContinueOnXoff As UInt32
        Friend fOutX As UInt32
        Friend fInX As UInt32
        Friend fErrorChar As UInt32
        Friend fNull As UInt32
        Friend fRtsControl As UInt32
        Friend fAbortOnError As UInt32
        Friend fDummy2 As UInt32
        Friend wReserved As UInt32
        Friend XonLim As UInt32
        Friend XoffLim As UInt32
        Friend ByteSize As Byte
        Friend Parity As Byte
        Friend StopBits As Byte
        Friend XonChar As Char
        Friend XoffChar As Char
        Friend ErrorChar As Char
        Friend EofChar As Char
        Friend EvtChar As Char
        Friend wReserved1 As UInt16
    End Class

    <DllImport("coredll.dll")> _
    Private Shared Function CreateFile _
    (ByVal lpFileName As String, _
     ByVal dwDesiredAccess As Integer, _
     ByVal dwShareMode As Integer, _
     ByVal lpSecurityAttributes As Integer, _
     ByVal dwCreationDisposition As Integer, _
     ByVal dwFlagAndAttributes As Integer, _
     ByVal hTemplateFile As Integer) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function GetCommState _
    (ByVal hFile As Integer, _
     ByVal mdcb As dcb) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function SetCommState _
    (ByVal hFile As Integer, _
     ByVal mdcb As dcb) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function ReadFile _
    (ByVal hFile As Integer, _
     ByVal Buffer() As Byte, _
     ByVal nNumberOfBytesToRead As Integer, _
     ByRef lpNumberOfBytesRead As Integer, _
     ByRef lpOverlapped As Integer) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function WriteFile _
    (ByVal hFile As Integer, _
     ByVal Buffer() As Byte, _
     ByVal nNumberOfBytesToWrite As Integer, _
     ByRef lpNumberOfBytesWritten As Integer, _
     ByVal lpOverlapped As Integer) As Boolean
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function CloseHandle _
    (ByVal hObject As Integer) As Integer
    End Function

    Dim inoutfileHandler As Long
    Dim numReadWrite As Integer
    Dim t1 As System.Threading.Thread
    Dim stopThread As Boolean = False

    Dim pdcb As New dcb

    Public Sub openPort()
        Dim ioPort As Short = 4
        inoutfileHandler = CreateFile("COM" & ioPort & ":", &HC0000000, 0, 0, 3, 0, 0)
        '设置波特率
        GetCommState(inoutfileHandler, pdcb)
        pdcb.BaudRate.Parse("4800")
        SetCommState(inoutfileHandler, pdcb)
        stopThread = False
        t1 = New Threading.Thread(AddressOf receiveLoop)
        t1.Start()
    End Sub

    Public Sub receiveLoop()
        Dim inbuff(300) As Byte
        Dim retCode As Integer = ReadFile _
        (inoutfileHandler, _
         inbuff, _
         inbuff.Length, _
         numReadWrite, _
         0)
        Application.DoEvents()
        While True
            If retCode = 0 Or stopThread Then
                Exit While
            Else
                Dim updateDelegate As New myDelegate(AddressOf displayReceivedMessage)

                updateDelegate.Invoke _
                (byteArrayToString(inbuff))
                ReDim inbuff(300)
                retCode = ReadFile(inoutfileHandler, inbuff, inbuff.Length, numReadWrite, 0)
                Application.DoEvents()
            End If
        End While
    End Sub

    Function byteArrayToString(ByVal b() As Byte) As String
        Dim str As String
        Dim enc As System.Text.ASCIIEncoding
        enc = New System.Text.ASCIIEncoding
        str = enc.GetString(b, 0, b.Length())
        Return str
    End Function

    Public Delegate Sub myDelegate(ByVal str As String)

    Public Sub displayReceivedMessage(ByVal str As String)
        If str.Length > 0 Then
            'MessageBox.Show(str)
            'MessageBox.Show(GetGPS(str, "X") & " " & GetGPS(str, "Y"))
            StrInfo = StrInfo + str
            StrX = GetGPS(str, "X")
            StrY = GetGPS(str, "Y")
            If StrX <> "" And StrX <> "-1" And StrX <> "V" Then
                MessageBox.Show("成功获得数据 经度" & StrX & " 纬度" & StrY)
                stopThread = True
                CloseHandle(inoutfileHandler)
            End If
        End If
    End Sub

    Private Sub MnuOpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuOpenPort.Click
        Call openPort()
    End Sub

    Private Sub MnuShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuShow.Click
        TxtInfo.Text = StrInfo
        TxtGPS.Text = StrX & " " & StrY
    End Sub

    Private Sub MnuClosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuClosePort.Click
        stopThread = True
        CloseHandle(inoutfileHandler)
    End Sub

    Private Function GetGPS(ByVal sGpsStr As String, ByVal sFindStr As String)
        Dim HanderStr As String = "$GPRMC" 'GPS串头
        Dim FindHander As Integer = sGpsStr.IndexOf(HanderStr)
        If FindHander < 0 Then
            Return "-1"
        Else
            sGpsStr = sGpsStr.Substring(FindHander, sGpsStr.Length - FindHander)
            Dim ArryTmp() As String = sGpsStr.Split(",")
            Try
                If ArryTmp(2) = "V" Then
                    Return "V" '信号不好
                Else
                    Select Case sFindStr
                        Case "X"    '返回经度
                            Return DM2DD(ArryTmp(5))
                        Case "Y"    '返回纬度
                            Return DM2DD(ArryTmp(3))
                        Case "T"
                            Return ArryTmp(9) & " " & ArryTmp(1)
                        Case "V"
                            Return Convert.ToString(Convert.ToDouble(ArryTmp(7)) * 1.852)
                    End Select
                End If
            Catch ex As Exception
                Return "V"
            End Try
        End If
    End Function
    Public Function DM2DD(ByVal DegreeMinutes As String) As String
        '转换NMEA协议的“度分”格式为十进制“度度”格式
        Dim sDegree As String
        Dim sMinute As String
        Dim sReturn As String = ""
        If DegreeMinutes.IndexOf(".") = 4 Then
            DegreeMinutes = DegreeMinutes.Replace(".", "")
            Dim sDegree1 As Double = Convert.ToDouble(DegreeMinutes.Substring(0, 2))
            Dim sDegree2 As Double = Convert.ToDouble(DegreeMinutes.Substring(2, DegreeMinutes.Length - 2))
            Dim sTmp As String = Convert.ToString(sDegree2 / 60)
            sDegree2 = Convert.ToDouble(sTmp.Substring(0, sTmp.Length))
            sDegree2 = sDegree2 / 10000
            sDegree = Convert.ToString(sDegree1 + sDegree2)
            If (sDegree.Length > 11) Then
                sDegree = sDegree.Substring(0, 11)
            End If
            sReturn = sDegree
        ElseIf DegreeMinutes.IndexOf(".") = 5 Then
            DegreeMinutes = DegreeMinutes.Replace(".", "")
            Dim sMinute1 As Double = Convert.ToDouble(DegreeMinutes.Substring(0, 3))
            Dim sMinute2 As Double = Convert.ToDouble(DegreeMinutes.Substring(3, DegreeMinutes.Length - 2))
            Dim sTmp As String = Convert.ToString(sMinute2 / 60)
            sMinute2 = Convert.ToDouble(sTmp.Substring(0, sTmp.Length))
            sMinute2 = sMinute2 / 10000
            sMinute = Convert.ToString(sMinute1 + sMinute2)
            If sMinute.Length > 10 Then
                sMinute = sMinute.Substring(0, 10)
                sReturn = sMinute
            End If
        End If
        Return sReturn
    End Function
End Class

  评论这张
 
阅读(1049)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018