同意楼上的 // Reset error state m_lLastError = ERROR_SUCCESS; // Check if the device is open if (m_hFile == 0) { // Set the internal error code m_lLastError = ERROR_INVALID_HANDLE; // Issue an error and quit _RPTF0(_CRT_WARN,"CSerial::Setup - Device is not opened\n"); return m_lLastError; } // Obtain the DCB structure for the device CDCB dcb; if (!::GetCommState(m_hFile,&dcb)) { // Obtain the error code m_lLastError = :: GetLastError(); // Display a warning _RPTF0(_CRT_WARN,"CSerial::Setup - Unable to obtain DCB information\n"); return m_lLastError; } // Set the new data dcb.fBinary = DWORD(1);//允许二进制 dcb.BaudRate = DWORD(eBaudrate); dcb.ByteSize = BYTE(eDataBits); dcb.Parity = BYTE(eParity); dcb.StopBits = BYTE(eStopBits); // Determine if parity is used dcb.fParity = (eParity != EParNone); // Set the new DCB structure if (!::SetCommState(m_hFile,&dcb)) { // Obtain the error code m_lLastError = ::GetLastError(); // Display a warning _RPTF0(_CRT_WARN,"CSerial::Setup - Unable to set DCB information\n"); return m_lLastError; } // Return successful return m_lLastError;
m_lLastError = ERROR_SUCCESS; // Check if the device is open
if (m_hFile == 0)
{
// Set the internal error code
m_lLastError = ERROR_INVALID_HANDLE; // Issue an error and quit
_RPTF0(_CRT_WARN,"CSerial::Setup - Device is not opened\n");
return m_lLastError;
} // Obtain the DCB structure for the device
CDCB dcb;
if (!::GetCommState(m_hFile,&dcb))
{
// Obtain the error code
m_lLastError = :: GetLastError(); // Display a warning
_RPTF0(_CRT_WARN,"CSerial::Setup - Unable to obtain DCB information\n");
return m_lLastError;
} // Set the new data
dcb.fBinary = DWORD(1);//允许二进制
dcb.BaudRate = DWORD(eBaudrate);
dcb.ByteSize = BYTE(eDataBits);
dcb.Parity = BYTE(eParity);
dcb.StopBits = BYTE(eStopBits); // Determine if parity is used
dcb.fParity = (eParity != EParNone); // Set the new DCB structure
if (!::SetCommState(m_hFile,&dcb))
{
// Obtain the error code
m_lLastError = ::GetLastError(); // Display a warning
_RPTF0(_CRT_WARN,"CSerial::Setup - Unable to set DCB information\n");
return m_lLastError;
} // Return successful
return m_lLastError;