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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
   | int ouvrirPort(int numport) { // Retourne 1 en cas de succès
    //char szPort[16];
	TCHAR szPort[15];
    //sprintf(szPort, "\\\\.\\COM%d:", numport);//modifie pour prendre les port com>9
	wsprintf(szPort, _T("COM%d:"), numport);
	hPort = CreateFile(
		(LPCTSTR)szPort,							//Pointer to the name of the port
		GENERIC_READ|GENERIC_WRITE,		//accès en lecture ou en écriture
		0,                             //Share mode
		NULL,                          //Pointer to the security attribute
		OPEN_EXISTING,                 //Cela indique que l'instance sera créée si que le port existe
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, //Port attributes
		NULL);
 
	if(hPort==INVALID_HANDLE_VALUE) {
		return 0;
	}
 
	if(!m_Ov.hEvent)
        m_Ov.hEvent=CreateEvent(NULL,   // no security attributes
                                FALSE,  // auto reset event
                                FALSE,  // not signaled
                                NULL    // no name
                                );
 
    SetEvent(m_Ov.hEvent);
 
 
	DCB PortDCB;
	if (!GetCommState (hPort, &PortDCB)) { // Structure de configuration
		return 0;
	}
	// mise à jour de la structure
	PortDCB.DCBlength = sizeof (DCB); 
	PortDCB.BaudRate = 9600; 
	PortDCB.fBinary = TRUE; 
	PortDCB.fParity = TRUE; 
	PortDCB.fOutxCtsFlow = FALSE; 
	PortDCB.fOutxDsrFlow = FALSE; 
	PortDCB.fDtrControl = DTR_CONTROL_ENABLE; 
	PortDCB.fDsrSensitivity = FALSE; 
	PortDCB.fTXContinueOnXoff = TRUE; 
	PortDCB.fOutX = FALSE; 
	PortDCB.fInX = FALSE; 
	PortDCB.fErrorChar = FALSE; 
	PortDCB.fNull = FALSE; 
	PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
	PortDCB.fAbortOnError = FALSE; 
	PortDCB.ByteSize = 8; 
	PortDCB.Parity = NOPARITY; 
	PortDCB.StopBits = ONESTOPBIT; 
 
	//Renvoie des nouvelles informations dans le registre de votre port série
	if (SetCommState (hPort, &PortDCB)) {
		GetCommTimeouts(hPort,&m_ComTimeouts);
		m_ComTimeouts.ReadIntervalTimeout = 0xFFFFFFFF;
		m_ComTimeouts.ReadTotalTimeoutMultiplier = 0;
		m_ComTimeouts.ReadTotalTimeoutConstant = 0;
		m_ComTimeouts.WriteTotalTimeoutMultiplier = 0;
		m_ComTimeouts.WriteTotalTimeoutConstant = 10000;
		if (!SetCommTimeouts(hPort,&m_ComTimeouts))
			return 0;
		memset(&m_OverlappedRead,0,sizeof(OVERLAPPED));
		m_OverlappedRead.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
		memset(&m_OverlappedWrite,0,sizeof(OVERLAPPED));
		m_OverlappedWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
		if (m_OverlappedRead.hEvent==NULL || m_OverlappedWrite.hEvent==NULL)
			return 0;
		if(m_OverlappedRead.hEvent!=NULL) 
			CloseHandle(m_OverlappedRead.hEvent);
		if(m_OverlappedWrite.hEvent!=NULL) 
			CloseHandle(m_OverlappedWrite.hEvent);
 
		// PRM2: Indique que l'on va détecter la réception (entrée de type DWORD)
		SetCommMask(hPort,EV_RXCHAR); // définit les évènements pour lesquels ont va réagir par rapport à notre instance
		return 1;
	} else
		return 0;
 
} | 
Partager