DWORD IO_API IO_write( HCOMM HandleComm,
LPCVOID lpBuffer,
DWORD RequestedLength,
LPDWORD pLengthWritten)
{
BOOL rc;
DWORD dwRet;
DWORD LastError;
DWORD CommError;
COMSTAT CommStat;
/* %v1.13 11/06/1997 DRE : mode overlapped conditionnel */
#ifdef OVERLAPPED_IO
OVERLAPPED OvlStruct;
#endif
/* %v1.13 11/06/1997 DRE : Fin */
DWORD ResidualLength;
DWORD EffectiveLength;
DWORD Offset;
#ifdef TRACE
CHAR szDebug[255];
#endif
#ifdef TRACE_DATA
wsprintf( szDebug, "Demande d'ecriture de 0x%08X (%u) octets sur Handle 0x%08X",
RequestedLength, RequestedLength, HandleComm);
TraceAndDump(__FILE__, __LINE__, szDebug, (LPBYTE)lpBuffer, RequestedLength);
#endif
dwRet = IO_NO_ERROR;
/* %v1.13 11/06/1997 DRE : mode overlapped conditionnel */
#ifdef OVERLAPPED_IO
/* Ecriture sur port en mode Overlapped @3 */
OvlStruct.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
OvlStruct.Offset = 0;
OvlStruct.OffsetHigh = 0;
#endif
/* %v1.13 11/06/1997 DRE : Fin */
Offset = 0;
ResidualLength = RequestedLength;
/* Boucle en attente de fin de transfert @7 */
/* %v1.13 11/06/1997 DRE : gestion du time-out */
/* while(ResidualLength) */
while(ResidualLength && dwRet == IO_NO_ERROR)
/* %v1.13 11/06/1997 DRE : Fin */
{
/* %v1.13 11/06/1997 DRE : mode overlapped conditionnel */
#ifdef OVERLAPPED_IO
rc = WriteFile( HandleComm, (BYTE *)lpBuffer + Offset, ResidualLength,
&EffectiveLength, &OvlStruct);
#else
rc = WriteFile( HandleComm, (BYTE *)lpBuffer + Offset, ResidualLength,
&EffectiveLength, NULL);
#endif
/* %v1.13 11/06/1997 DRE : Fin */
if(rc == FALSE)
{
/* %v1.13 11/06/1997 DRE : mode overlapped conditionnel */
#ifdef OVERLAPPED_IO
if(GetLastError() == ERROR_IO_PENDING)
{
/* Attente du resultat */
rc = WaitForSingleObject(OvlStruct.hEvent, INFINITE);
GetOverlappedResult(HandleComm, &OvlStruct, &EffectiveLength, TRUE);
if(EffectiveLength == 0)
{
/* Code retour 0 et Longueur ecrite 0 : time out ecriture port */
dwRet = IO_ERR_WRITE_TIMEOUT;
/* %v1.13 10/06/1997 DRE : gestion du time-out */
#ifdef TRACE
Trace(__FILE__, __LINE__, "Time-out en ecriture");
#endif
/* %v1.13 10/06/1997 DRE : Fin */
}
}
else
#endif
/* %v1.13 11/06/1997 DRE : Fin */
{
ClearCommError(HandleComm, &CommError, &CommStat);
LastError = GetLastError();
#ifdef TRACE
wsprintf( szDebug, "Erreur 0x%08X (%u) sur WriteFile Handle 0x%08X",
LastError, LastError, HandleComm);
Trace(__FILE__, __LINE__, szDebug);
FormatMessage( FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
LastError,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT),
szDebug,
sizeof( szDebug),
NULL);
Trace(__FILE__, __LINE__, szDebug);
wsprintf( szDebug, "CommError=%08lX", CommError);
TraceAndDump(__FILE__, __LINE__, szDebug, (LPBYTE)&CommStat, sizeof( COMSTAT));
#endif
switch(LastError)
{
case ERROR_INVALID_HANDLE:
dwRet = IO_ERR_NOT_OPEN;
break;
case ERROR_OUT_OF_PAPER:
dwRet = IO_ERR_PAPER_OUT;
break;
default:
dwRet = IO_ERR_GENERAL;
break;
}
}
}
/* MAJ longueur restant a ecrire */
ResidualLength -= EffectiveLength;
Offset += EffectiveLength;
}
/* %v1.13 11/06/1997 DRE : mode overlapped conditionnel */
#ifdef OVERLAPPED_IO
CloseHandle(OvlStruct.hEvent);
#endif
/* %v1.13 11/06/1997 DRE : Fin */
#ifdef TRACE
wsprintf( szDebug, "0x%08X (%u) octets emis sur Handle 0x%08X",
Offset, Offset, HandleComm);
Trace(__FILE__, __LINE__, szDebug);
#endif
/* %v1.13 11/06/1997 DRE : correction */
/* *pLengthWritten = EffectiveLength;*/
*pLengthWritten = RequestedLength - ResidualLength;
/* %v1.13 11/06/1997 DRE : Fin */
return dwRet;
}
Partager