控件spcomm
想法 :依次向串口1、2、3、4发测试数据
如果comm1收到回应比如:BB01000001dd 这时改一个全局就是havefound:='true'
但是明明收到了数也改了值,havefound还是说没变,就是:
if havefound<>;true' then
try
comm1.StopComm;
comm1.CommName:='com1';
comm1.StartComm;
sleep(100);
senddate('....');
except
end;
if havefound<>;true' then
try
comm1.StopComm;
comm1.CommName:='com2';
comm1.StartComm;
sleep(100);
senddate('....');
except
end;if havefound<>;true' then
try
comm1.StopComm;
comm1.CommName:='com3';
comm1.StartComm;
sleep(100);
senddate('....');
except
end;我明明连的com2上了,但是最后总是不能实现

解决方案 »

  1.   

    呵呵,程序结构的问题,该成这样试试:
    var
      StartTick: Cardinal;if havefound <> true then
    try
    comm1.StopComm;
    comm1.CommName:='com1';
    comm1.StartComm;StartTick := GetTickCount;
    while GetTickCount - StartTick < 200 do
      Application.ProcessMessage;
      
    senddate('....');
    except
    end;...
    ...
    ...
      

  2.   

    我度过了还是不行,好像comm1在接收时执行的havefound:='true'没用上,就是说明明收到了数据并把havefound改成true了,但系统还是认为havefound='false'
      

  3.   

    谢谢了,成功。只是
    StartTick := GetTickCount;
    while GetTickCount - StartTick < 200 do
      Application.ProcessMessage;和sleep有不同的地方,我了不想了,我的代码:
    procedure TMainForm.FormShow(Sender: TObject);
    begin
    writeini('comkou','');
    setcom;
    Comm1.StopComm;
    end;procedure writeini(str,str1:string);
    var
    myinifile:tinifile;
    comkou,version:string;
    begin
      MyIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName )+'system.ini');
      if str='comkou'  then
      MyIniFile.WriteString('system', 'comkou', str1);
      if str='version' then
      MyIniFile.WriteString('system', 'version', str1);
      MyIniFile.Free;
    end;procedure Setcom;
    var
      StartTick: Cardinal;
    begin
    if (readini('comkou') <> 'com1') and (readini('comkou') <> 'com2') and (readini('comkou') <> 'com3') and (readini('comkou') <> 'com4')  then
      try
      mainform.comm1.StopComm;
      mainform.comm1.CommName:='com1';
      mainform.comm1.StartComm;  sbuf[1]:=byte($EB); //帧头
      sbuf[2]:=byte($06);  //命令号
      sbuf[3]:=byte($00);
      sbuf[4]:=byte($00);
      sbuf[5]:=byte($06);
      sbuf[6]:=byte($BB);
      sleep(100);
      senddata(6);
      StartTick := GetTickCount;
      while GetTickCount - StartTick < 200 do
      Application.ProcessMessages;  except
      end;
    if (readini('comkou') <> 'com1') and (readini('comkou') <> 'com2') and (readini('comkou') <> 'com3') and (readini('comkou') <> 'com4')  then
      try
      mainform.comm1.StopComm;
      mainform.comm1.CommName:='com2';
      mainform.comm1.StartComm;  sbuf[1]:=byte($EB); //帧头
      sbuf[2]:=byte($06);  //命令号
      sbuf[3]:=byte($00);
      sbuf[4]:=byte($00);
      sbuf[5]:=byte($06);
      sbuf[6]:=byte($BB);
      sleep(100);
      senddata(6);
      StartTick := GetTickCount;
      while GetTickCount - StartTick < 200 do
      Application.ProcessMessages;
      except
      end;if (readini('comkou') <> 'com1') and (readini('comkou') <> 'com2') and (readini('comkou') <> 'com3') and (readini('comkou') <> 'com4')  then
      try
      mainform.comm1.StopComm;
      mainform.comm1.CommName:='com3';
      mainform.comm1.StartComm;  sbuf[1]:=byte($EB); //帧头
      sbuf[2]:=byte($06);  //命令号
      sbuf[3]:=byte($00);
      sbuf[4]:=byte($00);
      sbuf[5]:=byte($06);
      sbuf[6]:=byte($BB);
      sleep(100);
      senddata(6);
      StartTick := GetTickCount;
      while GetTickCount - StartTick < 200 do
      Application.ProcessMessages;
      except
      end;if (readini('comkou') <> 'com1') and (readini('comkou') <> 'com2') and (readini('comkou') <> 'com3') and (readini('comkou') <> 'com4')  then
      try
      mainform.comm1.StopComm;
      mainform.comm1.CommName:='com4';
      mainform.comm1.StartComm;  sbuf[1]:=byte($EB); //帧头
      sbuf[2]:=byte($06);  //命令号
      sbuf[3]:=byte($00);
      sbuf[4]:=byte($00);
      sbuf[5]:=byte($06);
      sbuf[6]:=byte($BB);
      sleep(100);
      senddata(6);
      StartTick := GetTickCount;
      while GetTickCount - StartTick < 200 do
      Application.ProcessMessages;
      except
      end;
    end;//这样就得到了一个System文件
    [system]
    comkou=com2
    当下关comm1哪个窗体用时再开
    procedure Tdaanform.FormShow(Sender: TObject);
    var i:integer;
    begin
    Comm1.CommName:=readini('comkou');
    Comm1.StartComm;
    end;