-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassUDPmenu.vb
332 lines (319 loc) · 11.7 KB
/
ClassUDPmenu.vb
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
Imports System.Text
Imports System.Net
Imports System.Net.NetworkInformation
Public Class ClassUDPmenu
#Region "Variables"
Private Sck As Sockets.Socket
Private _AdresseIP As String
Private _Port As Integer
Private _open As Boolean
#End Region
#Region "Propriétés"
Property AdresseIP() As String
Get
Return _AdresseIP
End Get
Set(ByVal value As String)
_AdresseIP = value
End Set
End Property
Property Open() As Boolean
Get
Return _open
End Get
Set(ByVal value As Boolean)
_open = value
End Set
End Property
Property Port() As Integer
Get
Return _Port
End Get
Set(ByVal value As Integer)
_Port = value
End Set
End Property
#End Region
#Region "Constructeur/Destructeur"
Public Sub New(ByVal adresseip As String, ByVal port As Integer)
_CheckAdress(adresseip)
_CheckPort(port)
_AdresseIP = adresseip
_Port = port
If (Ping()) Then
_open = True
Else
_open = False
End If
End Sub
#End Region
#Region "Méthodes privées"
Private Sub _CheckAdress(ByVal Adress As String)
Dim rg As New System.Text.RegularExpressions.Regex("^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$")
If rg.IsMatch(Adress) = False Then
Throw (New Exception("Invalid adress " + Adress))
End If
End Sub
Private Sub _CheckPort(ByVal Port As Integer)
Dim rg As New System.Text.RegularExpressions.Regex("^[0-9]{1,6}$")
If rg.IsMatch(Port.ToString) = False Or Port > 65635 Then
Throw (New Exception("Invalid port " + Port.ToString))
End If
End Sub
Private Function Ping() As Boolean
Dim pingSender As Ping = New Ping()
Dim options As PingOptions = New PingOptions()
' Use the default TTL value which is 128,
options.DontFragment = True
' Create a buffer of 32 bytes of data to be transmitted.
Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Dim buffer() As Byte = Encoding.ASCII.GetBytes(data)
Dim timeout As Integer = 1000
Dim reply As PingReply = pingSender.Send(_AdresseIP, timeout, buffer, options)
If reply.Status = IPStatus.Success Then
Return True
Else
Return False
End If
End Function
#End Region
#Region "Méthodes publiques"
Public Function Connect() As Boolean
Try
Sck = New Sockets.Socket(Sockets.AddressFamily.InterNetwork, Sockets.SocketType.Dgram, Sockets.ProtocolType.Udp)
Sck.SetSocketOption(Sockets.SocketOptionLevel.Socket, Sockets.SocketOptionName.ReceiveTimeout, 10000)
Sck.SetSocketOption(Sockets.SocketOptionLevel.Socket, Sockets.SocketOptionName.SendTimeout, 10000)
Catch ex As Sockets.SocketException
MessageBox.Show(ex.Message)
End Try
End Function
Public Sub Deconnect()
Sck.Close()
_open = False
End Sub
Public Function Envoi(ByVal Commande As String) As String()
Dim retstat, i As Integer
Dim Due As DateTime
Dim retour As String() = {"", "", "", ""}
Dim test As String = ""
Dim sendBytes, rcvBytes As Byte()
i = 0
Try
Dim IP As New IPEndPoint(IPAddress.Parse(_AdresseIP), _Port)
sendBytes = Encoding.ASCII.GetBytes(Commande)
retstat = Sck.SendTo(sendBytes, 0, sendBytes.Length, Sockets.SocketFlags.None, IP)
If retstat > 0 Then
If Commande = "!0" Or Commande = "!1" Then
Due = Now.AddMilliseconds(10)
Else
Due = Now.AddMilliseconds(2500) '2.5 second time-out
End If
Do While Sck.Available = 0 AndAlso Now < Due
Loop
If Sck.Available = 0 Then
retour.SetValue("timeout", i)
Return retour
End If
Do While Sck.Available > 0
ReDim rcvBytes(Sck.Available - 1)
retstat = Sck.ReceiveFrom(rcvBytes, 0, Sck.Available, Sockets.SocketFlags.None, CType(IP, EndPoint))
test = Encoding.ASCII.GetString(rcvBytes)
retour.SetValue(test, i)
Threading.Thread.Sleep(100)
i = i + 1
Loop
Else
retour.SetValue("fail on send", i)
Return retour
End If
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return retour
End Function
Public Function RetrieveVersion(ByVal ProductId As String, ByVal VersionId As String, ByVal VersionMoreID As String, ByVal VersionNumber As String) As String
Dim Retour As String = ""
Dim Product As String = ""
Dim Version As String = ""
'--------Retrieve Product----------
Select Case ProductId
'---- Low cost Family ----
Case "022"
Product = "Microcap-2"
Version = ""
Case "023"
Product = "Microcap-S5"
Select Case VersionId
Case "220"
Version = "Ethernet"
Case Else
Version = ""
End Select
Case "024"
Product = "Microcap IP"
Version = "Captor + Hortimax version"
Case "025"
Product = "Microcap IP model 2004"
Select Case VersionId
Case "140"
Version = "Captor version"
Case "240"
Version = "Hortimax version"
Case Else
Version = ""
End Select
Case "026"
Product = "Microcap IP model 2007"
Select Case VersionId
Case "180"
Version = "Captor version"
Case "280"
Version = "Hortimax version"
Case Else
Version = ""
End Select
Case "092"
Product = "KLM MII terminal"
Version = "Standard firmware Capnet"
Case "411"
Product = "Boxio-32/8"
Version = ""
' ---- Consul Family ----
Case "811"
Product = "Consul Standard"
Version = ""
Case "812"
Product = "Consul Attend"
Version = ""
Case "813"
Product = "Consul Captalk"
Select Case VersionId
Case "520"
Version = "2 line LCD"
Case "540"
Version = "4 line LCD"
Case Else
Version = ""
End Select
Case "831"
Product = "Consul Entry"
Select Case VersionId
Case "120"
Version = "2 line LCD"
Case "140"
Version = "4 line LCD"
Case "220"
Version = "Slave firmware"
Case Else
Version = ""
End Select
' ---- Omni Family ----
Case "851"
Product = "Omni Cadet (MI)"
Version = ""
Case "852"
Product = "Omni (MI)"
Select Case VersionId
Case "140"
Version = "before version 3.4e"
Case "240"
Version = "Dallas socket"
Case "340"
Version = "Marin chip"
Case Else
Version = ""
End Select
Case "861"
Product = "Omni Cadet (MII)"
Select Case VersionId
Case "280"
Version = "Cadet fingerprint identify"
Case Else
Version = ""
End Select
Case "862"
Product = "Omni (MII)"
Select Case VersionId
Case "280"
Version = "Attend fingerprint identify"
Case Else
Version = ""
End Select
Case "871"
Product = "Omni Cadet (MIII)"
Version = ""
Case "872"
Product = "Omni (MIII)"
Version = ""
Case "931"
Product = "Omni String terminal"
Select Case VersionId
Case "120"
Version = "2 line LCD"
Case "140"
Version = "4 line LCD"
Case Else
Version = ""
End Select
Case "932"
Product = "Omni String reader"
Version = ""
Case "941"
Product = "Omni Access reader"
Version = "Motorized card reader"
Case "942"
Product = "Omni Access reader"
Version = "eID reader"
' ---- UBOX & CMAS Family ----
Case "681"
Product = "UBOX model 2004"
Version = ""
Case "691"
Product = "CMAS slave"
Select Case VersionId
Case "100"
Version = "CMAS-UU"
Case "200"
Version = "CMAS-AT_SU (AT slave)"
Case "300"
Version = "CMAS-IU_SU (IU slave)"
Case Else
Version = ""
End Select
Case "692"
Product = "CMAS master"
Select Case VersionId
Case "100"
Version = "ATTEND firmware"
Case "200"
Version = "IU firmware"
Case Else
Version = ""
End Select
Case "693"
Product = "CMAS IO"
Select Case VersionId
Case "200"
Version = "CMAS-AT_IO (AT slave)"
Case "300"
Version = "CMAS-IU_IO (IU slave)"
Case Else
Version = ""
End Select
Case Else
Product = "Unknow Product"
End Select
'--------Retrieve Version----------
If String.Compare(VersionMoreID, "0000") = 0 Then
Version = Version + " version " + VersionNumber
ElseIf VersionMoreID.StartsWith("a") Then
Version = Version + " Attend " + VersionMoreID.Substring(1)
ElseIf VersionMoreID.StartsWith("c") Then
Version = Version + " Clock " + VersionMoreID.Substring(1)
Else
Version = Version + " " + VersionMoreID
End If
Return (Product + " " + Version)
End Function
#End Region
End Class