Author Topic: Polling in secondry thread  (Read 1025 times)

Fletch

  • Newbie
  • *
  • Posts: 29
    • View Profile
Polling in secondry thread
« on: October 13, 2011, 10:38:34 am »
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?

Code: [Select]
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

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Polling in secondry thread
« Reply #1 on: October 13, 2011, 10:55:44 am »
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

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Polling in secondry thread
« Reply #2 on: October 13, 2011, 11:15:22 am »
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

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Polling in secondry thread
« Reply #3 on: October 13, 2011, 11:35:40 am »
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

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Polling in secondry thread
« Reply #4 on: October 13, 2011, 02:27:00 pm »
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

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Polling in secondry thread
« Reply #5 on: October 14, 2011, 04:43:21 am »
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.