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

Dotnet Discussion :

RS232 (Serial) sous Access 2016 avec Class COM VB .NET


Sujet :

Dotnet

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Consultant
    Inscrit en
    Février 2017
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Consultant
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2017
    Messages : 12
    Points : 36
    Points
    36
    Par défaut RS232 (Serial) sous Access 2016 avec Class COM VB .NET
    Bonjour à tous,

    Dans le but d'alimenter une base de données Access 2016 avec deux ports RS232 (pour deux balances), j'ai créé une librairie dynamique COM Interop sous Visual Studio 2015 : (voir code ci-dessous)

    Nom : Reference.jpg
Affichages : 377
Taille : 33,3 Ko Nom : Class.jpg
Affichages : 444
Taille : 50,1 Ko

    Après avoir ajouté ma librairie dans Access sans problème et vérifié que les Objets apparaissent bien dans l'explorateur objet, j'ai déclaré deux objets dans Access :
    Public COM_BAL1 As new RS232Lib.ComRS232 avec (COM3, 9600,8,n,1)
    Public COM_BAL2 As new RS232Lib.ComRS232 avec (COM4, 115000,8,n,1)

    J'ai initié les variables sans erreur, mais lorsque je lis ou j'écris dans les ports, seul le dernier port initialisé est représenté.

    C'est comme si j'avais un seul objet ou que ma DLL ne supportait pas plus d'un objet...

    N'étant pas un spécialiste objet, ma question est pourquoi je ne peux pas déclarer plusieurs objets avec cette librairie...

    Merci d'avance de votre aide !

    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
    <ComClass(ComRS232.ClassId, ComRS232.InterfaceId, ComRS232.EventsId)> _
    Public Class ComRS232
     
    #Region "GUID COM"
        ' Ces GUID fournissent l'identité COM pour cette classe 
        ' et ses interfaces COM. Si vous les modifiez, les clients 
        ' existants ne pourront plus accéder à la classe.
        Public Const ClassId As String = "cdd3fd3e-c6a6-4987-9949-25106879e429"
        Public Const InterfaceId As String = "60ee0c79-14a4-479e-b140-8c09d42c4bf6"
        Public Const EventsId As String = "f21a9e4f-2dc9-4b63-8a43-2e1e0efe5106"
    #End Region
     
        ' Une classe COM pouvant être créée doit avoir Public Sub New() 
        ' sans paramètre, sinon, la classe ne sera pas 
        ' inscrite dans le Registre COM et ne pourra pas être créée 
        ' via CreateObject.
        Public Sub New()
            MyBase.New()
        End Sub
     
     
        Shared _continue As Boolean
        Shared _serialPort As SerialPort
        Shared _readThread As Thread
     
     
     
     
        Public Function GetPortName() As Object
            Dim ListPort() As String
            Dim i As Integer
     
            i = SerialPort.GetPortNames().GetUpperBound(0)
            ReDim ListPort(i)
     
            i = 0
            For Each s As String In SerialPort.GetPortNames()
                ListPort(i) = s
                i = i + 1
            Next
     
            Return ListPort
        End Function
     
     
        Public Sub InitPort()
            _serialPort = New SerialPort()
            _readThread = New Thread(AddressOf Read)
        End Sub
        Public Sub QuitPort()
            _serialPort.Close()
            _readThread = Nothing
            _serialPort = Nothing
        End Sub
     
        Public Sub OpenPort()
            If Not _serialPort.IsOpen Then _serialPort.Open()
        End Sub
     
        Public Sub ClosePort()
            If _serialPort.IsOpen Then _serialPort.Close()
        End Sub
     
     
        Public Function Read() As String
            Dim Message1 As String
            Message1 = ""
            Try
                Message1 = _serialPort.ReadExisting()
            Catch generatedExceptionName As TimeoutException
     
            End Try
            Return Message1
        End Function
     
     
        Public Sub Send(Inject As String)
     
            Try
                _serialPort.Write(Inject)
     
            Catch generatedExceptionName As TimeoutException
            End Try
     
     
        End Sub
     
        Public Function SetPortName(portName As String) As String
     
            Dim defaultPortName As String
     
            defaultPortName = _serialPort.PortName
     
            If portName = "" OrElse Not (portName.ToLower()).StartsWith("com") Then
                portName = defaultPortName
            Else
                _serialPort.PortName = portName
            End If
     
            Return portName
     
        End Function
     
        Public Function SetPortBaudRate(BaudRate As Integer) As Integer
            Dim defaultPortBaudRate As Integer
     
            defaultPortBaudRate = _serialPort.BaudRate
     
            If (BaudRate >= 150) And (BaudRate <= 256000) Then
                _serialPort.BaudRate = BaudRate
            End If
     
            Return _serialPort.BaudRate
     
        End Function
     
        Public Function SetPortParity(PortParity As String) As String
            Dim T_Parity As Parity
            If PortParity <> "" Then
                T_Parity = CType([Enum].Parse(GetType(Parity), PortParity, True), Parity)
                _serialPort.Parity = T_Parity
            Else
                T_Parity = _serialPort.Parity
            End If
     
            Return T_Parity.ToString
        End Function
     
        Public Function SetPortDataBits(PortDataBits As Integer) As Integer
            If (PortDataBits >= 5) And (PortDataBits <= 9) Then
                _serialPort.DataBits = PortDataBits
            End If
            Return _serialPort.DataBits
        End Function
     
        Public Function SetPortStopBits(PortStopBits As Integer) As Integer
            If (PortStopBits >= 0) And (PortStopBits <= 2) Then
                _serialPort.StopBits = PortStopBits
            End If
            Return _serialPort.StopBits
        End Function
     
        Public Function SetPortHandshake(PortHandshake As String) As String
            Dim T_Handshake As Handshake
            If PortHandshake <> "" Then
                T_Handshake = CType([Enum].Parse(GetType(Handshake), PortHandshake, True), Handshake)
                _serialPort.Handshake = T_Handshake
            End If
     
            Return _serialPort.Handshake.ToString()
        End Function
     
        Public Function SetPortWriteTimeout(WRT As Integer) As Integer
            If WRT <> 0 Then
                _serialPort.WriteTimeout = WRT
            End If
            Return _serialPort.WriteTimeout
        End Function
     
        Public Function SetPortReadTimeout(RET As Integer) As Integer
            If RET <> 0 Then
                _serialPort.ReadTimeout = RET
            End If
            Return _serialPort.ReadTimeout
        End Function
     
        Public Function SetPortBufferWriteSize(WR_BufferSize As Integer) As Integer
            If WR_BufferSize > 128 Then
                _serialPort.WriteBufferSize = WR_BufferSize
            End If
            Return _serialPort.WriteBufferSize
        End Function
     
        Public Function SetPortBufferReadSize(RD_BufferSize As Integer) As Integer
            If RD_BufferSize > 128 Then
                _serialPort.ReadBufferSize = RD_BufferSize
            End If
            Return _serialPort.ReadBufferSize
        End Function
     
    End Class

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Consultant
    Inscrit en
    Février 2017
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Consultant
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2017
    Messages : 12
    Points : 36
    Points
    36
    Par défaut Solution...
    Bien... Faute avouée est à moitié pardonnée (je l'espère !)

    Dans la DLL qui contient la Class ComRS232

    Il ne faut pas déclarer :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Shared _readThread As Thread
    Mais bien :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Public _continue As Boolean
    Public _serialPort As SerialPort
    Public _readThread As Thread
    ça résout le problème.

    En espérant que cette erreur profitera à quelqu'un. La librairie est parfaitement fonctionnelle et.. meilleure que MSCOMCTL.OCX

    Salutations à tous !



    Citation Envoyé par mitchch Voir le message
    Bonjour à tous,

    Dans le but d'alimenter une base de données Access 2016 avec deux ports RS232 (pour deux balances), j'ai créé une librairie dynamique COM Interop sous Visual Studio 2015 : (voir code ci-dessous)

    Nom : Reference.jpg
Affichages : 377
Taille : 33,3 Ko Nom : Class.jpg
Affichages : 444
Taille : 50,1 Ko

    Après avoir ajouté ma librairie dans Access sans problème et vérifié que les Objets apparaissent bien dans l'explorateur objet, j'ai déclaré deux objets dans Access :
    Public COM_BAL1 As new RS232Lib.ComRS232 avec (COM3, 9600,8,n,1)
    Public COM_BAL2 As new RS232Lib.ComRS232 avec (COM4, 115000,8,n,1)

    J'ai initié les variables sans erreur, mais lorsque je lis ou j'écris dans les ports, seul le dernier port initialisé est représenté.

    C'est comme si j'avais un seul objet ou que ma DLL ne supportait pas plus d'un objet...

    N'étant pas un spécialiste objet, ma question est pourquoi je ne peux pas déclarer plusieurs objets avec cette librairie...

    Merci d'avance de votre aide !

    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
    <ComClass(ComRS232.ClassId, ComRS232.InterfaceId, ComRS232.EventsId)> _
    Public Class ComRS232
     
    #Region "GUID COM"
        ' Ces GUID fournissent l'identité COM pour cette classe 
        ' et ses interfaces COM. Si vous les modifiez, les clients 
        ' existants ne pourront plus accéder à la classe.
        Public Const ClassId As String = "cdd3fd3e-c6a6-4987-9949-25106879e429"
        Public Const InterfaceId As String = "60ee0c79-14a4-479e-b140-8c09d42c4bf6"
        Public Const EventsId As String = "f21a9e4f-2dc9-4b63-8a43-2e1e0efe5106"
    #End Region
     
        ' Une classe COM pouvant être créée doit avoir Public Sub New() 
        ' sans paramètre, sinon, la classe ne sera pas 
        ' inscrite dans le Registre COM et ne pourra pas être créée 
        ' via CreateObject.
        Public Sub New()
            MyBase.New()
        End Sub
     
     
        Shared _continue As Boolean
        Shared _serialPort As SerialPort
        Shared _readThread As Thread
     
     
     
     
        Public Function GetPortName() As Object
            Dim ListPort() As String
            Dim i As Integer
     
            i = SerialPort.GetPortNames().GetUpperBound(0)
            ReDim ListPort(i)
     
            i = 0
            For Each s As String In SerialPort.GetPortNames()
                ListPort(i) = s
                i = i + 1
            Next
     
            Return ListPort
        End Function
     
     
        Public Sub InitPort()
            _serialPort = New SerialPort()
            _readThread = New Thread(AddressOf Read)
        End Sub
        Public Sub QuitPort()
            _serialPort.Close()
            _readThread = Nothing
            _serialPort = Nothing
        End Sub
     
        Public Sub OpenPort()
            If Not _serialPort.IsOpen Then _serialPort.Open()
        End Sub
     
        Public Sub ClosePort()
            If _serialPort.IsOpen Then _serialPort.Close()
        End Sub
     
     
        Public Function Read() As String
            Dim Message1 As String
            Message1 = ""
            Try
                Message1 = _serialPort.ReadExisting()
            Catch generatedExceptionName As TimeoutException
     
            End Try
            Return Message1
        End Function
     
     
        Public Sub Send(Inject As String)
     
            Try
                _serialPort.Write(Inject)
     
            Catch generatedExceptionName As TimeoutException
            End Try
     
     
        End Sub
     
        Public Function SetPortName(portName As String) As String
     
            Dim defaultPortName As String
     
            defaultPortName = _serialPort.PortName
     
            If portName = "" OrElse Not (portName.ToLower()).StartsWith("com") Then
                portName = defaultPortName
            Else
                _serialPort.PortName = portName
            End If
     
            Return portName
     
        End Function
     
        Public Function SetPortBaudRate(BaudRate As Integer) As Integer
            Dim defaultPortBaudRate As Integer
     
            defaultPortBaudRate = _serialPort.BaudRate
     
            If (BaudRate >= 150) And (BaudRate <= 256000) Then
                _serialPort.BaudRate = BaudRate
            End If
     
            Return _serialPort.BaudRate
     
        End Function
     
        Public Function SetPortParity(PortParity As String) As String
            Dim T_Parity As Parity
            If PortParity <> "" Then
                T_Parity = CType([Enum].Parse(GetType(Parity), PortParity, True), Parity)
                _serialPort.Parity = T_Parity
            Else
                T_Parity = _serialPort.Parity
            End If
     
            Return T_Parity.ToString
        End Function
     
        Public Function SetPortDataBits(PortDataBits As Integer) As Integer
            If (PortDataBits >= 5) And (PortDataBits <= 9) Then
                _serialPort.DataBits = PortDataBits
            End If
            Return _serialPort.DataBits
        End Function
     
        Public Function SetPortStopBits(PortStopBits As Integer) As Integer
            If (PortStopBits >= 0) And (PortStopBits <= 2) Then
                _serialPort.StopBits = PortStopBits
            End If
            Return _serialPort.StopBits
        End Function
     
        Public Function SetPortHandshake(PortHandshake As String) As String
            Dim T_Handshake As Handshake
            If PortHandshake <> "" Then
                T_Handshake = CType([Enum].Parse(GetType(Handshake), PortHandshake, True), Handshake)
                _serialPort.Handshake = T_Handshake
            End If
     
            Return _serialPort.Handshake.ToString()
        End Function
     
        Public Function SetPortWriteTimeout(WRT As Integer) As Integer
            If WRT <> 0 Then
                _serialPort.WriteTimeout = WRT
            End If
            Return _serialPort.WriteTimeout
        End Function
     
        Public Function SetPortReadTimeout(RET As Integer) As Integer
            If RET <> 0 Then
                _serialPort.ReadTimeout = RET
            End If
            Return _serialPort.ReadTimeout
        End Function
     
        Public Function SetPortBufferWriteSize(WR_BufferSize As Integer) As Integer
            If WR_BufferSize > 128 Then
                _serialPort.WriteBufferSize = WR_BufferSize
            End If
            Return _serialPort.WriteBufferSize
        End Function
     
        Public Function SetPortBufferReadSize(RD_BufferSize As Integer) As Integer
            If RD_BufferSize > 128 Then
                _serialPort.ReadBufferSize = RD_BufferSize
            End If
            Return _serialPort.ReadBufferSize
        End Function
     
    End Class

Discussions similaires

  1. activer les macros sous access 2007 avec code VBA
    Par aymane19 dans le forum VBA Access
    Réponses: 3
    Dernier message: 31/08/2012, 14h21
  2. Réponses: 2
    Dernier message: 25/07/2011, 21h50
  3. Réponses: 3
    Dernier message: 15/02/2011, 19h46
  4. Réponses: 4
    Dernier message: 22/10/2007, 12h09
  5. Probleme sous Access 2002 avec un fichier Access 2000
    Par branqueira dans le forum Access
    Réponses: 1
    Dernier message: 14/10/2005, 17h43

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