5

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.

Warren P
  • 65,725
  • 40
  • 181
  • 316
Grant
  • 233
  • 2
  • 5
  • 10
  • The close votes probably mean that your question lacks enough detail to be directly answerable. Are we supposed to know by magic what your C++ program does? Why not spell it out? – Warren P Nov 22 '11 at 03:14
  • 1
    I wish I could vote to not close. Please don't. This is a fair general question about the approach to use to talk to an uncommon device. Specifics can come later. Me, I want to know this high level answer too. – mj2008 Nov 22 '11 at 09:54

2 Answers2

7

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

Community
  • 1
  • 1
LU RD
  • 34,438
  • 5
  • 88
  • 296
  • 1
    It makes me smile every time I see someone using Robert's legacy :) ... +1 for pointing out his component. – 0xC0000022L Nov 21 '11 at 17:28
  • Thanks, for this component, are there any guides on how to use it. – Grant Nov 22 '11 at 11:58
  • In the HID components are demos which illustrates basic communication with USB devices plus a help file. On the HID page from Jan Axelson, there are some more Delphi examples written by Robert Marquardt. – LU RD Nov 22 '11 at 12:01
  • OK I have looked through the help and the demos, and I have been able to get the Vendor name and product name. How do I write Buffer[1] = 0x80; – Grant Nov 22 '11 at 18:04
  • In the ReadWriteDemo, you can select the usb device, enter a byte string in hex (e.g. $80) in the write section. Sending the buffer via CurrentDevice.WriteFile(Buf, ToWrite, Written) in the menu. If there is a response, this will appear in the read window. Hope this helps. – LU RD Nov 22 '11 at 18:30
  • I get an error when i try to write to the device with the ReadWriteDemo, Access violation at address 00000000. write of address 0000000. – Grant Nov 23 '11 at 14:46
  • 1
    Robert Marquardt's softgem link broken. now: http://www.soft-gems.net/index.php/controls/human-interface-device-controller-suite – david Apr 22 '16 at 03:18
3

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);
Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67