为什么我的MSComm控件的OnComm事件会不断的重复执行啊,导致接收的数据为空,报“variant array index out of bounds”的错误,我的MSComm控件的参数是这样的:procedure TForm1.FormCreate(Sender: TObject);
begin
MSComm1.CommPort := 1;
MSComm1.DTREnable := false; //确定在通信时是否使(DTR)线有效
MSComm1.RTSEnable := false; //
MSComm1.EOFEnable := false; //
MSComm1.Handshaking := comNone;//握手协议
MSComm1.NullDiscard := false; MSComm1.InBufferSize := 1024;
MSComm1.OutBufferSize := 1024;
MSComm1.InputLen := 0; //设置并返回Input属性从缓冲区中每次读取的字
MSComm1.RThreshold := 5; //设置或返回输入缓冲区中存放接收字符的最小数
MSComm1.SThreshold := 0; //输出
MSComm1.InputMode := comInputModeBinary;//设置或返回 Input 属性取回的数据的类型为16进制
MSComm1.Settings := '9600,n,8,1' //BCD码
end;OnComm事件是这样的:procedure TForm1.MSComm1Comm(Sender: TObject);
var
codesize : Integer;
begin
codesize := MSComm1.InBufferCount;
data := varArrayCreate([0,codesize-1],varByte);
case MSComm1.CommEvent of
comEvCD,comEvCTS,comEvDSR,comEvSend,comEvRing,comEvReceive:
begin
buffer := MSComm1.Input;
data := buffer;
MSComm1.InBufferCount := 0;
if (data[0] = $AA) and (data[1] = $3) and (data[2] = $A2) and (data[3] = $0) and (data[4] = $25) then
begin
Send_Succ := true;
if length(MemoShow.Text) = 7 then
begin
MemoShow.Text := '';
end;
if IntToStr(CommandCode) = '$A' then
begin
MemoShow.Text := '*';
end
else if IntToStr(CommandCode) = '$B' then
begin
MemoShow.Text := '#';
end
else
begin
MemoShow.Text := MemoShow.Text + IntToStr(CommandCode);
end;
end
else if (data[0] = $AA) and (data[1] = $3) and (data[2] = $A1) then
begin
FCS := 0;
buf[0] := StrToInt('$AA');
buf[1] := StrToInt('$3');
buf[2] := StrToInt('$A2');
buf[3] := StrToInt('$0');
buf[4] := StrToInt('$25');
MSComm1.InBufferCount := 0;
sBuf := buf;
MSComm1.Output := sBuf; State := data[3];
case State of
0:
begin
lbShow.Caption := '住户呼叫中心机.....';
end;
1:
begin
lbShow.Caption := '住户通话中.....';
end;
2:
begin
lbShow.Caption := '网络呼叫中.....';
end;
3:
begin
lbShow.Caption := '网络通话中.....';
end;
4:
begin
lbShow.Caption := '网络监视中....按*键退出!';
end;
5:
begin
lbShow.Caption := '无此住户';
end;
6:
begin
lbShow.Caption := '网络忙';
end;
7:
begin
lbShow.Caption := '网络空闲';
end;
end;
end
else
begin
Send_Succ := False;
end;
end;
end;
end;
begin
MSComm1.CommPort := 1;
MSComm1.DTREnable := false; //确定在通信时是否使(DTR)线有效
MSComm1.RTSEnable := false; //
MSComm1.EOFEnable := false; //
MSComm1.Handshaking := comNone;//握手协议
MSComm1.NullDiscard := false; MSComm1.InBufferSize := 1024;
MSComm1.OutBufferSize := 1024;
MSComm1.InputLen := 0; //设置并返回Input属性从缓冲区中每次读取的字
MSComm1.RThreshold := 5; //设置或返回输入缓冲区中存放接收字符的最小数
MSComm1.SThreshold := 0; //输出
MSComm1.InputMode := comInputModeBinary;//设置或返回 Input 属性取回的数据的类型为16进制
MSComm1.Settings := '9600,n,8,1' //BCD码
end;OnComm事件是这样的:procedure TForm1.MSComm1Comm(Sender: TObject);
var
codesize : Integer;
begin
codesize := MSComm1.InBufferCount;
data := varArrayCreate([0,codesize-1],varByte);
case MSComm1.CommEvent of
comEvCD,comEvCTS,comEvDSR,comEvSend,comEvRing,comEvReceive:
begin
buffer := MSComm1.Input;
data := buffer;
MSComm1.InBufferCount := 0;
if (data[0] = $AA) and (data[1] = $3) and (data[2] = $A2) and (data[3] = $0) and (data[4] = $25) then
begin
Send_Succ := true;
if length(MemoShow.Text) = 7 then
begin
MemoShow.Text := '';
end;
if IntToStr(CommandCode) = '$A' then
begin
MemoShow.Text := '*';
end
else if IntToStr(CommandCode) = '$B' then
begin
MemoShow.Text := '#';
end
else
begin
MemoShow.Text := MemoShow.Text + IntToStr(CommandCode);
end;
end
else if (data[0] = $AA) and (data[1] = $3) and (data[2] = $A1) then
begin
FCS := 0;
buf[0] := StrToInt('$AA');
buf[1] := StrToInt('$3');
buf[2] := StrToInt('$A2');
buf[3] := StrToInt('$0');
buf[4] := StrToInt('$25');
MSComm1.InBufferCount := 0;
sBuf := buf;
MSComm1.Output := sBuf; State := data[3];
case State of
0:
begin
lbShow.Caption := '住户呼叫中心机.....';
end;
1:
begin
lbShow.Caption := '住户通话中.....';
end;
2:
begin
lbShow.Caption := '网络呼叫中.....';
end;
3:
begin
lbShow.Caption := '网络通话中.....';
end;
4:
begin
lbShow.Caption := '网络监视中....按*键退出!';
end;
5:
begin
lbShow.Caption := '无此住户';
end;
6:
begin
lbShow.Caption := '网络忙';
end;
7:
begin
lbShow.Caption := '网络空闲';
end;
end;
end
else
begin
Send_Succ := False;
end;
end;
end;
end;
data := varArrayCreate([0,codesize-1],varByte);-------->>>>>>>>>>.
data := varArrayCreate([0,-1],varByte);是会出错的。
把这句放到里面。
case MSComm1.CommEvent of
comEvCD,comEvCTS,comEvDSR,comEvSend,comEvRing,comEvReceive:
begin
data := varArrayCreate([0,codesize-1],varByte);
buffer := MSComm1.Input;
”,就是这个data的数据为空的
if datasize<*** then
exit;
就是,如果datasize小于完整的数据长度,就是错误的数据,此时就不能进行下面的处理的。