Imports OpenNETCF
Public Class frmMain
Inherits System.Windows.Forms.Form
Windows Form Designer generated code
'---serial port to connect to the GPS receiver---
Private WithEvents serialPort As New IO.Ports.SerialPort
'---IP address of the server---
Private ServerIP As String = "Some IP adress"
'---the ID of the user---
Private ID As String = "1"
'---use for synchronization---
Dim sync As New Sync
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
txtGpsData.Text += serialPort.ReadExisting()
Dim lines() As String = txtGpsData.Text.Split(vbLf)
If lines.Length < 2 Then
Exit Sub
End If
If txtGpsData.Text.Length >= 500 Then
'---clear until the last $---
txtGpsData.Text = _
txtGpsData.Text.Substring(txtGpsData.Text.LastIndexOf("$"))
End If
If lines(lines.Length - 2).StartsWith("$GPGGA") Or _
lines(lines.Length - 2).StartsWith("$GPRMC") Then
processGPSData(lines(lines.Length - 2))
End If
End Sub
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived
txtGpsData.Invoke(New myDelegate(AddressOf updateTextBox))
End Sub
Private Sub processGPSData(ByVal str As String)
Dim rawLatLng As Double
Try
'---separate the GPS data into various fields---
Dim field() As String
field = str.Split(",")
Dim lat, lng As Double
Select Case field(0)
Case "$GPGGA"
'---latitude---
rawLatLng = Convert.ToDouble(field(2))
lat = (rawLatLng \ 100) + _
((rawLatLng - ((rawLatLng \ 100) * 100)) / 60)
'---latitude is negative if South---
If field(3) = "S" Then
lat *= -1.0
End If
'---longitude---
rawLatLng = Convert.ToDouble(field(4))
lng = (rawLatLng \ 100) + _
((rawLatLng - ((rawLatLng \ 100) * 100)) / 60)
'---longitude is negative if West---
If field(5) = "W" Then
lng *= -1.0
End If
'---display the lat and lng---
lblLat.Text = "Lat:" & lat
lblLng.Text = "Lng:" & lng
'---synchronize with the server---
sync.PerformSync(ServerIP, ID & ":" & lat & ":")
Case "$GPRMC"
'---display the speed---
If field(7) = String.Empty Then
lblSpeed.Text = "Speed: 0 km/h"
Else
lblSpeed.Text = "Speed: " & _
(Convert.ToDouble(field(7)) * 1.85).ToString _
& " km/h"
End If
End Select
Catch
MsgBox("An error has occurred")
End Try
End Sub
Private Sub mnuConnect_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuConnect.Click
'---close the port if it is already open---
If serialPort.IsOpen Then
serialPort.Close()
End If
'---set the parameters and open the port---
Try
With serialPort
.PortName = "COM7"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
serialPort.Open()
'---disable the Connect GPS menu item---
MenuItem1.Enabled = False
'---enable the Disconnect menu item---
MenuItem3.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub mnuIP_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuIP.Click
'---Input box for the user to enter the servers ip---
ServerIP = InputBox( _
"Please enter the IP address of server", "Server IP")
End Sub
Private Sub mnuID_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuID.Click
'---Lets the user tell the server the differenc between differnt users---
ID = InputBox("Please enter your ID", "ID")
End Sub
Private Sub mnuDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDisconnect.Click
serialPort.Close()
MenuItem1.Enabled = True '---Connect GPS---
MenuItem3.Enabled = False '---Disconnect---
End Sub
End Class