Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Jan : TIdTCPClient Problem

www.cryer.info
Managed Newsgroup Archive

TIdTCPClient Problem

Subject:TIdTCPClient Problem
Posted by:"F Alvarado" (f.alvara..@brainsoftware.net)
Date:Fri, 25 Jan 2008 08:50:07

{
In my application i use a TIdTCPClient to send/receive messages
My main form checks what messages to send to two diferent ports in same Host
My problem is that when I send two similar messages (EventSwitch '-1'
and '0') my application just resets.
I have paused (Sleep(n)) and proccessed messages
(Application.ProcessMessages) in several location
with no success. I have an applet with the same code and when I execute
it works just fine, so I'm thinking is
because of two traslaping calls (too fast) but Indy uses blocking sockets!
}

// uses ..., IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdAntiFreezeBase, IdAntiFreeze;

procedure TForm1.CheckStatus:
begin
   // Some code here
                      if IsFirstTime then
                       begin
                         VrSetLEDs('2', 'G');
                         SerialBoxNum := '';
                         if not DoPartReceived(LastValidRead, '-1') then
                         begin
                           VrSetLEDs('2', 'R');
                           Exit;
                         end;
                         // if not serial pallet number exists
                         {---- here application resets -----------}
                         if SerialPalletNum = '' then
                           if not DoPartReceived(LastValidRead, '0') then
                           begin
                             VrSetLEDs('2', 'R');
                             Exit;
                           end;
                         IsFirstTime := False;
                         VrLed1.Active := isFirstTime;
                       end;
                       if not DOoartProcessed(LastValidRead) then
                         begin
                           VrSetLEDs('3', 'R');
                           lAMSMsg.text := 'Fallo en PartProcessed';
                           Exit;
                         end
                       else
                         VrSetLEDs('3', 'G');
                       Sleep(100);
                       if not DoMovePartToStock(LastValidRead) then
                         begin
                           VrSetLEDs('4', 'R');
                           lAMSMsg.text := 'Fallo en MovePartToStock';
                           Exit;
                         end
                       else
                         begin
                           VrSetLEDs('4', 'G');
                           // Update Parcial Table
                           SetBoxStatus;
                         end;
                       Sleep(100);

end;


function TForm1.DoPartReceived(EventID, EventSwitch: String): Boolean;
var
   tSerialBoxNum, tSerialPalletNum, x, sData, Data: String;
   Errcode, sID, ErrorCode, NextStation, NextStatIdx, NextFuNo,
NextWorkPos, PartState: String;
   iSerie, i, ii: Integer;
   Char4, ParteEntera, len: Integer;
   ByteArr: Array of Byte;
   IntelFormat, ParteDecimal: Variant;
   s: Char;
begin
   // Build String
   sData := '<?xml version="1.0" encoding="UTF-8" ?><root><header
version="1.0" eventId="' + GetIdUniqNum + '"  eventName="partReceived"
eventSwitch="' + EventSwitch + '"><location lineNo="' + ThisStation1 +'"';
   sData := sData + ' statNo="' + ThisStation + '" statIdx="' +
ThisStation2 + '" fuNo="' + ThisStation3 + '" workPos="' + ThisStation4
+ '" toolPos="' + ThisStation5 + '"';
   sData := sData + ' processName="' + 'PACKING' + '"
application="PLC"/></header><event><partReceived
identifier="1"/></event><body>';
   sData := sData + '<items><item name="typeNo" value="' +
dbProducto.Text + '" dataType="8" />';
   sData := sData + '</items></body></root>';
   Data := sData;
   len := Length(Data) + 4;
   IntelFormat := (len / 256);
   ParteEntera := Trunc(IntelFormat);
   ParteDecimal := Abs(IntelFormat - ParteEntera);
   Char4 := ParteDecimal * 256;
   Data := Chr(0) + Chr(0) + Chr(ParteEntera) + Chr(Char4) + Data;
   SetLength(ByteArr, Length(Data) - 1);
   LogToFile(sData, 'Write SetPrtRec');   //write in a text file
   for i := 1 to Length(Data) do
   begin
     s := Data[i];
     ByteArr[i - 1] := Ord(s);
   end;
   with IdTCPClient1 do
   begin
     Disconnect;
     Host := AMSHost;
     Port := StrToInt(AMSPort2);
     Connect;
     OpenWriteBuffer;
     try
       // WriteChar(#0);
       // WriteChar(#0);
       // WriteChar(Char(ParteEntera));
       // WriteChar(Char(Char4));
       Write(Data);
     except
       CancelWriteBuffer;
       raise;
       Result := False;
       Exit;
     end;
     CloseWriteBuffer;
   end;
   Sleep(200);
   Application.ProcessMessages;
   try
     ii :=  IdTCPClient1.ReadInteger(True);
     x := IdTCPClient1.ReadLn('</root>', 1500, 2000);
     x := x + '</root>';
     LogToFile(x, 'Read SetPrtRec');
   except
     lAMSMsg.Text := 'SPR: Read from AMS Fail!';
     raise
   end;
   Application.ProcessMessages;
   IdTCPClient1.Disconnect;
   {
   Do something with response (x)
   }
   Result := True;
end;

Best Regards
Francisco Alvarado Salas

Replies:

www.cryer.info
Managed Newsgroup Archive