Ok. Look into usb2.pas.
Vendor = $04d8;
Product = $000b;
Changes these to suit your hardware.
procedure TUSB.DeviceChange(Sender:TObject);
This procedure gets called when HID hardware is added or removed.
You can see inside this procedure that HID-Device Vendor and Product are checked against the above values.
If match, then DeviceArrival(HidDev) will be called.
procedure TUSB.DeviceArrival(HidDev: TJvHidDevice);
This procedure add a new controller:
NewUSBController := TUSBController.Create(HidDev);
And this controller gets added into the lists of USB devices:
AUSBList.Items[newboard]:=NewUSBController;
function TUSB.HidReadWrite(Ctrl: TUSBController; ReadOnly:boolean):boolean;
Use this function to read and/or write towards your USB HID Device.
(in this example, I do send some calibration data towards the HID MicroChip controller.)
with AUSBList.Items[board] do
begin
FillChar(LocalData, SizeOf(LocalData), 0);
LocalData.Data[0] := byte(CMD_set_cal);
LocalData.Data[1] := position-1;
LocalData.Data[2] := 7;
LocalData.Data[3] := whattocal;
temp:=calval;
LocalData.Data[4] :=(temp MOD 256);
temp:=(temp DIV 256);
LocalData.Data[5] :=(temp MOD 256);
LocalData.Data[6] := DayOfTheMonth(Now);
LocalData.Data[7] := MonthOfTheYear(Now);
LocalData.Data[8] := (YearOf(Now) DIV 256);
LocalData.Data[9] := (YearOf(Now) MOD 256);
end;
error:=HidReadWrite(AUSBList.Items[board],False);
if (NOT error) then with AUSBList.Items[board].LocalData do
begin
if ( data[0]=byte(CMD_set_cal) ) AND ( data[1]=(position-1) ) then
begin
//do something with the data received
end else error:=True;
end;
My setup to show you why this is done like described above:
I have 20+MicroChip USB MCU connected to a single PC through USB.
These are all added into the list. And always have the same [board]number due to their unique HID serial number.
Each of these MCU connect through I2C towards another 20 MCU measuring devices. Totalling 400 connected MCU by USB onto a single PC. Running 24/7 for 6 months.
IMHO, USB is very good and stable and can be used just like a RS485 network on steroids.