• Welcome to Forum.Deepsoftware.Com. Please login or sign up.
 

Problem installing WinUSB - USBOsrKitDemo function

Started by nath.cf, May 12, 2011, 03:26:39 pm

Previous topic - Next topic

nath.cf

Hi! I find what was weong and now my program works fine. I'll show the code of the main program of the exemple i've done. Maybe it will help someone...

There are two Tedits, one for productID and another for VendorId they are used to search the device. The data is writen on memo1. Teh most interesting function are TForm2.BtOkClick and TForm2.nrUSBPipePair1AfterReceive.


unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, nrclasses, StdCtrls, nrusbpipe, nrusb, nrusbdriver;

type
  TForm2 = class(TForm)
    nrUSB1: TnrUSB;
    nrUSBPipePair1: TnrUSBPipePair;
    Label1: TLabel;
    EdVendorID: TEdit;
    Label2: TLabel;
    EdProductID: TEdit;
    LbNmDisp: TLabel;
    BtOk: TButton;
    Memo1: TMemo;
    nrUsbDriver1: TnrUsbDriver;
    Memo2: TMemo;
    procedure BtOkClick(Sender: TObject);
    procedure nrUSBPipePair1AfterReceive(Com: TObject; Buffer: Pointer;
      Received: Cardinal);
  private
    { Private declarations }
  public
    { Public declarations }
//   procedure WMDeviceChange(var Msg:TMessage); message WM_DEVICECHANGE;
  end;

var
  Form2: TForm2;

implementation
uses nrusbman;

{$R *.dfm}

procedure TForm2.BtOkClick(Sender: TObject);
Var vID,pID,indice,i : integer;
USBDev : TnrUsbDevice;
OutOK,InOK : boolean;

begin
//Atualiza a lista de dispositivos USB
nrUSB1.Update;
//VendorID e ProductID do dispositivo que deverá estar conectado
vID := StrToInt(EdVendorID.Text);//1203;
pID := StrToInt(EdProductID.Text);//12325;

indice := nrUSB1.DeviceIndexOf(vID,pID);
//Se indice for igual a -1, então o dispositivo não está conectado
//ao computador ou houve algum erro.
if (indice=-1) then
    ShowMessage('Dispositivo não encontrado')
else
  begin
    nrUSB1.SetDeviceIndex(indice);
    USBDev:=nrUSB1.UsbDevice;
    LbNmDisp.Caption:='Dispositivo : '+USBDev.NameFriendly;

   //verificar se o dispositivo já tem o driver WinUSB se não, ele será instalado.
   //TnrUSB precisa do WinUSB(Microsoft). Ele deve estar instalado antes acessar o dispositivo.
   if Lowercase(nrUSB1.UsbDevice.Service) <> 'teste' then
     begin
     nrUsbDriver1.Install;
     nrUSB1.UsbDevice.Service:='teste'
     end;
   nrUSB1.Active := True;

    //preencher pipes id
    try
      nrUSB1.Active := True;
      nrUSBPipePair1.PipeOut:=1;
      nrUSBPipePair1.PipeIn:=1;
      nrUSBPipePair1.Active := True;
    except
      nrUSB1.Active := False;
      nrUSBPipePair1.Active := False;
      ShowMessage('Erro na leitura dos dados.');
    end;


  end;
end;

procedure TForm2.nrUSBPipePair1AfterReceive(Com: TObject; Buffer: Pointer;
  Received: Cardinal);
var s:string;
    i : integer;
begin
//OutputDebugString(PChar('Rec:' + IntToStr(Received) + ' bytes'));
  s := '';
  for i := 0 to Received - 1 do
  begin
    s := s + IntToHex(Byte(PCHar(Buffer)[i]),1)+' ';
  end;
  Memo1.Lines.Add(s);
end;


end.


Hope it helps someone!
Nathalia C. França

Roman Novgorodov

Hello Natalia

Thank you for your information.

Roman Novgorodov
DeepSoftware
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.