Friday, 11 November 2022
  6 Replies
  1.1K Visits
  Subscribe
I'm attempting to connect to a TCP server. After connecting to the TCP server I need to send hex values "F7 25". After sending that it should return a mix of BCD, HEX and ASCII.

Using a TCP Client app from the windows store I was able to achieve this. See attachment: tcpClient.png

When attempting this with sgcWebsockets configured to use TCP according to the manual I do not seem to get a response at all. The end of of the message differs with each command so I do not know when its the end of the response. I do not have access to the server as it's a machine I'm connecting to.

The docs say the following:
Send "F7 25" (HEX) over TCP and the response will be as follows:
CODE - 4 BYTES (BCD) (1 ~ 999999)
RECORD SIZE - 2 BYTES (HEX)
CSV DATA - ASCII

Using the TCP client the response I got is as follows:

00 00 00 01 00 46 54 00 ED 21 09 00 00 50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 03 07 05 74 65 73 74 31 0D 07 05 74 65 73 74 32 0D 07 05 74 65 73 74 33 0D 07 05 74 65 73 74 34 0C 0C


I have attached a test project. You can also see the code below:

unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, IdCmdTCPClient,
IdContext, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
FMX.Memo.Types, FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox,
FMX.Memo, System.Threading, System.StrUtils, IdGlobal, sgcBase_Classes,
sgcSocket_Classes, sgcTCP_Classes, sgcWebSocket_Classes,
sgcWebSocket_Classes_Indy, sgcWebSocket_Client, sgcWebSocket, sgcWebSocket_Types;

type
TForm1 = class(TForm)
mmolOG: TMemo;
tlb1: TToolBar;
btn1: TButton;
btn2: TButton;
tcpClient: TIdTCPClient;
ws1: TsgcWebSocketClient;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure ws1Binary(Connection: TsgcWSConnection; const Data: TMemoryStream);
procedure ws1Connect(Connection: TsgcWSConnection);
procedure ws1Disconnect(Connection: TsgcWSConnection; Code: Integer);
procedure ws1Message(Connection: TsgcWSConnection; const Text: string);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.btn1Click(Sender: TObject);
begin
if ws1.Connected then
begin
ws1.Active := False;
Exit;
end;

ws1.Host := '192.168.1.111';
ws1.Port := 2111;
ws1.Specifications.RFC6455 := false;
ws1.Active := True;
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
// Send Hex "F7 25"
// ws1.WriteData(#$F7#$25);
ShowMessage(ws1.WriteAndWaitData(#$F7#$25));
end;

procedure TForm1.ws1Binary(Connection: TsgcWSConnection; const Data:
TMemoryStream);
begin
mmolOG.Lines.Add(data.ToString);
end;

procedure TForm1.ws1Connect(Connection: TsgcWSConnection);
begin
Connection.TCPEndOfFrameScanBuffer := eofScanAllBytes;

mmolOG.Lines.Add('Connected');
end;

procedure TForm1.ws1Disconnect(Connection: TsgcWSConnection; Code: Integer);
begin
mmolOG.Lines.Add('disconnected');
end;

procedure TForm1.ws1Message(Connection: TsgcWSConnection; const Text: string);
begin
mmolOG.Lines.Add(Text);
end;

end.


Can you please assist me where I'm going wrong?