TestWizard.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. // TestWizard.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "TestWizard.h"
  5. #include "SynSerial.h"
  6. #include "SerialPort.h"
  7. #ifdef __MAKE_PYD__
  8. #include "Python.h"
  9. #endif
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #endif
  13. //////////////////////////////////////////////////////////////////////////
  14. // 全局变量:是否开启load模式(可同时加载多个文件,相同KeyName覆盖)
  15. bool g_bEnableLoad = false;
  16. std::string g_strXMLPath;
  17. std::vector<KeyItem> g_vtKeyItem;
  18. CSynSerial g_synSerial;
  19. CSynSerial g_usbSwitch;
  20. CSerialPort g_serialPort;
  21. std::string tolower(const char *p);
  22. std::string toupper(const char *p);
  23. // 唯一的应用程序对象
  24. #ifdef __CONSOLE__
  25. CWinApp theApp;
  26. using namespace std;
  27. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  28. {
  29. int nRetCode = 0;
  30. // 初始化 MFC 并在失败时显示错误
  31. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  32. {
  33. // TODO: 更改错误代码以符合您的需要
  34. _tprintf(_T("错误: MFC 初始化失败\n"));
  35. nRetCode = 1;
  36. }
  37. else
  38. {
  39. #if 1
  40. int port;
  41. printf("Please enter the serial number =");
  42. scanf("%d", &port);
  43. // TODO: 在此处为应用程序的行为编写代码。
  44. if ( OpenDevice(port, 115200, 8, 0, 0) )
  45. {
  46. if ( OpenXML("D:\\SAT\\resource\\RCU\\TW\\G_AP_0F_IR.xml") == 0 )
  47. {
  48. SendCmd("*about");
  49. Sleep(1000);
  50. SendKey("POWER");
  51. }
  52. CloseDevice();
  53. }
  54. else
  55. {
  56. printf("OpenDevice fail\n");
  57. }
  58. #endif
  59. }
  60. system("pause");
  61. return nRetCode;
  62. }
  63. #endif
  64. #ifdef __MAKE_PYD__
  65. static PyObject* OpenXML(PyObject* self, PyObject* args)
  66. {
  67. return Py_BuildValue("b", 1);
  68. }
  69. static PyObject* LoadXML(PyObject* self, PyObject* args)
  70. {
  71. return Py_BuildValue("b", 1);
  72. }
  73. static PyObject* AddItem(PyObject* self, PyObject* args)
  74. {
  75. return Py_BuildValue("b", 1);
  76. }
  77. static PyObject* DelItem(PyObject* self, PyObject* args)
  78. {
  79. return Py_BuildValue("b", 1);
  80. }
  81. static PyObject* RemoveAllItem(PyObject* self, PyObject* args)
  82. {
  83. return Py_BuildValue("b", 1);
  84. }
  85. static PyObject* FindItem(PyObject* self, PyObject* args)
  86. {
  87. return Py_BuildValue("b", 1);
  88. }
  89. static PyObject* Save2XML(PyObject* self, PyObject* args)
  90. {
  91. return Py_BuildValue("b", 1);
  92. }
  93. static PyObject* OpenDevice(PyObject* self, PyObject* args)
  94. {
  95. return Py_BuildValue("b", 1);
  96. }
  97. static PyObject* SendKey(PyObject* self, PyObject* args)
  98. {
  99. return Py_BuildValue("b", 1);
  100. }
  101. static PyObject* CloseDevice(PyObject* self, PyObject* args)
  102. {
  103. return Py_BuildValue("b", 1);
  104. }
  105. // 描述方法,暴露给python的函数;
  106. static PyMethodDef TestWizard_Methods[] = {
  107. {"OpenXML", OpenXML, METH_VARARGS, "编辑:打开XML(只有一个文件存在)"},
  108. {"LoadXML", LoadXML, METH_VARARGS, "运行:加载XML(可多个文件累加)"},
  109. {"AddItem", AddItem, METH_VARARGS, "编辑:添加按键"},
  110. {"DelItem", DelItem, METH_VARARGS, "编辑:删除按键"},
  111. {"RemoveAllItem", RemoveAllItem, METH_VARARGS, "编辑:删除所有按键"},
  112. {"FindItem", FindItem, METH_VARARGS, "编辑:查找按键"},
  113. {"Save2XML", Save2XML, METH_VARARGS, "编辑:保存到XML"},
  114. {"OpenDevice", OpenDevice, METH_VARARGS, "运行:打开设备"},
  115. {"SendKey", SendKey, METH_VARARGS, "运行:发送按键"},
  116. {"CloseDevice", CloseDevice, METH_VARARGS, "运行:关闭设备"},
  117. {NULL,NULL}
  118. };
  119. // 初始模块;//格式:init<模块名称>
  120. PyMODINIT_FUNC initTestWizard()
  121. {
  122. // 初始化pyd函数列表;
  123. PyObject* m, * d;
  124. m = Py_InitModule("TestWizard", TestWizard_Methods);
  125. d = PyModule_GetDict(m);
  126. }
  127. #else
  128. std::string tolower(const char *p)
  129. {
  130. if ( p == NULL )
  131. return std::string();
  132. std::string str;
  133. while ( *p != '\0') {
  134. if ( *p >= 'A' && *p <= 'Z' )
  135. str.append(1,*p + 32);
  136. else
  137. str.append(1,*p);
  138. p++;
  139. }
  140. return str;
  141. }
  142. std::string toupper(const char *p)
  143. {
  144. if ( p == NULL )
  145. return std::string();
  146. std::string str;
  147. while ( *p != '\0') {
  148. if ( *p >= 'a' && *p <= 'z' )
  149. str.append(1,*p - 32);
  150. else
  151. str.append(1,*p);
  152. p++;
  153. }
  154. return str;
  155. }
  156. int __LoadXML(LPCTSTR lpXMLPath)
  157. {
  158. if ( !lpXMLPath || !PathFileExists(lpXMLPath) )
  159. {
  160. #ifdef __CONSOLE__
  161. printf("__LoadXML Error\n");
  162. #endif
  163. return -1;
  164. }
  165. g_strXMLPath = lpXMLPath;
  166. // 解析xml;
  167. tinyxml2::XMLDocument doc;
  168. if (tinyxml2::XML_SUCCESS != doc.LoadFile(lpXMLPath))
  169. {
  170. #ifdef __CONSOLE__
  171. printf("LoadFile Error\n");
  172. #endif
  173. return -1;
  174. }
  175. std::string ver;
  176. std::string desc;
  177. tinyxml2::XMLElement *pXmlRoot = NULL;
  178. if ((pXmlRoot = doc.RootElement()) != NULL) {
  179. if (_tcsicmp(pXmlRoot->Value(), "KeyList") == 0) {
  180. // 属性;
  181. const tinyxml2::XMLAttribute *pAttr = pXmlRoot->FirstAttribute();
  182. while (pAttr) {
  183. if (_tcsicmp(pAttr->Name(), "ver") == 0)
  184. ver = pAttr->Value();
  185. if (_tcsicmp(pAttr->Name(), "desc") == 0)
  186. desc = pAttr->Value();
  187. pAttr = pAttr->Next();
  188. }
  189. // 子项;
  190. tinyxml2::XMLElement *pXmlElent = pXmlRoot->FirstChildElement();
  191. while (pXmlElent) {
  192. KeyItem keyItem;
  193. if (_tcsicmp(pXmlElent->Value(), _T("ITEM")) == 0) {
  194. tinyxml2::XMLElement *pItem = pXmlElent->FirstChildElement();
  195. while (pItem) {
  196. if (_tcsicmp(pItem->Value(), _T("KeyName")) == 0)
  197. {
  198. keyItem.keyName = tolower(pItem->GetText());
  199. }
  200. else if (_tcsicmp(pItem->Value(), _T("KeyDesc")) == 0)
  201. {
  202. keyItem.keyDesc = pItem->GetText();
  203. }
  204. else if (_tcsicmp(pItem->Value(), _T("KeyType")) == 0)
  205. {
  206. keyItem.keyType = pItem->GetText();
  207. }
  208. else if (_tcsicmp(pItem->Value(), _T("KeyCode")) == 0)
  209. {
  210. keyItem.keyCode = pItem->GetText();
  211. }
  212. pItem = pItem->NextSiblingElement();
  213. }
  214. }
  215. // 如果已经添加过的;
  216. KeyItem *pkeyItem = FindItem(keyItem.keyName.c_str());
  217. if ( pkeyItem ) {
  218. pkeyItem->keyDesc = keyItem.keyDesc;
  219. pkeyItem->keyType = keyItem.keyType;
  220. pkeyItem->keyCode = keyItem.keyCode;
  221. }
  222. else
  223. g_vtKeyItem.push_back(keyItem);
  224. pXmlElent = pXmlElent->NextSiblingElement();
  225. }
  226. }
  227. }
  228. return 0;
  229. }
  230. TESTWIZARD_API int OpenXML(LPCTSTR lpXMLPath)
  231. {
  232. g_vtKeyItem.clear();
  233. return __LoadXML(lpXMLPath);
  234. }
  235. TESTWIZARD_API int LoadXML(LPCTSTR lpXMLPath)
  236. {
  237. return __LoadXML(lpXMLPath);
  238. }
  239. TESTWIZARD_API void AddItem(LPCTSTR lpKeyName, LPCTSTR lpKeyDesc, LPCTSTR lpKeyType, LPCTSTR lpKeyCode)
  240. {
  241. if ( lpKeyName == NULL || lpKeyName[0] != '\0' ||
  242. lpKeyType == NULL || lpKeyType[0] != '\0' ||
  243. lpKeyCode == NULL || lpKeyCode[0] != '\0' )
  244. {
  245. return;
  246. }
  247. KeyItem keyItem;
  248. keyItem.keyName = lpKeyName;
  249. keyItem.keyDesc = lpKeyDesc ? lpKeyDesc : "";
  250. keyItem.keyType = lpKeyType;
  251. keyItem.keyCode = lpKeyCode;
  252. g_vtKeyItem.push_back(keyItem);
  253. }
  254. TESTWIZARD_API void DelItem(LPCTSTR lpKeyName)
  255. {
  256. if ( !lpKeyName || lpKeyName[0] == '\0' )
  257. return;
  258. for ( std::vector<KeyItem>::iterator it = g_vtKeyItem.begin(); it != g_vtKeyItem.end(); it++ )
  259. {
  260. if ( _tcsicmp(it->keyName.c_str(), lpKeyName) == 0 )
  261. {
  262. g_vtKeyItem.erase(it);
  263. break;
  264. }
  265. }
  266. }
  267. TESTWIZARD_API void RemoveAllItem(void)
  268. {
  269. g_vtKeyItem.clear();
  270. }
  271. TESTWIZARD_API KeyItem* FindItem(LPCTSTR lpKeyName)
  272. {
  273. if ( !lpKeyName || lpKeyName[0] == '\0' )
  274. return NULL;
  275. for ( std::vector<KeyItem>::iterator it = g_vtKeyItem.begin(); it != g_vtKeyItem.end(); it++ )
  276. {
  277. if ( _tcsicmp(it->keyName.c_str(), lpKeyName) == 0 )
  278. {
  279. return &*it;
  280. break;
  281. }
  282. }
  283. return NULL;
  284. }
  285. TESTWIZARD_API int Save2XML(LPCTSTR lpXMLPath)
  286. {
  287. tinyxml2::XMLDocument doc;
  288. //添加申明可以使用如下两行
  289. tinyxml2::XMLDeclaration *declaration = doc.NewDeclaration();
  290. doc.InsertFirstChild(declaration);
  291. tinyxml2::XMLElement *root = doc.NewElement("KeyList");
  292. root->SetAttribute("ver", "1.0.0.1");
  293. root->SetAttribute("desc", "key info");
  294. doc.InsertEndChild(root);
  295. std::vector<KeyItem>::iterator it = g_vtKeyItem.begin();
  296. for (; it != g_vtKeyItem.end(); it++) {
  297. tinyxml2::XMLElement *pItem = doc.NewElement("ITEM");
  298. tinyxml2::XMLElement *pKeyName = doc.NewElement("KeyName");
  299. pKeyName->SetText(it->keyName.c_str());
  300. pItem->InsertEndChild(pKeyName);
  301. tinyxml2::XMLElement *pKeyDesc = doc.NewElement("KeyDesc");
  302. pKeyDesc->SetText(it->keyDesc.c_str());
  303. pItem->InsertEndChild(pKeyDesc);
  304. tinyxml2::XMLElement *pKeyType = doc.NewElement("KeyType");
  305. pKeyType->SetText(it->keyType.c_str());
  306. pItem->InsertEndChild(pKeyType);
  307. tinyxml2::XMLElement *pKeyCode = doc.NewElement("KeyCode");
  308. pKeyCode->SetText(it->keyCode.c_str());
  309. pItem->InsertEndChild(pKeyCode);
  310. root->InsertEndChild(pItem);
  311. }
  312. std::string path;
  313. if ( lpXMLPath && lpXMLPath[0] != '\0' ) {
  314. MKDIR(lpXMLPath);
  315. if ( PathFileExists(lpXMLPath) )
  316. path = lpXMLPath;
  317. }
  318. else
  319. {
  320. if ( PathFileExists(g_strXMLPath.c_str()) )
  321. path = g_strXMLPath;
  322. }
  323. if ( !PathFileExists(path.c_str()) )
  324. return -1;
  325. return doc.SaveFile( path.c_str() );
  326. }
  327. // 打开串口;
  328. TESTWIZARD_API BOOL OpenDevice(int nPort, DWORD dwBaudrate, BYTE ByteSize, BYTE Parity, BYTE StopBits)
  329. {
  330. // 关闭打开的;
  331. if ( g_synSerial.IsOpen() )
  332. g_synSerial.CloseSerialPort();
  333. g_synSerial.OpenSerialPort(nPort, dwBaudrate, ByteSize, Parity, StopBits, 0, 1000);
  334. return g_synSerial.IsOpen();
  335. }
  336. // 关闭串口;
  337. TESTWIZARD_API void CloseDevice()
  338. {
  339. if ( g_synSerial.IsOpen() )
  340. g_synSerial.CloseSerialPort();
  341. }
  342. TESTWIZARD_API bool SendCmd(LPCTSTR strCommand)
  343. {
  344. // 封装发送包;
  345. byte szSendData[MAX_PATH] = {0};
  346. _stprintf_s((TCHAR*)szSendData, MAX_PATH, _T("%s\r"), strCommand);
  347. #ifdef __CONSOLE__
  348. DWORD dwTickCout = GetTickCount();
  349. #endif
  350. DWORD dwNumberOfBytesWritten = g_synSerial.WriteComm(szSendData, _tcslen((TCHAR*)szSendData));
  351. if ( dwNumberOfBytesWritten != _tcslen((TCHAR*)szSendData) )
  352. return false;
  353. memset(szSendData, 0, MAX_PATH);
  354. DWORD dwNumberOfBytesRead = g_synSerial.ReadComm(szSendData, MAX_PATH);
  355. #ifdef __CONSOLE__
  356. dwTickCout = GetTickCount() - dwTickCout;
  357. printf("Time consuming=%ld\n\n", dwTickCout);
  358. #endif
  359. return true;
  360. }
  361. TESTWIZARD_API bool SendKey(LPCTSTR lpKeyName)
  362. {
  363. if ( !g_synSerial.IsOpen() )
  364. return false;
  365. KeyItem *pkeyItem = FindItem(lpKeyName);
  366. if ( pkeyItem == NULL )
  367. return false;
  368. // 封装发送包;
  369. byte szSendData[MAX_PATH] = {0};
  370. _stprintf_s((TCHAR*)szSendData, MAX_PATH, _T("*INPUT %s %s\r"), pkeyItem->keyType.c_str(), pkeyItem->keyCode.c_str());
  371. #ifdef __CONSOLE__
  372. DWORD dwTickCout = GetTickCount();
  373. #endif
  374. DWORD dwNumberOfBytesWritten = g_synSerial.WriteComm(szSendData, _tcslen((TCHAR*)szSendData));
  375. if ( dwNumberOfBytesWritten != _tcslen((TCHAR*)szSendData) )
  376. return false;
  377. memset(szSendData, 0, MAX_PATH);
  378. DWORD dwNumberOfBytesRead = g_synSerial.ReadComm(szSendData, MAX_PATH);
  379. #ifdef __CONSOLE__
  380. dwTickCout = GetTickCount() - dwTickCout;
  381. printf("Time consuming=%ld\n\n", dwTickCout);
  382. #endif
  383. return dwNumberOfBytesRead ? true : false;
  384. }
  385. TESTWIZARD_API bool ReadKey()
  386. {
  387. if ( !g_synSerial.IsOpen() )
  388. return false;
  389. byte szKeyData[MAX_PATH] = {0};
  390. DWORD dwNumberOfBytesRead = g_synSerial.ReadComm(szKeyData, MAX_PATH, 300);
  391. if ( dwNumberOfBytesRead <= 0 )
  392. return false;
  393. // 如果是C++11,使用std正则;[码类型]<码内容>0D 0A;
  394. // 不是 "[" 开头;
  395. if ( szKeyData[0] != 0x5B )
  396. return false;
  397. // 查找 "]" 的位置;
  398. int pos = 0;
  399. byte *p = szKeyData;
  400. while ( p++, pos++ )
  401. if (*p == 0x5D)
  402. break;
  403. // 不在有效范围内;
  404. if ( pos >= dwNumberOfBytesRead - 2 )
  405. return false;
  406. // 不是0d 0a结束;
  407. if ( szKeyData[dwNumberOfBytesRead-2] != 0x0D && szKeyData[dwNumberOfBytesRead-1] != 0x0A)
  408. return false;
  409. KeyItem keyItem;
  410. keyItem.keyType.append((char*)(&szKeyData[1]), pos-2);
  411. keyItem.keyCode.append((char*)(&szKeyData[pos+1]), dwNumberOfBytesRead-pos-4);
  412. return true;
  413. }
  414. TESTWIZARD_API LPCTSTR GetSignalsName()
  415. {
  416. static std::string signals;
  417. signals.clear();
  418. for ( std::vector<KeyItem>::iterator it = g_vtKeyItem.begin(); it != g_vtKeyItem.end(); it++ )
  419. {
  420. signals.append(it->keyName + "\n");
  421. }
  422. return signals.c_str();
  423. }
  424. //////////////////////////////////////////////////////////////////////////
  425. TESTWIZARD_API BOOL OpenUsbSwitch(int nPort, DWORD dwBaudrate, BYTE ByteSize, BYTE Parity, BYTE StopBits)
  426. {
  427. // 关闭打开的;
  428. if ( g_usbSwitch.IsOpen() )
  429. g_usbSwitch.CloseSerialPort();
  430. g_usbSwitch.OpenSerialPort(nPort, dwBaudrate, ByteSize, Parity, StopBits, 0, 1000);
  431. return g_usbSwitch.IsOpen();
  432. }
  433. TESTWIZARD_API bool SendSwitch(int nIndex /*=0*/)
  434. {
  435. if ( !g_usbSwitch.IsOpen() )
  436. return false;
  437. // 封装发送包;
  438. byte szSendData[MAX_PATH] = {0};
  439. _stprintf_s((TCHAR*)szSendData, MAX_PATH, _T("*SET PORT %c\r"), nIndex == 0 ? 'A' : 'B');
  440. DWORD dwNumberOfBytesWritten = g_usbSwitch.WriteComm(szSendData, _tcslen((TCHAR*)szSendData));
  441. if ( dwNumberOfBytesWritten != _tcslen((TCHAR*)szSendData) )
  442. return false;
  443. memset(szSendData, 0, MAX_PATH);
  444. DWORD dwNumberOfBytesRead = g_usbSwitch.ReadComm(szSendData, MAX_PATH);
  445. return dwNumberOfBytesRead ? true : false;
  446. }
  447. // 关闭串口;
  448. TESTWIZARD_API void CloseUsbSwitch()
  449. {
  450. if ( g_usbSwitch.IsOpen() )
  451. g_usbSwitch.CloseSerialPort();
  452. }
  453. #endif