• Welcome to Forum.Deepsoftware.Com. Please login or sign up.
 

nrUSB for detecting Device add / remove?

Started by moelski, October 03, 2009, 11:02:29 am

Previous topic - Next topic

moelski

Hi Roman,

is it possible to use nrUSB to detect USB device remove and insert? I know there is WMDeviceChange but how can we use it in order to get the device details?

Let me give you an pratical example:
You have an USB/Serial converter attached to the USB port. Our application is using such converter to receive data from several devices. While the receiving in our application is active the user unplugs the converter from the serial port.
In this case it would be great to popup a window like "you removed USB device XYZ from Port...".

The other way would be great, too. If  a user connects an USB / Serial converter the user gets a popup like "you have pluged in an USB device XYZ".

Can you give us an example / tipp how to achieve this ?

Greetz Dominik

Roman Novgorodov

Hello

You should call WMDeviceChange() method in WM_DEVICECHANGE.
Unfortunately, we don't have ready sample for your task.

I think code can be something like this:

interface
...
TForm1 = class(TForm)
...
procedure WMDeviceChange(var Msg:TMessage); message WM_DEVICECHANGE;
...

implementation

procedure TForm1.WMDeviceChange(var Msg: TMessage);
begin
  nrUsb1.WMDeviceChange(Msg);  // updates (reloads) device list
  if not YourDeviceExists()  // your original routine scans device list for your device and returns result
    then ShowMessage('My Device is not found!');
end;


Roman Novgorodov
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

moelski

Hi Roman,

I used this code to get the device details:

  nrUsb1.WMDeviceChange(Msg);
  Memo1.Lines.Add('Count : ' + IntToStr(nrUSB1.DeviceCount));
  for I := 0 to nrUSB1.DeviceCount - 1 do
    Memo1.Lines.Add(Format('%2d', [I]) + ' ' + nrUSB1.Device[I].NameFriendly
                                       + ' ' + nrUSB1.Device[i].PortName
                                       + ' ' + nrUSB1.Device[i].Manufacturer)

                              
But there are only very small details for the device available. Most properties (like manufacturer) are empty strings.

What can I do in order to get the full USB device properties?
And it seems that WMDeviceChange is called svereal times while plugin / unplug a device. Do you have an idea how to handle this in a correct way?

Greetz Dominik

Roman Novgorodov

Hello

You can try something like this:

  if nrUsb1.WMDeviceChange(Msg) then begin
    Memo1.Lines.Add('Count : ' + IntToStr(nrUSB1.DeviceCount));
    for I := 0 to nrUSB1.DeviceCount - 1 do
      Memo1.Lines.Add(Format('%2d', [I]) + ' ' + nrUSB1.Device[I].NameFriendly
                                       + ' ' + nrUSB1.Device[i].PortName
                                       + ' ' + nrUSB1.Device[i].Manufacturer
                                       + ' ' + nrUsb1.Device[ i ].VendorID
                                       + ' ' + nrUsb1.Device[ i ].ProductID)
  end;


You can use VendorID and ProductID properties for identify needed device.

Roman Novgorodov
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

moelski

Hi Roman,

well this works:
  nrUsb1.WMDeviceChange(Msg);
  for I := 0 to nrUSB1.DeviceCount - 1 do begin
    nrUSB1.DeviceIndex := I;
    nrUsb1.UsbDevice.UpdateDescriptors;
    Memo1.Lines.Add(Format('%2d', [I]) + ' ' + nrUSB1.UsbDevice.NameFriendly
                                       + ' ' + nrUsb1.UsbDevice.InstanceId
                                       + ' ' + nrUSB1.UsbDevice.Manufacturer);
    Memo1.Lines.Add(IntToStr(nrusb1.UsbDevice.VendorID));
    Memo1.Lines.Add(IntToStr(nrusb1.UsbDevice.ProductID));
  end;

But why can´t I read the InstanceID / Manufacturer?

Roman Novgorodov

Hello

The Manufacturer property can be empty if something wrong in device driver. But InstanceId should have value always.
Also if nrUsb1.RequestDescriptors is True (set it in design time), you don't need to call nrUsb1.UsbDevice.UpdateDescriptors()

Roman Novgorodov
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

moelski

Hi Roman,

  nrUsb1.WMDeviceChange(Msg);
  for I := 0 to nrUSB1.DeviceCount - 1 do begin
    nrUSB1.DeviceIndex := I;
    nrUsb1.UsbDevice.UpdateDescriptors;
    Memo1.Lines.Add(Format('%2d', [I]) + ' NameFriendly : ' + nrUSB1.UsbDevice.NameFriendly
                                       + ' InstanceId : ' + nrUsb1.UsbDevice.InstanceId
                                       + ' Driver : ' + nrUSB1.UsbDevice.Driver);
  end;

I used this code. But the InstanceID is almost empty:
Quote0 NameFriendly : USB Human Interface Device InstanceId :  Driver :
1 NameFriendly : Sierra Wireless MC8755 Device InstanceId : USB\VID_1199&PID_6802\5&2C279C08&0&2 Driver :
2 NameFriendly : USB Bluetooth Driver (V2.0+EDR) InstanceId :  Driver :


Could this be a bug ?

moelski


Roman Novgorodov

Hello

Please run USBBrowser (see Start\nrComm Lib\Tools menu)
Does it show correct info for your devices?

Roman Novgorodov
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.