Several small problems in MFC application

Recently, a test program of MFC framework is maintained to test the command protocol of HID equipment. Several small problems encountered and solved in the middle are recorded as follows:

 

1. Get the system time in milliseconds

SYSTEMTIME sys;
	GetLocalTime(&sys);
	char nTime[20];
	sprintf(nTime, "%02d:%02d:%02d.%03d : ", sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds);

 

2. When using hid_enumerate to get the list of hid devices, the used hid devices disappeared.

Later found

a. After connecting a device, hid_enumerate will no longer show this device

b. After the device connection is closed, the thread that reads the data does not exit, so the device is still not available when hid_enumerate is used to obtain the device. So the thread needs to be closed when the connection is closed.

       ThreadFlag = false;
	hid_close(HID_handle);
	HID_handle = NULL;
	DWORD exitCode = 0;
	for (;;)
	{
		GetExitCodeThread(pReadHIDThread, &exitCode);
		if (exitCode != STILL_ACTIVE)
		break;
	}

 Increase the flag judgment of the read data site, return 0 when false. Set the flag to false when closing the device, and judge that the thread has ended.

 

3. Get the input in the edit box and convert it to uint8_t. (mainly the number of bytes must be correct, otherwise it will overflow). Because it is a cstring, use swscanf()

        uint8_t buff[13];
	CString txt;
	GetDlgItem(IDC_EDIT_CFGCMD)->GetWindowText(txt);
	swscanf (txt, L "% hhx% hhx% hhx% hhx% hhx% hhx% hhx% hhx% hhx% hhx% hhx% hhx% hhx", & buff [0], & buff [1], & buff [2], & buff [3], & buff [4], & buff [5], & buff [6], & buff [7], & buff [8], & buff [9], & buff [10], & buff [11], & buff [12]);
	

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326454058&siteId=291194637