Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Jan : Re: Open a TCP Socket
| Subject: | Re: Open a TCP Socket |
| Posted by: | "Remy Lebeau (TeamB)" (no.spam@no.spam.com) |
| Date: | Fri, 1 Feb 2008 14:02:52 |
"Kim Jensen" <kim@comcasystems.com> wrote in message
news:47a389fd$1@newsgroups.borland.com...
> In SmartSniff I check TCP and show Characters over 127 as .
I would expect it to do that if you are viewing the data as ASCII text
instead of hex in its raw format.
> I received a : after connecting
Then you will have to call ReadChar() or ReadLn() immediately after calling
Connect(), depending on whether ':' has its own CRLF or not. Otherwise the
initial ':' will remain in the socket until after you send your first
command, messing up your reply handling since you will be processing the
replies out of order.
> I type ID then I receive
> outputs 0-3 = power sourcing outputs
> outputs 4-7 = power sourcing outputs
> outputs 8-11 = sinking outputs
> outputs 12-15 = sinking outputs
I am assuming that there are CRLF in between each of those "output" lines?
> I type MG @IN[00]
> then I receive:
> 1.0000
> :
Same with that. Is there a CRLF received after the "1.0000"?
Without seeing the actual raw data, it seems like you will have to call
ReadLn() in a loop until ':' or '?' are received. If the ':' and '?' are
themselves terminated with CRLF, then the reading of the reply data is very
straight-forward, ie:
procedure TMyForm.ReadReply(AReply: TStrings = nil);
var
Line: String;
begin
if AReply <> nil then AReply.Clear;
repeat
Line := IdTCPClient.IOHandler.ReadLn;
if (Line = ':') or (Line = '?') then
begin
// end of reply ...
if Line = '?' then raise Exception.Create('failed');
Exit;
end;
if AReply <> nil then AReply.Add(Line);
until False;
end;
procedure TMyForm.SendCommand(ACmd: String, AReply: TStrings = nil);
begin
IdTCPClient.IOHandler.WriteLn(ACmd);
ReadReply(AReply);
end;
procedure TMyForm.Button1Click(Sender: TObject);
var
Reply: TStringList;
begin
Reply := TStringList.Create;
try
IdTCPClient.Connect;
try
ReadReply;
// ok to send commands now...
SendCommand('ID', Reply);
// process Reply...
SendCommand('MG @IN[00]', Reply);
// process Reply...
finally
IdTCPClient.Disconnect;
end;
finally
Reply.Free;
end;
end;
However, if ':' and '?' do NOT have their own CRLF, then the coding becomes
a bit more tricky:
procedure TMyForm.ReadReply(AReply: TStrings = nil);
var
B: Byte;
Line: String;
begin
if AReply <> nil then AReply.Clear;
with IdTCPClient.IOHandler do
begin
repeat
while InputBuffer.Size = 0 do
begin
ReadFromSource(False);
CheckForDisconnect(True, True);
end;
B := InputBuffer.PeekByte(0);
if (B = Ord(':')) or (B = Ord('?')) then
begin
// end of reply, remove it from the buffer ...
InputBuffer.Remove(0);
if B = Ord('?') then raise Exception.Create('failed');
Exit;
end;
Line := IdTCPClient.IOHandler.ReadLn;
if AReply <> nil then AReply.Add(Line);
until False;
end;
end;
> Here is the copy of the Smartsniff's capture.
That is not the raw data I need to see.
Gambit