这是IdTCPClient
中传入的Delphi
连接的阅读主题。
unit UnitWorkerThread;
interface
uses
System.Classes,
IdTCPClient;
type
TDataEvent = procedure(const Data: Byte) of object;
TReadingThread = class(TThread)
private
FClient: TIdTCPClient;
FData : Byte;
FOnData: TDataEvent;
procedure DataReceived;
protected
procedure Execute; override;
public
constructor Create(AClient: TIdTCPClient); reintroduce;
property OnData: TDataEvent read FOnData write FOnData;
end;
implementation
constructor TReadingThread.Create(AClient: TIdTCPClient);
begin
inherited Create(True);
FClient := AClient;
end;
procedure TReadingThread.Execute;
begin
while not Terminated do
begin
FData := FClient.IOHandler.ReadByte;
if Assigned(FOnData) then
Synchronize(DataReceived);
end;
end;
procedure TReadingThread.DataReceived;
begin
if Assigned(FOnData) then
FOnData(FData);
end;
end.
这就是我在Form
上创建和处理它的方式。
procedure TFormLogin.IdTCPClient1Connected(Sender: TObject);
var
Thread : TReadingThread;
begin
try
Thread := TReadingThread.Create(IdTCPClient1);
Thread.OnData := DataReceived;
Thread.Start;
except
end;
end;
procedure TFormLogin.DataReceived(const Data: Byte);
begin
FormHome.ModbusClient1.PutRxData(Data);
end;
当逐步调试时,我无法理解为什么只接收数据。
可能导致什么呢?