如何检测USB设备何时连接/断开?

发布于 2024-12-26 07:21:55 字数 147 浏览 2 评论 0原文

我对使用 Delphi 使用 USB 设备感到不舒服,并且对编写设备驱动程序的细节几乎一无所知(尽管我在学习使用 GoASM 进行汇编时遇到过一些)。

该设备可以是 USB 调制解调器或 USB 打印机。

我需要的是一个方向和解决该主题的示例代码。

I don't feel comfortable harnessing USB device with Delphi and have a next to nothing knowledge of the details of writing device driver (though I've come accross some when learning assembly with GoASM).

The device could be either an usb modem or an usb printer.

What I need is a direction to go and sample code adressing the topic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2025-01-02 07:21:55

这是取自 检测 USB 设备是否已连接

 unit U_Usb;

 interface

uses
Windows, Messages, SysUtils, Classes, Forms;

type

PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
DEV_BROADCAST_HDR = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;

PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;
 DEV_BROADCAST_DEVICEINTERFACE = record
 dbcc_size: DWORD;
 dbcc_devicetype: DWORD;
 dbcc_reserved: DWORD;
  dbcc_classguid: TGUID;
  dbcc_name: short;
 end;

const
  GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
  DBT_DEVICEARRIVAL = $8000; // system detected a new device
  DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone
  DBT_DEVTYP_DEVICEINTERFACE = $00000005; // device interface class

type

 TComponentUSB = class(TComponent)
   private
   FWindowHandle: HWND;
   FOnUSBArrival: TNotifyEvent;
   FOnUSBRemove: TNotifyEvent;
   procedure WndProc(var Msg: TMessage);
   function USBRegister: Boolean;
   protected
   procedure WMDeviceChange(var Msg: TMessage); dynamic;
  public
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
  published
    property OnUSBArrival: TNotifyEvent read FOnUSBArrival write FOnUSBArrival;
    property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;
  end;

  implementation

constructor TComponentUSB.Create(AOwner: TComponent);
   begin
     inherited Create(AOwner);
     FWindowHandle := AllocateHWnd(WndProc);
     USBRegister;
   end;

   destructor TComponentUSB.Destroy;
    begin
   DeallocateHWnd(FWindowHandle);
    inherited Destroy;
   end;

 procedure TComponentUSB.WndProc(var Msg: TMessage);
     begin
     if (Msg.Msg = WM_DEVICECHANGE) then
      begin
      try
      WMDeviceChange(Msg);
    except
  Application.HandleException(Self);
   end;
   end
  else
     Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
   end;

  procedure TComponentUSB.WMDeviceChange(var Msg: TMessage);
   var
   devType: Integer;
     Datos: PDevBroadcastHdr;
   begin
    if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then
    begin
  Datos := PDevBroadcastHdr(Msg.lParam);
    devType := Datos^.dbch_devicetype;
     if devType = DBT_DEVTYP_DEVICEINTERFACE then
    begin // USB Device
     if Msg.wParam = DBT_DEVICEARRIVAL then
     begin
      if Assigned(FOnUSBArrival) then
       FOnUSBArrival(Self);
       end
        else
      begin
     if Assigned(FOnUSBRemove) then
      FOnUSBRemove(Self);
   end;
  end;
   end;
  end;

  function TComponentUSB.USBRegister: Boolean;
    var
      dbi: DEV_BROADCAST_DEVICEINTERFACE;
       Size: Integer;
     r: Pointer;
      begin
     Result := False;
     Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);
      ZeroMemory(@dbi, Size);
      dbi.dbcc_size := Size;
      dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
     dbi.dbcc_reserved := 0;
     dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;
     dbi.dbcc_name := 0;

     r := RegisterDeviceNotification(FWindowHandle, @dbi,
      DEVICE_NOTIFY_WINDOW_HANDLE
     );
      if Assigned(r) then Result := True;
    end;

    end. 

This was taken from detect if usb device is connected

 unit U_Usb;

 interface

uses
Windows, Messages, SysUtils, Classes, Forms;

type

PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
DEV_BROADCAST_HDR = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;

PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;
 DEV_BROADCAST_DEVICEINTERFACE = record
 dbcc_size: DWORD;
 dbcc_devicetype: DWORD;
 dbcc_reserved: DWORD;
  dbcc_classguid: TGUID;
  dbcc_name: short;
 end;

const
  GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
  DBT_DEVICEARRIVAL = $8000; // system detected a new device
  DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone
  DBT_DEVTYP_DEVICEINTERFACE = $00000005; // device interface class

type

 TComponentUSB = class(TComponent)
   private
   FWindowHandle: HWND;
   FOnUSBArrival: TNotifyEvent;
   FOnUSBRemove: TNotifyEvent;
   procedure WndProc(var Msg: TMessage);
   function USBRegister: Boolean;
   protected
   procedure WMDeviceChange(var Msg: TMessage); dynamic;
  public
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
  published
    property OnUSBArrival: TNotifyEvent read FOnUSBArrival write FOnUSBArrival;
    property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;
  end;

  implementation

constructor TComponentUSB.Create(AOwner: TComponent);
   begin
     inherited Create(AOwner);
     FWindowHandle := AllocateHWnd(WndProc);
     USBRegister;
   end;

   destructor TComponentUSB.Destroy;
    begin
   DeallocateHWnd(FWindowHandle);
    inherited Destroy;
   end;

 procedure TComponentUSB.WndProc(var Msg: TMessage);
     begin
     if (Msg.Msg = WM_DEVICECHANGE) then
      begin
      try
      WMDeviceChange(Msg);
    except
  Application.HandleException(Self);
   end;
   end
  else
     Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
   end;

  procedure TComponentUSB.WMDeviceChange(var Msg: TMessage);
   var
   devType: Integer;
     Datos: PDevBroadcastHdr;
   begin
    if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then
    begin
  Datos := PDevBroadcastHdr(Msg.lParam);
    devType := Datos^.dbch_devicetype;
     if devType = DBT_DEVTYP_DEVICEINTERFACE then
    begin // USB Device
     if Msg.wParam = DBT_DEVICEARRIVAL then
     begin
      if Assigned(FOnUSBArrival) then
       FOnUSBArrival(Self);
       end
        else
      begin
     if Assigned(FOnUSBRemove) then
      FOnUSBRemove(Self);
   end;
  end;
   end;
  end;

  function TComponentUSB.USBRegister: Boolean;
    var
      dbi: DEV_BROADCAST_DEVICEINTERFACE;
       Size: Integer;
     r: Pointer;
      begin
     Result := False;
     Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);
      ZeroMemory(@dbi, Size);
      dbi.dbcc_size := Size;
      dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
     dbi.dbcc_reserved := 0;
     dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;
     dbi.dbcc_name := 0;

     r := RegisterDeviceNotification(FWindowHandle, @dbi,
      DEVICE_NOTIFY_WINDOW_HANDLE
     );
      if Assigned(r) then Result := True;
    end;

    end. 
暮年 2025-01-02 07:21:55

您可以使用一些shell 脚本来执行此操作。
当设备连接时,命令 lsusb 显示此信息,并在设备断开连接后从列表中删除。
因此,在 shell 脚本中,您需要找到旧列表和当前列表之间的差异。
最后,您需要将此脚本作为 cron 作业 运行,它会定期检查。

问候,
巴伦·帕里查

You can use some shell script to do so.
when device is connected command lsusb shows this and removes from list after the device gets disconnected.
So in your shell script you need to find the difference between old list and current list.
Finally you need to run this script as a cron job, which checks in frequent interval.

Regards,
Barun Parichha

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文