1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| bool CCom::WriteComm(unsigned char *lpByte , DWORD dwBytesToWrite)
{
BOOL fWriteStat = FALSE;
DWORD dwBytesWritten = 0;
DWORD dwErrorFlags = 0;
DWORD dwWriteError = 0;
DWORD dwBytesSent=0;
COMSTAT ComStat;
TCHAR szError[ 128 ];
if(NULL == m_hCom)
return false;
fWriteStat = WriteFile( m_hCom, lpByte, dwBytesToWrite, &dwBytesWritten, &m_ovWrite);
dwWriteError = GetLastError();
if (!fWriteStat)
{
if(dwWriteError == ERROR_IO_PENDING)
{
while(!GetOverlappedResult(m_hCom, &m_ovWrite, &dwBytesWritten, TRUE ))
{
DWORD const dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
{
dwBytesSent += dwBytesWritten;
continue;
}
else
{
break;
}
}//while
dwBytesSent += dwBytesWritten;
if( dwBytesSent != dwBytesToWrite )
wsprintf(szError, TEXT("\nProbable Write Timeout: Total of %ld bytes sent"), dwBytesSent);
else
{
wsprintf(szError,TEXT("\n%ld bytes write\n"), dwBytesSent);
}
OutputDebugString(szError);
}
else
{
//Afficher l'erreur dans le debug
int nPrinted = wsprintf(szError, TEXT("\nError %lu : "), dwWriteError);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwWriteError, 0, szError, 128-nPrinted, NULL);
OutputDebugString(szError);
ClearCommError( m_hCom, &dwErrorFlags, &ComStat );
return false;
}
}//if
return true;
} |
Partager