DWORD dwNbBytesWritten; OVERLAPPED ov; ZeroMemory(&ov, sizeof(OVERLAPPED)); if(!Write(hCom, lpBuffer, dwNbBytesToWrite, &dwNbBytesWritten, &ov)) { // si erreur autre que envoi non terminé if(GetLastError() != ERROR_IO_PENDING) return 0; // on attend que l'envoi soit fini while(!GetOverlappedResult(hCom, &ov, &dwNbBytesWritten, TRUE)) { if(GetLastError() != ERROR_IO_INCOMPLETE) break; } }
// ouverture HANDLE hCOM = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, FILE_ATTRIBUTE_SYSTEM, FILE_FLAG_OVERLAPPED, NULL); // event à surveiller SetCommMask(hCOM, EV_TXEMPTY); // RTS=1 EscapeCommFunction(hCOM, SETRTS); // écriture OVERLAPPED ovWrite = {0}; DWORD dwBytesWritten; WriteFile(hCOM, "SALUT", 5, &dwBytesWritten, NULL); // attente fin de l'envoi OVERLAPPED ovWait = {0}; ovWait.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); DWORD dwEvtMask; if(!WaitCommEvent(hCOM, &dwEvtMask, &ovWait)) { // si toujours en attente if(GetLastError() == ERROR_IO_PENDING) { // on attent que l'opération se termine ou qu'elle soit interrompue DWORD dwResult; while(!GetOverlappedResult(hCOM, &ovWait, &dwResult, TRUE)) { if(GetLastError() != ERROR_IO_INCOMPLETE) break; } } } CloseHandle(ovWait.hEvent); // RTS=0 EscapeCommFunction(hCOM, CLRRTS); // fermeture CloseHandle(hCOM);
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question