-
Notifications
You must be signed in to change notification settings - Fork 1
/
GPIBDevice_gpib488.vb
482 lines (324 loc) · 15.3 KB
/
GPIBDevice_gpib488.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
Imports System.Text
Imports System.Runtime.InteropServices
Namespace IODevices
'uses gpib488.dll used by MCC and Keithley boards
' to change the name of the dll change the line: Private Const _GPIBDll As String = "gpib488.dll"
Public Class GPIBDevice_gpib488
Inherits IODevice
Protected buffer() As Byte
Public Property BufferSize() As Integer
Get
Return buffer.Length
End Get
Set(ByVal value As Integer)
buffer = New Byte(value) {}
End Set
End Property
Public Property IOTimeoutCode() As Integer 'see GpibConst
Get
Return _timeoutcode
End Get
Set(ByVal value As Integer)
_timeoutcode = value
GpibDll.ibconfig(devid, GpibDll.GpibConst.IbcTMO, value)
End Set
End Property
Protected gpibaddress As Integer
Protected gpibboard As Integer
Protected devid As Integer 'device id as returned by ibdev
Private Shared boardinitialized() As Boolean
Private _timeoutcode As Integer = GpibDll.GpibConst.T3s
Shared Sub New()
boardinitialized = New Boolean(10) {} 'max 10 boards?
Dim i As Integer
For i = 0 To boardinitialized.Length - 1 : boardinitialized(i) = False : Next
End Sub
Sub New(ByVal name As String, ByVal addr As String)
MyBase.New(name, addr) 'init base class storing name and addr
create(name, addr, 32 * 1024)
End Sub
Sub New(ByVal name As String, ByVal addr As String, ByVal defaultbuffersize As Integer)
MyBase.New(name, addr) 'init base class storing name and addr
create(name, addr, defaultbuffersize)
End Sub
Sub create(ByVal name As String, ByVal addr As String, ByVal defaultbuffersize As Integer)
statusmsg = "trying to create device '" + name + "' at address " + addr
IODevice.ParseGpibAddr(addr, gpibboard, gpibaddress)
If Not boardinitialized(gpibboard) Then
GpibDll.SendIFC(gpibboard)
boardinitialized(gpibboard) = True
End If
'catchinterfaceexceptions = False 'set when debugging read/write routines
BufferSize = defaultbuffersize
interfacelockid = 21
interfacename = "gpib488"
statusmsg = ""
'try to create device
Try
devid = GpibDll.ibdev(gpibboard, gpibaddress, 0, _timeoutcode, 1, 0)
Dim devsta As Integer = GpibDll.ThreadIbsta()
If (devsta And GpibDll.GpibConst.EERR) <> 0 Then
Throw New Exception("cannot get device descriptor on board " & gpibboard)
End If
statusmsg = "sending clear to device " & name
GpibDll.ibclr(devid)
Catch ex As System.AccessViolationException
'in this dll happens when USB-GPIB board not connected!!! (however sendIFC has no problem!
Throw New Exception("exception thrown when trying to create device: GPIB board not connected?")
End Try
'EOI configuration
GpibDll.ibconfig(devid, GpibDll.GpibConst.IbcEOSwrt, 0)
GpibDll.ibconfig(devid, GpibDll.GpibConst.IbcEOT, 1)
AddToList()
statusmsg = ""
End Sub
Protected Overrides Sub DisposeDevice()
If devid <> 0 Then
GpibDll.ibonl(devid, 0)
End If
End Sub
Protected Overrides Function Send(ByVal cmd As String, ByRef errcode As Integer, ByRef errmsg As String) As Integer
'send cmd, return 0 if ok, 1 if timeout, other if other error
Dim retval As Integer
Dim sta As Integer
Dim err As Boolean
Dim tmo As Boolean = False
Try
retval = 0
sta = GpibDll.ibwrt(devid, cmd, Len(cmd))
err = sta And GpibDll.GpibConst.EERR
If err Then
errcode = GpibDll.ThreadIberr()
tmo = (errcode = GpibDll.GpibConst.EABO)
If tmo Then
retval = 1
errmsg = " write timeout"
Else
retval = 2
Dim s As String = GpibDll.GpibConst.errmsg(errcode)
If s <> "" Then
errmsg = "error in 'send':" + s
Else
errmsg = "error in 'send' "
End If
End If
End If
Catch ex As Exception
err = True
retval = 2
errmsg = "exception in ibwrt:\n" + ex.Message
End Try
Return retval
End Function
'--------------------------
Protected Overrides Function PollMAV(ByRef mav As Boolean, ByRef statusbyte As Byte, ByRef errcode As Integer, ByRef errmsg As String) As Integer 'poll for status, return MAV bit
'spoll, return 0 if ok, 1 if timeout, other if other error
Dim retval As Integer
Dim sta As Integer
Dim err As Boolean
Dim tmo As Boolean = False
Try 'reading
retval = 0
sta = GpibDll.ibrsp(devid, statusbyte)
err = sta And GpibDll.GpibConst.EERR
mav = statusbyte And MAVmask 'SerialPollFlags.MessageAvailable
If err Then
errcode = GpibDll.ThreadIberr()
tmo = (errcode = GpibDll.GpibConst.EABO)
If tmo Then
retval = 1
errmsg = "serial poll timeout"
Else
retval = 2
Dim s As String = GpibDll.GpibConst.errmsg(errcode)
If s <> "" Then
errmsg = "serial poll error:" + s
Else
errmsg = "serial poll error"
End If
End If
End If
Catch ex As Exception
retval = 2
errmsg = "exception in ibrsp:\n" + ex.Message
End Try
Return retval
End Function
''--------------------
Protected Overrides Function ReceiveByteArray(ByRef arr As Byte(), ByRef EOI As Boolean, ByRef errcode As Integer, ByRef errmsg As String) As Integer
Dim retval As Integer = 0
Dim err As Boolean
Dim sta As Integer
Dim cnt As Integer
Try 'reading
cnt = buffer.Length
sta = GpibDll.ibrd(devid, buffer, cnt)
err = sta And GpibDll.GpibConst.EERR
If err Then
errcode = GpibDll.ThreadIberr()
If errcode = GpibDll.GpibConst.EABO Then
retval = 1
errmsg = "receive timeout"
Else
retval = 2
Dim s As String = GpibDll.GpibConst.errmsg(errcode)
If s <> "" Then
errmsg = s
Else
errmsg = "error in 'receive' "
End If
End If
Else
cnt = GpibDll.ThreadIbcnt()
arr = New Byte(cnt - 1) {}
Array.Copy(buffer, arr, cnt)
EOI = sta And GpibDll.GpibConst.EEND
retval = 0
End If
Catch ex As Exception
retval = 2
errmsg = "exception in ibrd:\n" + ex.Message
End Try
Return retval
End Function
Protected Overrides Function ClearDevice(ByRef errcode As Integer, ByRef errmsg As String) As Integer
Dim retval As Integer = 0
Dim err As Boolean
Dim sta As Integer
Try
sta = GpibDll.ibclr(devid)
err = sta And GpibDll.GpibConst.EERR
If err Then
errcode = GpibDll.ThreadIberr()
retval = 1
Dim s As String = GpibDll.GpibConst.errmsg(errcode)
If s <> "" Then
errmsg = "error in 'cleardevice': " + s
Else
errmsg = "error in 'cleardevice' "
End If
Else
retval = 0
End If
Catch ex As Exception
retval = 1
errmsg = "exception in ibclr:\n" + ex.Message + "\n cannot clear device "
End Try
Return retval
End Function
'********************************************************************
' dll import functions
Friend Class GpibDll
Private Const _GPIBDll As String = "gpib488.dll"
Protected Friend Class GpibConst
'status constants
Public Const EERR = &H8000 ' Error detected
Public Const TIMO = &H4000 '
Public Const EEND = &H2000 ' EOI or EOS detected
'some errors
Public Const EABO As Integer = 6 'Timeout
Public Const ECIC As Integer = 1 ' Board must be CIC for this function
Public Const ENOL As Integer = 2 ' no listeners
Public Const ENEB As Integer = 7 ' Invalid board specified
'timeout option
Public Const T10ms As Integer = 7
Public Const T30ms As Integer = 8
Public Const T100ms As Integer = 9
Public Const T300ms As Integer = 10
Public Const T1s As Integer = 11
Public Const T3s As Integer = 12
Public Const T10s As Integer = 13
'eot options
Public Const NULLend As Integer = &H0
Public Const NLend As Integer = &H1
Public Const DABend As Integer = &H2
' some ibconfig() options
Public Const IbcPAD As Integer = &H1
Public Const IbcSAD As Integer = &H2
Public Const IbcTMO As Integer = &H3
Public Const IbcEOT As Integer = &H4
Public Const IbcEOSrd As Integer = &HC
Public Const IbcEOSwrt As Integer = &HD
Public Const IbcEOScmp As Integer = &HE
Public Const IbcEOSchar As Integer = &HF
Public Shared Function errmsg(ByVal errno As Integer) As String
Dim s As String = ""
Select Case errno 'most common errors
Case ECIC : s = "Board is not CIC"
Case ENOL : s = "no listeners"
Case ENEB : s = "Invalid board specified"
End Select
Return s
End Function
End Class
<DllImport(_GPIBDll, EntryPoint:="SendIFC")> _
Private Shared Sub _SendIFC(ByVal board As Integer)
End Sub
Protected Friend Shared Sub SendIFC(ByVal board As Integer)
_SendIFC(board)
End Sub
<DllImport(_GPIBDll, EntryPoint:="ibdev")> _
Private Shared Function _ibdev(ByVal ubrd As Integer, ByVal pad As Integer, ByVal sad As Integer, ByVal tmo As Integer, ByVal eot As Integer, ByVal eos As Integer) As Integer
End Function
Protected Friend Shared Function ibdev(ByVal board As Integer, ByVal pad As Integer, ByVal sad As Integer, ByVal tmo As Integer, ByVal eot As Integer, ByVal eos As Integer) As Integer
Return _ibdev(board, pad, sad, tmo, eot, eos)
End Function
<DllImport(_GPIBDll, EntryPoint:="ibonl")> _
Private Shared Function _ibonl(ByVal ud As Integer, ByVal v As Integer) As UInteger
End Function
Protected Friend Shared Function ibonl(ByVal ud As Integer, ByVal v As Integer) As UInteger
Return _ibonl(ud, v)
End Function
<DllImport(_GPIBDll, EntryPoint:="ibconfig")> _
Private Shared Function _ibconfig(ByVal ud As Integer, ByVal opt As Integer, ByVal v As Integer) As Integer
End Function
Protected Friend Shared Function ibconfig(ByVal ud As Integer, ByVal opt As Integer, ByVal v As Integer) As Integer
Return _ibconfig(ud, opt, v)
End Function
<DllImport(_GPIBDll, EntryPoint:="ibwrt")> _
Private Shared Function _ibwrt(ByVal ud As Integer, <MarshalAs(UnmanagedType.LPStr)> ByVal buf As String, ByVal count As IntPtr) As Integer
End Function
'here the IntPtr function is used to detect and adapt to the 32 vs 64 bit environment
Protected Friend Shared Function ibwrt(ByVal ud As Integer, ByVal buf As String, ByVal count As Integer) As Integer
Return _ibwrt(ud, buf, New IntPtr(count))
End Function
<DllImport(_GPIBDll, EntryPoint:="ibrd")> _
Private Shared Function _ibrd(ByVal ud As Integer, <MarshalAs(UnmanagedType.LPArray), Out()> ByVal buffer As Byte(), ByVal count As IntPtr) As Integer
End Function
Protected Friend Shared Function ibrd(ByVal ud As Integer, ByVal buffer As Byte(), ByVal count As Integer) As Integer
Return _ibrd(ud, buffer, New IntPtr(count))
End Function
<DllImport(_GPIBDll, EntryPoint:="ibclr")> _
Private Shared Function _ibclr(ByVal ud As Integer) As Integer
End Function
Protected Friend Shared Function ibclr(ByVal ud As Integer) As Integer
Return _ibclr(ud)
End Function
<DllImport(_GPIBDll, EntryPoint:="ibrsp")> _
Private Shared Function _ibrsp(ByVal ud As Integer, ByRef spr As Byte) As Integer
End Function
Protected Friend Shared Function ibrsp(ByVal ud As Integer, ByRef spr As Byte) As Integer
Return _ibrsp(ud, spr)
End Function
<DllImport(_GPIBDll, EntryPoint:="ThreadIbsta")> _
Private Shared Function _ThreadIbsta() As Integer
End Function
Protected Friend Shared Function ThreadIbsta() As Integer
Return _ThreadIbsta()
End Function
<DllImport(_GPIBDll, EntryPoint:="ThreadIberr")> _
Private Shared Function _ThreadIberr() As Integer
End Function
Protected Friend Shared Function ThreadIberr() As Integer
Return _ThreadIberr()
End Function
<DllImport(_GPIBDll, EntryPoint:="ThreadIbcnt")> _
Private Shared Function _ThreadIbcnt() As Integer
End Function
Protected Friend Shared Function ThreadIbcnt() As Integer
Return _ThreadIbcnt()
End Function
End Class
End Class
End Namespace