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)
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 !
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 |
Partager