在onReceiveData事件中获得端口数据,下面是我程序中的一段代码:
procedure TNiuka_inform.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
str,str1:string;
i,state,postion:integer;
rbuf:array [1..1024] of char;
begin
inherited;
try
begin
str:='';
state:=0;
str1:='';
postion:=0;
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
str:=str+rbuf[i];
for i:=1 to length(str) do
begin
if copy(str,i,1)=' 'then
begin
state:=state+1;
if state<2 then
postion:=i;
end;
if state=2 then
begin
str1:=copy(str,postion,i-postion);
break;
end;
end;
if edit16.Focused then
edit16.Text:=inttostr(strtoint(str1));
if edit17.Focused then
edit17.Text:=inttostr(strtoint(str1));
end;
except
on e:exception do
begin
showmessage(e.message);
end;
end;
end;