不知如何在下面的程序中添加代码实现将接收的数据放在一个十三维的数组中。(因为我接收的数据是来自温度节点的数据,接收后在memo1中显示的格式为7E 42 21 02 03 01 01 1B 64 18 01 01 7E 7E 42 21 02 03 01 01 1C 68 18 01 01 7E )我希望另外定义一个十三维的数组,以便于对数组中的数据进行查找等。
procedure TserialportForm.MSComm1Comm(Sender:TObject);
     var
     i,InputLen:Integer;
     tmpInt:Integer;
     tmpvar:Variant;
     InputString:String;
     begin
     if MSComm1.CommEvent=ComEvReceive then
     begin
     InputLen:=MSComm1.InBufferCount;
     //接收二进制数据,转换为十六进制显示
     if HexShow then
     begin
     tmpvar:=MSComm1.Input;
     InputString:='';
     for i:=0 to InputLen-1 do
     begin
     tmpInt:=tmpvar[i];
     InputString:=InputString+upperCase(IntToHex(tmpINt,2)+' ');
 end;
     end
     //直接接收字符
     else begin
     InputString:=MSComm1.Input;
     end;
          serialportForm.Memo1.Text:=serialportForm.Memo1.Text+InputString;
     end
end;

解决方案 »

  1.   

    这是我修改过的程序:   
     procedure TserialportForm.MSComm1Comm(Sender:TObject);
         var
          sum,i,j,InputLen:Integer;
        tmpInt:Integer;
         tmpvar:Variant;
        InputString:String;
         tem:currency;
        c:array[0..12] of string;
         begin
         if MSComm1.CommEvent=ComEvReceive then
         begin
         InputLen:=MSComm1.InBufferCount;
         //接收二进制数据,转换为十六进制显示
         if HexShow then
         begin
         tmpvar:=MSComm1.Input;
         InputString:='';
         for i:=0 to InputLen-1 do
         begin
         tmpInt:=tmpvar[i];
         InputString:=InputString+UpperCase(IntToHex(tmpINt,2)+' ');
         if i<12 then
        c[i]:=IntToHex(tmpINt,2)
       else if i>12 then
         c[i mod 13]:=IntToHex(tmpINt,2);  if c[0]='7E' then
       begin
       for j:=i+1 to InputLen-1 do
         begin
         tmpInt:=tmpvar[j];
         if (j>0)and (j<12) then
        c[j]:=IntToHex(tmpINt,2)
       else if j>12 then
         c[j mod 13]:=IntToHex(tmpINt,2);
      if c[5]='01' then
      begin
                        sum:=hextoint(c[9]+c[8]);
                                 tem:=sum*0.01-40;
                                 if (tem>0)  and (tem<40)   then
                                 begin
                                 edit1.Text:=currtostr(tem);
                                     button4Click(sender);
                                 end
                                 else edit1.Text:='';
      end
      else if  c[5]='02' then  begin
                           sum:=hextoint(c[9]+c[8]);
                                 tem:=sum*0.01-40;
                             if (tem>0)  and (tem<40)   then
                                 begin
                                 edit2.Text:=currtostr(tem);
                                     button3Click(sender);
                                 end
                                 else edit2.Text:='';    end;
        end;
        end
        else if c[0]='42' then
    begin
       for j:=i+1 to InputLen-1 do
         begin
         tmpInt:=tmpvar[j];
         if (j>0)and (j<12) then
        c[j]:=IntToHex(tmpINt,2)
       else if j>12 then
         c[j mod 13]:=IntToHex(tmpINt,2);
      if c[4]='01' then
      begin
                            sum:=hextoint(c[8]+c[7]);
                                 tem:=sum*0.01-40;
                                 if (tem>0)  and (tem<40)   then
                                 begin
                                 edit1.Text:=currtostr(tem);
                                     button4Click(sender);
                                 end
                                 else edit1.Text:='';  end
      else if  c[4]='02' then
      begin
                            sum:=hextoint(c[8]+c[7]);
                                 tem:=sum*0.01-40;
                                 if (tem>0)  and (tem<40)   then
                                 begin
                                 edit2.Text:=currtostr(tem);
                                     button3Click(sender);
                                 end
                                 else edit2.Text:='';    end;
        end;
        end;
        end;
        end;
         end
           //直接接收字符
         else begin
         InputString:=MSComm1.Input;
         end;
              serialportForm.Memo1.Text:=serialportForm.Memo1.Text+InputString ;
         end;但是修改后,当我接收节点数据时,显示在edit1.text 或edit2.text中的开始是没有反映的,需要几十秒后才有反映,有时个显示的值也会出现错误,不知道是循环错误还是其它?
      

  2.   

    如果有做过单片机与串口通信这方面的,应该见过,我主要是想不到其它办法,因为数据是一个以7E开头,7E结尾,或以42开头,以7E结尾。所以我想着数据以十维来存,然后通过判断如果满足条件,则利用公式,计算温度值,并将其显示在文本框中!
      

  3.   

    我的想法具体是这样:delphi设计的串口调试助手,接收来自两个温度节点的数据显示在memo1中,然后我想着利用一个十三维的数组对数据查找,(因为来自节点的数据是一个以7E开头,7E结尾,或以42开头,以7E结尾的格式)如果符合条件,则显示在edit1和edit2中,此时触发button事件将其存入数据库中。代码如下:
        //处理控件的该事件,获取底层交换数据和连线状态
        procedure TserialportForm.MSComm1Comm(Sender:TObject);
         var
          sum,i,j,inputlen:Integer;
        tmpInt:Integer;
         tmpvar:Variant;
        InputString:String;
         tem:currency;
        c:array[0..12] of string;
         begin
         if MSComm1.CommEvent=ComEvReceive then
         begin
          inputlen:=MSComm1.InBufferCount;
         //接收二进制数据,转换为十六进制显示
         if HexShow then
         begin
          tmpvar:=MSComm1.Input;     InputString:='';
         for i:=0 to inputlen-1 do
         begin
         tmpInt:=tmpvar[i];
         InputString:=InputString+UpperCase(IntToHex(tmpINt,2)+' ');
         if i<12 then
        c[i]:=IntToHex(tmpINt,2)
       else if i>12 then
         c[i mod 13]:=IntToHex(tmpINt,2);  if c[0]='7E' then
       begin
       for j:=i+1 to inputlen-1 do
         begin
         tmpInt:=tmpvar[j];
         if (j>0)and (j<12) then
        c[j]:=IntToHex(tmpINt,2)
       else if j>12 then
         c[j mod 13]:=IntToHex(tmpINt,2);
      if c[5]='01' then
      begin
                        sum:=hextoint(c[9]+c[8]);
                                 tem:=sum*0.01-40;                             if sum<>-1   then
                                 begin
                                     edit1.Text:=currtostr(tem);
                                     button4Click(sender);
                                 end
                                 else edit1.Text :='wrong data';
      end
      else if  c[5]='02' then  begin
                           sum:=hextoint(c[9]+c[8]);
                                 tem:=sum*0.01-40;                         if sum<>-1  then
                                 begin
                                    edit2.Text:=currtostr(tem);
                                     button3Click(sender);
                                 end
                                 else edit2.Text:='wrong data';
    ;    end;
        end;
        end
        else if c[0]='42' then
    begin
       for j:=i+1 to inputlen-1 do
         begin
         tmpInt:=tmpvar[j];
         if (j>0)and (j<12) then
        c[j]:=IntToHex(tmpINt,2)
       else if j>12 then
         c[j mod 13]:=IntToHex(tmpINt,2);
      if c[4]='01' then
      begin
                            sum:=hextoint(c[8]+c[7]);
                                 tem:=sum*0.01-40;                             if sum<>-1   then
                                 begin
                                      edit1.Text:=currtostr(tem);
                                     button4Click(sender);
                                 end
                                 else edit1.Text:='wrong data';  end
      else if  c[4]='02' then
      begin
                            sum:=hextoint(c[8]+c[7]);
                                 tem:=sum*0.01-40;                             if sum<>-1   then
                                 begin
                                 edit2.Text:=currtostr(tem);
                                     button3Click(sender);
                                 end
                                 else edit2.Text:='wrong data';    end;
        end;
        end;
        end;
        end;
         end
           //直接接收字符
         else begin
         InputString:=MSComm1.Input;
         end;
              serialportForm.Memo1.Text:=serialportForm.Memo1.Text+InputString ;
         end;
         //设置MSComm控件的数据接收的方式
        procedure TserialportForm.Checkbox3Click(Sender:TObject);
         begin
         if  checkbox3.Checked then
         begin
         MSComm1.InputMode:=1;
         HexShow:=True;
         end
         else begin
          MSComm1.InputMode:=0;
          HexShow:=False;
          end;
          end;procedure TserialportForm.Button4Click(Sender: TObject);
    begin
     adoquery1.Append ;
    adoquery1.FieldByName('时间').Asdatetime :=(now);
     adoquery1.FieldByName('节点号').AsInteger :=1;
      adoquery1.FieldByName('温度').Asfloat :=strtofloat(edit1.text);
      adoquery1.Post;
    end;procedure TserialportForm.Button3Click(Sender: TObject);
    begin
     adoquery1.Append ;
    adoquery1.FieldByName('时间').Asdatetime :=(now);
     adoquery1.FieldByName('节点号').AsInteger :=2;
      adoquery1.FieldByName('温度').Asfloat :=strtofloat(edit2.text);
      adoquery1.Post;
    end;存在的问题是:最后存入SQL中data表的数据存在很多重复。另外edit1 edit2中的数据开始是没有反映的?所以希望大家帮我想想办法,让存入SQL中data表的数据不存在重复,并且使得edit1 edit2中的数据只要当memo1中开始显示时就能正确反映
      

  4.   

    我的想法具体是这样:delphi设计的串口调试助手,接收来自两个温度节点的数据显示在memo1中,然后我想着用一个一维十三个元素的数组对数据查找,(因为来自节点的数据是一个以7E开头,7E结尾,或以42开头,以7E结尾的格式)如果符合条件,则显示在edit1和edit2中,此时触发button事件将其存入数据库中。代码如下:
      //处理控件的该事件,获取底层交换数据和连线状态
      procedure TserialportForm.MSComm1Comm(Sender:TObject);
      var
      sum,i,j,inputlen:Integer;
      tmpInt:Integer;
      tmpvar:Variant;
      InputString:String;
      tem:currency;
      c:array[0..12] of string;
      begin
      if MSComm1.CommEvent=ComEvReceive then
      begin
      inputlen:=MSComm1.InBufferCount;
      //接收二进制数据,转换为十六进制显示
      if HexShow then
      begin
      tmpvar:=MSComm1.Input;  InputString:='';
      for i:=0 to inputlen-1 do
      begin
      tmpInt:=tmpvar[i];
      InputString:=InputString+UpperCase(IntToHex(tmpINt,2)+' ');
      if i<12 then
      c[i]:=IntToHex(tmpINt,2)
      else if i>12 then
      c[i mod 13]:=IntToHex(tmpINt,2);  if c[0]='7E' then
      begin
      for j:=i+1 to inputlen-1 do
      begin
      tmpInt:=tmpvar[j];
      if (j>0)and (j<12) then
      c[j]:=IntToHex(tmpINt,2)
      else if j>12 then
      c[j mod 13]:=IntToHex(tmpINt,2);
      if c[5]='01' then
      begin
      sum:=hextoint(c[9]+c[8]);
      tem:=sum*0.01-40;  if sum<>-1 then
      begin
      edit1.Text:=currtostr(tem);
      button4Click(sender);
      end
      else edit1.Text :='wrong data';
      end
      else if c[5]='02' then  begin
      sum:=hextoint(c[9]+c[8]);
      tem:=sum*0.01-40;  if sum<>-1 then
      begin
      edit2.Text:=currtostr(tem);
      button3Click(sender);
      end
      else edit2.Text:='wrong data';
    ;  end;
      end;
      end
      else if c[0]='42' then
    begin
      for j:=i+1 to inputlen-1 do
      begin
      tmpInt:=tmpvar[j];
      if (j>0)and (j<12) then
      c[j]:=IntToHex(tmpINt,2)
      else if j>12 then
      c[j mod 13]:=IntToHex(tmpINt,2);
      if c[4]='01' then
      begin
      sum:=hextoint(c[8]+c[7]);
      tem:=sum*0.01-40;  if sum<>-1 then
      begin
      edit1.Text:=currtostr(tem);
      button4Click(sender);
      end
      else edit1.Text:='wrong data';  end
      else if c[4]='02' then
      begin
      sum:=hextoint(c[8]+c[7]);
      tem:=sum*0.01-40;  if sum<>-1 then
      begin
      edit2.Text:=currtostr(tem);
      button3Click(sender);
      end
      else edit2.Text:='wrong data';  end;
      end;
      end;
      end;
      end;
      end
      //直接接收字符
      else begin
      InputString:=MSComm1.Input;
      end;
      serialportForm.Memo1.Text:=serialportForm.Memo1.Text+InputString ;
      end;
      //设置MSComm控件的数据接收的方式
      procedure TserialportForm.Checkbox3Click(Sender:TObject);
      begin
      if checkbox3.Checked then
      begin
      MSComm1.InputMode:=1;
      HexShow:=True;
      end
      else begin
      MSComm1.InputMode:=0;
      HexShow:=False;
      end;
      end;procedure TserialportForm.Button4Click(Sender: TObject);
    begin
     adoquery1.Append ;
    adoquery1.FieldByName('时间').Asdatetime :=(now);
     adoquery1.FieldByName('节点号').AsInteger :=1;
      adoquery1.FieldByName('温度').Asfloat :=strtofloat(edit1.text);
      adoquery1.Post;
    end;procedure TserialportForm.Button3Click(Sender: TObject);
    begin
     adoquery1.Append ;
    adoquery1.FieldByName('时间').Asdatetime :=(now);
     adoquery1.FieldByName('节点号').AsInteger :=2;
      adoquery1.FieldByName('温度').Asfloat :=strtofloat(edit2.text);
      adoquery1.Post;
    end;存在的问题是:最后存入SQL中data表的数据存在很多重复。另外edit1 edit2中的数据开始是没有反映的?所以希望大家帮我想想办法,让存入SQL中data表的数据不存在重复,并且使得edit1 edit2中的数据只要当memo1中开始显示时就能正确反映