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

📄 unit1.pas

📁 用Delphi开发的USB源程序
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, Buttons, SUIMemo, FileCtrl,
  SUIListBox, ToolWin, SUIToolBar, SUITreeView, ImgList, SUIScrollBar,
  SUIForm;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Timer1: TTimer;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel4: TPanel;
    suiMemo1: TsuiMemo;
    suiTreeView1: TsuiTreeView;
    Panel3: TPanel;
    ImageList1: TImageList;
    Splitter1: TSplitter;
    suiToolBar1: TsuiToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ImageList2: TImageList;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    ToolButton6: TToolButton;
    ToolButton8: TToolButton;
    Button6: TButton;
    Panel5: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure suiTreeView1Collapsed(Sender: TObject; Node: TTreeNode);
    procedure suiTreeView1Enter(Sender: TObject);
    procedure suiTreeView1Expanded(Sender: TObject; Node: TTreeNode);
  private
    { Private declarations }
    function UsbGetDeviceDescriptor(phDeviceHandle: THandle):string;
  public
    function UsbOpenDriver(var phDeviceHandle: THandle;const devname: string): boolean;
    procedure CMD_Send(hDevice:THandle;flag:byte;cmd:byte;len:WORD);
    procedure set_green();
    procedure set_red();
    { Public declarations }
  end;
const USB_DEVICE_NAME                   = 'Navtexv1_x';
const GlxNavtex_IOCTL_INDEX             = $00000800;
const FILE_DEVICE_UNKNOWN               = $00000022;
const METHOD_BUFFERED                   = $0;
const METHOD_IN_DIRECT                  = $1;
const METHOD_OUT_DIRECT                 = $2;
const METHOD_NEITHER                    = $3;
const FILE_ANY_ACCESS                   = $0;

const IOCTL_GlxNavtex_GET_DEVICE_DESCRIPTOR
                                        = (FILE_DEVICE_UNKNOWN shl 16) or
                                          (FILE_ANY_ACCESS shl 14) or
                                          ((GlxNavtex_IOCTL_INDEX+0) shl 2) or
                                          (METHOD_BUFFERED);
const IOCTL_GlxNavtex_BULK_WRITE
                                        = (FILE_DEVICE_UNKNOWN shl 16) or
                                          (FILE_ANY_ACCESS shl 14) or
                                          ((GlxNavtex_IOCTL_INDEX+3) shl 2) or
                                          (METHOD_IN_DIRECT);
const IOCTL_GlxNavtex_BULK_READ
                                        = (FILE_DEVICE_UNKNOWN shl 16) or
                                          (FILE_ANY_ACCESS shl 14) or
                                          ((GlxNavtex_IOCTL_INDEX+4) shl 2) or
                                          (METHOD_OUT_DIRECT);

  type BULK_TRANSFER_CONTROL = packed record
        pipeNum:DWord;
  end;
  type USB_DEVICE_DESCRIPTOR = packed record
        bLength :byte;
        bDescriptorType :byte;
        bcdUSB :word;
        bDeviceClass :byte;
        bDeviceSubClass :byte;
        bDeviceProtocol :byte;
        bMaxPacketSize0 :byte;
        dVendor :word;
        idProduct :word;
        bcdDevice :word;
        iManufacturer :byte;
        iProduct :byte;
        iSerialNumber :byte;
        bNumConfigurations :byte;
  end;
  type xUSB_CMD = packed record
        flag:byte;
        cmd:byte;
        case integer of
        0:(len:word);
        1:(index:word);
  end;
var
  Form1: TForm1;
  USB_CMD:xUSB_CMD;
implementation
//※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
//资源描述:
//        Pipe0--EndPoint1--BulkOUT
//        Pipe1--EndPoint2--BulkIN
//        Pipe2--EndPoint3--ISO
//※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
uses Unit2;
{$R *.dfm}
{$R my.res}
//---------------------------------------------------------------------------------------------------------------------
//【创建人  】 gliethttp
//【创建日期】 2006-07-24
//【功能描述】打开USB设备
//--------------------------------------------------------
//【修改人  】
//【修改日期】
//【修改原因】
//---------------------------------------------------------------------------------------------------------------------
function Tform1.UsbOpenDriver(var phDeviceHandle: THandle;const devname: string): boolean;
 var completeDeviceName:array[0..63] of char;
begin
 StrPCopy(completeDeviceName,'\\.\'+devname);
 //以completeDeviceName 为设备名,打开设备
 phDeviceHandle:=CreateFile(completeDeviceName,GENERIC_WRITE,
                            FILE_SHARE_WRITE,nil,OPEN_EXISTING,0,0);
 if phDeviceHandle=INVALID_HANDLE_VALUE then
 begin
  result:=false;
 end else
     begin
      result:=true;
     end;
end;
//---------------------------------------------------------------------------------------------------------------------
//【创建人  】 gliethttp
//【创建日期】 2006-07-24
//【功能描述】获取设备描述符
//--------------------------------------------------------
//【修改人  】
//【修改日期】
//【修改原因】
//---------------------------------------------------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
 var hDevice:THandle;
     s:string;
begin
 hDevice:=0;
 if(not UsbOpenDriver(hDevice, USB_DEVICE_NAME))then
 begin
  exit;
 end;
 s:=form1.UsbGetDeviceDescriptor(hDevice);
 showmessage(s);
 CloseHandle(hDevice);
end;
//---------------------------------------------------------------------------------------------------------------------
//【创建人  】 gliethttp
//【创建日期】 2006-07-24
//【功能描述】
//--------------------------------------------------------
//【修改人  】
//【修改日期】
//【修改原因】
//---------------------------------------------------------------------------------------------------------------------
function TForm1.UsbGetDeviceDescriptor(phDeviceHandle: THandle): string;
 var output:USB_DEVICE_DESCRIPTOR;
     nBytes:DWORD;
     bResult:boolean;
begin
 bResult:=DeviceIoControl(phDeviceHandle,
                          IOCTL_GlxNavtex_GET_DEVICE_DESCRIPTOR,
                          nil,
                          0,
                          @output,
                          sizeof(USB_DEVICE_DESCRIPTOR),
                          nBytes,
                          nil);
 result:='bLength               ='+inttohex(output.bLength,2)+#10+#13+
         'bDescriptorType       ='+inttohex(output.bDescriptorType,2)+#10+#13+
         'bcdUSB                ='+inttohex(output.bcdUSB,2)+#10+#13+
         'bDeviceClass          ='+inttohex(output.bDeviceClass,2)+#10+#13+
         'bDeviceSubClass       ='+inttohex(output.bDeviceSubClass,2)+#10+#13+
         'bDeviceProtocol       ='+inttohex(output.bDeviceProtocol,2)+#10+#13+
         'bMaxPacketSize0       ='+inttohex(output.bMaxPacketSize0,2)+#10+#13+
         'dVendor               ='+inttohex(output.dVendor,2)+#10+#13+
         'idProduct             ='+inttohex(output.idProduct,2)+#10+#13+
         'bcdDevice             ='+inttohex(output.bcdDevice,2)+#10+#13+
         'iManufacturer         ='+inttohex(output.iManufacturer,2)+#10+#13+
         'iProduct              ='+inttohex(output.iProduct,2)+#10+#13+
         'iSerialNumber         ='+inttohex(output.iSerialNumber,2)+#10+#13+
         'bNumConfigurations    ='+inttohex(output.bNumConfigurations,2);
end;
//---------------------------------------------------------------------------------------------------------------------
//【创建人  】 gliethttp
//【创建日期】 2006-07-24
//【功能描述】
//--------------------------------------------------------
//【修改人  】
//【修改日期】
//【修改原因】
//---------------------------------------------------------------------------------------------------------------------
procedure TForm1.Timer1Timer(Sender: TObject);
 var hDevice:THandle;
begin
 hDevice:=0;
 if(not form1.UsbOpenDriver(hDevice, 'Navtexv1_x'))then

⌨️ 快捷键说明

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