调试MSCOMM32控件的时候,偶尔出现一个错误:Variant array index of out of bounds。
怎么解决?

解决方案 »

  1.   

    相关代码如下:
    VAR
      reData:array of byte;//Variant;
      restr:string;
      mscommdata:array[0..4]of word;
      i,l:longint;procedure TForm1.MSComm1Comm(Sender: TObject);   //接收数据
    var
       wdata0,wdata1:word;
    begin
      redata:=mscomm1.Input;      // 接收数据
      restr:='';
      for i:=0 to vararrayhighbound(redata,1) do
        begin
          restr:=restr + inttohex(redata[i],2)+' ';
        end;
      memo3.Lines.Add(restr);
      for i:=0 to 4 do                           //提起前五位数据
          mscommdata[i]:=redata[i];
      if redata[0] =241 then
         begin
          wdata0:=mscommdata[2]*256+mscommdata[1];
          wdata1:=mscommdata[4]*256+mscommdata[3];
          y1:=y10-round(wdata0/40);           //第一通道波形数据
          y2:=y20-round(wdata1/40);           //第二通道波形数据
          label14.Caption:=inttostr(wdata0);
         end
         else ;//exit;end;在单步执行时,发现不了!大家帮看哈!!
      

  2.   

    会不会是redata:=mscomm1.Input的时候长度不到5,而
    for i:=0 to 4 do                           //提起前五位数据
          mscommdata[i]:=redata[i];
    时候造成越界的
      

  3.   

    在memo3.Lines.Add(restr);之前用 Try Except end ;之后也用
    try 
    except 将I的值和数组的长度等信息显示出来。
    end;你确定是收到5个字节的数据吗?
    如果确定是5个,则定义成reData:array[0..4] of byte;
      

  4.   

    加Try Except end 我试过了,没有错误。
    我定义为MSCOMM接收到5字节数据才响应事件,也得到验证了
    如果定义为:reData:array[0..4] of byte;
    那么这句代码该怎么改:redata:=mscomm1.Input;      // 接收数据