123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- // TestWizard.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include "stdafx.h"
- #include "TestWizard.h"
- #include "SynSerial.h"
- #include "SerialPort.h"
- #ifdef __MAKE_PYD__
- #include "Python.h"
- #endif
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////////
- // 全局变量:是否开启load模式(可同时加载多个文件,相同KeyName覆盖)
- bool g_bEnableLoad = false;
- std::string g_strXMLPath;
- std::vector<KeyItem> g_vtKeyItem;
- CSynSerial g_synSerial;
- CSynSerial g_usbSwitch;
- CSerialPort g_serialPort;
- std::string tolower(const char *p);
- std::string toupper(const char *p);
- // 唯一的应用程序对象
- #ifdef __CONSOLE__
- CWinApp theApp;
- using namespace std;
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: 更改错误代码以符合您的需要
- _tprintf(_T("错误: MFC 初始化失败\n"));
- nRetCode = 1;
- }
- else
- {
- #if 1
- int port;
- printf("Please enter the serial number =");
- scanf("%d", &port);
- // TODO: 在此处为应用程序的行为编写代码。
- if ( OpenDevice(port, 115200, 8, 0, 0) )
- {
- if ( OpenXML("D:\\SAT\\resource\\RCU\\TW\\G_AP_0F_IR.xml") == 0 )
- {
- SendCmd("*about");
- Sleep(1000);
- SendKey("POWER");
- }
- CloseDevice();
- }
- else
- {
- printf("OpenDevice fail\n");
- }
- #endif
- }
- system("pause");
- return nRetCode;
- }
- #endif
- #ifdef __MAKE_PYD__
- static PyObject* OpenXML(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- static PyObject* LoadXML(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- static PyObject* AddItem(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- static PyObject* DelItem(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- static PyObject* RemoveAllItem(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- static PyObject* FindItem(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- static PyObject* Save2XML(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- static PyObject* OpenDevice(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- static PyObject* SendKey(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- static PyObject* CloseDevice(PyObject* self, PyObject* args)
- {
- return Py_BuildValue("b", 1);
- }
- // 描述方法,暴露给python的函数;
- static PyMethodDef TestWizard_Methods[] = {
- {"OpenXML", OpenXML, METH_VARARGS, "编辑:打开XML(只有一个文件存在)"},
- {"LoadXML", LoadXML, METH_VARARGS, "运行:加载XML(可多个文件累加)"},
- {"AddItem", AddItem, METH_VARARGS, "编辑:添加按键"},
- {"DelItem", DelItem, METH_VARARGS, "编辑:删除按键"},
- {"RemoveAllItem", RemoveAllItem, METH_VARARGS, "编辑:删除所有按键"},
- {"FindItem", FindItem, METH_VARARGS, "编辑:查找按键"},
- {"Save2XML", Save2XML, METH_VARARGS, "编辑:保存到XML"},
- {"OpenDevice", OpenDevice, METH_VARARGS, "运行:打开设备"},
- {"SendKey", SendKey, METH_VARARGS, "运行:发送按键"},
- {"CloseDevice", CloseDevice, METH_VARARGS, "运行:关闭设备"},
- {NULL,NULL}
- };
- // 初始模块;//格式:init<模块名称>
- PyMODINIT_FUNC initTestWizard()
- {
- // 初始化pyd函数列表;
- PyObject* m, * d;
- m = Py_InitModule("TestWizard", TestWizard_Methods);
- d = PyModule_GetDict(m);
- }
- #else
- std::string tolower(const char *p)
- {
- if ( p == NULL )
- return std::string();
- std::string str;
- while ( *p != '\0') {
- if ( *p >= 'A' && *p <= 'Z' )
- str.append(1,*p + 32);
- else
- str.append(1,*p);
- p++;
- }
- return str;
- }
- std::string toupper(const char *p)
- {
- if ( p == NULL )
- return std::string();
- std::string str;
- while ( *p != '\0') {
- if ( *p >= 'a' && *p <= 'z' )
- str.append(1,*p - 32);
- else
- str.append(1,*p);
- p++;
- }
- return str;
- }
- int __LoadXML(LPCTSTR lpXMLPath)
- {
- if ( !lpXMLPath || !PathFileExists(lpXMLPath) )
- {
- #ifdef __CONSOLE__
- printf("__LoadXML Error\n");
- #endif
- return -1;
- }
- g_strXMLPath = lpXMLPath;
- // 解析xml;
- tinyxml2::XMLDocument doc;
- if (tinyxml2::XML_SUCCESS != doc.LoadFile(lpXMLPath))
- {
- #ifdef __CONSOLE__
- printf("LoadFile Error\n");
- #endif
- return -1;
- }
- std::string ver;
- std::string desc;
- tinyxml2::XMLElement *pXmlRoot = NULL;
- if ((pXmlRoot = doc.RootElement()) != NULL) {
- if (_tcsicmp(pXmlRoot->Value(), "KeyList") == 0) {
- // 属性;
- const tinyxml2::XMLAttribute *pAttr = pXmlRoot->FirstAttribute();
- while (pAttr) {
- if (_tcsicmp(pAttr->Name(), "ver") == 0)
- ver = pAttr->Value();
- if (_tcsicmp(pAttr->Name(), "desc") == 0)
- desc = pAttr->Value();
- pAttr = pAttr->Next();
- }
- // 子项;
- tinyxml2::XMLElement *pXmlElent = pXmlRoot->FirstChildElement();
- while (pXmlElent) {
- KeyItem keyItem;
- if (_tcsicmp(pXmlElent->Value(), _T("ITEM")) == 0) {
- tinyxml2::XMLElement *pItem = pXmlElent->FirstChildElement();
- while (pItem) {
- if (_tcsicmp(pItem->Value(), _T("KeyName")) == 0)
- {
- keyItem.keyName = tolower(pItem->GetText());
- }
- else if (_tcsicmp(pItem->Value(), _T("KeyDesc")) == 0)
- {
- keyItem.keyDesc = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("KeyType")) == 0)
- {
- keyItem.keyType = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("KeyCode")) == 0)
- {
- keyItem.keyCode = pItem->GetText();
- }
- pItem = pItem->NextSiblingElement();
- }
- }
- // 如果已经添加过的;
- KeyItem *pkeyItem = FindItem(keyItem.keyName.c_str());
- if ( pkeyItem ) {
- pkeyItem->keyDesc = keyItem.keyDesc;
- pkeyItem->keyType = keyItem.keyType;
- pkeyItem->keyCode = keyItem.keyCode;
- }
- else
- g_vtKeyItem.push_back(keyItem);
- pXmlElent = pXmlElent->NextSiblingElement();
- }
- }
- }
- return 0;
- }
- TESTWIZARD_API int OpenXML(LPCTSTR lpXMLPath)
- {
- g_vtKeyItem.clear();
- return __LoadXML(lpXMLPath);
- }
- TESTWIZARD_API int LoadXML(LPCTSTR lpXMLPath)
- {
- return __LoadXML(lpXMLPath);
- }
- TESTWIZARD_API void AddItem(LPCTSTR lpKeyName, LPCTSTR lpKeyDesc, LPCTSTR lpKeyType, LPCTSTR lpKeyCode)
- {
- if ( lpKeyName == NULL || lpKeyName[0] != '\0' ||
- lpKeyType == NULL || lpKeyType[0] != '\0' ||
- lpKeyCode == NULL || lpKeyCode[0] != '\0' )
- {
- return;
- }
- KeyItem keyItem;
- keyItem.keyName = lpKeyName;
- keyItem.keyDesc = lpKeyDesc ? lpKeyDesc : "";
- keyItem.keyType = lpKeyType;
- keyItem.keyCode = lpKeyCode;
- g_vtKeyItem.push_back(keyItem);
- }
- TESTWIZARD_API void DelItem(LPCTSTR lpKeyName)
- {
- if ( !lpKeyName || lpKeyName[0] == '\0' )
- return;
- for ( std::vector<KeyItem>::iterator it = g_vtKeyItem.begin(); it != g_vtKeyItem.end(); it++ )
- {
- if ( _tcsicmp(it->keyName.c_str(), lpKeyName) == 0 )
- {
- g_vtKeyItem.erase(it);
- break;
- }
- }
- }
- TESTWIZARD_API void RemoveAllItem(void)
- {
- g_vtKeyItem.clear();
- }
- TESTWIZARD_API KeyItem* FindItem(LPCTSTR lpKeyName)
- {
- if ( !lpKeyName || lpKeyName[0] == '\0' )
- return NULL;
- for ( std::vector<KeyItem>::iterator it = g_vtKeyItem.begin(); it != g_vtKeyItem.end(); it++ )
- {
- if ( _tcsicmp(it->keyName.c_str(), lpKeyName) == 0 )
- {
- return &*it;
- break;
- }
- }
- return NULL;
- }
- TESTWIZARD_API int Save2XML(LPCTSTR lpXMLPath)
- {
- tinyxml2::XMLDocument doc;
- //添加申明可以使用如下两行
- tinyxml2::XMLDeclaration *declaration = doc.NewDeclaration();
- doc.InsertFirstChild(declaration);
- tinyxml2::XMLElement *root = doc.NewElement("KeyList");
- root->SetAttribute("ver", "1.0.0.1");
- root->SetAttribute("desc", "key info");
- doc.InsertEndChild(root);
- std::vector<KeyItem>::iterator it = g_vtKeyItem.begin();
- for (; it != g_vtKeyItem.end(); it++) {
- tinyxml2::XMLElement *pItem = doc.NewElement("ITEM");
- tinyxml2::XMLElement *pKeyName = doc.NewElement("KeyName");
- pKeyName->SetText(it->keyName.c_str());
- pItem->InsertEndChild(pKeyName);
- tinyxml2::XMLElement *pKeyDesc = doc.NewElement("KeyDesc");
- pKeyDesc->SetText(it->keyDesc.c_str());
- pItem->InsertEndChild(pKeyDesc);
- tinyxml2::XMLElement *pKeyType = doc.NewElement("KeyType");
- pKeyType->SetText(it->keyType.c_str());
- pItem->InsertEndChild(pKeyType);
- tinyxml2::XMLElement *pKeyCode = doc.NewElement("KeyCode");
- pKeyCode->SetText(it->keyCode.c_str());
- pItem->InsertEndChild(pKeyCode);
- root->InsertEndChild(pItem);
- }
- std::string path;
- if ( lpXMLPath && lpXMLPath[0] != '\0' ) {
- MKDIR(lpXMLPath);
- if ( PathFileExists(lpXMLPath) )
- path = lpXMLPath;
- }
- else
- {
- if ( PathFileExists(g_strXMLPath.c_str()) )
- path = g_strXMLPath;
- }
- if ( !PathFileExists(path.c_str()) )
- return -1;
- return doc.SaveFile( path.c_str() );
- }
- // 打开串口;
- TESTWIZARD_API BOOL OpenDevice(int nPort, DWORD dwBaudrate, BYTE ByteSize, BYTE Parity, BYTE StopBits)
- {
- // 关闭打开的;
- if ( g_synSerial.IsOpen() )
- g_synSerial.CloseSerialPort();
- g_synSerial.OpenSerialPort(nPort, dwBaudrate, ByteSize, Parity, StopBits, 0, 1000);
- return g_synSerial.IsOpen();
- }
- // 关闭串口;
- TESTWIZARD_API void CloseDevice()
- {
- if ( g_synSerial.IsOpen() )
- g_synSerial.CloseSerialPort();
- }
- TESTWIZARD_API bool SendCmd(LPCTSTR strCommand)
- {
- // 封装发送包;
- byte szSendData[MAX_PATH] = {0};
- _stprintf_s((TCHAR*)szSendData, MAX_PATH, _T("%s\r"), strCommand);
- #ifdef __CONSOLE__
- DWORD dwTickCout = GetTickCount();
- #endif
- DWORD dwNumberOfBytesWritten = g_synSerial.WriteComm(szSendData, _tcslen((TCHAR*)szSendData));
- if ( dwNumberOfBytesWritten != _tcslen((TCHAR*)szSendData) )
- return false;
- memset(szSendData, 0, MAX_PATH);
- DWORD dwNumberOfBytesRead = g_synSerial.ReadComm(szSendData, MAX_PATH);
- #ifdef __CONSOLE__
- dwTickCout = GetTickCount() - dwTickCout;
- printf("Time consuming=%ld\n\n", dwTickCout);
- #endif
- return true;
- }
- TESTWIZARD_API bool SendKey(LPCTSTR lpKeyName)
- {
- if ( !g_synSerial.IsOpen() )
- return false;
- KeyItem *pkeyItem = FindItem(lpKeyName);
- if ( pkeyItem == NULL )
- return false;
- // 封装发送包;
- byte szSendData[MAX_PATH] = {0};
- _stprintf_s((TCHAR*)szSendData, MAX_PATH, _T("*INPUT %s %s\r"), pkeyItem->keyType.c_str(), pkeyItem->keyCode.c_str());
- #ifdef __CONSOLE__
- DWORD dwTickCout = GetTickCount();
- #endif
- DWORD dwNumberOfBytesWritten = g_synSerial.WriteComm(szSendData, _tcslen((TCHAR*)szSendData));
- if ( dwNumberOfBytesWritten != _tcslen((TCHAR*)szSendData) )
- return false;
- memset(szSendData, 0, MAX_PATH);
- DWORD dwNumberOfBytesRead = g_synSerial.ReadComm(szSendData, MAX_PATH);
- #ifdef __CONSOLE__
- dwTickCout = GetTickCount() - dwTickCout;
- printf("Time consuming=%ld\n\n", dwTickCout);
- #endif
- return dwNumberOfBytesRead ? true : false;
- }
- TESTWIZARD_API bool ReadKey()
- {
- if ( !g_synSerial.IsOpen() )
- 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;
- }
- TESTWIZARD_API LPCTSTR GetSignalsName()
- {
- static std::string signals;
- signals.clear();
- for ( std::vector<KeyItem>::iterator it = g_vtKeyItem.begin(); it != g_vtKeyItem.end(); it++ )
- {
- signals.append(it->keyName + "\n");
- }
- return signals.c_str();
- }
- //////////////////////////////////////////////////////////////////////////
- TESTWIZARD_API BOOL OpenUsbSwitch(int nPort, DWORD dwBaudrate, BYTE ByteSize, BYTE Parity, BYTE StopBits)
- {
- // 关闭打开的;
- if ( g_usbSwitch.IsOpen() )
- g_usbSwitch.CloseSerialPort();
- g_usbSwitch.OpenSerialPort(nPort, dwBaudrate, ByteSize, Parity, StopBits, 0, 1000);
- return g_usbSwitch.IsOpen();
- }
- TESTWIZARD_API bool SendSwitch(int nIndex /*=0*/)
- {
- if ( !g_usbSwitch.IsOpen() )
- return false;
- // 封装发送包;
- byte szSendData[MAX_PATH] = {0};
- _stprintf_s((TCHAR*)szSendData, MAX_PATH, _T("*SET PORT %c\r"), nIndex == 0 ? 'A' : 'B');
- DWORD dwNumberOfBytesWritten = g_usbSwitch.WriteComm(szSendData, _tcslen((TCHAR*)szSendData));
- if ( dwNumberOfBytesWritten != _tcslen((TCHAR*)szSendData) )
- return false;
- memset(szSendData, 0, MAX_PATH);
- DWORD dwNumberOfBytesRead = g_usbSwitch.ReadComm(szSendData, MAX_PATH);
- return dwNumberOfBytesRead ? true : false;
- }
- // 关闭串口;
- TESTWIZARD_API void CloseUsbSwitch()
- {
- if ( g_usbSwitch.IsOpen() )
- g_usbSwitch.CloseSerialPort();
- }
- #endif
|