[C++] WaitCommEvent
Résolu
skarsnick
Messages postés
79
Date d'inscription
Statut
Membre
Dernière intervention
-
skarsnick Messages postés 79 Date d'inscription Statut Membre Dernière intervention -
skarsnick Messages postés 79 Date d'inscription Statut Membre Dernière intervention -
Bonjour :)
J'ai un p'tit soucis que j'ai assez bien cerné mais je ne vois pas comment passé outre
Voilà mon code:
Mon problème et vous l'aurez peut être compris, c'est que mon if (WaitCommEvent || WaitCommEvent) dès qu'un des deux évènements se passe (soit sur COM 1 soit sur COM 4) il s'arrête sur le premier if.
En faite j'aimerais qu'ils attendent un évènement de la part de COM 1 ou COM 4 et qu'une fois que l'évènement a eu lieu, qu'il test if (dwEvtMask1 & EV_DSR) ET if (dwEvtMask4 & EV_DSR)
bref quand je titille mon COM 1 il DO UN TRUC, quand je titille mon COM 4 il attend bêtement au niveau du if (dwEvtMask1 & EV_DSR) ...
J'ai peur de devoir utiliser un thread :(
Une idée ?
Merci:)
J'ai un p'tit soucis que j'ai assez bien cerné mais je ne vois pas comment passé outre
Voilà mon code:
void CS8Dlg::initialisation() { HANDLE hCom1,hCom4; OVERLAPPED o,q; DWORD dwEvtMask1,dwEvtMask4; DCB dcb; BOOL fSuccess1,fSuccess4; // OPEN THE SERIAL 1 hCom1 = CreateFile( TEXT("COM1"), GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // default security attributes OPEN_EXISTING, // must use OPEN_EXISTING 0, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); // OPEN THE SERIAL 4 hCom4 = CreateFile( TEXT("COM4"), GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // default security attributes OPEN_EXISTING, // must use OPEN_EXISTING 0, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); if (hCom1 == INVALID_HANDLE_VALUE || hCom4 == INVALID_HANDLE_VALUE ) { return; // You're divises are not correctly linked } SetupComm(hCom1,5000,5000); SetupComm(hCom4,5000,5000); fSuccess1 = GetCommState(hCom1, &dcb); fSuccess4 = GetCommState(hCom4, &dcb); if (!fSuccess1 || !fSuccess4) { return; // No communication with your modules } // CONFIGURE THE SERIAL COMMUNICATION // Fill in DCB: 9 ,600 bps, 8 data bits, no parity, and 1 stop bit. dcb.BaudRate = CBR_9600; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit dcb.fNull=FALSE; dcb.fRtsControl=RTS_CONTROL_ENABLE; dcb.fInX=FALSE; dcb.fOutX=FALSE; dcb.fDtrControl=DTR_CONTROL_ENABLE; SetCommState(hCom1,&dcb); SetCommState(hCom4,&dcb); if (!SetCommState(hCom1,&dcb) || !SetCommState(hCom4,&dcb)) { return; //Problem with the configuration of the serial communication } fSuccess1 = SetCommMask(hCom1, EV_DSR); fSuccess4 = SetCommMask(hCom4, EV_DSR); if (!fSuccess1 || !fSuccess4) { return; // Problem with the actual states of the modules } // Create an event object for use by WaitCommEvent. o.hEvent = CreateEvent( NULL, // default security attributes TRUE, // manual-reset event FALSE, // not signaled NULL // no name ); // Create an event object for use by WaitCommEvent. q.hEvent = CreateEvent( NULL, // default security attributes TRUE, // manual-reset event FALSE, // not signaled NULL // no name ); // Initialize the rest of the OVERLAPPED structure to zero. o.Internal = 0; o.InternalHigh = 0; o.Offset = 0; o.OffsetHigh = 0; assert(o.hEvent); // Initialize the rest of the OVERLAPPED structure to zero. q.Internal = 0; q.InternalHigh = 0; q.Offset = 0; q.OffsetHigh = 0; assert(q.hEvent); if (WaitCommEvent(hCom1, &dwEvtMask1, &o) || WaitCommEvent(hCom4, &dwEvtMask4, &q)) { if (dwEvtMask1 & EV_DSR) { //DO UN TRUC } if (dwEvtMask4 & EV_DSR) { //DO UN autre TRUC } } return; }
Mon problème et vous l'aurez peut être compris, c'est que mon if (WaitCommEvent || WaitCommEvent) dès qu'un des deux évènements se passe (soit sur COM 1 soit sur COM 4) il s'arrête sur le premier if.
En faite j'aimerais qu'ils attendent un évènement de la part de COM 1 ou COM 4 et qu'une fois que l'évènement a eu lieu, qu'il test if (dwEvtMask1 & EV_DSR) ET if (dwEvtMask4 & EV_DSR)
bref quand je titille mon COM 1 il DO UN TRUC, quand je titille mon COM 4 il attend bêtement au niveau du if (dwEvtMask1 & EV_DSR) ...
J'ai peur de devoir utiliser un thread :(
Une idée ?
Merci:)
1 réponse
le problème est ici:
if (WaitCommEvent(hCom1, &dwEvtMask1, &o) || WaitCommEvent(hCom4, &dwEvtMask4, &q))
en attendant l'évènement du COM 1, le proc ne regarde pas l'état du port 4, il se bloque sur son observation du COM1, donc il ne faut pas les mettre dans le même if,
un thread me semble la seule solution
if (WaitCommEvent(hCom1, &dwEvtMask1, &o) || WaitCommEvent(hCom4, &dwEvtMask4, &q))
en attendant l'évènement du COM 1, le proc ne regarde pas l'état du port 4, il se bloque sur son observation du COM1, donc il ne faut pas les mettre dans le même if,
un thread me semble la seule solution