From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Using NI-VISA to detect USB insertion/removal

Solved!
Go to solution

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.

0 Kudos
Message 1 of 9
(6,031 Views)

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

Regards,

Kyle M.
Applications Engineering
National Instruments
0 Kudos
Message 2 of 9
(6,011 Views)

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?

0 Kudos
Message 3 of 9
(5,991 Views)

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.

 

 

0 Kudos
Message 4 of 9
(5,969 Views)
Solution
Accepted by topic author softengr

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;
}

 

 

0 Kudos
Message 5 of 9
(5,954 Views)

Hi! softengr,

 

Thanks for sharing.  It provides much help for problems that I'm dealing now.

0 Kudos
Message 6 of 9
(5,939 Views)

Glad that it helped Smiley Happy

0 Kudos
Message 7 of 9
(5,923 Views)

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.

0 Kudos
Message 8 of 9
(3,570 Views)

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: http://www.ni.com/product-documentation/53413/en/

 

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

0 Kudos
Message 9 of 9
(3,546 Views)