DAControl.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // DAControl.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "DAControl.h"
  5. #include "SynSerial.h"
  6. #ifdef __MAKE_PYD__
  7. #include "Python.h"
  8. #endif
  9. #define SENDLEN 6
  10. bool g_bEnableLoad = false;
  11. std::string g_strXMLPath;
  12. std::vector<KeyItem> g_vtKeyItem;
  13. CSynSerial g_synSerial;
  14. // 最大电压值;
  15. float g_fMaxVoltage = 0.0;
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #endif
  19. #ifdef __CONSOLE__
  20. // 唯一的应用程序对象
  21. CWinApp theApp;
  22. using namespace std;
  23. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  24. {
  25. int nRetCode = 0;
  26. // 初始化 MFC 并在失败时显示错误
  27. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  28. {
  29. // TODO: 更改错误代码以符合您的需要
  30. _tprintf(_T("错误: MFC 初始化失败\n"));
  31. nRetCode = 1;
  32. }
  33. else
  34. {
  35. // TODO: 在此处为应用程序的行为编写代码。
  36. // 获取个位数和小数位;
  37. int a = 0, b = 0; // 个位数,小数位;
  38. TCHAR szVolt[16] = {0};
  39. _gcvt_s(szVolt, 1.256, 3);
  40. sscanf_s(szVolt, _T("%d.%d"), &a, &b);
  41. byte szSendData[SENDLEN] = {0};
  42. _stprintf_s((TCHAR*)szSendData, SENDLEN, _T("%c%c%c%c%c"), 0x5A, 0, a, b, 0xA5);
  43. int len = sizeof(szSendData);
  44. printf("%s, %d,%d\n", szSendData, a, b);
  45. }
  46. system("pause");
  47. return nRetCode;
  48. }
  49. #endif
  50. std::string tolower(const char *p)
  51. {
  52. if ( p == NULL )
  53. return std::string();
  54. std::string str;
  55. while ( *p != '\0') {
  56. if ( *p >= 'A' && *p <= 'Z' )
  57. str.append(1,*p + 32);
  58. else
  59. str.append(1,*p);
  60. p++;
  61. }
  62. return str;
  63. }
  64. std::string toupper(const char *p)
  65. {
  66. if ( p == NULL )
  67. return std::string();
  68. std::string str;
  69. while ( *p != '\0') {
  70. if ( *p >= 'a' && *p <= 'z' )
  71. str.append(1,*p - 32);
  72. else
  73. str.append(1,*p);
  74. p++;
  75. }
  76. return str;
  77. }
  78. int __LoadXML(LPCTSTR lpXMLPath)
  79. {
  80. if ( !lpXMLPath || !PathFileExists(lpXMLPath) )
  81. return -1;
  82. g_strXMLPath = lpXMLPath;
  83. // 解析xml;
  84. tinyxml2::XMLDocument doc;
  85. if (tinyxml2::XML_SUCCESS != doc.LoadFile(lpXMLPath))
  86. return -1;
  87. tinyxml2::XMLElement *pXmlRoot = NULL;
  88. if ((pXmlRoot = doc.RootElement()) != NULL) {
  89. if (_tcsicmp(pXmlRoot->Value(), "KeyList") == 0) {
  90. // 属性;
  91. const tinyxml2::XMLAttribute *pAttr = pXmlRoot->FirstAttribute();
  92. if (pAttr) {
  93. if (_tcsicmp(pAttr->Name(), "MaxVolt") == 0) {
  94. // 读出最大电压值;
  95. const char* pVal = pAttr->Value();
  96. if ( pVal ) {
  97. g_fMaxVoltage = _tstof(pVal);
  98. }
  99. }
  100. }
  101. // 没有限制电压值;
  102. if ( g_fMaxVoltage == 0.0 )
  103. return -1;
  104. // 子项;
  105. tinyxml2::XMLElement *pXmlElent = pXmlRoot->FirstChildElement();
  106. while (pXmlElent) {
  107. KeyItem keyItem;
  108. if (_tcsicmp(pXmlElent->Value(), _T("ITEM")) == 0) {
  109. tinyxml2::XMLElement *pItem = pXmlElent->FirstChildElement();
  110. while (pItem) {
  111. if (_tcsicmp(pItem->Value(), _T("KeyName")) == 0) {
  112. keyItem.keyName = tolower(pItem->GetText());
  113. }
  114. else if (_tcsicmp(pItem->Value(), _T("KeyDesc")) == 0) {
  115. keyItem.keyDesc = pItem->GetText();
  116. }
  117. else if (_tcsicmp(pItem->Value(), _T("KeyVolt")) == 0) {
  118. keyItem.keyVolt = _tstof(pItem->GetText());
  119. }
  120. else if (_tcsicmp(pItem->Value(), _T("KeyIndex")) == 0) {
  121. keyItem.keyIndex = _tstoi(pItem->GetText());
  122. }
  123. pItem = pItem->NextSiblingElement();
  124. }
  125. }
  126. // 如果已经添加过的;
  127. KeyItem *pkeyItem = FindItem(keyItem.keyName.c_str());
  128. if ( pkeyItem ) {
  129. pkeyItem->keyDesc = keyItem.keyDesc;
  130. pkeyItem->keyVolt = keyItem.keyVolt;
  131. pkeyItem->keyIndex = keyItem.keyIndex;
  132. }
  133. else
  134. g_vtKeyItem.push_back(keyItem);
  135. pXmlElent = pXmlElent->NextSiblingElement();
  136. }
  137. }
  138. }
  139. return 0;
  140. }
  141. //////////////////////////////////////////////////////////////////////////
  142. // API函数接口;
  143. DACONTROL_API KeyItem* FindItem(LPCTSTR lpKeyName)
  144. {
  145. if ( !lpKeyName || lpKeyName[0] == '\0' )
  146. return NULL;
  147. for ( std::vector<KeyItem>::iterator it = g_vtKeyItem.begin(); it != g_vtKeyItem.end(); it++ ) {
  148. if ( _tcsicmp(it->keyName.c_str(), lpKeyName) == 0 ) {
  149. return &*it;
  150. break;
  151. }
  152. }
  153. return NULL;
  154. }
  155. DACONTROL_API int OpenXML(LPCTSTR lpXMLPath)
  156. {
  157. g_vtKeyItem.clear();
  158. return __LoadXML(lpXMLPath);
  159. }
  160. DACONTROL_API int LoadXML(LPCTSTR lpXMLPath)
  161. {
  162. return __LoadXML(lpXMLPath);
  163. }
  164. // 打开串口;
  165. DACONTROL_API BOOL OpenDevice(int nPort, DWORD dwBaudrate, BYTE ByteSize, BYTE Parity, BYTE StopBits)
  166. {
  167. // 关闭打开的;
  168. if ( g_synSerial.IsOpen() )
  169. g_synSerial.CloseSerialPort();
  170. g_synSerial.OpenSerialPort(nPort, dwBaudrate, ByteSize, Parity, StopBits, 0, 1000);
  171. return g_synSerial.IsOpen();
  172. }
  173. // 关闭串口;
  174. DACONTROL_API void CloseDevice()
  175. {
  176. if ( g_synSerial.IsOpen() )
  177. g_synSerial.CloseSerialPort();
  178. }
  179. DACONTROL_API bool SendKey(LPCTSTR lpKeyName)
  180. {
  181. if ( !g_synSerial.IsOpen() )
  182. return false;
  183. KeyItem *pkeyItem = FindItem(lpKeyName);
  184. if ( pkeyItem == NULL )
  185. return false;
  186. // 获取个位数和小数位;
  187. int a = 0, b = 0; // 个位数,小数位;
  188. TCHAR szVolt[16] = {0};
  189. _gcvt_s(szVolt, pkeyItem->keyVolt, 3);
  190. sscanf_s(szVolt, _T("%d.%d"), &a, &b);
  191. // 封装发送包[0x5A, <通道号>, <电压个位数>, <电压小数位>, <A5>];
  192. // 串口返回:原样返回数据;
  193. byte szSendData[SENDLEN] = {0};
  194. _stprintf_s((TCHAR*)szSendData, SENDLEN, _T("%c%c%c%c%c"), 0x5A, pkeyItem->keyIndex, a, b, 0xA5);
  195. DWORD dwNumberOfBytesWritten = g_synSerial.WriteComm(szSendData, SENDLEN - 1);
  196. if ( dwNumberOfBytesWritten != (SENDLEN - 1) )
  197. return false;
  198. DWORD dwNumberOfBytesRead = g_synSerial.ReadComm(szSendData, dwNumberOfBytesWritten);
  199. return dwNumberOfBytesRead ? true : false;
  200. }
  201. #ifdef __MAKE_PYD__
  202. //////////////////////////////////////////////////////////////////////////
  203. // PYD接口;
  204. static PyObject* OpenXML(PyObject* self, PyObject* args)
  205. {
  206. // 电压表程序路径;
  207. const char* pszXmlPath = NULL;
  208. if (!PyArg_ParseTuple(args, "s", &pszXmlPath))
  209. return NULL;
  210. // 加载成功返回0;
  211. return Py_BuildValue("i", ::OpenXML(pszXmlPath));
  212. }
  213. static PyObject* LoadXML(PyObject* self, PyObject* args)
  214. {
  215. // 电压表程序路径;
  216. const char* pszXmlPath = NULL;
  217. if (!PyArg_ParseTuple(args, "s", &pszXmlPath))
  218. return NULL;
  219. // 加载成功返回0;
  220. return Py_BuildValue("i", ::LoadXML(pszXmlPath));
  221. }
  222. static PyObject* OpenDevice(PyObject* self, PyObject* args)
  223. {
  224. int nPort;
  225. if (!PyArg_ParseTuple(args, "i", &nPort))
  226. return NULL;
  227. return Py_BuildValue("b", ::OpenDevice(nPort, 9600, 8, 0, 1)); // 返回None;
  228. }
  229. //描述:连接设备,默认连接索引为0的设备;
  230. static PyObject* CloseDevice(PyObject* self, PyObject* args)
  231. {
  232. ::CloseDevice();
  233. return Py_BuildValue("");
  234. }
  235. static PyObject* SendKey(PyObject* self, PyObject* args)
  236. {
  237. const char* pszKenName = NULL;
  238. if (!PyArg_ParseTuple(args, "s", &pszKenName))
  239. return NULL;
  240. return Py_BuildValue("b", ::SendKey(pszKenName));
  241. }
  242. // 描述方法,暴露给python的函数;
  243. static PyMethodDef DAControl_Methods[] = {
  244. {"OpenXML",OpenXML,METH_VARARGS,"打开电压表文件(清空之前的)"},
  245. {"LoadXML", LoadXML, METH_VARARGS, "打开电压表文件(不清空,累加)"},
  246. {"OpenDevice", OpenDevice, METH_VARARGS, "打开设备"},
  247. {"CloseDevice", CloseDevice, METH_VARARGS, "关闭设备"},
  248. {"SendKey", SendKey, METH_VARARGS, "发送物理电压"},
  249. {NULL,NULL}
  250. };
  251. // 初始模块;//格式:init<模块名称>
  252. PyMODINIT_FUNC initDAControl()
  253. {
  254. // 初始化pyd函数列表;
  255. PyObject* m, * d;
  256. m = Py_InitModule("DAControl", DAControl_Methods);
  257. d = PyModule_GetDict(m);
  258. }
  259. #endif