UpsParadigmProcess.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "stdafx.h"
  2. #include "UpsParadigmProcess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "NoticeQueue.h"
  6. HANDLE g_PARADIGMRequestThread = NULL;
  7. HANDLE g_PARADIGMResponseThread = NULL;
  8. CRITICAL_SECTION g_csParadigmReadOneData;
  9. //UPS PARADIGM动态库输出函数
  10. HINSTANCE g_hPARADIGMLibModule;
  11. PARADIGM_DLLInitCom pPARADIGM_DLLInitCom;
  12. PARADIGM_DLLInit pPARADIGM_DLLInit;
  13. PARADIGM_DLLUnInit pPARADIGM_DLLUnInit;
  14. int ParadigmCommandSend();
  15. void UnInitParadigmDll();
  16. int GetParadigmFromIni(char chCmd[32], int &nAddr,int &nVer, int &nCid2,
  17. char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID], char chDataLen[MAX_ID]);
  18. BOOL LoadParadigmDll(CString strpath)
  19. {
  20. char strFile[256] = {0};
  21. g_hPARADIGMLibModule = NULL;
  22. sprintf(strFile, "%s\\dll\\upsparadigm.dll", strpath);
  23. InitializeCriticalSection( &g_csParadigmReadOneData );
  24. g_hPARADIGMLibModule = AfxLoadLibrary(strFile);
  25. //UPSPARADIGM 动态库初始化
  26. if (NULL != g_hPARADIGMLibModule)
  27. {
  28. pPARADIGM_DLLInitCom =( PARADIGM_DLLInitCom)::GetProcAddress(g_hPARADIGMLibModule, "PARADIGM_DLLInitCom");
  29. pPARADIGM_DLLInit =(PARADIGM_DLLInit)::GetProcAddress(g_hPARADIGMLibModule, "PARADIGM_DLLInit");
  30. pPARADIGM_DLLUnInit =(PARADIGM_DLLUnInit)::GetProcAddress(g_hPARADIGMLibModule, "PARADIGM_DLLUnInit");
  31. return TRUE;
  32. }
  33. else
  34. {
  35. return FALSE;
  36. }
  37. }
  38. BOOL InitParadigmComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  39. {
  40. if (pPARADIGM_DLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  41. return TRUE;
  42. else
  43. return FALSE;
  44. }
  45. int ParadigmRequestData(
  46. int nPort,
  47. int nDevAddr,
  48. char chDevUid[20],
  49. int iVarID,
  50. char chRs232cmd[32])
  51. {
  52. char chMsg[80] = {0};
  53. //BYTE byAdr = 0x00;//起始位
  54. //BYTE byVer = 0x00;
  55. //BYTE byCid2 = 0x00;
  56. //BYTE byCid1 = 0xAA;//控制标识码
  57. WORD wLenId = 0x00;//INFO字节长度
  58. BYTE byCmdType = 0x00;//命令类型
  59. //BYTE byCmdId = 0x00;
  60. int nAddr = 0x0;
  61. int nVer = 0x0;
  62. int nCid1 = 0x2A;
  63. int nCid2 = 0x0;
  64. int nCmdID = 0x0;
  65. char byDataFlag = 0;
  66. char chAdrBeginPos[MAX_ID] = {0};
  67. char chCmdLen[MAX_ID] = {0};
  68. char chDataLen[MAX_ID] = {0};
  69. GetParadigmFromIni((char*)(LPCTSTR)chRs232cmd,
  70. nAddr,
  71. nVer,
  72. nCid2,
  73. chAdrBeginPos,
  74. chCmdLen,
  75. chDataLen);
  76. //TRACE("起始索引=%d, 变量长度=%d,数据长度=%d\r\n", atoi(chAdrBeginPos), atoi(chCmdLen), atoi(chDataLen));
  77. int nRet = pPARADIGM_DLLInit( nPort,
  78. nDevAddr,
  79. nAddr, //设备地址描述
  80. nVer,
  81. nCid1, //控制标识码
  82. nCid2, //命令信息
  83. wLenId, //INFO字节长度
  84. byCmdType, //命令类型
  85. nCmdID,
  86. atoi(chDataLen),
  87. //48,
  88. atoi(chAdrBeginPos),
  89. atoi(chCmdLen),
  90. chMsg,
  91. &byDataFlag);
  92. if(nRet==0)
  93. {
  94. nRet = ParadigmSingleResponseData(chDevUid, iVarID, chMsg, byDataFlag, nCid2 );
  95. }
  96. return nRet;
  97. }
  98. int ParadigmSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80], char byDataFlag, BYTE byCid2)
  99. {
  100. int nDeviceIndex = -1, nVarIndex = -1;
  101. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  102. if( bFind == FALSE ) return -1;
  103. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  104. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  105. EnterCriticalSection(&g_csParadigmReadOneData);
  106. pDev->m_dwOnlineTick = GetTickCount();
  107. LeaveCriticalSection(&g_csParadigmReadOneData);
  108. DWORD nValue = 0;
  109. WORD wdValue = 0;
  110. double fCoef = pBaseVar->m_dbCoefficient;
  111. int nDataLen = pBaseVar->m_iDataLen;
  112. int nVarItemID = pBaseVar->m_nVarItemID;
  113. if( nVarItemID > 0 )//BIT位变量
  114. {
  115. int nStartBit = pBaseVar->m_nStartBit;
  116. int nEndBit = pBaseVar->m_nEndBit;
  117. int nStartReg, nEndReg;
  118. if( nEndBit - nStartBit < 7 )// 一般告警状态会这样子定义,Modbus Rtu 码暂时没有碰到,没有经过调试,待测试
  119. {
  120. if( nDataLen == 1 )
  121. {
  122. char chBuffer[8] = {0};
  123. memset(chBuffer, 0, sizeof(chBuffer));
  124. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[0 + 1]) & 0x00FF );
  125. itoa(nValue, chBuffer, 2);
  126. CString strTemp;
  127. switch( nEndBit - nStartBit )
  128. {
  129. case 0:
  130. strTemp.Format("%c", chBuffer[nStartBit]);
  131. break;
  132. case 1:
  133. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  134. break;
  135. case 2:
  136. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  137. break;
  138. case 3:
  139. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  140. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  141. break;
  142. case 4:
  143. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  144. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  145. break;
  146. case 5:
  147. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  148. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  149. break;
  150. case 6:
  151. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  152. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  153. break;
  154. }
  155. nValue = atoi(strTemp);
  156. }
  157. else if( nDataLen == 2 )
  158. {
  159. char chBuffer[16] = {0};
  160. memset(chBuffer, 0, sizeof(chBuffer));
  161. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  162. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  163. itoa(nValue, chBuffer, 2);
  164. CString strTemp;
  165. switch( nEndBit - nStartBit )
  166. {
  167. case 0:
  168. strTemp.Format("%c", chBuffer[nStartBit]);
  169. break;
  170. case 1:
  171. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  172. break;
  173. case 2:
  174. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  175. break;
  176. case 3:
  177. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  178. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  179. break;
  180. case 4:
  181. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  182. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  183. break;
  184. case 5:
  185. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  186. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  187. break;
  188. case 6:
  189. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  190. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  191. break;
  192. }
  193. nValue = atoi(strTemp);
  194. }
  195. }
  196. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  197. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  198. {
  199. nStartReg = (int)nStartBit / 8;
  200. nEndReg = (int)(nEndBit + 1) / 8;
  201. nValue = (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF );
  202. }
  203. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  204. {
  205. nStartReg = (int)nStartBit / 8;
  206. nEndReg = (int)(nEndBit + 1) / 8;
  207. memcpy(&wdValue, chMsg, sizeof(WORD));
  208. nValue = ( ( (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF ) ) << 8 ) |
  209. ( ( (AsciiToBYTE(chMsg[nStartReg + 2]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 3]) & 0x00FF ) ) & 0x0000FFFF );
  210. }
  211. EnterCriticalSection( &g_csParadigmReadOneData );
  212. pBaseVar->m_dbData = (double)(nValue * fCoef);
  213. if( pBaseVar->m_nRearm != 0 )
  214. {
  215. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  216. }
  217. LeaveCriticalSection( &g_csParadigmReadOneData );
  218. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  219. }
  220. else
  221. {
  222. // 联合类型变量做特殊处理
  223. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  224. {
  225. if( nDataLen == 2 )
  226. {
  227. //int类型
  228. union __UNION_VAR_INT{
  229. char ch[2];
  230. int value;
  231. }unionVarInt;
  232. unionVarInt.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  233. unionVarInt.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  234. EnterCriticalSection( &g_csParadigmReadOneData );
  235. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  236. if( pBaseVar->m_nRearm != 0 )
  237. {
  238. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  239. }
  240. LeaveCriticalSection( &g_csParadigmReadOneData );
  241. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  242. }
  243. else if( nDataLen == 4 )
  244. {
  245. //float类型
  246. union __UNION_VAR_FLOAT{
  247. char ch[4];
  248. float value;
  249. }unionVarFloat;
  250. unionVarFloat.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  251. unionVarFloat.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  252. unionVarFloat.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  253. unionVarFloat.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  254. EnterCriticalSection( &g_csParadigmReadOneData );
  255. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  256. if( pBaseVar->m_nRearm != 0 )
  257. {
  258. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  259. }
  260. LeaveCriticalSection( &g_csParadigmReadOneData );
  261. //TRACE("%s\r\n", chMsg);
  262. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  263. }
  264. else if( nDataLen == 8 )
  265. {
  266. //double类型
  267. union __UNION_VAR_DOUBLE{
  268. char ch[8];
  269. double value;
  270. }unionVarDouble;
  271. unionVarDouble.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  272. unionVarDouble.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  273. unionVarDouble.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  274. unionVarDouble.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  275. unionVarDouble.ch[4] = (AsciiToBYTE(chMsg[8]) << 4) | (AsciiToBYTE(chMsg[9]) & 0x00FF);
  276. unionVarDouble.ch[5] = (AsciiToBYTE(chMsg[10]) << 4) | (AsciiToBYTE(chMsg[11]) & 0x00FF);
  277. unionVarDouble.ch[6] = (AsciiToBYTE(chMsg[12]) << 4) | (AsciiToBYTE(chMsg[13]) & 0x00FF);
  278. unionVarDouble.ch[7] = (AsciiToBYTE(chMsg[14]) << 4) | (AsciiToBYTE(chMsg[15]) & 0x00FF);
  279. EnterCriticalSection( &g_csParadigmReadOneData );
  280. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  281. if( pBaseVar->m_nRearm != 0 )
  282. {
  283. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  284. }
  285. LeaveCriticalSection( &g_csParadigmReadOneData );
  286. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  287. }
  288. }
  289. else
  290. {
  291. switch( nDataLen )
  292. {
  293. case 1:
  294. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  295. break;
  296. case 2:
  297. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  298. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  299. break;
  300. case 3: // 一般为字符串类型,不作处理
  301. break;
  302. case 4: // 保留,暂时不知道什么处理
  303. break;
  304. case 5: // 一般为字符串类型,不作处理
  305. break;
  306. case 6: // 一般为字符串类型,不作处理
  307. break;
  308. case 7: // 一般为字符串类型,不作处理
  309. break;
  310. case 8: // 保留,暂时没有用到
  311. break;
  312. }
  313. EnterCriticalSection( &g_csParadigmReadOneData );
  314. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  315. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  316. {
  317. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  318. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  319. pBaseVar->m_dbData = nValue *
  320. fCoef *
  321. fScale;
  322. }
  323. else
  324. {
  325. pBaseVar->m_dbData = (double)(nValue * fCoef);
  326. }
  327. if( pBaseVar->m_nRearm != 0 )
  328. {
  329. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  330. }
  331. //g_RtuReadOneData.dbData = (double)(nValue * fCoef);
  332. LeaveCriticalSection( &g_csParadigmReadOneData );
  333. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, pBaseVar->m_dbData);
  334. }
  335. }
  336. EnterCriticalSection(&g_csParadigmReadOneData);
  337. pDev->m_dwOnlineTick = GetTickCount();
  338. LeaveCriticalSection(&g_csParadigmReadOneData);
  339. return 0;
  340. }
  341. void UnInitParadigmDll()
  342. {
  343. if (g_hPARADIGMLibModule != NULL && ( NULL != pPARADIGM_DLLUnInit ))
  344. {
  345. pPARADIGM_DLLUnInit();
  346. }
  347. DeleteCriticalSection( &g_csParadigmReadOneData );
  348. if (g_hPARADIGMLibModule!= NULL)
  349. {
  350. AfxFreeLibrary(g_hPARADIGMLibModule);
  351. g_hPARADIGMLibModule = NULL;
  352. }
  353. }
  354. int GetParadigmFromIni(char chCmd[32], int &nAddr,int &nVer, int &nCid2,
  355. char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID], char chDataLen[MAX_ID])
  356. {
  357. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  358. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  359. CHAR strCid2[MAX_ID] ="";
  360. char strAdr[MAX_ID]="";
  361. CHAR strVer[MAX_ID]={0};
  362. CHAR strParaSum[MAX_ID] = {0};
  363. CHAR strParaIndex[MAX_ID] = {0};
  364. wsprintf(strFile, "%s\\config\\upsparadigm.ini", g_strDirectory);
  365. //模块版本
  366. GetPrivateProfileString("VER", "Ver", "", strVer, sizeof(strVer), strFile);
  367. //命令模块
  368. GetPrivateProfileString(chCmd,"cmdModule", "", strValue, sizeof(strValue), strFile);
  369. //命令ID的参数索引
  370. GetPrivateProfileString(chCmd, "Para_Index", "", strParaIndex, sizeof(strParaIndex), strFile);
  371. //命令长度
  372. GetPrivateProfileString(chCmd, "cmdlen", "", chCmdLen, sizeof(chCmdLen), strFile);
  373. //命令的CID2
  374. GetPrivateProfileString(strValue, "Cid2", "", strCid2, sizeof(strCid2), strFile);
  375. //模块设备地址
  376. GetPrivateProfileString(strValue, "Addr", "", strAdr, sizeof(strAdr), strFile);
  377. //模块命令参数的数量
  378. GetPrivateProfileString(strValue, "ParaSum", "", strParaSum, sizeof(strParaSum), strFile);
  379. //数据总长度
  380. GetPrivateProfileString(strValue, "datalen", "", chDataLen, sizeof(chDataLen), strFile);
  381. //读:起始位置,写:设置命令
  382. char ParaIndex[MAX_ID]={0};
  383. strcpy(ParaIndex, "para");
  384. strcat(ParaIndex, strParaIndex);
  385. GetPrivateProfileString(strValue, ParaIndex, "", chAdrBeginPos, sizeof(chAdrBeginPos), strFile);
  386. nCid2 = atoi(strCid2);
  387. nAddr = atoi(strAdr);
  388. nVer = atoi(strVer);
  389. return 0;
  390. }