• Welcome to Forum.Deepsoftware.Com. Please login or sign up.
 
April 27, 2024, 01:51:30 am

News:

SMF - Just Installed!


Send-Wait-Receive

Started by Fletch, September 19, 2011, 07:29:41 am

Previous topic - Next topic

Fletch

Hi

I am trying out nrComm.  I need to poll a device and wait for answer within a certain time.  There is also a fixed amount of bytes to receive.  If no answer is receive within that time move on and poll the next device.  Below is some code I have put together but I need some help to fine tune it or if there is a better way to do it all help will be appreciated.

nrComm1.SendData(SendBuff,20);
  if nrComm1.WaitForBytes(20,200) then
  begin
   nrComm1.ReadAll(RecBuff);
   for i:=0 to SizeOf(RecBuff)-1 do
   begin
   S := S + IntToStr(Byte(PAnsiChar(RecBuff)[i]))+' ';
   end;
   MemoRec.Text:=MemoRec.Text+s;
  end;


Thanks

Roman Novgorodov

Hello

Thank you for your interest in our product.

Yes, your code is correct. You are using synchronous way for get data from serial port.

Usual we prefer asynchronous ways (OnAfterReceive event and timeouts), but in simple cases it is unnecessary.

Also You can take a look the following samples:
Demos\Codes\codes_demo.dpr
Demos\DataProc\dataproc.dpr
Demos\SimplePacket\SimplePacket.dpr

and this board:
http://forums.nrcommlib.com/index.php?board=19.0

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

Fletch

Can you give me a simple code example of doing the same thing in asynchronous (OnAfterReceive event and timeouts)

Roman Novgorodov

Hello

You can try something like following:


var RecBuff:AnsiString;
    countRecBuff:integer;

procedure TForm1.bSendClick(Sender: TObject);
begin
  RecBuff := '';
  countRecBuff := 0;
  Timer1.Enabled := True;
  nrComm1.SendData(SendBuff,20);
end;

procedure TForm1.nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
  Received: Cardinal);
var i:integer;
begin
  if Timer1.Enabled then
    for i := 0 to Received - 1 do begin
      RecBuff := RecBuff + IntToStr(Byte(PAnsiChar(Buffer)[i]));
      Inc(countRecBuff);
      if countRecBuff = 20 then begin
        DoSomethingWithReply(RecBuff);
        break;
      end;
  end;
end;

procedure TForm1.DoSomethingWithReply(S:string);
begin
  // handler of correct reply ...
  Timer1.Enabled := False;
  MemoRec.Text := MemoRec.Text+s;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  // handler for timeout error ...
  Timer1.Enabled := False;
  ListBox1.Items.Add('Timeout!!!!');
end;


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

Fletch

I am picking up a problem with adding bytes to the PAnsichar.  My first byte is #0 and the PAnsiChar wont accept the first byte as #0.  This is the bytes I want to send.

var
  SendBuff : PAnsiChar;
  I : Integer;
begin
  SendBuff[0] := #0;
  SendBuff[1] := #19;
  SendBuff[2] := #45;
  SendBuff[3] := #240;


Can you help
Thanks

Roman Novgorodov

Hello

Please clarify what do you mean
Quote... the PAnsiChar wont accept the first byte as #0.


Do you get compiler error or exception?
Please show error message.

Roman Novgorodov
DeepSoftware LLC

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

Fletch

If the 1st byte is #0(SendBuff[0] := #0) then the PAnsichar does not accept any bytes(it remains empty).  If I change the first byte to say #1(SendBuff[0] := #1) then everything is fine and all the bytes thereafter are stored in the pointer.  I am using Delphi XE2.  I spent a couple of hours trying to work out this problem.

Roman Novgorodov

Hello

Why do you think that

Quotethe PAnsichar does not accept any bytes(it remains empty).


?

If you try to check SendBuff content as string (in debugger and etc.) you will see empty string because string contains zero char at first place. But if you will check SendBuff[1] SendBuff[2] and others, all will be correct.

Please note that PAnsichar is Delphi (Windows) type and nrComm Lib does not control it.

Roman Novgorodov
DeepSoftware LLC

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

Fletch

Hi
I am using a loop back plug and I monitor what comes back to me. Can you explain to me why if I send the following
to the comm port I get nothing back.   If the first byte is 0 then nrComm sends nothing.

SendBuff[0] := #0; 
SendBuff[1] := #19; 
SendBuff[2] := #45; 
SendBuff[3] := #240;

But if I send this to the comm port I get the same back which is correct.  The first byte is greater than 0.

SendBuff[0] := #1; 
SendBuff[1] := #19; 
SendBuff[2] := #45; 
SendBuff[3] := #240;

Thanks







Roman Novgorodov

Hello

We have tested again and as expected all works fine.

1) Please open Codes demo.
2) Add drop new button and add following method:

procedure TForm1.Button2Click(Sender: TObject);
var data:array [0..4] of byte;
    pData:PAnsiChar;
begin
  pData := @data[0];
  pData[0] := #0;
  pData[1] := #1;
  pData[2] := #2;
  pData[3] := #3;
  pData[4] := #4;
  nrComm1.SendData(pData, 5);
end;


3) Now run two copies of Codes demo on PC wit two serial ports connected over null mode modem cable.
4) Click button on the one app and you will see correct codes on another instance.

Please create simple project that shows your problem and upload it here. It seems like you are doing something wrong.

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