Author Topic: nrUSB for detecting Device add / remove?  (Read 5307 times)

moelski

  • Full Member
  • ***
  • Posts: 122
    • View Profile
nrUSB for detecting Device add / remove?
« on: October 03, 2009, 02:02:29 pm »
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

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: nrUSB for detecting Device add / remove?
« Reply #1 on: October 04, 2009, 12:41:28 pm »
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:

Code: [Select]
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

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: nrUSB for detecting Device add / remove?
« Reply #2 on: October 04, 2009, 06:34:57 pm »
Hi Roman,

I used this code to get the device details:

Code: [Select]
  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

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: nrUSB for detecting Device add / remove?
« Reply #3 on: October 05, 2009, 03:01:22 am »
Hello

You can try something like this:

Code: [Select]
  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

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: nrUSB for detecting Device add / remove?
« Reply #4 on: October 05, 2009, 12:44:59 pm »
Hi Roman,

well this works:
Code: [Select]
  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

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: nrUSB for detecting Device add / remove?
« Reply #5 on: October 05, 2009, 03:33:04 pm »
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

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: nrUSB for detecting Device add / remove?
« Reply #6 on: October 05, 2009, 06:26:22 pm »
Hi Roman,

Code: [Select]
  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:
Quote
0 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

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: nrUSB for detecting Device add / remove?
« Reply #7 on: October 11, 2009, 05:56:56 am »
Hi Roman,

any idea about my last post ?

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: nrUSB for detecting Device add / remove?
« Reply #8 on: October 12, 2009, 03:58:20 am »
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.