How can i communicate with an HID USB device in delphi

16,418

Solution 1

See Jan Axelson's USB page for examples. He has written a book also. USB Complete.

See also Robert Marquardt's HID controller suite for Delphi.

If you are using Delphi 2009 or newer, follow the link given in the answer on SO question :using-hidcontroller-on-delphi-2010

Solution 2

You can use QueryDosDevice to obtain the full device name. List all entries before you plug-in the device, and after, and see which new entry appears in the list. (I've found that most HID devices apear twice in the list, haven't found why yet). The code will contain "USB" "VID" "PID" and a GUID.

You can use this code with CreateFile if you prefix it with ´\\?\´ and use this Handle as a Serial Port (I personally prefer using THandleStream). The code could look like this:

var
  h:THandle;
begin
  h:=CreateFile(
    PChar('\\?\'+MyPortName),
    GENERIC_WRITE or GENERIC_READ,FILE_SHARE_WRITE or FILE_SHARE_READ,
    nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  if h=INVALID_HANDLE_VALUE then RaiseLastOSError;
  MyPort:=THandleStream.Create(h);
  SetCommTimeouts(h,MyFCommTimeouts);
Share:
16,418
Grant
Author by

Grant

Updated on June 11, 2022

Comments

  • Grant
    Grant almost 2 years

    I have been researching this problem for a while now and I just can't seem to get it right. I have a C++ version of the software I would like to make in delphi, but I can't get it to work in delphi. I need some sort of tutorial or guide that can show me how to connect to, read and write data to a HID USB device.