gpt4 book ai didi

delphi - Jedi USB项目读写Delphi

转载 作者:行者123 更新时间:2023-12-03 00:17:22 27 4
gpt4 key购买 nike

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

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;

最佳答案

我制作了一个您可以使用的测试平台:

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 以及输出数据。

关于delphi - Jedi USB项目读写Delphi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8258075/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com