123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- // DAControl.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include "stdafx.h"
- #include "DAControl.h"
- #include "SynSerial.h"
- #ifdef __MAKE_PYD__
- #include "Python.h"
- #endif
- #define SENDLEN 6
- bool g_bEnableLoad = false;
- std::string g_strXMLPath;
- std::vector<KeyItem> g_vtKeyItem;
- CSynSerial g_synSerial;
- // 最大电压值;
- float g_fMaxVoltage = 0.0;
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- #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
- {
- // TODO: 在此处为应用程序的行为编写代码。
- // 获取个位数和小数位;
- int a = 0, b = 0; // 个位数,小数位;
- TCHAR szVolt[16] = {0};
- _gcvt_s(szVolt, 1.256, 3);
- sscanf_s(szVolt, _T("%d.%d"), &a, &b);
- byte szSendData[SENDLEN] = {0};
- _stprintf_s((TCHAR*)szSendData, SENDLEN, _T("%c%c%c%c%c"), 0x5A, 0, a, b, 0xA5);
- int len = sizeof(szSendData);
- printf("%s, %d,%d\n", szSendData, a, b);
- }
- system("pause");
- return nRetCode;
- }
- #endif
- 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) )
- return -1;
- g_strXMLPath = lpXMLPath;
- // 解析xml;
- tinyxml2::XMLDocument doc;
- if (tinyxml2::XML_SUCCESS != doc.LoadFile(lpXMLPath))
- return -1;
- tinyxml2::XMLElement *pXmlRoot = NULL;
- if ((pXmlRoot = doc.RootElement()) != NULL) {
- if (_tcsicmp(pXmlRoot->Value(), "KeyList") == 0) {
- // 属性;
- const tinyxml2::XMLAttribute *pAttr = pXmlRoot->FirstAttribute();
- if (pAttr) {
- if (_tcsicmp(pAttr->Name(), "MaxVolt") == 0) {
- // 读出最大电压值;
- const char* pVal = pAttr->Value();
- if ( pVal ) {
- g_fMaxVoltage = _tstof(pVal);
- }
- }
- }
-
- // 没有限制电压值;
- if ( g_fMaxVoltage == 0.0 )
- return -1;
- // 子项;
- 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("KeyVolt")) == 0) {
- keyItem.keyVolt = _tstof(pItem->GetText());
- }
- else if (_tcsicmp(pItem->Value(), _T("KeyIndex")) == 0) {
- keyItem.keyIndex = _tstoi(pItem->GetText());
- }
- pItem = pItem->NextSiblingElement();
- }
- }
- // 如果已经添加过的;
- KeyItem *pkeyItem = FindItem(keyItem.keyName.c_str());
- if ( pkeyItem ) {
- pkeyItem->keyDesc = keyItem.keyDesc;
- pkeyItem->keyVolt = keyItem.keyVolt;
- pkeyItem->keyIndex = keyItem.keyIndex;
- }
- else
- g_vtKeyItem.push_back(keyItem);
- pXmlElent = pXmlElent->NextSiblingElement();
- }
- }
- }
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////
- // API函数接口;
- DACONTROL_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;
- }
- DACONTROL_API int OpenXML(LPCTSTR lpXMLPath)
- {
- g_vtKeyItem.clear();
- return __LoadXML(lpXMLPath);
- }
- DACONTROL_API int LoadXML(LPCTSTR lpXMLPath)
- {
- return __LoadXML(lpXMLPath);
- }
- // 打开串口;
- DACONTROL_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();
- }
- // 关闭串口;
- DACONTROL_API void CloseDevice()
- {
- if ( g_synSerial.IsOpen() )
- g_synSerial.CloseSerialPort();
- }
- DACONTROL_API bool SendKey(LPCTSTR lpKeyName)
- {
- if ( !g_synSerial.IsOpen() )
- return false;
- KeyItem *pkeyItem = FindItem(lpKeyName);
- if ( pkeyItem == NULL )
- return false;
- // 获取个位数和小数位;
- int a = 0, b = 0; // 个位数,小数位;
- TCHAR szVolt[16] = {0};
- _gcvt_s(szVolt, pkeyItem->keyVolt, 3);
- sscanf_s(szVolt, _T("%d.%d"), &a, &b);
- // 封装发送包[0x5A, <通道号>, <电压个位数>, <电压小数位>, <A5>];
- // 串口返回:原样返回数据;
- byte szSendData[SENDLEN] = {0};
- _stprintf_s((TCHAR*)szSendData, SENDLEN, _T("%c%c%c%c%c"), 0x5A, pkeyItem->keyIndex, a, b, 0xA5);
- DWORD dwNumberOfBytesWritten = g_synSerial.WriteComm(szSendData, SENDLEN - 1);
- if ( dwNumberOfBytesWritten != (SENDLEN - 1) )
- return false;
- DWORD dwNumberOfBytesRead = g_synSerial.ReadComm(szSendData, dwNumberOfBytesWritten);
- return dwNumberOfBytesRead ? true : false;
- }
- #ifdef __MAKE_PYD__
- //////////////////////////////////////////////////////////////////////////
- // PYD接口;
- static PyObject* OpenXML(PyObject* self, PyObject* args)
- {
- // 电压表程序路径;
- const char* pszXmlPath = NULL;
- if (!PyArg_ParseTuple(args, "s", &pszXmlPath))
- return NULL;
- // 加载成功返回0;
- return Py_BuildValue("i", ::OpenXML(pszXmlPath));
- }
- static PyObject* LoadXML(PyObject* self, PyObject* args)
- {
- // 电压表程序路径;
- const char* pszXmlPath = NULL;
- if (!PyArg_ParseTuple(args, "s", &pszXmlPath))
- return NULL;
- // 加载成功返回0;
- return Py_BuildValue("i", ::LoadXML(pszXmlPath));
- }
- static PyObject* OpenDevice(PyObject* self, PyObject* args)
- {
- int nPort;
- if (!PyArg_ParseTuple(args, "i", &nPort))
- return NULL;
- return Py_BuildValue("b", ::OpenDevice(nPort, 9600, 8, 0, 1)); // 返回None;
- }
- //描述:连接设备,默认连接索引为0的设备;
- static PyObject* CloseDevice(PyObject* self, PyObject* args)
- {
- ::CloseDevice();
- return Py_BuildValue("");
- }
- static PyObject* SendKey(PyObject* self, PyObject* args)
- {
- const char* pszKenName = NULL;
- if (!PyArg_ParseTuple(args, "s", &pszKenName))
- return NULL;
- return Py_BuildValue("b", ::SendKey(pszKenName));
- }
- // 描述方法,暴露给python的函数;
- static PyMethodDef DAControl_Methods[] = {
- {"OpenXML",OpenXML,METH_VARARGS,"打开电压表文件(清空之前的)"},
- {"LoadXML", LoadXML, METH_VARARGS, "打开电压表文件(不清空,累加)"},
- {"OpenDevice", OpenDevice, METH_VARARGS, "打开设备"},
- {"CloseDevice", CloseDevice, METH_VARARGS, "关闭设备"},
- {"SendKey", SendKey, METH_VARARGS, "发送物理电压"},
- {NULL,NULL}
- };
- // 初始模块;//格式:init<模块名称>
- PyMODINIT_FUNC initDAControl()
- {
- // 初始化pyd函数列表;
- PyObject* m, * d;
- m = Py_InitModule("DAControl", DAControl_Methods);
- d = PyModule_GetDict(m);
- }
- #endif
|