• AVR Freaks

Hot!USB Device - HID - Custom Demos VS Delphi

Author
winai
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2010/02/16 09:17:56
  • Location: 0
  • Status: offline
2010/02/17 18:23:29 (permalink)
0

USB Device - HID - Custom Demos VS Delphi

Hello
I'm learn about HID class I use Microchip Demo (USB Device - HID - Custom Demos) Project I have Problem in this code

case 0x37: //Read POT command. Uses ADC to measure an analog voltage
{
WORD_VAL w;
if(!HIDTxHandleBusy(USBInHandle))
{
mInitPOT();
w = ReadPOT(); //Use ADC to read the I/O pin voltage.
ToSendDataBuffer[0] = 0x37; //Echo back to the host
ToSendDataBuffer[1] = w.v[0]; //Measured analog voltage LSB
ToSendDataBuffer[2] = w.v[1]; //Measured analog voltage MSB
USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
}
}
In this case how i can send command and read by using Delphi JvHidControllerClass

Best regards

Winai
#1

6 Replies Related Threads

    alfred.glanzer
    Starting Member
    • Total Posts : 77
    • Reward points : 0
    • Joined: 2009/11/20 08:42:42
    • Location: 0
    • Status: offline
    RE: USB Device - HID - Custom Demos VS Delphi 2010/02/18 00:54:19 (permalink)
    0
    First define a record to hold the HID-data
     

    TReport = packed record
       ReportID: byte;
       Data: array [0..64] of byte;
    end;
     
    // the length of 64 should be the same as CapsOutputReportByteLength and/or CapsInputReportByteLength
     
     
    Then do something lik this:
     

    var
      HidData:TReport;

      written:DWORD;
    begin

      HidData.ReportID:=0;
      HidData.Data[0]:=$37;

      HidController.WriteFile(HidData, HidController.Caps.OutputReportByteLength, Written));

      //HidController.WriteFile(HidData, 64, Written));

      HidController.ReadFile(HidData, HidController.Caps.InputReportByteLength, Written));
      //HidController.ReadFile(HidData, 64, Written));
    end;

     
     
    This should do the job. HidData now holds the requested data.
     
    Good luck, Alfred.
    #2
    winai
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2010/02/16 09:17:56
    • Location: 0
    • Status: offline
    RE: USB Device - HID - Custom Demos VS Delphi 2010/02/18 03:38:32 (permalink)
    0
    Thanks Alfred

    I use your code but It not effect
    This my code
    Please help me

    unit BasicMain;

    interface

    uses
    Windows, SysUtils, Classes, Controls, StdCtrls, Forms,
    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);

    public
    end;

    var
    MainForm: TMainForm;
    MyDevice: TJvHidDevice;

    implementation

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

    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 = $04D8) and (HidDev.Attributes.ProductID = $003F) then
    begin
    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]:=$37;

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

    end.
    #3
    alfred.glanzer
    Starting Member
    • Total Posts : 77
    • Reward points : 0
    • Joined: 2009/11/20 08:42:42
    • Location: 0
    • Status: offline
    RE: USB Device - HID - Custom Demos VS Delphi 2010/02/20 13:22:23 (permalink)
    0
    Well,

    I do not know exactly how you use the JvHidControllerClass, but I will give it a try !

    After enumeration, you have to CheckOut a device to be able to connect.

    So try add this in HidCtlEnumerate:

    //************************

    HidCtl.CheckOut(HidDev);
    //************************
    MyDevice := HidDev;

    Personally, I am in favor of using DeviceRemoval(HidDev: TJvHidDevice) and DeviceArrival(HidDev: TJvHidDevice).

    Code example:

    In FormCreate:

    //HidCtl:=TJvHidDeviceController.Create(Application); // only needed if you create HidCtl runtime, as I do

    HidCtl.OnArrival:= DeviceArrival;
    HidCtl.OnRemoval:= DeviceRemoval;



    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 = $04D8) AND
    (HidDev.Attributes.ProductID = $003F) AND
    (HidDev.Caps.OutputReportByteLength = SizeOf(TReport)) ) then
    begin
    if HidDev.CheckOut then
    begin
    MyDevice := HidDev;
    end;
    end;
    end;


    Wish you good luck again !!
    Alfred.
    post edited by alfred.glanzer - 2010/02/20 13:23:36
    #4
    winai
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2010/02/16 09:17:56
    • Location: 0
    • Status: offline
    RE: USB Device - HID - Custom Demos VS Delphi 2010/02/26 19:25:20 (permalink)
    0
    WOW.........
    It Worked grin
    Thanks Alfred.
    #5
    pix_l
    New Member
    • Total Posts : 1
    • Reward points : 0
    • Joined: 2019/07/17 12:41:57
    • Location: 0
    • Status: offline
    Re: RE: USB Device - HID - Custom Demos VS Delphi 2019/07/19 12:05:17 (permalink)
    0
    Hi.
    I understand the theme is too old, but I can't find an answer for my question on the Net. I buy this device

    If I just open some editable window such as Office document, Notepad and so on, I can see serial number of ID card, but I need to have this serial number on variable in my Delphi application. I try to use HidControllerClass. I can see my device, but I can't catch serial number because OnDeviceData just not called. Please help! I broke my mind already.
    #6
    katela
    Super Member
    • Total Posts : 1321
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: online
    Re: RE: USB Device - HID - Custom Demos VS Delphi 2019/07/20 16:05:32 (permalink)
    0
    This thread is too old, start your own thread and if you question is from the windows side, then it's better to post it in appropriate forums like .Net or Delphi forums instead of here. You could get better assistance there.

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5