TestWizard.cpp 12 KB

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