none
串口通信的问题 RRS feed

  • 问题

  • 小弟所需的流程是这样:

    1。往com1写命令,这是一个写的过程;

    2。这个命令可以控制设备返回信息,然后显示;

     

    遇到的问题:

    1。waitcommevent()只对EV_RXCHAR有反映,但是 EV_CTS完全没用,但是我接受的是字符串,希望用前者;

    2。这个接受字符串,中间有好长的停顿,导致接口认为是两个字符串comstat.cbInQue == 0,有什么好方法可以解决呢?我现在是再等

     

     for (;Wink
     {
      for (int i = 0; i<100000000; i++);
      // Gain ownership of the comm port critical section.
      // This process guarantees no other part of this program
      // is using the port object.
      
      EnterCriticalSection(&port->m_csCommunicationSync);

      // ClearCommError() will update the COMSTAT structure and
      // clear any other errors.
      
      bResult = ClearCommError(port->m_hComm, &dwError, &comstat);

      LeaveCriticalSection(&port->m_csCommunicationSync);

      // start forever loop.  I use this type of loop because I
      // do not know at runtime how many loops this will have to
      // run. My solution is to start a forever loop and to
      // break out of it when I have processed all of the
      // data available.  Be careful with this approach and
      // be sure your loop will exit.
      // My reasons for this are not as clear in this sample
      // as it is in my production code, but I have found this
      // solutiion to be the most efficient way to do this.
      
      if (comstat.cbInQue == 0)
      {
       // break out when all bytes have been read
       break;
      }
          
      EnterCriticalSection(&port->m_csCommunicationSync);

      if (bRead)
      {
       bResult = ReadFile(port->m_hComm,  // Handle to COMM port
              RXBuff,    // RX Buffer Pointer
              min(comstat.cbInQue, 256),     // Read one byte
              &BytesRead,   // Stores number of bytes read
              &port->m_ov);  // pointer to the m_ov structure

       if(0 != BytesRead)
       {
        RXBuff[BytesRead] = '\0';
        port->m_rdBuffer = port->m_rdBuffer + (CString)RXBuff;
       }

       // deal with the error code
       if (!bResult) 
       {
        switch (dwError = GetLastError())
        {
         case ERROR_IO_PENDING:  
          {
           // asynchronous i/o is still in progress
           // Proceed on to GetOverlappedResults();
           bRead = FALSE;
           break;
          }
         default:
          {
           // Another error has occured.  Process this error.
           port->ProcessErrorMessage("ReadFile()");
           break;
          }
        }
       }
       else
       {
        // ReadFile() returned complete. It is not necessary to call GetOverlappedResults()
        bRead = TRUE;
       }
      }  // close if (bRead)

      if (!bRead)
      {
       bRead = TRUE;
       bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port
                &port->m_ov,  // Overlapped structure
                &BytesRead,  // Stores number of bytes read
                TRUE);    // Wait flag
       RXBuff[BytesRead] = '\0';
       port->m_rdBuffer = port->m_rdBuffer + (CString)RXBuff;
       // deal with the error code
       if (!bResult) 
       {
        port->ProcessErrorMessage("GetOverlappedResults() in ReadFile()");
       } 
      }  // close if (!bRead)
        
      LeaveCriticalSection(&port->m_csCommunicationSync);

     } // end forever loop

    2008年7月2日 10:56

答案