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

Polling in secondry thread

Started by Fletch, October 13, 2011, 07:38:34 am

Previous topic - Next topic

Fletch

Hi

I am using a secondry thread to continuosly poll a device.  Using the function attached, I send out 3 bytes (241 16 176) and wait for a response using the WaitForBytes method.  For this test, I connected a loop back plug so I should receive the same bytes back.  The WaitforBytes method returns a true and I get 3 bytes back, but only the 1st byte is correct.  I receive back 241 0 0.
If I remove the receive data part of the function and activate the OnAfterReceive event to display the bytes I get the same back.  It gives me the same return bytes 241 0 0

What am I doing wrong?

function TDataModule2.SSerialData(Port : Integer;
                          SendTotal : Integer;
                          SndData : Arry1;
                          Delay : Integer;
                          RecTotal : Integer;
                          var RecData : Arry1;
                          FixedLen : Boolean) : Boolean;
var
I : Integer;
AAStr : AnsiString;
Begin
try
  {******  SEND DATA ********}
case Port of
1:
begin
  Port1.SendData(@SndData[1], SendTotal);
end;
2:
begin
  Port2.SendData(@SndData[1], SendTotal);
end;
3:
begin
  Port3.SendData(@SndData[1], SendTotal);
end;
4:
begin
  Port4.SendData(@SndData[1], SendTotal);
end;
end;
Sleep(200);
{**** RECEIVE DATA ******}
  if RecTotal > 0 then
  begin
   case Port of
   1:
   begin
    if Port1.WaitForBytes(RecTotal,Delay) then
     begin
      Port1.ReadAll(@RecData[1]);
      Result := True;
     end;
   end;
   2:
   begin
    if Port2.WaitForBytes(RecTotal,Delay) then
     begin
      Port2.ReadAll(@RecData[1]);
      Result := True;
     end;
   end;
   3:
   begin
    if Port3.WaitForBytes(RecTotal,Delay) then
     begin
      Port3.ReadAll(@RecData[1]);
      Result := True;
     end;
   end;
   4:
   begin
    if Port4.WaitForBytes(RecTotal,Delay) then
     begin
      Port4.ReadAll(@RecData[1]);
      Result := True;
     end;
   end;
  end;

Roman Novgorodov

Hello

Please note that for correct working of your (synchronous) sample you need exclude any asynchronous operations with Port1, [Port2 ...]
Check that PortX.OnAfterReceive,  PortX.Terminal and Port1.DataProcessor are nil.

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

They are all set to nil, except when I used the OnAfterReceive to check the data coming through.  I then disabled the code to receive data in the function.

Any other suggestions

Fletch

The strange thing is, I can use similar code in a simple test app(main thread) where I use a button to send the data, instead of continuous polling(secondry thread) and that works fine.  I have tried to slow down the polling but it makes no difference.

Fletch

I found my problem.  I was using an array in integers for send and receiving data where I should have been using array of Byte.

Any comments

Roman Novgorodov

Hello

Thank you for information.
Yes, serial communication transfers bytes (8-bit chars, octets).

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.