Einzelnen Beitrag anzeigen

Benutzerbild von BigWumpus
BigWumpus

Registriert seit: 21. Aug 2007
Ort: Hannover
6 Beiträge
 
#5

Re: Newbee! Anwendung für USB/HID - startet nicht auf andere

  Alt 28. Sep 2007, 08:34
Hier mal das Proggi als Quelle.

Das Gerüst wurde mittels EasyHID automatisch erzeugt und ich habe noch etwas "Leben" eingehaucht.


Code:
unit FormMain;

interface

uses
   Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
  ExtDlgs;

const

   // input and out buffer size constants...
   BufferInSize = 32;
   BufferOutSize = 32;
type

   // input and output buffers...
   TBufferIn = array[0..BufferInSize] of char;
   TBufferOut = array[0..BufferOutSize] of char;

   // main form
   TMain = class(TForm)
    Datentext: TMemo;
    Programmname: TLabel;
    ReadButton: TButton;
    LabelConnected: TLabel;
    DeviceLabel: TLabel;
    DeleteButton: TButton;
    SaveButton: TButton;
    SaveDialog: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ReadButtonClick(Sender: TObject);
    procedure DeleteButtonClick(Sender: TObject);
    procedure SaveButtonClick(Sender: TObject);
    private
      FBufferIn:TBufferIn;
      FBufferOut:TBufferOut;
      function USBEvent(var Msg: TMessage): Boolean;
    public
    end;

var
  Main: TMain;

implementation

uses
   cUSBInterface,
   cUSBInterfaceTypes;

const
   // vendor and product ID constants...
   VENDOR_ID          = 6017;
   PRODUCT_ID         = 2000;

{$R *.DFM}

procedure TMain.FormCreate(Sender: TObject);
begin
   Application.HookMainWindow(USBEvent);
   Connect(Application.Handle);
end;

procedure TMain.FormDestroy(Sender: TObject);
begin
   Application.UnHookMainWindow(USBEvent);
end;

procedure TMain.ReadButtonClick(Sender: TObject);
Var DevHandle:cardinal;
begin
   //Buffer löschen
   DatenText.Clear;
   //Daten vom USB-Device abrufen
[...]
   DevHandle := GetHandle(VENDOR_ID, PRODUCT_ID);
   Write(DevHandle,@FBufferOut);
   //Löschen-Knopf aktivieren
   DeleteButton.Enabled := true;
   SaveButton.Enabled := true;
end;

{
****************************************************************************
* Name   : USBEvent                                                      *
* Purpose : DLL message handler hook                                      *
****************************************************************************
}
function TMain.USBEvent(var Msg: TMessage): Boolean;
var
   DevHandle:cardinal;
   USBDatenText:string;
   I: Integer;
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
               //SetReadNotify(DevHandle,true);
               SetReadNotify(DevHandle,true);
[das hier wird durchlaufen]
               LabelConnected.Caption:='connected';
               DeviceLabel.Caption:='?';

[...]
               //Lesen-Knopf aktivieren
               ReadButton.Enabled := true;
            end;
            result := true;
         end;

         // a HID device has been device removed...
         NOTIFY_UNPLUGGED :
         begin
            DevHandle := Msg.LParam; // handle of HID device in this message
            if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
            begin
               LabelConnected.Caption:='not connected';
               DeviceLabel.Caption:='';
               ReadButton.enabled := false;
               DeleteButton.enabled := false;
            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);
               case FBufferIn[1] of
                  'T','L':
                  begin
[...]
                  end;
               end;
            end;
            result := true;
         end;
      end;
   end;
end;

end.
Bernd Rüter
Auf die Dauer hilft nur Power !!!
  Mit Zitat antworten Zitat