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

TnrHid.WriteReport

Started by PHOTON, March 31, 2009, 10:51:48 am

Previous topic - Next topic

PHOTON

Hi Roman,

Thanks for your help.
The TnrHid.SendString and TnrHid.SendChar work properly.

Now I have a problem with the procedure TnrHid.writeReport.
My program  :

const bufferSendSize=31;
Type  TBufferSend=array[0..31] of byte;
var    i : integer;
       VBufferSend : TBufferSend;
       PBuffer : Pointer;

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
  PBuffer:=@VBufferSend;
  VBufferSend[0]:=0;
  for i:=1 to length(edit2.Text)+1 do VBufferSend:=ord(edit2.text[i-1]);
  nrHid1.writeReport(PBuffer,bufferSendSize);
end;

What make I wrong ? normally the microcontroller must me send back the byte that I send
but there is nothing.

Thanks for your help

best regards

chedges

March 31, 2009, 10:59:17 am #1 Last Edit: March 31, 2009, 11:01:10 am by chedges
once you have written the report, are you reading the data back using ReadReport? I'm doing it slightly differently but it does the same thing.

This is how I'm reading reports in my printer application.

type
   HIDReport = Array of Byte;

function TForm6.HIDRead : HIDReport;
var
  I: Integer;
  ToRead : Cardinal;
  Report : HIDReport;
begin
    ToRead := nrHID1.HidDevice.InputReportLength; //Get the Report length - my printer returns 256bytes
    SetLength(Report, ToRead);

    //init the buffer
    for I := 0 to ToRead - 1 do
      Report := 0;
    try
       Result := nil;
       if HidD_GetInputReport(nrHID1.Handle, Report, ToRead) then
         for i := 1 to ToRead - 1 do
           if PAnsiChar(Report) <> #0 then
           begin
              SetLength(Result, Length(Result)+1);
              Result[Length(Result)-1] := Byte(PAnsiChar(Report));
           end;
    except
      on E: Exception do
        ShowMessage(E.Message);
    end;

end;

PHOTON

Hi Chedges,

Thanks for your answer.
If I use the demo program I can read the ReportID.
The microcontroller (18F4550) is programmed in such manner that If I send a byte, he sends
the byte back to the PC. The program works properly when I use TnrHid.SendString.
Roman said me that I can also use the following methods :
nrHid1.Writereport(const pcReport: pointer; const reportSize: cardinal): boolean;
I have tried to use this procedure unsuccessfully.

best regards

chedges

Hi Photon,

This is how I'm using WriteReport. I can confirm this works perfectly for my application.
I am using a licensed copy of nrComm version 2.22. I'm not sure what version you have

procedure TForm6.HIDWrite(Data : String);
var
  ToWrite, NumberOfBytesWritten: Cardinal;
  FGL : AnsiString;
  FGLPtr : PAnsiChar;
  cnt, Err : integer;

  Report : Array of Byte;
begin

    ToWrite := nrHid1.HidDevice.OutputReportLength; //Get the Report length - my printer returns 256bytes

    if Length(Data) > ToWrite then
    begin
      raise Exception.Create('Data length exceeds HID report length of ' + IntToStr(ToWrite-1) + ' bytes');
      exit;
    end;

    //Pad the buffer
    FGL := #0 + Data; // set the report Id
    While Length(FGL) <> ToWrite do
       FGL := FGL + #0; //Pad out to the correct buffer length

    SetLength(Report, ToWrite);
    for cnt  := 0 to ToWrite - 1 do
      Report[cnt] := 0;

    for cnt := 1 to Length(Data) do
      Report[cnt] := Ord(Data[cnt]);

    nrHID1.WriteReport(Report, ToWrite);

end;

regards,

Chris

Roman Novgorodov

Hello

Yes, Chris is right.
Usual HID device requires that the length of outgoing data is nrHid1.HidDevice.OutputReportLength.

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