Bonjour,

Le programme vise à gérer une pédale et qu'elle puisse être à nouveau détectée si on venait à la débrancher puis à la rebrancher.
Voici le main :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
int _tmain(int argc, _TCHAR* argv[])
{
 
	//log(WITH_TIME, SCORE_CLASS_INSTANTIATION);
	static score& My_Score	= score::getInstance();
 
	pedal My_Pedal;
 
	My_Pedal.enumerate_pedal();
 
	if (My_Pedal.search_pedal_port())
		My_Pedal.activate_pedal();
 
	//My_Pedal.enumerate_pedal();
 
    Jeu jeu;
    jeu.executer();
 
	//My_Score.execute();
	//My_Score.parse(XmlFilename);
 
	std::cin >> key;
 
	My_Pedal.deactivate_pedal();
 
	return 0; 
}
Et voici la classe pedal : le header :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#pragma once
 
#include <string>
 
#include "toolbox.h"
#include "template_timer.h"
#include "const_pedal.h"
#include "tserial_event.h"
#include "enumser.h"
 
class pedal
{
public:
 
	void													enumerate_pedal(void);
	void													activate_pedal(void);
	bool													search_pedal_port(void);
	void													deactivate_pedal(void);
 
	static char*											reply_iss_version;
	static char*											reply_serial_number;
	static char*											reply_configuration;
	static char*											reply_io_state;
 
	// state comes with UP or DOWN
	static bool												pedal_state;
 
 
private:
 
	void                                                    wait_for_reply(void);
	void													OnTimedEvent_PedalReply(void);
	void													OnTimedEvent_PedalRequest(void);
	void													OnTimedEvent_PedalEnumeration(void);
 
	// the callback function has to be a static one if any class member !
	static void												SerialEventManager(uint32 object, uint32 event);
	static void												OnDataArrival(int size, char *buffer);
	static void												todo_on_pedalchange(void);
	void													request_iss_version(void);
	void													request_pedal_state(void);
	void													request_serial_number(void);
	void													configure_io_mode(void);
 
 
	TTimer<pedal>											timer_pedal_enumeration;
	TTimer<pedal>											timer_pedal_request;
	TTimer<pedal>											timer_pedal_reply;
 
	static char*											serial_number;
	static std::string 										firmware_revision_number;
	// status comes with OK or not 
	static bool												pedal_status;
 
	static bool												iss_version_received;	
	static bool												io_mode_received;
	static bool												pedal_state_received;
	static bool												pedal_state_mem;
	static bool												first_pedalrequest;
	bool													pedalreply_timeelapsed;
 
 
};
Et le cpp :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include "stdafx.h"
#include "pedal.h"
 
#include <sstream>
 
 
tserial_event*			com;
int						error;
 
CSimpleArray<UINT>		ports;
CSimpleArray<CString>	friendlyNames;
CSimpleArray<CString>	sPorts;
int						i;
char*					pedalport = PEDALPORT_UNKNOWN;
 
// **********************************************************
// * todo_on_pedalchange									*
// *														*
// * put here the stuff you have to do when a state change  *
// * occurs                                                 *
// **********************************************************
void pedal::todo_on_pedalchange(void)
{
	if (pedal_state)
		printf("Pedal ACTIVATED \n");
	else
		printf("Pedal UNACTIVATED \n");
}
 
 
// **********************************************************
// * OnDataArrival  										*
// **********************************************************
void pedal::OnDataArrival(int size, char *buffer)
{
	std::ostringstream			oss;
	std::string					sbuffer;
	int							i_num;
 
    if ((size>0) && (buffer!=0))
    {
        buffer[size] = 0;
		sbuffer = buffer;
#ifdef DISPLAY_ONDATA_ARRIVAL
        printf("OnDataArrival: %s ",buffer);
		printf("\n");
#endif
		switch (size)
		{
		case 1 :    reply_io_state = buffer;
					// pedal is linked to the first IO input
					i_num = int(buffer[0]) & 1;
					pedal_state = (i_num == 0);
					if (first_pedalrequest)
					{
						pedal_state_mem = pedal_state;
						first_pedalrequest = false;
					}
					else
					{
						if (pedal_state != pedal_state_mem)
						{
							pedal_state_mem = pedal_state;
							todo_on_pedalchange();
						}
					}
					pedal_state_received = true;
					break;
		case 2 :    reply_configuration = buffer;
					// converting to ascii
					i_num = int(buffer[0]);
					if (i_num < 0)
						i_num = i_num + 256;
					pedal_status = (i_num == 0xff);
					io_mode_received = true;
					break;
		case 3 :	reply_iss_version = buffer;
					i_num = buffer[1];
					oss << i_num;
					firmware_revision_number = oss.str();
					iss_version_received = true;
					break;
		case 8 :	reply_serial_number = buffer;
					serial_number = reply_serial_number;
					break;
		}
    }
}
 
// **********************************************************
// * SerialEventManager										*
// **********************************************************
void pedal::SerialEventManager(uint32 object, uint32 event)
{
    char *buffer;
    int   size;
    tserial_event *com;
 
    com = (tserial_event *) object;
    if (com!=0)
    {
        switch(event)
        {
            case  SERIAL_CONNECTED  :
#ifdef DISPLAY_CONNECTED
                                        printf("Connected ! \n");
#endif
                                        break;
            case  SERIAL_DISCONNECTED  :
#ifdef DISPLAY_DISCONNECTED
                                        printf("Disonnected ! \n");
#endif
                                        break;
            case  SERIAL_DATA_SENT  :
#ifdef DISPLAY_DATA_SENT
                                        printf("Data sent ! \n");
#endif
                                        break;
            case  SERIAL_RING       :
#ifdef DISPLAY_DRING
                                        printf("DRING ! \n");
#endif
                                        break;
            case  SERIAL_CD_ON      :
#ifdef DISPLAY_CARRIER_DETECTED
                                        printf("Carrier Detected ! \n");
#endif
                                        break;
            case  SERIAL_CD_OFF     :
#ifdef DISPLAY_NO_MORE_CARRIER
                                        printf("No more carrier ! \n");
#endif
                                        break;
            case  SERIAL_DATA_ARRIVAL  :
                                        size   = com->getDataInSize();
                                        buffer = com->getDataInBuffer();
                                        OnDataArrival(size, buffer);
                                        com->dataHasBeenRead();
                                        break;
        }
    }
}
 
// **********************************************************
// * enumerate_serial_ports									*
// **********************************************************
bool enumerate_serial_ports(void)
{
  //Initialize COM (Required by CEnumerateSerial::UsingWMI)
  CoInitialize(NULL);
 
  //Initialize COM security (Required by CEnumerateSerial::UsingWMI)
  HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
  if (FAILED(hr))
  {
    printf("Failed to initialize COM security, Error:%x\n", hr);
    CoUninitialize();
    return false; 
  }
 
 
#if defined CENUMERATESERIAL_USE_STL
  std::vector<UINT> ports;
#if defined _UNICODE
  std::vector<std::wstring> friendlyNames;
  std::vector<std::wstring> sPorts;
#else
  std::vector<std::string> friendlyNames;
  std::vector<std::string> sPorts;
#endif  
  size_t i;
#elif defined _AFX
  CUIntArray ports;
  CStringArray friendlyNames;
  CStringArray sPorts;
  INT_PTR i;
#else
  /*
  CSimpleArray<UINT> ports;
  CSimpleArray<CString> friendlyNames;
  CSimpleArray<CString> sPorts;
  int i;
  */
 
#endif
 
///////////////////////// Serial Ports Enumeration Methods ////////////////////////////////////////////  
 
// CREATEFILE_SERIAL_ENUMERATION_METHOD
 
#ifdef CREATEFILE_SERIAL_ENUMERATION_METHOD
  printf(CREATEFILE);
  if (CEnumerateSerial::UsingCreateFile(ports))
	goto Co_Uninitialize;
  else
  {
    printf(CREATEFILE_FAILURE, GetLastError());
	return false;
  }
#endif
 
 
// QUERY_DOSDEVICE_SERIAL_ENUMERATION_METHOD
 
#ifdef QUERY_DOSDEVICE_SERIAL_ENUMERATION_METHOD
  printf(QUERY_DOSDEVICE);
  if (CEnumerateSerial::UsingQueryDosDevice(ports))
	  goto Co_Uninitialize;
  else
  {
    printf(QUERY_DOSDEVICE_FAILURE, GetLastError());
	return false;
  }
#endif
 
 
// GET_DEDAULT_COMMCONFIG_SERIAL_ENUYMERATION_METHOD
 
#ifdef GET_DEDAULT_COMMCONFIG_SERIAL_ENUYMERATION_METHOD
  printf(GET_DEFAULT_COMMCONFIG);
  if (CEnumerateSerial::UsingGetDefaultCommConfig(ports))
	  goto Co_Uninitialize;
  else
  {
    printf(GET_DEFAULT_COMMCONFIG_FAILURE, GetLastError());
	return false;
  }
#endif
 
 
// GUID_DEVINTERFACE_SERIAL_ENUMERATION_METHOD
// buggy on Windows 7 Professional
 
 #ifdef GUID_DEVINTERFACE_SERIAL_ENUMERATION_METHOD
  // Issue while testing on Windows 7 Professional 
  printf(GUID_DEVINTERFACE);
  if (CEnumerateSerial::UsingSetupAPI1(ports, friendlyNames))
	goto Co_Uninitialize;
  else
  {
    printf(GUID_DEVINTERFACE_FAILURE, GetLastError());
	return false;
  }
#endif
 
 
// PORTS_DEVICE_INFORMATION_SERIAL_ENUMERATION_METHOD 
 
 #ifdef PORTS_DEVICE_INFORMATION_SERIAL_ENUMERATION_METHOD  
  printf(PORT_DEVICE_INFORMATION);
  if (CEnumerateSerial::UsingSetupAPI2(ports, friendlyNames))
	goto Co_Uninitialize;
  else
  {
    printf(PORT_DEVICE_INFORMATION_FAILURE, GetLastError());
	return false;
  }
#endif
 
 
// ENUMPORTS_SERIAL_ENUMERATION_METHOD
// buggy on Windows 7 Professional
 
#ifdef ENUMPORTS_SERIAL_ENUMERATION_METHOD
  printf(ENUMPORTS);
  if (CEnumerateSerial::UsingEnumPorts(ports))
	goto Co_Uninitialize;
  else
  {
    printf(ENUMPORTS_FAILURE, GetLastError());
	return false;
  }
#endif
 
 
// WMI_SERIAL_ENUMERATION_METHOD
 
#ifdef WMI_SERIAL_ENUMERATION_METHOD
  printf(WMI);
  if (CEnumerateSerial::UsingWMI(ports, friendlyNames))
	goto Co_Uninitialize;
  else
  {
	printf(WMI_FAILURE, GetLastError());
	return false;
  }
#endif
 
 
// COM08_SERIAL_ENUMERATION_METHOD 
// buggy on Windows 7 Professional
 
#ifdef COM08_SERIAL_ENUMERATION_METHOD 
  printf(COM08);
  if (CEnumerateSerial::UsingComDB(ports))
	goto Co_Uninitialize;
  else
  {
    printf(COM08_FAILURE), GetLastError();
	return false;
  }
#endif
 
 
// REGISTRY_SERIAL_ENUMERATION_METHOD   
 
#ifdef REGISTRY_SERIAL_ENUMERATION_METHOD 
  printf(REGISTRY);
  if (CEnumerateSerial::UsingRegistry(sPorts))
	goto Co_Uninitialize;
  else
  {
    printf(REGISTRY_FAILURE, GetLastError());
	return false;
  }
#endif
 
Co_Uninitialize:
 
  //close down COM
  CoUninitialize();
  return true;
}
 
// **********************************************************
// * OnTimedEvent_PedalEnumeration							*
// **********************************************************
void pedal::OnTimedEvent_PedalEnumeration(void)
{
 
	printf("Attempting pedal enumeration ...\n");
}
 
// **********************************************************
// * OnTimedEvent_PedalRequest								*
// **********************************************************
void pedal::OnTimedEvent_PedalRequest(void)
{
#ifdef DISPLAY_READING_PEDAL_STATE
	printf("Reading pedal state ...\n");
#endif
	this->request_pedal_state();
}
 
// **********************************************************
// * OnTimedEvent_PedalReply								*
// **********************************************************
void pedal::OnTimedEvent_PedalReply(void)
{
	printf("Waiting for pedal reply ... \n");
	pedalreply_timeelapsed = true;
}
 
// **********************************************************
// * enumerate_pedal										*
// **********************************************************
void pedal::enumerate_pedal(void)
{
	timer_pedal_enumeration.SetTimedEvent(this,&pedal::OnTimedEvent_PedalEnumeration);
	timer_pedal_enumeration.Start(PEDAL_ENUMERATION_PERIOD);
}
 
// **********************************************************
// * activate_pedal											*
// **********************************************************
void pedal::activate_pedal(void)
{
	timer_pedal_request.SetTimedEvent(this,&pedal::OnTimedEvent_PedalRequest);
	timer_pedal_request.Start(PEDAL_REQUEST_PERIOD);
}
 
// **********************************************************
// * wait_for_reply											*
// **********************************************************
void pedal::wait_for_reply(void)
{
	this->pedalreply_timeelapsed = false;
	timer_pedal_reply.SetTimedEvent(this,&pedal::OnTimedEvent_PedalReply);
	timer_pedal_reply.Start(PEDAL_REPLY_PERIOD,NOT_IMMEDIATELY,ONCE);
}
 
 
// **********************************************************
// * request_iss_version									*
// **********************************************************
void pedal::request_iss_version(void)
{
	iss_version_received = false;
	com->sendData(ISS_VERSION_REQUEST,2);
	com->setRxSize(3);								// waiting for 3 bytes
}
 
// **********************************************************
// * request_serial_number									*
// **********************************************************
void pedal::request_serial_number(void)
{
	com->sendData(SERIAL_NUMBER_REQUEST,2);
	com->setRxSize(8);								// waiting for 8 bytes
}
 
// **********************************************************
// * request_pedal_state									*
// **********************************************************
void pedal::request_pedal_state(void)
{
	pedal_state_received = false;
	com->sendData(PEDAL_STATE_REQUEST,1);
	com->setRxSize(1);								// waiting for 1 byte
}
 
// **********************************************************
// * configure_io_mode										*
// **********************************************************
void pedal::configure_io_mode(void)
{
	pedal_status = PEDAL_NOK;
	com->sendData(CONFIGURE_IO_MODE,3);
	com->setRxSize(2);								// waiting for 2 bytes
}
 
// **********************************************************
// * search_pedal_port 										*
// *														*
// * enumerate the serial ports	on the tablet				*
// * search for the one connected to the pedal				*
// **********************************************************
bool pedal::search_pedal_port(void)
{
	std::string			sport_number;
	std::string			sport_name;
	char*			    cport_name;
	int					imax;
 
	if (enumerate_serial_ports())
	{
		#ifdef CENUMERATESERIAL_USE_STL
			imax = ports.size();
		#else
			imax = ports.GetSize();
		#endif
		i = 0;
		while ((pedalport == PEDALPORT_UNKNOWN)&&(i != imax))
		{
			sport_number = to_string(ports[i]);
			sport_name = "COM" + sport_number;
			cport_name = const_cast<char*>(sport_name.c_str());
 
			com = new tserial_event();
			if (com!=0)
			{
				com->setManager(this->SerialEventManager);
				error = com->connect(cport_name, PEDAL_PORT_SPEED, SERIAL_PARITY_NONE, PEDAL_PORT_BITS, PEDAL_PORT_MODEM_EVENT);
				if (!error)
				{
					this->request_iss_version();
					this->wait_for_reply();
					while ((!iss_version_received)&&(!this->pedalreply_timeelapsed));
					if (iss_version_received)
					{
						pedalport = cport_name;
						this->configure_io_mode();
						this->wait_for_reply();
						while ((!io_mode_received)&&(!this->pedalreply_timeelapsed));
						if (io_mode_received)
						{
							this->request_pedal_state();
							this->wait_for_reply();
							while ((!pedal_state_received)&&(!this->pedalreply_timeelapsed));
							return (pedal_state_received);
						}
					}
					else
					{
						com->disconnect();
						i++;
					}
				}
			}
		}
		return PEDAL_UNDETECTED;
	}
 
}
 
// **********************************************************
// * deactivate_pedal										*
// **********************************************************
void pedal::deactivate_pedal(void)
{
	timer_pedal_request.Stop();
	timer_pedal_reply.Stop();
 
	if (com != 0)
		com->disconnect();
}

En l'état, enumerate ne fait rien hormis d'afficher un message toutes les 2 secondes.
Il y a une séquence d'initialisation de la pédale et si OK alors on vient interroger par timer la pédale toutes les 200 ms avec une attente de données de 20 ms.
Tout cela fonctionne à merveille.
Maintenant je souhaiterais déplacer :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
if (My_Pedal.search_pedal_port())
    My_Pedal.activate_pedal();
pour le mettre dans enumerate.

Si je fais celà, le timer initialisé par wait_for_reply() ne se lance pas et donc je boucle dans le premier while de la méthode search_pedal_port().
Je ne comprends pas pourquoi ....


Un tout petit peu d'aide serait bienvenue ... Merci