我用vb把串口封装到dll里
现在遇到个问题,我WriteFile之后想直接就ReadFile,
如果用断点一步一步执行就能把串口里的数据取出来,
如果直接运行,就是读不出来,不知道是不是需要延时,该怎么做?请教一下!!

解决方案 »

  1.   

    如果需要多次使用ReadFile and WriteFile时,应该考虑没有数据读时的情况,如果没有数据读的话,那么你的程序在什么 什么呢?for ( ; ; ) {
       if (WaitCommEvent(hComm, &dwCommEvent, NULL)) {
          do {
             if (ReadFile(hComm, &chRead, 1, &dwRead, NULL))
                // A byte has been read; process it.
             else
                // An error occurred in the ReadFile call.
                break;
          } while (dwRead);
       }
       else
          // Error in WaitCommEvent
          break;
    }这段代码是MSDN上的,根据需要在WaitCommEvent中加入超时设置
      

  2.   

    楼上方法不错,
    你在vb中应该判断writefile是否成功,然后增加延时,在readfile
      

  3.   

    可以Sleep一下再读应该就可以了!