正文

用VB 实现GPS 信息处理22005-09-29 20:19:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/5456.html

分享到:

2. 2. 4  ShowData 过程

该过程具体实现GPS 信息在文本框区显示、采集的功能,它通过添加数据到txt Term 控件的Text 属性,过滤控制字符,如空格、回车、换行, 并且写数据到一个打开的记录文件。空格符从它的左侧删除,在Text 属性, 或

者从传递字符串中;换行符将被修改为回车; txt Term 控件的Text 属性的尺寸也被监视,使它不能超过MAX2TERMSIZE 的要求。其实现过程如下:

Private Static Sub ShowData ( Term As Control , Data As String)

On Error GoTo Handler

Const MAXTERMSIZE = 16000

Dim TermSize As Long , i

’以下为显示GPS 信息时用的变量

Dim InString As String

Dim Utils As New CParseUtils

Dim tempStr As String

Dim n As Integer

Dim cnt As Integer

’确定现存的文本不会太大

TermSize = Len ( Term. Text )

If TermSize > MAXTERMSIZE Then

 Term. Text = Mid $( Term. Text , 4097)

 TermSize = Len ( Term. Text )

End If

’指到txt Term 的数据的结尾处

Term. SelStart = TermSize

’过滤/ 处理空格符

Do

 i = InStr (Data , Chr $(8) )

 If i Then

   If i = 1 Then

    Term. SelStart = TermSize - 1

    Term. SelLength = 1

    Data = Mid $(Data , i + 1)

   Else

    Data = Left $(Data , i - 2) & Mid $(Data , i +1)

   End If

 End If

Loop While i

’除去换行符

Do

 i = InStr (Data , Chr $(10) )

 If i Then

   Data = Left $(Data , i - 1) & Mid $(Data , i + 1)

 End If

Loop While i

’确定所有的回车都包含换行符

i = 1

Do

 i = InStr (i , Data , Chr $(13) )

 If i Then

   Data = Left $(Data , i) & Chr $(10) & Mid $(Da2ta , i + 1)

   i = i + 1

 End If

Loop While i

’添加过滤的数据到SelText 属性

Term. SelText = Data

 ’如果需要记录数据到文件

If hLogFile Then

 i = 2

 Do

   Err = 0

   Put hLogFile , , Data

   Dim data0 As String

   If Err Then

    i = MsgBox( Error $, 21)

    If i = 2 Then

      mnuCloseLog - Click

    End If

   End If

 Loop While i < > 2

End If

Term. SelStart = Len ( Term. Text ) ’定位信息显示部分开始

InString = Data

cnt = Utils. Count Parts ( InString , Chr (10) )

  For n = 0 To cnt - 1

   tempStr = Utils. Parse ( InString , n , Chr (10) )

    ReDim Preserve sArray(n)

    sArray(n) = tempStr

  Next n

  Set Utils = Nothing

  sCnt = cnt

  Call playNMEA(sArray) ’显示部分结束

Exit Sub

Handler :

MsgBox Error $

Resume Next

End Sub

2. 3  实时记录和保存GPS 数据

程序运行时,如执行“打开记录文件”,则打开要记录的文件和端口,在GPS 信息区实时显示接收到的GPS 报文[ 2 ] ,并录入到记录文件中;若执行“关闭记录文件”,则关闭记录文件(但文本框中仍然显示GPS 信息) 。

Private Sub mnuOpenLog - Click()

’执行“打开记录文件”

 Dim replace

 On Error Resume Next

 With OpenLog ’CommonDialog 控件

    . Flags = cdlOFNHideReadOnly Or cdlOFNExplorer

    . CancelError = True    ’从用户处获得记录

文件名称

    . DialogTitle = “打开记录文件”

    . Filter = “文件格式1 ( 3 . LOG) | 3 . log| 文件格式2 ( 3 . txt) | 3 . txt| 文件格式3 ( 3 . 3 ) | 3 . 3 ”

 End With

 Do

   OpenLog. FileName = “”

   OpenLog. ShowOpen

   If Err = cdlCancel Then Exit Sub

   Temp = OpenLog. FileName

   ’如果文件已经存在, 询问用户是否希望覆盖此文件或在此文件基础上添加内容

   Ret = Len (Dir $( Temp) )

   If Err Then

    MsgBox Error $, 48

    Exit Sub

   End If

   If Ret Then

    replace = MsgBox(“代替存在的- ”+ Temp +“吗?”, 35)

   Else

    replace = 0

   End If

 Loop While replace = 2

 ’用户单击“确定”按钮, 则删除此文件

 If replace = 6 Then

   Kill Temp

   If Err Then

    MsgBox Error $, 48

    Exit Sub

   End If

 End If

 ’打开记录文件

 hLogFile = FreeFile

 Open Temp For Binary Access Write As hLogFile

 If Err Then

   MsgBox Error $, 48

   Close hLogFile

   hLogFile = 0

   Exit Sub

 Else

   ’到文件结尾处来添加新数据

   Seek hLogFile , LOF(hLogFile) + 1

 End If

End Sub

Private Sub mnuCloseLog - Click()

’执行“关闭记录文件”

  Close hLogFile

  hLogFile = 0

End Sub

2. 4  回放GPS 数据

GPS 数据的回放,是将存有GPS 信息的文件打开,并重新读取、显示其信息的过程。它包括一个打开过程[ 2 ]和一个播放过程。具体实现代码如下(要用到前面给出的模块中的方法) :

Private Sub mnuOpenBFile - Click()

’读取存有GPS 信息的回放文件

  Dim sFile As String

Dim Sentence

On Error GoTo errorhandle

With OpenBFile

    ’从用户处获得回放文件名称

    . DialogTitle = “打开回放文件”

    . Filter = “文件格式1 ( 3 . LOG) | 3 . log| 文件格式2 ( 3 . txt) | 3 . txt| 文件格式3 ( 3 . 3 ) | 3 . 3 ”

    . ShowOpen

    If Len( . FileName) = 0 Then

      Exit Sub

    End If

      sFile = . FileName

    End With

    Open sFile For Input As # 1

    sCnt = 0

     Do While Not EOF (1)

       Line Input # 1 , Sentence

       ReDim Preserve sArray(sCnt)

       sArray(sCnt) = Sentence

       sCnt = sCnt + 1

     Loop

Close # 1

 Exit Sub

errorhandle :

  Close # 1

  MsgBox“打开文件错误!”

End Sub

Private Sub mnuPlayBFile - Click()

’播放GPS 记录文件

Call playNMEA(sArray)

End Sub

3  结 论

通过对GPS 通讯NMEA 协议的分析,以RMC 语句的“$GPRMC”串为例,给出了开发GPS 数据处理的方法。该方法具有简单、高效、实用的特点。需要说明的是,用RMC 语句不能读取高程数据,如果要在应用中读取高程数据, 可应用文中的方法, 再编写一个读GGA 语句(“$GPGGA”串) 的类,其中第9 个数据段就是高程信息。

阅读(2580) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册