Je sèche depuis 2 jours et j'ai essayé plusieurs méthodes mais avec toujours le même resultat.

Je récupère via le port COM5 ( arduino ) des valeurs de pas d'une roue codeuse ( ex : 50 51 52 53 etc ... )
J'utilise un timer pour voir ce qui arrive du port COM
J'ai un fichier excel dans lequel j'ai des valeurs que je souhaite comparer.

Mon problème c'est que pour lire mes valeurs dans excel j’utilise le code suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 appExcel = CreateObject("Excel.application")
        wbExcel = appExcel.Workbooks.Open("C:\Users\Fred\Documents\fred.xlsm")
        awbExcel = appExcel.ActiveWorkbook
        wsExcel = appExcel.Worksheets("KU01-20")
        appExcel.Visible = False
Si je place ce code dans le timer, il m'ouvre excel des millions de fois
Si je le place dans le Private Sub Form1_Load, le problème c'est qu'a la lecture d'une cellule excel dans le Sub timer, il plante.

Voici le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
Imports System
Imports System.IO.Ports
 
 
 
 
 
Public Class Form1
    Public Cumul As Integer
    Public Topmarquage As Integer
    Public Z1 As Integer
    Public appExcel As Object
    Public wbExcel As Object
    Public wsExcel As Object
    Public awbExcel As Object
 
    Public Nomfil As String
    Public marquage1 As Integer
    Public marquage2 As Integer
    Public longueur As Integer
    Public longueur2 As Integer
    Public quantite As Integer
    Public batch As Integer
    Public bascule As Integer
 
 
    Dim comPORT As String
    Dim receivedData As String = ""
 
 
    Friend Sub hbwait(ByVal ms_to_wait As Long)
        Dim endwait As Double
        endwait = Environment.TickCount + ms_to_wait
        While Environment.TickCount < endwait
            System.Threading.Thread.Sleep(1)
            Application.DoEvents()
        End While
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
 
        bascule = 1
        Cumul = 0
        Z1 = 2
 
        appExcel = CreateObject("Excel.application")
        wbExcel = appExcel.Workbooks.Open("C:\Users\Fred\Documents\fred.xlsm")
        awbExcel = appExcel.ActiveWorkbook
        wsExcel = appExcel.Worksheets("KU01-20")
        appExcel.Visible = False 'rendre Excel visible voilà ce qui manque
 
 
 
 
        Timer2.Enabled = False
        comPORT = ""
        For Each sp As String In My.Computer.Ports.SerialPortNames
            comPort_ComboBox.Items.Add(sp)
        Next
 
 
 
 
 
    End Sub
 
 
    Private Sub comPort_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
        If (comPort_ComboBox.SelectedItem <> "") Then
            comPORT = comPort_ComboBox.SelectedItem
        End If
    End Sub
 
 
    Private Sub connect_BTN_Click(sender As Object, e As EventArgs) Handles connect_BTN.Click
        If (connect_BTN.Text = "Connect") Then
            If (comPORT <> "") Then
                SerialPort1.Close()
                SerialPort1.PortName = comPORT
                SerialPort1.BaudRate = 576000
                SerialPort1.DataBits = 8
                SerialPort1.Parity = Parity.None
                SerialPort1.StopBits = StopBits.One
                SerialPort1.Handshake = Handshake.None
                SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
                SerialPort1.ReadTimeout = 10000
 
                SerialPort1.Open()
                connect_BTN.Text = "Dis-connect"
                Timer2.Enabled = True
                Timer_LBL.Text = "Timer: ON"
            Else
                MsgBox("Select a COM port first")
            End If
        Else
            SerialPort1.Close()
            connect_BTN.Text = "Connect"
            Timer2.Enabled = False
            Timer_LBL.Text = "Timer: OFF"
        End If
 
 
 
    End Sub
 
 
 
 
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
 
 
 
        receivedData = ReceiveSerialData()
        RichTextBox1.Text = receivedData
 
 
 
 
        appExcel = CreateObject("Excel.application")
        wbExcel = appExcel.Workbooks.Open("C:\Users\Fred\Documents\fred.xlsm")
        awbExcel = appExcel.ActiveWorkbook
        wsExcel = appExcel.Worksheets("KU01-20")
        appExcel.Visible = True
 
 
 
 
 
        If receivedData.Contains(Cumul) Then
            If bascule = 1 Then
                Topmarquage = 1
 
                Dim MyCOMPort As SerialPort
                Dim PortName As String      'To Store the Portname of the form COMxx,eg COM31
                PortName = "COM15"
 
                MyCOMPort = New SerialPort()
 
                MyCOMPort.PortName = PortName           ' Assign the port name to the MyCOMPort object
                MyCOMPort.BaudRate = 9600           ' Assign th Baudrate to the MyCOMPort object
                MyCOMPort.Parity = Parity.None        ' Parity bits = none  
                MyCOMPort.DataBits = 8                  ' No of Data bits = 8
                MyCOMPort.StopBits = StopBits.One       ' No of Stop bits = 1
 
                MyCOMPort.Open()                       ' Open the port
                MyCOMPort.Write("B")                   ' Write an ascii "A"
                MyCOMPort.Close()
                MyCOMPort.Open()                       ' Open the port
                MyCOMPort.Write("A")                   ' Write an ascii "A"
                MyCOMPort.Close()
                bascule = 2
            End If
 
            If bascule = 2 Then
                Topmarquage = 2
 
                Dim MyCOMPort As SerialPort
                Dim PortName As String      'To Store the Portname of the form COMxx,eg COM31
                PortName = "COM15"
 
                MyCOMPort = New SerialPort()
 
                MyCOMPort.PortName = PortName           ' Assign the port name to the MyCOMPort object
                MyCOMPort.BaudRate = 9600           ' Assign th Baudrate to the MyCOMPort object
                MyCOMPort.Parity = Parity.None        ' Parity bits = none  
                MyCOMPort.DataBits = 8                  ' No of Data bits = 8
                MyCOMPort.StopBits = StopBits.One       ' No of Stop bits = 1
 
                MyCOMPort.Open()                       ' Open the port
                MyCOMPort.Write("B")                   ' Write an ascii "A"
                MyCOMPort.Close()
                MyCOMPort.Open()                       ' Open the port
                MyCOMPort.Write("A")                   ' Write an ascii "A"
                MyCOMPort.Close()
                bascule = 1
            End If
 
            Nomfil = wsExcel.Range("B" & Z1).value
            marquage1 = 45
            marquage2 = 45
            longueur = wsExcel.Range("E" & Z1).value
            longueur2 = wsExcel.Range("E" & Z1 + 1).value
            quantite = wsExcel.Range("C" & Z1).value
            batch = wsExcel.Range("D" & Z1).value
 
 
            If Z1 < 1000 Then
                If Cumul > longueur2 Then
                    Z1 += 1
                End If
                MsgBox("valeur de Z1 :" & Z1)
 
 
 
 
 
                If Topmarquage = 1 Then
 
                    Cumul = Cumul + longueur + marquage1
 
                    MsgBox("1er : " & Cumul)
                    Topmarquage = 0
                End If
 
 
                If Topmarquage = 2 Then
 
                    Cumul = Cumul + longueur2 - marquage2
                    MsgBox("2ieme : " & Cumul)
                    Topmarquage = 0
                End If
 
            End If
 
        End If
 
    End Sub
 
 
    Function ReceiveSerialData() As String
        Dim Incoming As String
        Try
            Incoming = SerialPort1.ReadExisting()
            If Incoming Is Nothing Then
                Return "nothing" & vbCrLf
            Else
                Return Incoming
            End If
        Catch ex As TimeoutException
            Return "Error: Serial Port read timed out."
        End Try
 
    End Function
J'ai essayé aussi les Thread mais avec le timer ça faisait pas bon ménage ( plantage )

Merci par avance pour votre aide
PS : j'ai commencé à programmer ya 4 jours ....