Timer et DataReceived ne font pas bon ménage
Bonjour
Dans mon soft, j'ai une gestion de liaison série à faire.
pour la réception des trames, j'utilise SerialPort.DataReceived
Pas de problème ca fonctionne très bien.
Je souhait dépiler ma trame de réception en dehors de ma fonction SerialPort.DataReceived et donc comme on me l'a conseillé, j'utilise un timer de 250 ms.
Bien sur dans Form1_Load je met en marche mon timer1 (Timer1.start())
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
| Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Timer1.Stop()
Thread.Sleep(100)
If (Not (U8_FifoIncIn = U8_FifoIncOut) Or Not (u8_FifoRxState = True)) Then
NbCarReceived = SerialPort1.BytesToRead
SerialPort1.Read(U8t_FifoRX, U8_FifoIncIn, NbCarReceived)
U8_FifoIncIn = U8_FifoIncIn + NbCarReceived
If (U8_FifoIncIn = TailleBufferReception) Then
U8_FifoIncIn = 0
End If
If (U8_FifoIncOut = U8_FifoIncIn) Then
u8_FifoRxState = True
End If
End If
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
If U8_FifoIncOut < U8_FifoIncIn Then 'On a des caractère a dépiler
Select Case U8_EtapeAnalyse
Case RechercheDebutTrame
If U8t_FifoRX(U8_FifoIncOut) = 170 Then
U8_FifoIncOut = U8_FifoIncOut + 1
U8_EtapeAnalyse = RechercheTypeTrame
End If
Case RechercheTypeTrame
If U8t_FifoRX(U8_FifoIncOut) = 160 Then 'c'est une trame de type dimming A0 hex
TypeTrameCourte = True
U8_EtapeAnalyse = AnalyseTrameLongueurOctet
ElseIf U8t_FifoRX(U8_FifoIncOut) = 80 Then 'c'est une trame de type mise à jour 50hex
TypeTrameCourte = True
U8_EtapeAnalyse = AnalyseTrameLongueurOctet
ElseIf U8t_FifoRX(U8_FifoIncOut) = 7 Then 'c'est une trame de type lecture de configuration 07hex
TypeTrameCourte = False
U8_EtapeAnalyse = AnalyseTrameLongueurOctet
Else
U8_EtapeAnalyse = AnalyseTrameInconnue
End If
U8_FifoIncOut = U8_FifoIncOut + 1
Case AnalyseTrameLongueurOctet
U8_NombreOctetTrameEnCoursAnalyse = U8t_FifoRX(U8_FifoIncOut)
'U8_FifoIncOut = U8_FifoIncOut + 1
If (TypeTrameCourte = True) Then 'c"est une trame courte
U8_EtapeAnalyse = AnalyseTrameDimmingAckNack
Else
U8_EtapeAnalyse = AnalyseTrameLectureParametres
CptLectureTrameConfiguration = 0
End If
U8_FifoIncOut = U8_FifoIncOut + 1
Case AnalyseTrameLectureParametres
TrameDeValeurLectureConfiguration(CptLectureTrameConfiguration) = U8t_FifoRX(U8_FifoIncOut)
CptLectureTrameConfiguration = CptLectureTrameConfiguration + 1
U8_FifoIncOut = U8_FifoIncOut + 1
If CptLectureTrameConfiguration = TailleBufferLectureConfiguration + 1 Then
U8_EtapeAnalyse = AnalyseTrameDimmingRecupChekSum
End If
Case AnalyseTrameDimmingAckNack
If U8t_FifoRX(U8_FifoIncOut) = 0 Then 'on a recu ACK
ResulatTrameDimming = "ACK"
U8_EtapeAnalyse = AnalyseTrameDimmingRecupChekSum
ElseIf U8t_FifoRX(U8_FifoIncOut) = 1 Then 'on a recu NACK
ResulatTrameDimming = "NACK"
U8_EtapeAnalyse = AnalyseTrameDimmingRecupChekSum
Else
U8_EtapeAnalyse = AnalyseTrameInconnue
End If
U8_FifoIncOut = U8_FifoIncOut + 1
Case AnalyseTrameDimmingRecupChekSum
U8_ChekSumTrameEnCoursAnalyse = U8t_FifoRX(U8_FifoIncOut)
'U8_ChekSumCalculeTrameEnCoursAnalyse = CalculChekSumTrame()
If U8_ChekSumTrameEnCoursAnalyse <> U8_ChekSumCalculeTrameEnCoursAnalyse Then
'MessageBox.Show("Le cheksum de la trame est OK")
Else
'MessageBox.Show("Le cheksum de la trame est NOK")
End If
U8_FifoIncOut = U8_FifoIncOut + 1
U8_EtapeAnalyse = RechercheDebutTrame
Case CalculChekSumTrame
Case Else 'Trame inconnue
End Select
End If
Timer1.Start()
End Sub |
Quand je lance mon soft, je passe bien dans ma fonction Timer1.Tick et si je ne recoit pas de trame, pas de problème je passe toujours dans l'interval programmé dans ma fonction Timer1.Tick
Par contre,à chaque réception de trame je passe bien dans ma fonction SerialPort1_DataReceived, mais je ne passe plus jamais par ma fonction Timer1.Tick (alors que Timer1 = Start)
Une idée
merci