N
Neo_leaf
Unregistered / Unconfirmed
GUEST, unregistred user!
UDPServer : TIdUDPServer;
lbLog : TListBox;
启动服务:
procedure TFrm_UDPServer.btnStartClick(Sender: TObject);
begin
try
UDPServer.DefaultPort := StrToInt(EdtPort.Text);
UDPServer.Active := True;
lbLog.Items.Add('启动服务器端成功!');
except
lbLog.Items.Add('启动服务器端失败!');
end;
end;
procedure TFrm_UDPServer.BtnSendClick(Sender: TObject);
var
sBuf, sTest : AnsiString;
Buf: array [0..1023] of Char;
iSize,i: Integer;
begin
sBuf := Trim(EdtSendData.Text); //'40 40 40 40 01 fb 05 00 00 00 00 21 00 01 00 00 23';
sBuf := StringReplace(sBuf, ' ', '', [rfReplaceAll]);
iSize := Length(sBuf) div 2;
ZeroMemory(@Buf, SizeOf(Buf));
HexToBin(PChar(@sBuf[1]), Buf, SizeOf(Buf));
sTest := '';
for i := 0 to iSize-1 do
sTest := sTest + Buf;
UDPServer.SendBuffer(Trim(EdtDesIP.Text),StrToInt(EdtDesPort.Text),Buf, iSize);
(这里发送一条命令,是Connect Device的,如果连续发送多条命令,有验证密码、读取设备等)
{
UDPServer.SendBuffer(Trim(EdtDesIP.Text),StrToInt(EdtDesPort.Text),‘check psw’, iSize);
UDPServer.SendBuffer(Trim(EdtDesIP.Text),StrToInt(EdtDesPort.Text),‘Get Device Id’, iSize);
}
lbLog.Items.Add(s);
end;
读取返回值:
procedure TFrm_UDPServer.UDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
DataStringStream: TStringStream;
i : Integer;
b : byte;
begin
DataStringStream := TStringStream.Create('');
try
DataStringStream.CopyFrom(AData, AData.Size);
s := '';
DataStringStream.Position := 0;
while(DataStringStream.Position<DataStringStream.Size)do
begin
try
DataStringStream.Read(b,1);
s := s+IntToHex(b,2);
except
end;
end;
end;
end;
-----------------
现在是这样的,当发送一条命令时,在OnUDPRead事件里读取返回结果,没问题;
但是,当我同时发送三条命令时,则,程序的运行顺序是:
1、连续运行三条发送命令
2、进入OnUDPRead事件里读取返回结果(三个结果一起读出)。
我本来想,应该是发送一条命令后立即去读结果,然而不是这样的,
请问各位,
该如何做才可以使其发送一条命令后就立即去读结果??
200分呈上。。。。。。
lbLog : TListBox;
启动服务:
procedure TFrm_UDPServer.btnStartClick(Sender: TObject);
begin
try
UDPServer.DefaultPort := StrToInt(EdtPort.Text);
UDPServer.Active := True;
lbLog.Items.Add('启动服务器端成功!');
except
lbLog.Items.Add('启动服务器端失败!');
end;
end;
procedure TFrm_UDPServer.BtnSendClick(Sender: TObject);
var
sBuf, sTest : AnsiString;
Buf: array [0..1023] of Char;
iSize,i: Integer;
begin
sBuf := Trim(EdtSendData.Text); //'40 40 40 40 01 fb 05 00 00 00 00 21 00 01 00 00 23';
sBuf := StringReplace(sBuf, ' ', '', [rfReplaceAll]);
iSize := Length(sBuf) div 2;
ZeroMemory(@Buf, SizeOf(Buf));
HexToBin(PChar(@sBuf[1]), Buf, SizeOf(Buf));
sTest := '';
for i := 0 to iSize-1 do
sTest := sTest + Buf;
UDPServer.SendBuffer(Trim(EdtDesIP.Text),StrToInt(EdtDesPort.Text),Buf, iSize);
(这里发送一条命令,是Connect Device的,如果连续发送多条命令,有验证密码、读取设备等)
{
UDPServer.SendBuffer(Trim(EdtDesIP.Text),StrToInt(EdtDesPort.Text),‘check psw’, iSize);
UDPServer.SendBuffer(Trim(EdtDesIP.Text),StrToInt(EdtDesPort.Text),‘Get Device Id’, iSize);
}
lbLog.Items.Add(s);
end;
读取返回值:
procedure TFrm_UDPServer.UDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
DataStringStream: TStringStream;
i : Integer;
b : byte;
begin
DataStringStream := TStringStream.Create('');
try
DataStringStream.CopyFrom(AData, AData.Size);
s := '';
DataStringStream.Position := 0;
while(DataStringStream.Position<DataStringStream.Size)do
begin
try
DataStringStream.Read(b,1);
s := s+IntToHex(b,2);
except
end;
end;
end;
end;
-----------------
现在是这样的,当发送一条命令时,在OnUDPRead事件里读取返回结果,没问题;
但是,当我同时发送三条命令时,则,程序的运行顺序是:
1、连续运行三条发送命令
2、进入OnUDPRead事件里读取返回结果(三个结果一起读出)。
我本来想,应该是发送一条命令后立即去读结果,然而不是这样的,
请问各位,
该如何做才可以使其发送一条命令后就立即去读结果??
200分呈上。。。。。。