for   i:=1   to   9   do 
            begin 
                  s:=pchar(label3.Caption); 
                  if   opencom(1,9600)=1   then 
                  begin 
                         if     (senddata(s,length(label3.Caption))=0)     then 
                                                        begin 
                                                        Memo1.Text:=label3.Caption+'   '+'写卡失败'; 
                                                          APPLICATION.ProcessMessages; 
                                                          sleep(1500); 
                                                          closecom();                                                             end   ;                             if     (senddata(s,length(label3.Caption))=1)   then 
                                           begin 
                                           Memo1.Text:=label3.Caption+'   '+'写卡成功'; 
                                           label3.caption:=inttostr(strtoint(label3.caption)+1); 
                                           APPLICATION.ProcessMessages; 
                                            sleep(1500); 
                                            closecom(); 
                                            end; 
                      end; 
            end;   在第一次循环中第3个if(senddata(s,length(label3.Caption))=1) 里的条件要满足2次,里面的语句才能执行。问题出在哪里呢???

解决方案 »

  1.   

    通讯代码这么写?
    1.有必要每次都执行打开串的操作么?(对于同一个串口)
    2.为什么不把发送过程定义成一个函数?
    3.从你的操作来看,我不明白你为什么要发送9次同样的数据给设备;一般,我是按下面的操作顺序完成通讯的
    var
      rlt:boolean;
      trycount:byte;trycount:=1;
    rlt:=false;
    rtl:=openCOmm();
    if rlt then
    while trycount<3 do
    begin
    rlt:=sendData();
    if rlt then
      begin
        //.....发送成功 !
        trycount:=4;
      end
    else
      inc(trycount);
    end;
    closeCOmm;