绝地USB项目读写Delphi
我正在使用绝地usb hid组件连接到hid设备,并从中读取和写入数据。我一直无法写入设备。我一直在使用此代码。绝地USB项目读写Delphi,delphi,usb,hid,jedi,Delphi,Usb,Hid,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 hiddat
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.
填写您的供应商ID和ProductID以及输出数据。我制作了一个测试平台,您可以使用:
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.
填写您的VendorID和ProductID以及输出数据。一行有效地完成了writefile方法是否被接受的技巧:
ToWrite := TheDev.Caps.OutputReportByteLength;
TheDev.WriteFile(buffer,towrite,written);
设备只接受正确缓冲区长度的写入。如果只写入缓冲区长度的一部分,则该方法不起作用。有效地,一行执行以下操作,即是否接受writefile方法:
ToWrite := TheDev.Caps.OutputReportByteLength;
TheDev.WriteFile(buffer,towrite,written);
设备只接受正确缓冲区长度的写入。如果只写入缓冲区长度的一部分,则不起作用。您需要提供更多信息。你怎么知道你没有写信?会发生什么?GetLastError()是否有错误消息、异常或值?我看到有人在没有解释原因的情况下否决了你的投票,这对他们来说不是很友好,但这将是原因——目前来看,这个问题没有足够的信息可以回答。好的,ondevicedata,我把它放在label7中。说明:=hiddev.ProductName+'-'+floatostr(大小),这样我可以检查它是否是正确的设备,当我运行它时,标签上只写着“65”,而且设备没有收到命令,我知道这一点,因为设备与其他程序一起工作。你需要提供更多信息。你怎么知道你没有写信?会发生什么?GetLastError()是否有错误消息、异常或值?我看到有人在没有解释原因的情况下否决了你的投票,这对他们来说不是很友好,但这将是原因——目前来看,这个问题没有足够的信息可以回答。好的,ondevicedata,我把它放在label7中。说明:=hiddev.ProductName+'-'+floatostr(大小),这样我可以检查它是否是正确的设备,当我运行它时,标签上只写着“65”,并且设备没有收到命令,我知道这一点,因为设备与其他程序一起工作。谢谢,但是readfile命令使程序冻结,并且它仍然没有到达设备。删除readfile行。添加OnDeviceData方法并将大小输出到某个文本标签。请告诉我们有关设备和状态输出数据大小和输入数据大小的详细信息。label3。标题:=hiddev.ProductName+'-'+Floattostr(大小);我把这行放在OnDeviceData中,标签标题不变。我的设备是一个18F2550,在接收到命令时打开或关闭继电器。我有一个正在运行的VC++演示,下面是链接。MyDevice.Caps.OutputReportByTellength和MyDevice.Caps.InputReportByTellength的报告大小是多少?好的,那么我没有合理的解释了。你有USB协议分析仪来监控流量吗?有这样的软件,例如。谢谢,但是readfile命令使程序冻结,并且它仍然没有到达设备。删除readfile行。添加OnDeviceData方法并将大小输出到某个文本标签。请告诉我们有关设备和状态输出数据大小和输入数据大小的详细信息。label3。标题:=hiddev.ProductName+'-'+Floattostr(大小);我把这行放在OnDeviceData中,标签标题不变。我的设备是一个18F2550,在接收到命令时打开或关闭继电器。我有一个正在运行的VC++演示,下面是链接。MyDevice.Caps.OutputReportByTellength和MyDevice.Caps.InputReportByTellength的报告大小是多少?好的,那么我没有合理的解释了。你有USB协议分析仪来监控流量吗?有这样的软件,比如。