IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

TIMER et objet EXCEL


Sujet :

VB.NET

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Directeur technique
    Inscrit en
    Août 2018
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Directeur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2018
    Messages : 1
    Points : 1
    Points
    1
    Par défaut TIMER et objet EXCEL
    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 ....

  2. #2
    Membre à l'essai
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2018
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure et Loir (Centre)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2018
    Messages : 33
    Points : 21
    Points
    21
    Par défaut
    Bonjour!

    Une idée (elle vaut ce qu'elle vaut, je ne l'ai pas testée) mais si on garde ton idée de Timer.

    Si en tout début de code tu déclares une variable, genre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Dim CompteurTimer as Integer = 0
    Si du coup tu fais ça avec ton code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
     
    If CompteurTimer=0 then
    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
    CompteurTimer=CompteurTimer + 1
    End if



    Ensuite, lorsque tu as fini de recevoir tes données, tu remets ton compteur à 0 (au moment ou tu sauves et fermes ton classeur par exemple)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     
    CompteurTimer=0
    Logiquement, il ne te lancerait ce qu'il y a dans ton timer qu'une fois non?

    C'est juste une idée comme ça

    Bon codage!

  3. #3
    Membre à l'essai
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2018
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure et Loir (Centre)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2018
    Messages : 33
    Points : 21
    Points
    21
    Par défaut
    Bonjour! Je me demandais si ma piste t'avais aidée à résoudre ton problème! Bonne fin de soirée!

  4. #4
    Membre expérimenté
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2005
    Messages
    562
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Distribution

    Informations forums :
    Inscription : Juillet 2005
    Messages : 562
    Points : 1 511
    Points
    1 511
    Par défaut
    La solution choisis, Interop Excel, impose d'avoir excel sur la machine, et la présence d'une instance excel lancée.
    Tu pourrais te passer de toutes ces contraintes en ne passant pas par excel, mais par une bibliothèque tiers, dont la meilleure est sans doute EEPlus .
    Tu pourras ainsi lire des .xlsx (ou autre .xlsm ...) ou les créer. A partir du moment ou tu n'as pas besoin de l'interface d'excel, EEplus est sans doute la meilleure solution.

    J@ck.
    Pas de réponse par MP, merci.

    Penser au ça fait plaisir

Discussions similaires

  1. [VBA-W]Atteindre une feuille d'un objet excel dans word
    Par tazamorte dans le forum VBA Word
    Réponses: 1
    Dernier message: 04/06/2007, 18h55
  2. Objet Excel PIA
    Par devicomProg dans le forum Delphi .NET
    Réponses: 2
    Dernier message: 01/05/2007, 14h31
  3. Pb de gestion des objets Excel
    Par tedparker dans le forum Access
    Réponses: 2
    Dernier message: 11/09/2006, 17h13
  4. Réponses: 5
    Dernier message: 08/01/2006, 23h42
  5. instancier un objet Excel
    Par RobinNono dans le forum ASP
    Réponses: 1
    Dernier message: 13/12/2005, 15h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo