#眉標=GPS開發範例 #副標=定位系統運作原理與開發實作 #大標=自製GPS衛星定位系統 #作者=文/陳秀玲 ============= 程式1 Dim objGGA As GGA '宣告GGA類型的物件變數 Dim objVTG As VTG '宣告VTG類型的物件變數 Dim objGSV As GSV '宣告GSV類型的物件變數 Private Sub Form_Load() MSComm1.CommPort = 1 '設定連接埠為COM1 MSComm1.Settings = "4800,n,8,1" '4800速率、無同位檢查、8資料位元、停止位元1 MSComm1.PortOpen = True '開啟連接埠COM1 MSComm1.RThreshold = 128 End Sub Private Sub Form_Unload(Cancel As Integer) MSComm1.PortOpen = False '關閉連接埠COM1 End Sub '時間事件程序,每半秒鐘執行一次 Private Sub Timer1_Timer() Dim strBuffer As String If MSComm1.InBufferCount >= 1 Then 'RS-232的輸入暫存區資料>1,則執行下一段 strBuffer = MSComm1.Input 'strBuffer等於輸入的資料 Call ParseSentence(strBuffer, objGGA, objVTG, objGSV) '呼叫顯示數據物件的副程式 '將項目文字指定給Text Text1.Text = (Left(objGGA.時間, 2) + 8) & ":" & Mid(objGGA.時間, 3, 2) Text2.Text = objGGA.使用中的衛星顆數 Text3.Text = Left(objGGA.經度, 2) & ":" & Mid(objGGA.經度, 3, 2) & "." & Mid(objGGA.經度, 6, 2) & ":" & Mid(objGGA.經度, 8, 2) & ":" & objGGA.strNSIndicator Text4.Text = Left(objGGA.緯度, 3) & ":" & Mid(objGGA.緯度, 4, 2) & "." & Mid(objGGA.緯度, 7, 2) & ":" & Mid(objGGA.緯度, 9, 2) & ":" & objGGA.strEWIndicator Text5.Text = objGGA.高度 & " " & objGGA.strAltUnits Text6.Text = objVTG.相對位移速度1 & " " & objVTG.strSpeedUnit1 Text7.Text = objVTG.相對位移速度2 & " " & objVTG.strSpeedUnit2 If objGSV.StrSeq = "1" Then Text8.Text = objGSV.衛星仰角 Text9.Text = objGSV.衛星方位角 Text10.Text = objGSV.天空中衛星總數 End If ' 顯示執行畫面的字串=時間 Form1.Caption = "KxLan GPS Module. UTC Time = " & (Left(objGGA.時間, 2) + 8) & ":" & Mid(objGGA.時間, 3, 2) End If End Sub ================ ============= 程式2 Public Type GGA 時間 As String 經度 As Variant strNSIndicator As String 緯度 As Variant strEWIndicator As String strPositionFix As String 使用中的衛星顆數 As String strIIDOP As String 高度 As String strAltUnits As String strGeoid As String strSepUnits As String strDgpsAge As String strDgpsid As String strchecksum As String strTerminator As String End Type Public Type VTG strCourse1 As String strReference1 As String strCourse2 As String strReference2 As String 相對位移速度1 As String 相對位移速度2 As String strSpeedUnit1 As String strSpeedUnit2 As String End Type Public Type GSV StrNoMessages As String StrSeq As String StrSatsinview As String 天空中衛星總數 As String 衛星仰角 As String 衛星方位角 As String StrSNR1 As String StrSatId2 As String StrElevation2 As String StrAzimuth2 As String StrSNR2 As String StrSatId3 As String StrElevation3 As String StrAzimuth3 As String StrSNR3 As String StrSatId4 As String StrElevation4 As String StrAzimuth4 As String StrSNR4 As String End Type Public Sub ParseSentence(InBuff As String, MyGGA As GGA, Myvtg As VTG, MyGSV As GSV) Dim aryData(25) Dim x, z, st, fn, intst As Integer Dim strSentence, strTemp, strDummy As String '抓訊號的開頭 ”$” For x = 1 To Len(InBuff) If Mid(InBuff, x, 1) = "$" Then st = x Exit For End If Next '抓訊號的結尾(vbcr換格字元). For x = st To Len(InBuff) If Mid(InBuff, x, 1) = vbCr Then fn = x Exit For End If Next ' Use st and fn to slice the string to a sentence. strSentence = Mid(InBuff, st, fn - st) ' 算訊號裡有幾個逗號,存到intComm For x = 1 To Len(strSentence) If Mid(strSentence, x, 1) = "," Then intComma = intComma + 1 End If Next ' 把每個逗號之間的資料存到所對應的矩陣, 如$GPGGA第一筆資料所對應的矩陣為aryData(1), 然後把矩陣資料存到strUttcTime字串. z = 8 For x = 1 To intComma Do While Mid(strSentence, z, 1) <> "," And Mid(strSentence, z, 1) <> "*" aryData(x) = aryData(x) & Mid(strSentence, z, 1) z = z + 1 Loop z = z + 1 Next Select Case Left(strSentence, 6) Case "$GPGGA" MyGGA.時間 = aryData(1) MyGGA.經度 = aryData(2) MyGGA.strNSIndicator = aryData(3) MyGGA.緯度 = aryData(4) MyGGA.strEWIndicator = aryData(5) MyGGA.strPositionFix = aryData(6) MyGGA.使用中的衛星顆數 = aryData(7) MyGGA.strIIDOP = aryData(8) MyGGA.高度 = aryData(9) MyGGA.strAltUnits = aryData(10) MyGGA.strGeoid = aryData(11) MyGGA.strSepUnits = aryData(12) MyGGA.strDgpsAge = aryData(13) MyGGA.strDgpsid = aryData(14) Case "$GPVTG" Myvtg.strCourse1 = aryData(1) Myvtg.strReference1 = aryData(2) Myvtg.strCourse2 = aryData(3) Myvtg.strReference2 = aryData(4) Myvtg.相對位移速度1 = aryData(5) Myvtg.strSpeedUnit1 = aryData(6) Myvtg.相對位移速度2 = aryData(7) Myvtg.strSpeedUnit2 = aryData(8) Case "$GPGSV" MyGSV.StrNoMessages = aryData(1) MyGSV.StrSeq = aryData(2) MyGSV.StrSatsinview = aryData(3) MyGSV.天空中衛星總數 = aryData(4) MyGSV.衛星仰角 = aryData(5) MyGSV.衛星方位角 = aryData(6) MyGSV.StrSNR1 = aryData(7) MyGSV.StrSatId2 = aryData(8) MyGSV.StrElevation2 = aryData(9) MyGSV.StrAzimuth2 = aryData(10) MyGSV.StrSNR2 = aryData(11) MyGSV.StrSatId3 = aryData(12) MyGSV.StrElevation3 = aryData(13) MyGSV.StrAzimuth3 = aryData(14) MyGSV.StrSNR3 = aryData(15) MyGSV.StrSatId4 = aryData(16) MyGSV.StrElevation4 = aryData(17) MyGSV.StrAzimuth4 = aryData(18) MyGSV.StrSNR4 = aryData(19) End Select End Sub ================