Kod: Tümünü seç
unit FormMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
// input and out buffer size constants...
BufferInSize = 8;
BufferOutSize = 8;
type
// input and output buffers...
TBufferIn = array[0..BufferInSize] of byte;
TBufferOut = array[0..BufferOutSize] of byte;
// main form
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FBufferIn:TBufferIn;
FBufferOut:TBufferOut;
function USBEvent(var Msg: TMessage): Boolean;
public
end;
var
Form1: TForm1;
implementation
uses
cUSBInterface,
cUSBInterfaceTypes;
const
// vendor and product ID constants...
VENDOR_ID = ****;
PRODUCT_ID = ****;
{$R *.DFM}
{
****************************************************************************
* Name : Create *
* Purpose : Create the main form *
****************************************************************************
}
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.HookMainWindow(USBEvent);
Connect(Application.Handle);
end;
{
****************************************************************************
* Name : Destroy *
* Purpose : Free the main form *
****************************************************************************
}
procedure TForm1.FormDestroy(Sender: TObject);
begin
Application.UnHookMainWindow(USBEvent);
end;
{
****************************************************************************
* Name : USBEvent *
* Purpose : DLL message handler hook *
****************************************************************************
}
function TForm1.USBEvent(var Msg: TMessage): Boolean;
var
DevHandle:cardinal;
// TextBuffer:array[0..255] of char;
begin
result := False;
if Msg.Msg = WM_HID_EVENT then
begin
case Msg.WParam of
// a HID device has been plugged in...
NOTIFY_PLUGGED :
begin
// is it our HID device...
DevHandle := Msg.LParam; // handle of HID device in this message
if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
begin
// add your code here, for example...
form1.Caption:='Aygıt Kontrol Bulundu';
// GetProductName(DevHandle, TextBuffer, 256);
// ALabel.Caption := string(TextBuffer);
end;
result := true;
end;
// a HID device has been device removed...
NOTIFY_UNPLUGGED :
begin
// is it our HID device...
DevHandle := Msg.LParam; // handle of HID device in this message
if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
begin
form1.Caption:='Aygıt Kaldırıldı';
end;
result := true;
end;
// a HID device has been attached or removed. This event is fired after
// either NotifyPlugged or NotifyUnplugged.
NOTIFY_CHANGED :
begin
// get the handle of the device we are interested in
// and set it's read notification flag to true...
DevHandle := GetHandle(VENDOR_ID,PRODUCT_ID);
SetReadNotify(DevHandle,true);
result := true;
end;
// a HID device has sent some data..
NOTIFY_READ :
begin
DevHandle := Msg.LParam; // handle of HID device in this message
if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
begin
// read the data - remember that first byte is report ID...
Read(DevHandle,@FBufferIn);
form1.Label1.Caption:=FBufferIn;
// process data here...
end;
result := true;
end;
end;
end;
end;
(*
if you want to write some data to the USB device, then
try using something like this...
// first element is report ID, set to 0...
FBufferOut[0] := $0;
// fill the buffer with some data...
for index := 1 to BufferOutSize do
FBufferOut[index] := index;
DevHandle := GetHandle(VendorID, ProductID);
Write(DevHandle,@FBufferOut);
*)
end.
Yalnız buffer bilgilrine nasıl ulaşcam anlayamadım.
Kod: Tümünü seç
private
FBufferIn:TBufferIn;
FBufferOut:TBufferOut;
function USBEvent(var Msg: TMessage): Boolean;
public
end;
Bunu hangi değişken tipine atayarak işleyebilirim.
Teşekkür Ederim...