Browse Source

ReadKey有问题。

scbc.sat2 5 years ago
parent
commit
f9d982a2de
2 changed files with 36 additions and 2 deletions
  1. 35 1
      TestWizard/TestWizard/TestWizard.cpp
  2. 1 1
      TestWizard/TestWizard/TestWizard.h

+ 35 - 1
TestWizard/TestWizard/TestWizard.cpp

@@ -42,6 +42,10 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 		// TODO: 在此处为应用程序的行为编写代码。
 		if ( OpenDevice(7, 115200, 8, 0, 0) )
 		{
+			while ( ReadKey() )
+				Sleep(100);
+			Sleep(10000000);
+
 			OpenXML("E:\\bin\\TestWizard\\data.xml");
 			
 			int time = 1500;
@@ -383,7 +387,37 @@ TESTWIZARD_API bool SendKey(LPCTSTR lpKeyName)
 
 TESTWIZARD_API bool ReadKey()
 {
-	return false;
+	byte szKeyData[MAX_PATH] = {0};
+	DWORD dwNumberOfBytesRead = g_synSerial.ReadComm(szKeyData, MAX_PATH, 300);
+	if ( dwNumberOfBytesRead <= 0 )
+		return false;
+
+	// 如果是C++11,使用std正则;[码类型]<码内容>0D 0A;
+	// 不是 "[" 开头;
+	if ( szKeyData[0] != 0x5B )
+		return false;
+
+	// 查找 "]" 的位置;
+	int pos = 0;
+	byte *p = szKeyData;
+	while ( p++, pos++ )
+		if (*p == 0x5D)
+			break;
+
+	// 不在有效范围内;
+	if ( pos >= dwNumberOfBytesRead - 2 )
+		return false;
+	
+
+	// 不是0d 0a结束;
+	if ( szKeyData[dwNumberOfBytesRead-2] != 0x0D &&  szKeyData[dwNumberOfBytesRead-1] != 0x0A)
+		return false;
+
+	KeyItem keyItem;
+	keyItem.keyType.append((char*)(&szKeyData[1]), pos-2);
+	keyItem.keyCode.append((char*)(&szKeyData[pos+1]), dwNumberOfBytesRead-pos-4);
+
+	return true;
 }
 
 #endif

+ 1 - 1
TestWizard/TestWizard/TestWizard.h

@@ -32,5 +32,5 @@ TESTWIZARD_API void CloseDevice();
 // ·¢ËͰ´¼ü;
 TESTWIZARD_API bool SendKey(LPCTSTR lpKeyName);
 // ¶Áȡѧϰ°´¼ü;
-TESTWIZARD_API bool ReaddKey();
+TESTWIZARD_API bool ReadKey();
 #endif