Jedi USB项目读写Delphi [英] Jedi USB project read and write Delphi

查看:532
本文介绍了Jedi USB项目读写Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jedi usb hid组件来连接,读取和写入HID设备。我一直无法写入设备。我一直在使用这个代码。

I am using the Jedi usb hid component to connect to, read and write from a HID device. I have been unable to write to the device. I have been using this code.

type
TReport = Packed record
 ReportID: byte;
 Data: array [0..64] of byte;
 end;

procedure TForm1.Button1Click(Sender: TObject);
var
 I:integer;
 HidData:TReport;
 written:DWORD;
begin
hiddata.ReportID:=0;
hiddata.Data[0]:=0;
hiddata.Data[1]:=$80;
  for I := 2 to 64 do
    hiddata.Data[I]:=$FF;
currentdevice.WriteFile(hiddata,currentdevice.Caps.OutputReportByteLength,written);
end;


推荐答案

我做了一个可以使用的测试平台: / p>

I made a test platform which you can use :

unit BasicMain; 

interface 

uses 
Windows, Messages, SysUtils, Classes, Graphics, Controls, StdCtrls, Forms, Dialogs,
JvHidControllerClass, JvComponentBase; 

type 

TReport = packed record 
  ReportID: byte; 
  Data: array [0..64] of byte; 
end; 

TMainForm = class(TForm) 
  HidCtl: TJvHidDeviceController; 
  DeviceList: TListBox; 
  Label1: TLabel; 
  Label2: TLabel; 
  Button1: TButton; 
  procedure HidCtlDeviceChange(Sender: TObject); 
  function HidCtlEnumerate(HidDev: TJvHidDevice;const Idx: Integer): Boolean; 
  procedure Button1Click(Sender: TObject); 
  procedure FormCreate( Sender : TObject);
  procedure DeviceRemoval(HidDev: TJvHidDevice); 
  procedure DeviceArrival(HidDev: TJvHidDevice); 
  public 
end; 

var 
  MainForm: TMainForm; 
  MyDevice: TJvHidDevice; 

implementation 

{$R *.dfm} 
{ ***************************************************************************** } 

Const 
  MyVendorID = $04D8;   // Put in your matching VendorID
  MyProductID = $003F;  // Put in your matching ProductID

procedure TMainForm.FormCreate( Sender : TObject);
begin
  HidCtl.OnArrival:= DeviceArrival; 
  HidCtl.OnRemoval:= DeviceRemoval; 
end;

procedure TMainForm.DeviceRemoval(HidDev: TJvHidDevice); 
begin 
  if ((Assigned(MyDevice)) and (NOT MyDevice.IsPluggedIn)) then 
  begin 
    HidCtl.CheckIn(MyDevice); 
  end; 
end; 

procedure TMainForm.DeviceArrival(HidDev: TJvHidDevice); 
begin 
  if ((HidDev.Attributes.VendorID = MyVendorID) AND 
     (HidDev.Attributes.ProductID = MyProductID) AND 
     (HidDev.Caps.OutputReportByteLength = SizeOf(TReport)) ) then 
  begin 
    if HidDev.CheckOut then 
    begin 
      MyDevice := HidDev; 
    end; 
  end; 
end; 

procedure TMainForm.HidCtlDeviceChange(Sender: TObject); 
begin 
  Label1.Caption := '-'; 
  Label2.Caption := '-'; 
  MyDevice := nil; 
  DeviceList.Clear; 
  HidCtl.Enumerate; 
end; 

function TMainForm.HidCtlEnumerate(HidDev: TJvHidDevice;const Idx: Integer): Boolean; 
begin 
  DeviceList.Items.Add( 
  Format('%.4x/%.4x', [HidDev.Attributes.VendorID,HidDev.Attributes.ProductID])); 
  if (HidDev.Attributes.VendorID = MyVendorID) and (HidDev.Attributes.ProductID =         MyProductID) then 
  begin 
    HidCtl.CheckOut(HidDev);
    MyDevice := HidDev; 
    Label1.Caption := Format('%.4x/%.4x', [MyDevice.Attributes.VendorID ,   MyDevice.Attributes.ProductID]); 
    Label2.Caption := 'Length = '+ IntToStr(MyDevice.Caps.OutputReportByteLength) + ' ' + IntToStr(MyDevice.Caps.InputReportByteLength); 
  end; 
  Result := True; 
end; 

procedure TMainForm.Button1Click(Sender: TObject); 
var 
  HidData : TReport; 
  written : DWORD; 
begin 
  HidData.ReportID:=0; 
  HidData.Data[0]:=$80;
  // Fill with more data 

  MyDevice.WriteFile(HidData, MyDevice.Caps.OutputReportByteLength, Written); 
  MyDevice.ReadFile(HidData, MyDevice.Caps.InputReportByteLength, Written); 
end; 

end. 






object MainForm: TMainForm
  Left = 0
  Top = 0
  Caption = 'MainForm'
  ClientHeight = 341
  ClientWidth = 535
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 48
    Top = 8
    Width = 31
    Height = 13
    Caption = 'Label1'
  end
  object Label2: TLabel
    Left = 48
    Top = 27
    Width = 31
    Height = 13
    Caption = 'Label2'
  end
  object Button1: TButton
    Left = 48
    Top = 46
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object ListBox1: TListBox
    Left = 48
    Top = 96
    Width = 465
    Height = 97
    ItemHeight = 13
    TabOrder = 1
  end
end

填写您的VendorID和ProductID以及输出数据。

Fill in your VendorID and ProductID and the output data.

这篇关于Jedi USB项目读写Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆