1 pièce(s) jointe(s)
Utiliser un thread à chaque appui sur un bouton d'une IG
Bonjour,
Je suis actuellement en train de travailler sur un logiciel de test. Le logicielle sera sur un ordinateur équipé d'un dongle BT. En effet, le logicielle doit être capable de communiquer avec une Raspberry via BT. J'ai donc créer un réseau PAN qui me permet d'obtenir une adresse IP pour chaque machine. Grâce à cela, j'ai pu utiliser un socket TCP/IP pour l'échange. Jusque la tout va bien.
Mon soucis dans ce logicielle c'est que j'ai une étape préliminaire qui est de tester si ma communication BT fonctionne et si une autre RPi (en reseau local avec la 1ère RPi) répond. J'ai donc utiliser la méthode du multithread pour ouvrir un thread à chaque fois que j'appui sur le bouton test de l'IG, ce qui me permet de tester si tous les réglages sont bien réalisé. Comme vous l'aurez compris, cela ne fonctionne pas.
Voici le code pour vous mettre dans le bain:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Public Class Preliminary_Check
Dim Ip As String = "192.168.50.2"
Dim port As String = "1000"
Dim Socket_client As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim MyEp As IPEndPoint = New IPEndPoint(IPAddress.Parse(Ip), port)
Dim check_step As Integer = 0
Dim Thread_PreliminaryCheck As New Thread(AddressOf Test1_PreliminaryCheck)
Delegate Sub dTest_ping_BOX1()
Private Sub Preliminary_Check_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Supprime le fichier de Log à chaque démarrage
My.Computer.FileSystem.DeleteFile("C:\log\log.txt")
Next_Btn.Enabled = False
BT_pict.Image = Image.FromFile("C:\img\BT_off.png")
BT_Check_label.Text = " Disconnected "
Box_pict.Image = Image.FromFile("C:\img\red_cross.png")
Box_Check_Label.Text = " Disconnected "
WriteInLog("Socket Client initialisé")
End Sub
Private Sub Quit_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Quit_Btn.Click
Me.Close()
Login.Close()
Socket_client.Close()
End Sub
Public Sub Test_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test_Btn.Click
Thread_PreliminaryCheck.Start()
End Sub
Private Sub Test_ping_BOX2(ByVal Socket)
Dim cmd As Byte() = System.Text.Encoding.ASCII.GetBytes("ping box2")
Dim buffer_byte(50) As Byte
Dim buffer_string As String
Socket.Send(cmd)
Socket.Receive(buffer_byte)
buffer_string = System.Text.Encoding.ASCII.GetString(buffer_byte)
WriteInLog("Resultat du ping ... : " & buffer_string)
If StrComp(buffer_string, "OK", CompareMethod.Text) = 0 Then
Box_pict.Image = Image.FromFile("C:\img\check.png")
Box_Check_Label.Text = " Connected "
check_step = check_step + 1
Socket.Close()
End If
End Sub
Private Sub Test1_PreliminaryCheck()
Me.Invoke(New dTest_ping_BOX1(AddressOf Test_ping_BOX1))
End Sub
Private Sub Test_ping_BOX1()
Test_Btn.Enabled = False
check_step = 0
Try
'Connexion
WriteInLog("Connexion au serveur....")
Socket_client.Connect(MyEp)
WriteInLog("Connexion.... OK")
BT_pict.Image = Image.FromFile("C:\img\BT_on.png")
BT_Check_label.Text = " Connected "
check_step = check_step + 1
Catch ex As Exception
WriteInLog("Erreur lors de la tentative de connexion : " & ex.ToString)
End Try
Try
'Test Ping
WriteInLog("Envoi de la commande ping...")
Test_ping_BOX2(Socket_client)
Catch ex As Exception
WriteInLog("Erreur lors de la tentative d'envoi : " & ex.ToString)
End Try
If check_step = 2 Then
Next_Btn.Enabled = True
If Thread_PreliminaryCheck.IsAlive = True Then
Thread_PreliminaryCheck.Abort()
End If
ElseIf check_step = 1 Then
MsgBox("Erreur lors du test. Veuillez vérifier la connexion de la BOX2", vbCritical + vbOK, "Erreur")
Test_Btn.Enabled = True
If Thread_PreliminaryCheck.IsAlive = True Then
Thread_PreliminaryCheck.Abort()
End If
ElseIf check_step = 0 Then
MsgBox("Erreur lors de la connexion.Veuillez vérifier la mise en tension de la BOX1", vbCritical, "Erreur")
Test_Btn.Enabled = True
If Thread_PreliminaryCheck.IsAlive = True Then
Thread_PreliminaryCheck.Abort()
End If
End If
End Sub
Private Sub Next_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Next_Btn.Click
Me.Close()
Test_Window.Show()
End Sub
Private Sub WriteInLog(ByVal Mess)
Const ForAppending = 8 'ForReading = 1, ForWriting = 2,
Dim oFso, f
oFso = CreateObject("Scripting.FileSystemObject")
f = oFso.OpenTextFile("C:\log\log.txt", ForAppending, True)
f.writeLine(Now & " : " & Mess)
f.Close()
End Sub
End Class |
Ceci est ma fenêtre :
Pièce jointe 268577
Le test fonctionne la première fois que je clic sur le bouton test. Une fois terminé, si il y a un soucis j'affiche un message de résolution et je veux permettre à l'utilisateur de pouvoir réessayer le test. Avez-vous une idée?