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 121 122 123 124 125 126 127 128
|
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim resultat1 As String
Dim resultat2 As String
Dim resultat3 As String
Private Delegate Sub box1(ByVal resultat1 As String)
Private Delegate Sub box2(ByVal resultat2 As String)
Private Delegate Sub box3(ByVal resultat3 As String)
Private _continue As Boolean
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BT_Init.Enabled = False
With SerialPort1
.PortName = "COM3"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
Dim readThread As Thread = New Thread(AddressOf Read)
SerialPort1.ReadTimeout = 1000
SerialPort1.Open()
_continue = True
readThread.Start()
If _continue = False Then
MsgBox("coucou2", 64, "coucou2")
End If
End Sub
Public Sub Read()
While (_continue)
Try
System.Threading.Thread.Sleep(300)
Dim octet_recu As Byte = 9 'SerialPort1.ReadByte
Dim tab(octet_recu - 1) As Byte ' Tableau acceuillant les données au format byte
SerialPort1.Read(tab, 0, octet_recu) 'lecture du port
'traitement du tableau de données
Catch ex As IndexOutOfRangeException
MsgBox("Les mesures transmises par l'interface ne sont pas correctes", 64, "Données incorrectes")
_continue = False
SerialPort1.Close()
Catch ex As TimeoutException
MsgBox("Aucunes données ne sont transmises", 64, "Erreur")
resultat1 = "aucune mesure"
resultat2 = "Aucune mesure"
resultat3 = "Aucune mesure"
If MsgBoxResult.Ok Then
_continue = False
SerialPort1.Close()
Call initialisation()
End If
Catch ex As ThreadAbortException
_continue = False
SerialPort1.Close()
Catch e As InvalidOperationException
SerialPort1.Close()
_continue = False
System.Threading.Thread.CurrentThread.Abort()
Me.Close()
End Try
If Me.InvokeRequired Then
Me.Invoke(New box3(AddressOf _box2), resultat3)
Me.Invoke(New box2(AddressOf _box3), resultat2)
Me.Invoke(New box1(AddressOf _box1), resultat1)
Else : _continue = False
SerialPort1.Close()
End If
End While
End Sub
Public Sub initialisation()
MsgBox("coucou", 64, "coucou")
If _continue = False Then
MsgBox("coucou1", 64, "coucou1")
System.Threading.Thread.CurrentThread.Abort()
BT_Init.Enabled = True
End If
End Sub
Private Sub FrmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim msgbox_ferm As DialogResult
msgbox_ferm = MessageBox.Show("Souhaitez-vous quitter cette apllication?", "Quitter", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If msgbox_ferm = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
If msgbox_ferm = Windows.Forms.DialogResult.Yes Then
SerialPort1.Close()
_continue = False
System.Threading.Thread.CurrentThread.Abort()
Exit Sub
End If
End Sub
Private Sub _box2(ByVal resultat2 As String)
TextBox2.Text = resultat2
End Sub
Private Sub _box3(ByVal resultat3 As String)
TextBox3.Text = resultat3
End Sub
Private Sub _box1(ByVal resultat1 As String)
TextBox1.Text = resultat1
End Sub
End Class |