procedure TF_COMTest.SendStr;
var
i: Integer;
Strs : array [1..30] of Char;
begin
Edit1.Text:=StrSend;
StrRecv:='';
TimeOut:=0;
for i:=1 to Length(StrSend) do Strs:=StrSend;
sio_write(Port,@Strs,Length(StrSend));
sio_write(Port,@Strs,7);
CheckRecv;
end;
function TF_COMTest.ReceiveStr:String;
var
ret, i:Integer;
Datas : array [1..22] of Char;
begin
Result:='';
ret:=sio_iqueue(Port);
if ret>0 then
begin
if ret>20 then ret:=20;
sio_read(Port,@Datas,ret);
for i:=1 to ret do
begin
Result:=Result+Datas;
end;
end;
end;
procedure TF_COMTest.Open;
var
ret:Integer;
begin
Port:=RadioGroup1.ItemIndex+1;
ret := sio_open(Port);
if ret <> SIO_OK then
begin
MessageDlg('通信端口 COM'+IntToStr(Port)+'不存在或在使用中',mtWarning,[mbOK],0);
Exit;
end;
sio_ioctl(Port,B9600,P_EVEN or BIT_7 or STOP_1);
SendRead;
end;
procedure TF_COMTest.FormCreate(Sender: TObject);
begin
ShowOk:=False;
Port:=RadioGroup1.ItemIndex+1;
ScanTimes:=0;
TimeOut:=0;
ComErr:=False;
StrSend:='';
StrRecv:='';
StartID:=2;
EndID:=2;
MemAddr:=0;
ID:=StartID-1;
end;
procedure TF_COMTest.FormShow(Sender: TObject);
begin
if ShowOk=True then Exit;
ShowOk:=True;
Open;
ScanTimer(Sender);
end;
procedure TF_COMTest.FormClose(Sender: TObject; var Action: TCloseAction);
begin
sio_close(Port);
end;
procedure TF_COMTest.CheckRecv;
var
i, LenSend, LenRecv:Integer;
begin
TimeOut:=TimeOut+1;
StrRecv:=StrRecv+ReceiveStr;
Edit2.Text:=Copy(StrRecv,1,16);
LenSend:=Length(StrSend);
LenRecv:=Length(StrRecv);
if (LenRecv<12) then Exit;
i:=1;
while i<LenRecv do
begin
if Copy(StrRecv,i,LenSend)=StrSend then
begin
StrRecv:=Copy(StrRecv,i+LenSend,LenRecv-LenSend-i+1);
LenRecv:=Length(StrRecv);
i:=LenRecv+1;
end
else
begin
i:=i+1;
end;
end;
end;
procedure TF_COMTest.SendRead;
begin
ID:=ID+1;
if ID>EndID then ID:=StartID;
StrSend:=')'+CHR(13); //')'
SendStr;
end;
procedure TF_COMTest.scanTimer(Sender: TObject);
begin
if ID<StartID then ID:=StartID;
if StrSend<>'' then CheckRecv;
if TimeOut>ScanTimes then
begin
StrSend:='';
ComErr:=True;
end;
if StrSend='' then SendRead;
end;