以前我写的是
if (hcom=0) then
CloseHandle(hcom);可是有时候hcom有值时 com是关的 那么再关就会出错 打开也不可以了 如何解决

解决方案 »

  1.   

    hcom=0时就代表hcom表示的端口已经关闭了,所以你的代码可改为:
    if(hcom=0) then
        exit;
    如果想重新打开一个端口,可以使用Windows API的CreateFile函数来实现。如:
        hcom=CreateFile(pchar(com1),GENERIC_READ and GENERIC_WRITE,0,nil,OPEN_EXISTING,0,0);    //打开以读写的方式com1串口
      

  2.   

    可是但串口有值时我想把它关闭为什么不行呢?
    if(hcom<>0) then
        CloseHandle(hcom);<-------有时会出错
      

  3.   

    hcom 是全局变量吗?if (hcom <> 0) then
      try
        CloseHandel(hcom);
      except
      end;