K8061 in Excel VBA CloseDevices crash

Hey there,

Just getting K8061 up and running in 64-bit Excel, I’ve noticed the CloseDevices function stops the card communicating but I’m unable to reconnect to the card after using OpenDevice(), I just get a -1 returned, but then I can’t doing anything else with the card until I either unplug/plug back in or quit the Excel sheet and start again. I can’t do attachments but the declarations part of the code I’m using is shown below, which hopefully will work in both 32-bit and 64-bit Offices. The remaining code is just a row of buttons testing each function. Anyone else had a similar issue?

Option Explicit
#If Win64 Then
Public Declare PtrSafe Function OpenDevice Lib “k8061.dll” () As Long
Public Declare PtrSafe Sub CloseDevices Lib “k8061.dll” ()
Public Declare PtrSafe Function ReadAnalogChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long) As Long
Public Declare PtrSafe Function PowerGood Lib “k8061.dll” (ByVal CardAddress As Long) As Boolean
Public Declare PtrSafe Function Connected Lib “k8061.dll” (ByVal CardAddress As Long) As Boolean
Public Declare PtrSafe Sub ReadVersion Lib “k8061.dll” (ByVal CardAddress As Long, ByRef Buffer As Integer)
Public Declare PtrSafe Sub ReadAllAnalog Lib “k8061.dll” (ByVal CardAddress As Long, Buffer As Long)
Public Declare PtrSafe Sub OutputAnalogChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long, ByVal data As Long)
Public Declare PtrSafe Sub OutputAllAnalog Lib “k8061.dll” (ByVal CardAddress As Long, Buffer As Long)
Public Declare PtrSafe Sub ClearAnalogChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long)
Public Declare PtrSafe Sub SetAllAnalog Lib “k8061.dll” (ByVal CardAddress As Long)
Public Declare PtrSafe Sub ClearAllAnalog Lib “k8061.dll” (ByVal CardAddress As Long)
Public Declare PtrSafe Sub SetAnalogChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long)
Public Declare PtrSafe Sub OutputAllDigital Lib “k8061.dll” (ByVal CardAddress As Long, ByVal data As Long)
Public Declare PtrSafe Sub ClearDigitalChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long)
Public Declare PtrSafe Sub ClearAllDigital Lib “k8061.dll” (ByVal CardAddress As Long)
Public Declare PtrSafe Sub SetDigitalChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long)
Public Declare PtrSafe Sub SetAllDigital Lib “k8061.dll” (ByVal CardAddress As Long)
Public Declare PtrSafe Function ReadDigitalChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long) As Boolean
Public Declare PtrSafe Function ReadAllDigital Lib “k8061.dll” (ByVal CardAddress As Long) As Long
Public Declare PtrSafe Sub OutputPWM Lib “k8061.dll” (ByVal CardAddress As Long, ByVal data As Long)

Public Declare PtrSafe Function SetTimer Lib “user32” ( _
ByVal HWnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As Long
Public Declare PtrSafe Function KillTimer Lib “user32” ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long

#Else
Public Declare Function OpenDevice Lib “k8061.dll” () As Long
Public Declare Sub CloseDevices Lib “k8061.dll” ()
Public Declare Function ReadAnalogChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long) As Long
Public Declare Function PowerGood Lib “k8061.dll” (ByVal CardAddress As Long) As Boolean
Public Declare Function Connected Lib “k8061.dll” (ByVal CardAddress As Long) As Boolean
Public Declare Sub ReadVersion Lib “k8061.dll” (ByVal CardAddress As Long, Buffer As Long)
Public Declare Sub ReadAllAnalog Lib “k8061.dll” (ByVal CardAddress As Long, Buffer As Long)
Public Declare Sub OutputAnalogChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long, ByVal Data As Long)
Public Declare Sub OutputAllAnalog Lib “k8061.dll” (ByVal CardAddress As Long, Buffer As Long)
Public Declare Sub ClearAnalogChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long)
Public Declare Sub SetAllAnalog Lib “k8061.dll” (ByVal CardAddress As Long)
Public Declare Sub ClearAllAnalog Lib “k8061.dll” (ByVal CardAddress As Long)
Public Declare Sub SetAnalogChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long)
Public Declare Sub OutputAllDigital Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Data As Long)
Public Declare Sub ClearDigitalChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long)
Public Declare Sub ClearAllDigital Lib “k8061.dll” (ByVal CardAddress As Long)
Public Declare Sub SetDigitalChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long)
Public Declare Sub SetAllDigital Lib “k8061.dll” (ByVal CardAddress As Long)
Public Declare Function ReadDigitalChannel Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Channel As Long) As Boolean
Public Declare Function ReadAllDigital Lib “k8061.dll” (ByVal CardAddress As Long) As Long
Public Declare Sub OutputPWM Lib “k8061.dll” (ByVal CardAddress As Long, ByVal Data As Long)
Public Declare Function SetTimer Lib “user32” ( _
ByVal HWnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib “user32” ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long

#End If

UPDATE Sorry it seems I can in fact reconnect to the card despite it still returning a -1 rather than the card address after calling CloseDevices. I’m not sure if that’s intentional but it means I can work around it.

Thank you!

Thank you for the feedback.
Normally the CloseDevices is the last action of the application program before termination.
If the OpenDevice() is used after CloseDevices, the DLL still “remembers” the opened cards and if all cards were “opened” it returns -1. So you can continue the use of the earlier opened cards without individually “opening” them again.