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
| int nId;
nId=3;
/* variables locales */
char szCOM[16];
/* construction du nom du port, tentative d'ouverture */
sprintf(szCOM, "COM%d", nId);
g_hCOM = CreateFile(szCOM, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL);
/* affectation taille des tampons d'émission et de réception */
SetupComm(g_hCOM, RX_SIZE, TX_SIZE);
/* configuration du port COM */
if(!SetCommTimeouts(g_hCOM, &g_cto) || !SetCommState(g_hCOM, &g_dcb))
{
AfxMessageBox("COM problem : Le programme ne fonctionnera pas correctement.", MB_ICONSTOP);
UpdateData(TRUE);
CloseHandle(g_hCOM);
return FALSE;
}
/* on vide les tampons d'émission et de réception, mise à 1 DTR */
PurgeComm(g_hCOM, PURGE_TXCLEAR|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
EscapeCommFunction(g_hCOM, SETDTR);
return TRUE;
UpdateData(FALSE);
CloseCOM(); |