我记得api中可以检测串口是否打开,可我忘记了怎么写了,所以请都各位了。
我用的是delphi(分不够再加).

解决方案 »

  1.   

    wzrlover(流光逝水) 
    这个语句怎么写呀,我现在急用。谢了
      

  2.   

    给你一个我调试过的吧
    function OpenSB(Com,baud:integer):integer;
    begin
      hOilM:=CreateFile(pchar('COM'+inttostr(Com)),
                        GENERIC_READ or GENERIC_WRITE,
                        0,nil,OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,0);
      if hOilM = 0 then
      begin
        result:=-1;
        exit;
      end;
      if (not BuildCommDCB(pchar('baud='+inttostr(baud)+' parity=N data=8 stop=1'),lpdcb)) then
      begin
        result:=-2;
        exit;
      end;
      if (not SetCommState(hOilM,lpdcb)) then
      begin
        result:=-3;
        exit;
      end;
      if (not GetCommTimeOuts(hOilM, Rs232timeout)) then
      begin
        result:=-4;
        exit;
      end;
      Rs232TimeOut.ReadIntervalTimeout:=10;
      Rs232TimeOut.ReadTotalTimeoutMultiplier:=1;
      Rs232TimeOut.ReadTotalTimeoutConstant:=10;
      if (not SetCommTimeOuts(hOilM,Rs232TimeOut)) then
      begin
        result:=-5;
        exit;
      end;
      result:=0;
    end;