我在使用SerialPort的时候发现该类只在读ASC码的时候正常例如发送‘1’  它读的是49但是在读16进制数的时候就有问题了16进制的123 读进去的是18  读12 也是18请问 这是怎么回事啊
手上分不多 就这些了

解决方案 »

  1.   

    CSerialPort还是比较好用的,对十六进制的数据同样适用,其实都是存储在内存中的数据,并没有什么区别,但是有可能存储方式不同,比如long int本来为四个字节,但是在压缩方式下,并不是四个字节,可以参考msdn中#pragma 的解释。
      

  2.   

    兔子 你用过CSerialPort没有 就是这里的问题啊  
    我的程序从这里开始就有问题  程序接受ASC码发送的数据很好 但是接收16进制的数据就有问题
    例如123  我可以接受‘1’‘2’‘3’但是123就不行  感觉就是不能接收数  只可以接受字符 
    不知道是怎么回事
      

  3.   

    void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
    {
    BOOL  bRead = TRUE; 
    BOOL  bResult = TRUE;
    DWORD dwError = 0;
    DWORD BytesRead = 0;
    unsigned char RXBuff;//这里就是读的数据 我改成INT也不行 for (;;) 

    // 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 COMMport 
           &RXBuff, // RX Buffer Pointer
          1, // Read one byte
      &BytesRead,// Stores number of bytes read
    &port->m_ov);// pointer to the m_ov structure
    // 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 // deal with the error code 
    if (!bResult)  
    {
    port->ProcessErrorMessage("GetOverlappedResults() in ReadFile()");
    }
    }  // close if (!bRead)

    LeaveCriticalSection(&port->m_csCommunicationSync); // notify parent that a byte was received
    ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, 
    (LPARAM) port->m_nPortNr);
    } // end forever loop}
    读数据的代码
      

  4.   

    数据的本质是一样的,读0x123和0x12都只读到了0x12所以你只读到了18,还是检查一下具体的读数据的数据类型和内存的使用吧
      

  5.   

    sorry 我对发送源的理解有问题  浪费大家的时间了  不好意思  谢谢大家的帮忙