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
| Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim TrameDemandeIdentification As String
Dim CompteurCarRecu As Integer
Dim NbCarRecevoir As Integer
Dim SofRecu As Boolean
Dim TabCaractereRecu() As Byte = New Byte(29) {}
Dim indice1 As Integer
Dim indice2 As Integer
ReDim TabCaractereRecu(40)
TrameDemandeIdentification = "55000180EF07"
TextBox2.Text = ""
SerialPort1.PortName = "com8"
SerialPort1.Open()
SerialPort1.WriteTimeout = 500
CompteurCarRecu = 0
NbCarRecevoir = 0
indice1 = 0
indice2 = 0
SofRecu = False
SerialPort1.DiscardInBuffer()
SerialPort1.Write(hex2ascii(TrameDemandeIdentification))
hbwait(50)
FinTimerReception = False
TextBox2.Text = "Début"
SerialPort1.ReadTimeout = 1000
Try
Do
Dim Incoming As Integer = SerialPort1.BytesToRead
indice2 = Incoming
If Incoming <> 0 Then
SerialPort1.Read(TabCaractereRecu, indice1, indice2)
CompteurCarRecu = CompteurCarRecu + Incoming
indice1 = CompteurCarRecu
If TabCaractereRecu(0) = &H55 And SofRecu = False Then
SofRecu = True
If Incoming >= 3 Then
NbCarRecevoir = (TabCaractereRecu(1) * 256) + TabCaractereRecu(2) + 5
End If
If CompteurCarRecu = NbCarRecevoir Then
TextBox2.Text = CompteurCarRecu.ToString + " / " + NbCarRecevoir.ToString
Exit Do
End If
End If
ElseIf FinTimerReception = True Then
TextBox2.Text = "Time out"
Exit Do
End If
Loop
Catch ex As TimeoutException
Console.WriteLine("Error: Serial Port read timed out.")
Finally
If SerialPort1 IsNot Nothing Then SerialPort1.Close()
End Try
End Sub |
Partager