我现在利用spcomm控件进行串口通讯,由于要传送的字节比较大,
所以我就对数据包进行分割,每次传送521字节(包括包头和结束字符)。
发送程序如下:
procedure TFCOMM.Button2Click(Sender: TObject);   
var
  i,j:integer;
 begin
   count:=0;
   memo1.Clear;
   for j:=1 to 30 do
     begin
       sbuf[521*(j-1)+1]:=byte($aa);
       sbuf[521*(j-1)+2]:=byte($01);
       sbuf[521*(j-1)+3]:=byte($80);
       sbuf[521*(j-1)+4]:=byte($00);
       sbuf[521*(j-1)+5]:=byte($20);
       sbuf[521*(j-1)+6]:=byte($00);
       sbuf[521*(j-1)+7]:=byte($00);
       sbuf[521*(j-1)+8]:=byte($00);
       for i:=0 to ((512 div 16)-1) do
         begin
           sbuf[521*(j-1)+9+16*i]:=byte($49);
           sbuf[521*(j-1)+9+16*i+1]:=byte($07);
           sbuf[521*(j-1)+9+16*i+2]:=byte($37);
           sbuf[521*(j-1)+9+16*i+3]:=byte($a9);
           sbuf[521*(j-1)+9+16*i+4]:=byte($e6);
           sbuf[521*(j-1)+9+16*i+5]:=byte($af);
           sbuf[521*(j-1)+9+16*i+6]:=byte($06);
           sbuf[521*(j-1)+9+16*i+7]:=byte($fc);           sbuf[521*(j-1)+9+16*i+8]:=byte($01);
           sbuf[521*(j-1)+9+16*i+9]:=byte($68);
           sbuf[521*(j-1)+9+16*i+10]:=byte($30);
           sbuf[521*(j-1)+9+16*i+11]:=byte($4e);
           sbuf[521*(j-1)+9+16*i+12]:=byte($ac);
           sbuf[521*(j-1)+9+16*i+13]:=byte($b3);
           sbuf[521*(j-1)+9+16*i+14]:=byte($31);
           sbuf[521*(j-1)+9+16*i+15]:=byte($d2);
         end;
       sbuf[521*j]:=byte($55);
       senddata1;{调用发送函数}
    end;
end;procedure senddata1;     //自定义的发送数据过程
  var
    i,j:integer;
    commflg:boolean;
  begin
    viewstring:='';
    commflg:=true;
    for i:=(count*521+1) to ((length(sbuf) div 30)+count*521) do
      begin
        if not fcomm.comm2.writecommdata(@sbuf[i],1) then
          begin
            commflg:=false;
             messagedlg('发送失败!!!!!',mterror,[mbyes],0);
            break;
          end;
        sleep(2);         //发送时字节间的延时
        viewstring:=viewstring+inttohex(sbuf[i],2)+'';
      end;
      viewstring:='';
      for i:=(9+521*count) to (520+521*count) do
        begin
          viewstring:=viewstring+inttohex(sbuf[i],2)+'';
        end;
    fcomm.memo1.lines.add(viewstring);
    count:=count+1;     
    messagedlg('发送成功!',mtinformation,[mbyes],0);//!!!注意这一行,加上这一行,传送正确。但是去掉这一行,就会出现“发送失败!”,实在是不明白!!!
    if not commflg then
      begin
        messagedlg('发送失败!',mterror,[mbyes],0)
      end;
  end;sbuf:array[1..15630] of byte;
messagedlg('发送成功!',mtinformation,[mbyes],0);//!!!注意这一行,加上这一行,传送正确。
   //但是去掉这一行,就会出现“发送失败!”,实在是不明白!!!
请哪位大侠给看看呀,谢谢!!