Hej Fyller på från mitt gamla prog i VB 6Läs GPS signaler (NMEA)
Detta är en fristående fortsättning på den serie av frågor och svar som startades av Johannes Strömberg 2007-08-04 under namnet "Komunicera med GPS-enheten på mobiltfn."
Det problem som tydligen uppstod vid "automatisk" läsning av Kom-porten i de kodexemplen undviks här genom att läsningen aktiveras av en Timer-kontroll med intervallen 100.
Jag kör denna kod på en Qtek9090 och en BlueI GPSmottagare.
Först kommer koden härnedan, sedan också koden för själva formuläret, Form1.designer.vb, med alla textrutor knappar etc...
Hoppas detta exempel kastar lite ljus över problematiken...
Form1.vb
Imports System.IO.Ports.SerialPort
Imports System.Text
Imports System.IO.Ports
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With SerialPort1
.PortName = "COM6"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
End With
End Sub
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
Try
If SerialPort1.IsOpen = False Then
SerialPort1.Open()
End If
Timer1.Enabled = True
Catch ex As Exception
MsgBox("Kolla om porten är öppnad och gpsen startad")
End Try
End Sub
Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
If SerialPort1.IsOpen = True Then
SerialPort1.Close()
End If
Timer1.Enabled = False
Application.Exit()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim NMEA As String
Dim NMEApart() As String
Try
NMEA = SerialPort1.ReadLine
Catch ex As Exception
MsgBox(ex.ToString)
Timer1.Enabled = False
Exit Sub
End Try
NMEApart = Split(NMEA, ",")
UTC_TextBox.Text = NMEApart(1)
Select Case NMEApart(0)
Case "$GPRMC" 'Minsta rekomenderade informationen för navigering
MODE_TextBox.Text = NMEApart(2)
LAT_TextBox.Text = NMEApart(3)
LON_TextBox.Text = NMEApart(5)
Replace(NMEApart(7), ".", ",")
'Dim Knots As Double = CDbl(NMEApart(7))
KMH_Textbox.Text = NMEApart(7) 'Format(Knots / 1.852, "###.#")
Case "$GPGGA" 'Innehåller bla höjdinformation
ALT_TextBox.Text = NMEApart(7)
End Select
End Sub
Private Sub LAT_TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LAT_TextBox.TextChanged
End Sub
End Class
..... och sedan koden för gränssnittet
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.SerialPort1 = New System.IO.Ports.SerialPort(Me.components)
Me.Timer1 = New System.Windows.Forms.Timer
Me.StartButton = New System.Windows.Forms.Button
Me.StopButton = New System.Windows.Forms.Button
Me.LAT_TextBox = New System.Windows.Forms.TextBox
Me.LON_TextBox = New System.Windows.Forms.TextBox
Me.UTC_TextBox = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.MODE_TextBox = New System.Windows.Forms.TextBox
Me.Label5 = New System.Windows.Forms.Label
Me.ALT_TextBox = New System.Windows.Forms.TextBox
Me.KMH_Textbox = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Timer1
'
'
'StartButton
'
Me.StartButton.Location = New System.Drawing.Point(8, 8)
Me.StartButton.Name = "StartButton"
Me.StartButton.Size = New System.Drawing.Size(64, 25)
Me.StartButton.TabIndex = 0
Me.StartButton.Text = "Start"
'
'StopButton
'
Me.StopButton.Location = New System.Drawing.Point(172, 11)
Me.StopButton.Name = "StopButton"
Me.StopButton.Size = New System.Drawing.Size(65, 22)
Me.StopButton.TabIndex = 1
Me.StopButton.Text = "Stopp"
'
'LAT_TextBox
'
Me.LAT_TextBox.Location = New System.Drawing.Point(8, 123)
Me.LAT_TextBox.Name = "LAT_TextBox"
Me.LAT_TextBox.Size = New System.Drawing.Size(104, 21)
Me.LAT_TextBox.TabIndex = 2
Me.LAT_TextBox.Text = "TextBox1"
'
'LON_TextBox
'
Me.LON_TextBox.Location = New System.Drawing.Point(121, 122)
Me.LON_TextBox.Name = "LON_TextBox"
Me.LON_TextBox.Size = New System.Drawing.Size(104, 21)
Me.LON_TextBox.TabIndex = 3
Me.LON_TextBox.Text = "TextBox1"
'
'UTC_TextBox
'
Me.UTC_TextBox.Location = New System.Drawing.Point(8, 65)
Me.UTC_TextBox.Name = "UTC_TextBox"
Me.UTC_TextBox.Size = New System.Drawing.Size(90, 21)
Me.UTC_TextBox.TabIndex = 4
Me.UTC_TextBox.Text = "TextBox1"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(10, 48)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(120, 14)
Me.Label1.Text = "UTC (HHMMSS)"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(116, 89)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(109, 31)
Me.Label2.Text = "Longitud (DDDMM.ddd)"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(8, 89)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(90, 31)
Me.Label3.Text = "Latitud (DDMM.ddd)"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(116, 48)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(120, 14)
Me.Label4.Text = "A=OK, V=No good"
'
'MODE_TextBox
'
Me.MODE_TextBox.Location = New System.Drawing.Point(116, 65)
Me.MODE_TextBox.Name = "MODE_TextBox"
Me.MODE_TextBox.Size = New System.Drawing.Size(109, 21)
Me.MODE_TextBox.TabIndex = 12
Me.MODE_TextBox.Text = "TextBox1"
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(10, 234)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(77, 46)
Me.Label5.Text = "Höjd (m) över Mean Sea Level"
'
'ALT_TextBox
'
Me.ALT_TextBox.Location = New System.Drawing.Point(107, 245)
Me.ALT_TextBox.Name = "ALT_TextBox"
Me.ALT_TextBox.Size = New System.Drawing.Size(79, 21)
Me.ALT_TextBox.TabIndex = 15
Me.ALT_TextBox.Text = "TextBox1"
'
'KMH_Textbox
'
Me.KMH_Textbox.Font = New System.Drawing.Font("Tahoma", 36.0!, System.Drawing.FontStyle.Regular)
Me.KMH_Textbox.Location = New System.Drawing.Point(35, 150)
Me.KMH_Textbox.Multiline = True
Me.KMH_Textbox.Name = "KMH_Textbox"
Me.KMH_Textbox.Size = New System.Drawing.Size(151, 70)
Me.KMH_Textbox.TabIndex = 21
Me.KMH_Textbox.Text = "999.9"
Me.KMH_Textbox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(96.0!, 96.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
Me.AutoScroll = True
Me.ClientSize = New System.Drawing.Size(240, 294)
Me.ControlBox = False
Me.Controls.Add(Me.KMH_Textbox)
Me.Controls.Add(Me.ALT_TextBox)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.MODE_TextBox)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.UTC_TextBox)
Me.Controls.Add(Me.LON_TextBox)
Me.Controls.Add(Me.LAT_TextBox)
Me.Controls.Add(Me.StopButton)
Me.Controls.Add(Me.StartButton)
Me.MinimizeBox = False
Me.Name = "Form1"
Me.Text = "NMEA-reader"
Me.ResumeLayout(False)
End Sub
Friend WithEvents SerialPort1 As System.IO.Ports.SerialPort
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents StartButton As System.Windows.Forms.Button
Friend WithEvents StopButton As System.Windows.Forms.Button
Friend WithEvents LAT_TextBox As System.Windows.Forms.TextBox
Friend WithEvents LON_TextBox As System.Windows.Forms.TextBox
Friend WithEvents UTC_TextBox As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents MODE_TextBox As System.Windows.Forms.TextBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents ALT_TextBox As System.Windows.Forms.TextBox
Friend WithEvents KMH_Textbox As System.Windows.Forms.TextBox
End Class
Sv: Läs GPS signaler (NMEA)
<b>$GPRMC,091735,A,5557.6727,N,01246.6953,E,0.0,150.4,200406,1.8,E,A*1C
$GPRMB,A,0.70,L,B-BOJ,G-VAEST,5600.0500,N,01243.4000,E,3.014,322.2,,V,A*64
$GPGGA,104226,5557.6727,N,01246.6953,E,1,05,2.0,24.5,M,39.8,M,,*79
$GPGLL,5557.6727,N,01246.6953,E,104226,A,A*4A
$GPBOD,335.6,T,333.8,M,G-VAESTPRI,B-BOJ*13
$GPBWC,104226,5600.0500,N,01243.4000,E,150.2,T,320.4,M,2.014,N,G-VAESTPRI,A*1D
$GPVTG,150.4,T,148.6,M,0.0,N,0.0,K,A*23
$GPXTE,A,A,0.70,L,N,A*04
$PGRME,8.7,M,11.3,M,14.3,M*24
$PGRMZ,80,f,3*23
$PGRMM,WGS 84*06</b>
Om någon önskar kan jag förklara i detalj. (,) komma är separator för de olika posterna
$GPRMC ovan står kl 09:17:35 Nord 55,57.... Ost 012,46.... osv kurs 150 datum 2004 06