07-18-2011 03:32 PM - edited 07-18-2011 03:33 PM
Is there a way to use NI-VISA to detect when a USB device has been inserted or removed?
I am currently using Windows API in my LabWindows application to capture the WM_DEVICECHANGE event. When this event is called, I check to see if the USB devices are still connected to my PC by using viFindRsrc function. On Windows XP 32-bit, this approached worked successfully. On my Windows 7 64-bit system this is no longer working. After I remove the USB device, the viFindRsrc function is still indicating that the device is connected even though it isn't.
Also, when a USB device is controlled by a NI-VISA driver, I cannot capture the Windows DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events. I am able to see these events for other USB devices that are not controlled by a NI-VISA driver.
Solved! Go to Solution.
07-19-2011 06:32 PM
softengr,
In what way is the code no longer working? Errors, unexpected behavior, crashing, etc?
Have you tried using a sample piece of code like listed here on MSDN: http://msdn.microsoft.com/en-us/library/aa363215(v=VS.85).aspx ?
Regards,
Kyle Mozdzyn
Applications Engineering
National Instruments
07-25-2011 10:44 AM
Unexpected behavior: In the past (Windows XP, 32-bit OS) I was able to disconnect a USB device and then call viFindRsrc to detect that the device is no longer there. On my WIndows 7, 64-bit OS, I will disconnect a USB device and then a call to viFindRsrc says that it is still there.
Yes I tried code similar to the Windows API you referenced. I was able to catch the device arrival and device removal events for all USB devices that were not controlled by a NI-VISA driver.
I do not know why the application is behaving differently on the Windows 7 OS. Any ideas? Are my Windows 7 UAC settings interfering with NI-VISA?
07-28-2011 08:16 AM
I believe that there is a bug with NI-VISA 5.0.3 when using it with Windows 7, 64-bit OS.
For all USB devices that are NOT controlled by a NI-VISA generated driver, I am able to successfully capture the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events. For all USB devices that ARE controlled by a NI-VISA generated driver, I cannot capture the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events. I am only able to capture the DBT_DEVNODES_CHANGED event when the USB device is using the NI-VISA driver.
07-28-2011 02:13 PM - edited 07-28-2011 02:16 PM
I solved the issue.
The behavior of the NI-VISA driver requires that you register to receive Windows notifications. The other USB devices that weren't controlled by a NI-VISA driver didn't require me to register to receive the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events. I received them by default...
Here is the code to register USB events
Here is the code where I register the USB events:
.
.
.
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
intptr_t postHandle = 0;
/* Windows HWND */
HWND hWnd = 0;
long long unsigned int hWndVal = 0;
/* Get HWND of LabWindows\CVI panel */
GetPanelAttribute(panelHandle,ATTR_SYSTEM_WINDOW_HANDLE,&hWndVal);
hWnd = (HWND)hWndVal;
/*
* USB Raw Device
*
* Device Interface Class GUID = {a5dcbf10-6530-11d2-901f-00c04fb951ed}
*
*/
GUID_CLASS_USB_DEVICE.Data1 = 0xA5DCBF10L;
GUID_CLASS_USB_DEVICE.Data2 = 0x6530;
GUID_CLASS_USB_DEVICE.Data3 = 0x11D2;
GUID_CLASS_USB_DEVICE.Data4[0] = 0x90;
GUID_CLASS_USB_DEVICE.Data4[1] = 0x1F;
GUID_CLASS_USB_DEVICE.Data4[2] = 0x00;
GUID_CLASS_USB_DEVICE.Data4[3] = 0xC0;
GUID_CLASS_USB_DEVICE.Data4[4] = 0x4F;
GUID_CLASS_USB_DEVICE.Data4[5] = 0xB9;
GUID_CLASS_USB_DEVICE.Data4[6] = 0x51;
GUID_CLASS_USB_DEVICE.Data4[7] = 0xED;
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = GUID_CLASS_USB_DEVICE;
/*
* Installs a callback function for WM_DEVICECHANGE Windows message posted
* or sent to a LabWindows/CVI panel.
*/
InstallWinMsgCallback (panelHandle, WM_DEVICECHANGE,
WindowsDeviceDetection, VAL_MODE_INTERCEPT, NULL, &postHandle);
/*
* Register device notification for USB Raw Device interface class GUID
*/
RegisterDeviceNotification(hWnd, &NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
Here is the code that responds:
static int CVICALLBACK WindowsDeviceDetection (int panelHandle, int message,
unsigned int* wParam, unsigned int* lParam, void* callbackData)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
switch (message)
{
/* Received Windows WM_DEVICECHANGE message */
case WM_DEVICECHANGE:
{
switch (*wParam)
{
/*
* Received Windows device-change event "DBT_DEVNODES_CHANGED".
*
* Device has been added or removed from the system.
*/
case DBT_DEVNODES_CHANGED:
printf("[%s] DBT_DEVNODES_CHANGED\n",TimeStr());
break;
/*
* Received Windows device-change event "DBT_DEVICEARRIVAL".
*
*
*/
case DBT_DEVICEARRIVAL:
printf("[%s] DBT_DEVICEARRIVAL\n",TimeStr());
break;
/*
* Received Windows device-change event "DBT_DEVICEREMOVECOMPLETE".
*
*
*/
case DBT_DEVICEREMOVECOMPLETE:
printf("[%s] DBT_DEVICEREMOVECOMPLETE\n",TimeStr());
break;
default:
break;
}
}
}
return 0;
}
07-29-2011 02:49 PM
Hi! softengr,
Thanks for sharing. It provides much help for problems that I'm dealing now.
08-01-2011 09:05 AM
Glad that it helped ![]()
04-07-2017 09:44 AM
I am seeing this same problem with LabVIEW 2011 using NI-VISA 16.0. Is there a solution for LabVIEW to allow me to detect when a USB device is disconnected? I am using the VISA Find Resource node to detect when the device is connected but it still finds the device even after it is disconnected.
04-10-2017
10:30 AM
- last edited on
11-18-2024
04:49 PM
by
Content Cleaner
Hi Brian_L._Bowers,
Because this forum was created in 2011, it would be better to create a new forum post to get more visibility. I also noticed this is posted in the LabWindows/CVI forum, but it sounds like you are working with LabVIEW.
In the meantime, the problem might be occurring because of compatibility issues between LabVIEW 2011 and NI-VISA 16.0. I’ve attached the NI-VISA and LabVIEW Version Compatibility information below.
NI-VISA and LabVIEW Version Compatibility: https://www.ni.com/en/support/documentation/compatibility/16/ni-visa-and-labview-version-compatibili...
It looks like if you want to continue using LabVIEW 2011 the 14.0.1 NI-VISA drivers would need to be installed.
Thanks,
Shane K
Applications Engineering