Jedi USB project read and write Delphi

Grant picture Grant · Nov 24, 2011 · Viewed 9.9k times · Source

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;

Answer

LU RD picture LU RD · Nov 24, 2011

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

Fill in your VendorID and ProductID and the output data.