为什么WriteFile()总是返回异常代码6
为什么WriteFile()总是返回错误代码6?
为什么WriteFile()总是返回错误代码6?
为什么WriteFile()总是返回错误代码6?
- C/C++ code
void CCallLeeKuDlg::OnBtn1() { // TODO: Add your control notification handler code here HANDLE m_hComm; COMMTIMEOUTS m_CommTimeouts; char *szPort = new char[10]; char *szBaud = new char[50]; UINT uPort = 1; UINT uBaud = 19200; char parity = 'N'; UINT databits = 8; UINT stopbits = 1; DWORD dwCommEvents = EV_RXCHAR | EV_CTS; UINT nBufferSize = 512; sprintf(szPort, "COM%d", uPort); sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", uBaud, parity, databits, stopbits); // initialize critical section // InitializeCriticalSection(&m_csCommunicationSync); //利用一般开启档案的 CreatFile()即可开启 serial port device用 CreateFile() API. m_hComm = CreateFile(szPort, // communication port string (COMX) GENERIC_READ | GENERIC_WRITE, // read/write types 0, // comm devices must be opened with exclusive access NULL, // no security attributes OPEN_EXISTING, // comm devices must use OPEN_EXISTING FILE_FLAG_OVERLAPPED, // Async I/O 0); //设置超时时间 set the timeout values m_CommTimeouts.ReadIntervalTimeout = 1000; m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000; m_CommTimeouts.ReadTotalTimeoutConstant = 1000; m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000; m_CommTimeouts.WriteTotalTimeoutConstant = 1000; if(SetCommTimeouts(m_hComm, &m_CommTimeouts)) { //设置串口事件 if (SetCommMask(m_hComm, dwCommEvents)) { //设定及取得通讯状态 SetCommPortState(m_hComm, uBaud, parity, databits, stopbits); } else AfxMessageBox("SetCommMask Failed!"); } else AfxMessageBox("SetCommTimeouts Failed!"); // flush the port ??? PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); // HANDLE hFile = m_hComm; CString strSend = "isabc"; OVERLAPPED m_ov;//??? BOOL bWrite = TRUE; BOOL bResult = TRUE; DWORD BytesSent = 0; // Initailize variables m_ov.Offset = 0; m_ov.OffsetHigh = 0; // Clear buffer PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); bResult = WriteFile(m_hComm, // Handle to COMM Port strSend.GetBuffer(strSend.GetLength()), // Pointer to message buffer in calling finction strSend.GetLength(), // Length of message to send by isabc &BytesSent, // Where to store the number of bytes sent &m_ov); // Overlapped structure if(!bResult) { DWORD dwError = GetLastError(); CString strErr; strErr.Format("Error Code :%d",dwError); AfxMessageBox(strErr); switch (dwError) { case ERROR_IO_PENDING: { // continue to GetOverlappedResults() BytesSent = 0; bWrite = FALSE; break; } default: { // all other error codes AfxMessageBox("WriteFile() happen other error!"); } } } else if (!bWrite) { bWrite = TRUE; bResult = GetOverlappedResult(m_hComm, // Handle to COMM port &m_ov, // Overlapped structure &BytesSent, // Stores number of bytes sent TRUE); // Wait flag LeaveCriticalSection(&m_csCommunicationSync); // deal with the error code if (!bResult) { AfxMessageBox("GetOverlappedResults() in WriteFile()"); } } // end if (!bWrite) if (BytesSent != strSend.GetLength()) { TRACE("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strSend.GetLength()); } }