-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGPIBDevice_ADLink.vb
649 lines (457 loc) · 22 KB
/
GPIBDevice_ADLink.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
Imports System
Imports System.Data
Imports System.Diagnostics
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Threading
Namespace IODevices
'uses gpib-32.dll from ADLINK (but call syntax compatible with the NI dll of the same name: can use this library for NI boards too)
'this name conflicts with old versions of dlls by NI, Keithley or MCC
' to change the name of the dll change the line: Private Const _GPIBDll As String = "gpib-32.dll"
Public Class GPIBDevice_ADLink
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 Byte
Protected gpibboard As Integer
'device id as returned by ibdev
Protected devid As Integer = 0
Private _timeoutcode As Integer = GpibDll.GpibConst.T3s ' default value
'variables and code used by notify
Private Shared boardud As Integer()
'board unit descriptors for all boards
Public Shared delaynotify As Integer = 5
'delay before rearming
Private Shared notifylist As List(Of GPIBDevice_ADLink)
Private Shared locklist As New Object()
'list of devices to notify
Private Shared notifymask As Integer()
' masks for each board
Private Shared userdata As Integer
'not used
Private Shared cbdelegate As GpibDll.NotifyCallback
Private _enableNotify As Boolean = False
Public Overrides Property EnableNotify() As Boolean
'for pending notify events
Get
Return _enableNotify
End Get
Set(ByVal value As Boolean)
If Not _enableNotify AndAlso value Then
SyncLock locklist
If notifycount(gpibboard) = 0 Then
notifymask(gpibboard) = GpibDll.GpibConst.SRQI
GpibDll.ibnotify(boardud(gpibboard), GpibDll.GpibConst.SRQI, cbdelegate, userdata)
End If
notifylist.Add(Me)
End SyncLock
_enableNotify = True
End If
If _enableNotify AndAlso Not value Then
SyncLock locklist
notifylist.Remove(Me)
If notifycount(gpibboard) = 0 Then
notifymask(gpibboard) = 0 'let pending notify events finish
Thread.Sleep(delaynotify)
GpibDll.ibnotify(boardud(gpibboard), 0, cbdelegate, userdata)
End If
End SyncLock
_enableNotify = False
End If
End Set
End Property
Private Shared Function notifycount(ByVal boardnum As Integer) As Integer
Dim count As Integer = 0
For Each device As GPIBDevice_ADLink In notifylist
If boardnum = device.gpibboard Then
count += 1
End If
Next
Return count
End Function
'notify callback: public delegate uint NotifyCallback(int ud, int ibsta, int iberr, int ibcnt, [MarshalAs(UnmanagedType.AsAny)] object RefData);//refdata not used here
Public Shared Function cbnotify(ByVal ud As Integer, ByVal ibsta As Integer, ByVal iberr As Integer, ByVal ibcnt As Integer, ByRef RefData As Integer) As Integer
'refdata not used here
Dim board As Integer = 0
Dim retval As Integer
SyncLock locklist
For Each device As GPIBDevice_ADLink In notifylist
If ud = boardud(device.gpibboard) Then
device.WakeUp() 'interrupt waiting for next read/poll trial
board = device.gpibboard 'identify board n°
End If
Next
retval = notifymask(board)
End SyncLock
If retval <> 0 Then Thread.Sleep(delaynotify) 'delay before rearming
Return retval 'return mask to rearm for next notify (or 0 if disabling : safer if notify disabled while there are pending events)
End Function
'static constructor
Shared Sub New()
Const maxboards As Integer = 10
boardud = New Integer(maxboards - 1) {}
notifymask = New Integer(maxboards - 1) {}
Dim i As Integer = 0
For i = 0 To boardud.Length - 1
boardud(i) = 0
notifymask(i) = 0
Next
notifylist = New List(Of GPIBDevice_ADLink)()
cbdelegate = DirectCast(AddressOf cbnotify, GpibDll.NotifyCallback)
End Sub
Public 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
Public 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
'common part of constructor
Private Sub create(ByVal name As String, ByVal addr As String, ByVal defaultbuffersize As Integer)
IODevice.ParseGpibAddr(addr, gpibboard, gpibaddress)
Try
If boardud(gpibboard) <= 0 Then
Dim boardname As String = "GPIB" & gpibboard.ToString().Trim()
statusmsg = "trying to initialize " & boardname
Dim bud As Integer = GpibDll.ibfind(boardname)
If bud <> -1 Then
boardud(gpibboard) = bud
Else
Throw New Exception("cannot find GPIB board n°" & gpibboard)
End If
'GpibDll.SendIFC(gpibboard); //some devices don't like it...
GpibDll.ibconfig(boardud(gpibboard), GpibDll.GpibConst.IbcAUTOPOLL, 0) ' disable autopolling
End If
'catchinterfaceexceptions = False 'set when debugging read/write routines
BufferSize = defaultbuffersize
interfacelockid = 20
interfacename = "ADLink"
'try to create device
statusmsg = "trying to create device '" & name & "' at address " & addr
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 may happen 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
Dim sta As Integer = 0
sta = GpibDll.ibconfig(devid, GpibDll.GpibConst.IbcEOSwrt, 0)
sta = GpibDll.ibconfig(devid, GpibDll.GpibConst.IbcEOT, 1)
AddToList()
statusmsg = ""
End Sub
Protected Overrides Sub DisposeDevice()
If devid <> 0 Then
EnableNotify = False
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 = 0
Dim sta As Integer = 0
Dim err As Boolean = False
Dim tmo As Boolean = False
Try
retval = 0
sta = GpibDll.ibwrt(devid, cmd, cmd.Length)
err = (sta And GpibDll.GpibConst.EERR) <> 0
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 Not String.IsNullOrEmpty(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 = 0
Dim sta As Integer = 0
Dim err As Boolean = False
Dim tmo As Boolean = False
'reading
Try
retval = 0
sta = GpibDll.ibrsp(devid, statusbyte)
err = (sta And GpibDll.GpibConst.EERR) <> 0
mav = (statusbyte And MAVmask) <> 0
'status=1 tmo on send, =3 tmo on rcv, =4 other err on send, =6 other err on rcv
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 Not String.IsNullOrEmpty(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 = 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 = False
Dim sta As Integer = 0
Dim cnt As Integer = 0
'reading
Try
cnt = buffer.Length
sta = GpibDll.ibrd(devid, buffer, cnt)
err = (sta And GpibDll.GpibConst.EERR) <> 0
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 Not String.IsNullOrEmpty(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) <> 0
retval = 0
End If
Catch ex As Exception
retval = 2
errmsg = 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 = False
Dim sta As Integer = 0
Try
sta = GpibDll.ibclr(devid)
err = (sta And GpibDll.GpibConst.EERR) <> 0
If err Then
errcode = GpibDll.ThreadIberr()
retval = 1
Dim s As String = GpibDll.GpibConst.errmsg(errcode)
If Not String.IsNullOrEmpty(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 = ex.Message & vbLf & " cannot clear device "
End Try
Return retval
End Function
'********************************************************************
' dll import functions
Friend Class GpibDll
Private Const _GPIBDll As String = "gpib-32.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 EADR As Integer = 3 ' Board not addressed correctly
Public Const ENEB As Integer = 7 ' Invalid board specified
Public Const EBUS As Integer = 14 ' Command error on bus
'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 IbcPPC As Integer = &H5
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 Const IbcAUTOPOLL As Integer = &H7
Public Const SRQI As Integer = &H1000'mask for SRQ board level notify
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"
Exit Select
Case ENOL
s = "no listeners"
Exit Select
Case ENEB
s = "Invalid board specified"
Exit Select
Case EADR
s = "Board not addressed correctly"
Exit Select
Case EBUS
s = "Command error on bus"
Exit Select
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:="ibfind")> _
Private Shared Function _ibfind(<MarshalAs(UnmanagedType.LPStr)> ByVal name As String) As Integer
End Function
Protected Friend Shared Function ibfind(ByVal name As String) As Integer
Return _ibfind(name)
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 Integer) As Integer
End Function
Protected Friend Shared Function ibwrt(ByVal ud As Integer, ByVal buf As String, ByVal count As Integer) As Integer
Return _ibwrt(ud, buf, 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 Integer) 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, 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
' notify event handler functions
' C prototype for callback handler:
'int __stdcall Callback (int ud,int ibsta,int iberr,long ibcntl,void * RefData)
<UnmanagedFunctionPointer(CallingConvention.StdCall)> _
Public Delegate Function NotifyCallback(ByVal ud As Integer, ByVal ibsta As Integer, ByVal iberr As Integer, ByVal ibcntl As Integer, ByRef RefData As Integer) As Integer
'refdata not used here
'uint ibnotify (int ud,int mask,GpibNotifyCallback_t Callback,void * RefData)
<DllImport(_GPIBDll, EntryPoint:="ibnotify")> _
Public Shared Function ibnotify(ByVal ud As Integer, ByVal mask As Integer, <MarshalAs(UnmanagedType.FunctionPtr)> ByVal callback As NotifyCallback, ByRef RefData As Integer) As UInteger
End Function
End Class
End Class
End Namespace