Bonjour,
J'ai récupéré le code source Lecteur GPS sur le site codePPC de stéphane Sibué, j'essaie d'y ajouter une procédure qui me permette de comparer latitude et longitude récupérée de la trame GPS avec latitude et longitude cible du point à atteindre. Si les coordonnées correspondent à un intervalle de tolérance près, on déclenche l'affichage d'une autre fenêtre (ici wTuBrules)
J'utilise VB.NET Compact Framework 2.0 qui ne me renvoie aucune erreur lors de la compilation, par contre qd je transfère sur le PDA et que je lance l'application, ça lance un message d'erreur.
Je pense que l'erreur provient de la logique de programmation, il y des notions de threads que je ne maîtrise pas par exemple...
N'hésitez pas à me demander des explications complémentaires.
Merci
Voilà ce que ça donne:
Public Class LectGPS
Private Delegate Sub DelegateAjouterTrame(ByVal wTrame As String)
Private pAjouterTrame As New DelegateAjouterTrame(AddressOf AjouterTrame)
Private Delegate Sub DelegateAfficherGPRMC(ByVal wLatitude As String, ByVal wLongitude As String)
Private pAfficherGPRMC As New DelegateAfficherGPRMC(AddressOf AfficherGPRMC)
Private Sub BOU_Ouvrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BOU_Ouvrir.Click
TextBox1.Text = ListBox1.Items(0).ToString
TextBox2.Text = ListBox1.Items(1).ToString
SP_GPS.Open()
End Sub
Private Function TesterTrame(ByVal wTrame As String) As Boolean
REM Une trame vide est forcément invalide
If wTrame = "" Then Return False
REM La trame doit commencer par $
If wTrame.Chars(0) <> "$"c Then Return False
REM Le *CS doit être présent
If wTrame.Substring(wTrame.Length - 3, 1) <> "*" Then Return False
REM Calcul du CheckSum
Dim wCS As Integer
For i As Integer = 1 To wTrame.Length - 4
wCS = wCS Xor Asc(wTrame.Chars(i))
Next
Dim wHCS As String = Hex(wCS)
If wHCS.Length < 2 Then wHCS = "0" & wHCS
REM Si le CheckSum calculé est <> du CheckSum donné
If wHCS <> wTrame.Substring(wTrame.Length - 2, 2) Then Return False
REM Tout va bien
Return True
End Function
Private Sub AjouterTrame(ByVal wTrame As String)
REM On enlève le * CheckSum de la fin
Dim wPos As Integer = wTrame.IndexOf("*"c)
If wPos > -1 Then wTrame = wTrame.Substring(0, wPos)
REM On ajoute la trame à la liste
Dim wIndex As Integer = LST_Trames.Items.Add(wTrame)
REM On sélectionne la nouvelle trame
LST_Trames.SelectedIndex = wIndex
REM Pour ne pas saturer la mémoire
REM On limite le remplissage de la liste
Do While LST_Trames.Items.Count > 50
LST_Trames.Items.RemoveAt(0)
Loop
End Sub
Private Sub SP_GPS_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SP_GPS.DataReceived
Static wBuffer As String = ""
Dim wCount As Integer = SP_GPS.BytesToRead
Dim wData(wCount) As Char
Dim wLen As Integer = SP_GPS.Read(wData, 0, wCount)
wBuffer &= New String(wData, 0, wLen)
Dim k As Integer
Do
k = wBuffer.IndexOf(vbCrLf)
If k > -1 Then
Dim wTrame As String = wBuffer.Substring(0, k)
wBuffer = wBuffer.Substring(k + 2)
If TesterTrame(wTrame) Then
Me.Invoke(pAjouterTrame, wTrame)
If wTrame.Substring(0, 7) = "$GPRMC," Then DecoderTrameGPRMC(wTrame)
End If
Else
Exit Do
End If
Loop
End Sub
Private Sub AfficherGPRMC(ByVal wLatitude As String, ByVal wLongitude As String)
TXT_Latitude.Text = wLatitude
TXT_Longitude.Text = wLongitude
End Sub
Private Sub DecoderTrameGPRMC(ByVal wTrame As String)
REM Liste des élements composant une trame RMC
REM 00 GPRMC()
REM 01 Heure du fix
REM 02 Alerte (A=OK ; V=WARNIG)
REM 03 Latitude au format ddmm.ss
REM 04 Sens de la latitude (N=Nord=Positif, S=Sud=Négatif)
REM 05 Longitude au format dddmm.ss
REM 06 Sens de la longitude (E=Est=Positif, W=Ouest=Négatif)
REM 07 Vitesse au sol en Knots (noeuds)
REM 08 Cap vrai
REM 09 Date du fix
REM 10 Déclinaison magnétique
REM 11 Sens de la déclinaison magnétique
Dim wItems() As String
Dim wLatitude As String = ""
Dim wLongitude As String = ""
REM On éclate les différents éléments de la trame
wItems = wTrame.Split(",")
REM La trame doit faire au moins 12 éléments
If wItems.Length < 12 Then Throw New ArgumentException
REM Le premier élement doit être "GPRMC", Sinon Erreur
If wItems(0) <> "$GPRMC" Then Throw New ArgumentException
REM Si alerte=A les données sont valables
If wItems(2) = "A" Then
wLatitude = wItems(3)
wLongitude = wItems(5)
End If
If _
wLatitude < ListBox1.Items(0) + 0.01 _
And wLatitude > ListBox1.Items(0) - 0.01 _
And wLongitude > ListBox1.Items(1) - 0.01 _
And wLongitude < ListBox1.Items(1) + 0.01 Then
Dim wTuBrules As New TuBrules
wTuBrules.Show()
ElseIf Me.Invoke(pAfficherGPRMC, wLatitude, wLongitude) Then
End If
REM Si alerte=A les données sont valables
Me.Invoke(pAfficherGPRMC, wLatitude, wLongitude)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SP_GPS.IsOpen Then SP_GPS.Close()
Me.Close()
End Sub
End Class
Franck