⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbtest.dpr

📁 AVR模拟USB上位机控制代码,DELPHI
💻 DPR
字号:
(* LIBUSB-WIN32, Generic Windows USB Library
 * Copyright (c) 2002-2004 Stephan Meyer <ste_meyer@web.de>
 * Copyright (c) 2000-2004 Johannes Erdfelt <johannes@erdfelt.com>
 *
 * LIBUSB-WIN32 translation
 * Copyright (c) 2004 Yvo Nelemans <ynlmns@xs4all.nl>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
program USBTest;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  LibUSB in 'LibUSB.pas';


procedure usb_showinfo;

  procedure print_endpoint(endpoint: usb_endpoint_descriptor);
  begin
    writeln('      bEndpointAddress: ', IntToHex(endpoint.bEndpointAddress,2), 'h');
    writeln('      bmAttributes:     ', IntToHex(endpoint.bmAttributes, 2), 'h');
    writeln('      wMaxPacketSize:   ', endpoint.wMaxPacketSize);
    writeln('      bInterval:        ', endpoint.bInterval);
    writeln('      bRefresh:         ', endpoint.bRefresh);
    writeln('      bSynchAddress:    ', endpoint.bSynchAddress);
  end;

  procedure print_altsetting(iinterface: usb_interface_descriptor);
  var
    I: integer;
  begin
    writeln('    bInterfaceNumber:   ', iinterface.bInterfaceNumber);
    writeln('    bAlternateSetting:  ', iinterface.bAlternateSetting);
    writeln('    bNumEndpoints:      ', iinterface.bNumEndpoints);
    writeln('    bInterfaceClass:    ', iinterface.bInterfaceClass);
    writeln('    bInterfaceSubClass: ', iinterface.bInterfaceSubClass);
    writeln('    bInterfaceProtocol: ', iinterface.bInterfaceProtocol);
    writeln('    iInterface:         ', iinterface.iInterface);

    for I := 0 to iinterface.bNumEndpoints-1 do
      begin
        print_endpoint(iinterface.endpoint[I]);
      end;
  end;

  procedure print_interface(iinterface: usb_interface);
  var
    I: integer;
  begin
    for I := 0 to iinterface.num_altsetting-1 do
      begin
        print_altsetting(iinterface.altsetting[I]);
      end;
  end;

  procedure print_configuration(config: usb_config_descriptor);
  var
    I: integer;
  begin
    writeln('  wTotalLength:         ', config.wTotalLength);
    writeln('  bNumInterfaces:       ', config.bNumInterfaces);
    writeln('  bConfigurationValue:  ', config.bConfigurationValue);
    writeln('  iConfiguration:       ', config.iConfiguration);
    writeln('  bmAttributes:         ', IntToHex(config.bmAttributes, 2), 'h');
    writeln('  MaxPower:             ', config.MaxPower);

  for I := 0 to config.bNumInterfaces-1 do
    begin
      print_interface(config.iinterface[I]);
    end;
  end;

var
  bus: pusb_bus;
  dev: pusb_device;
  udev: pusb_dev_handle;
  ret,
  I: integer;
  S: array [0..255] of char;
begin
  usb_init; // Initialize libusb


  usb_find_busses; // Finds all USB busses on system
  usb_find_devices; // Find all devices on all USB devices
  bus := usb_get_busses; // Return the list of USB busses found

  Writeln('bus/device  idVendor/idProduct');
  while Assigned(bus) do
    begin
      dev := bus^.devices;
      while Assigned(dev) do
        begin
          writeln(bus^.dirname, '/', dev^.filename,
                  '     ',
                  '0x' + IntToHex(dev^.descriptor.idVendor, 4),
                  '/',
                  '0x' + IntToHex(dev^.descriptor.idProduct, 4));


          udev := usb_open(dev);
          if Assigned(udev) then
            begin
              if dev^.descriptor.iManufacturer > 0 then
                begin
                  ret := usb_get_string_simple(udev, dev^.descriptor.iManufacturer, S, sizeof(S));
                  if (ret > 0) then
                    begin
                      writeln('- Manufacturer : ', S);
                    end
                    else
                    begin
                      writeln('- Unable to fetch manufacturer string');
                    end;
                end;

              if (dev^.descriptor.iProduct > 0) then
                begin
                  ret := usb_get_string_simple(udev, dev^.descriptor.iProduct, S, sizeof(S));
                  if (ret > 0) then
                    begin
                      writeln('- Product      : ', S);
                    end
                    else
                    begin
                      writeln('- Unable to fetch product string');
                    end;
                end;

              if (dev^.descriptor.iSerialNumber > 0) then
                begin
                  ret := usb_get_string_simple(udev, dev^.descriptor.iSerialNumber, S, sizeof(S));
                  if (ret > 0) then
                    begin
                      writeln('- Serial Number: ', S);
                    end
                    else
                    begin
                      writeln('- Unable to fetch serial number string');
                    end;
                end;

              usb_close(udev);
            end;

          if not assigned(dev^.config) then
            begin
              writeln('  Couldn''t retrieve descriptors');
              continue;
            end;

          for I := 0 to dev^.descriptor.bNumConfigurations-1 do
            begin
              print_configuration(dev^.config[i]);
            end;

          dev := dev^.next;
        end;

      bus := bus^.next;
    end;
end;


begin
  usb_showinfo;

  writeln;
  write('Press enter');
  readln; // just to not let the dos screen go away
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -