UpsParadigmProcess.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. default:
  155. LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
  156. }
  157. nValue = atoi(strTemp);
  158. }
  159. else if( nDataLen == 2 )
  160. {
  161. char chBuffer[16] = {0};
  162. memset(chBuffer, 0, sizeof(chBuffer));
  163. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  164. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  165. itoa(nValue, chBuffer, 2);
  166. CString strTemp;
  167. switch( nEndBit - nStartBit )
  168. {
  169. case 0:
  170. strTemp.Format("%c", chBuffer[nStartBit]);
  171. break;
  172. case 1:
  173. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  174. break;
  175. case 2:
  176. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  177. break;
  178. case 3:
  179. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  180. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  181. break;
  182. case 4:
  183. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  184. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  185. break;
  186. case 5:
  187. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  188. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  189. break;
  190. case 6:
  191. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  192. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  193. break;
  194. default:
  195. LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
  196. }
  197. nValue = atoi(strTemp);
  198. }
  199. }
  200. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  201. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  202. {
  203. nStartReg = (int)nStartBit / 8;
  204. nEndReg = (int)(nEndBit + 1) / 8;
  205. nValue = (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF );
  206. }
  207. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  208. {
  209. nStartReg = (int)nStartBit / 8;
  210. nEndReg = (int)(nEndBit + 1) / 8;
  211. memcpy(&wdValue, chMsg, sizeof(WORD));
  212. nValue = ( ( (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF ) ) << 8 ) |
  213. ( ( (AsciiToBYTE(chMsg[nStartReg + 2]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 3]) & 0x00FF ) ) & 0x0000FFFF );
  214. }
  215. EnterCriticalSection( &g_csParadigmReadOneData );
  216. pBaseVar->m_dbData = (double)(nValue * fCoef);
  217. if( pBaseVar->m_nRearm != 0 )
  218. {
  219. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  220. }
  221. LeaveCriticalSection( &g_csParadigmReadOneData );
  222. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  223. }
  224. else
  225. {
  226. // 联合类型变量做特殊处理
  227. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  228. {
  229. if( nDataLen == 2 )
  230. {
  231. //int类型
  232. union __UNION_VAR_INT{
  233. char ch[2];
  234. int value;
  235. }unionVarInt;
  236. unionVarInt.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  237. unionVarInt.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  238. EnterCriticalSection( &g_csParadigmReadOneData );
  239. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  240. if( pBaseVar->m_nRearm != 0 )
  241. {
  242. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  243. }
  244. LeaveCriticalSection( &g_csParadigmReadOneData );
  245. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  246. }
  247. else if( nDataLen == 4 )
  248. {
  249. //float类型
  250. union __UNION_VAR_FLOAT{
  251. char ch[4];
  252. float value;
  253. }unionVarFloat;
  254. unionVarFloat.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  255. unionVarFloat.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  256. unionVarFloat.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  257. unionVarFloat.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  258. EnterCriticalSection( &g_csParadigmReadOneData );
  259. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  260. if( pBaseVar->m_nRearm != 0 )
  261. {
  262. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  263. }
  264. LeaveCriticalSection( &g_csParadigmReadOneData );
  265. //TRACE("%s\r\n", chMsg);
  266. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  267. }
  268. else if( nDataLen == 8 )
  269. {
  270. //double类型
  271. union __UNION_VAR_DOUBLE{
  272. char ch[8];
  273. double value;
  274. }unionVarDouble;
  275. unionVarDouble.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  276. unionVarDouble.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  277. unionVarDouble.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  278. unionVarDouble.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  279. unionVarDouble.ch[4] = (AsciiToBYTE(chMsg[8]) << 4) | (AsciiToBYTE(chMsg[9]) & 0x00FF);
  280. unionVarDouble.ch[5] = (AsciiToBYTE(chMsg[10]) << 4) | (AsciiToBYTE(chMsg[11]) & 0x00FF);
  281. unionVarDouble.ch[6] = (AsciiToBYTE(chMsg[12]) << 4) | (AsciiToBYTE(chMsg[13]) & 0x00FF);
  282. unionVarDouble.ch[7] = (AsciiToBYTE(chMsg[14]) << 4) | (AsciiToBYTE(chMsg[15]) & 0x00FF);
  283. EnterCriticalSection( &g_csParadigmReadOneData );
  284. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  285. if( pBaseVar->m_nRearm != 0 )
  286. {
  287. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  288. }
  289. LeaveCriticalSection( &g_csParadigmReadOneData );
  290. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  291. }
  292. }
  293. else
  294. {
  295. switch( nDataLen )
  296. {
  297. case 1:
  298. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  299. break;
  300. case 2:
  301. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  302. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  303. break;
  304. case 3: // 一般为字符串类型,不作处理
  305. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  306. break;
  307. case 4: // 保留,暂时不知道什么处理
  308. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  309. break;
  310. case 5: // 一般为字符串类型,不作处理
  311. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  312. break;
  313. case 6: // 一般为字符串类型,不作处理
  314. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  315. break;
  316. case 7: // 一般为字符串类型,不作处理
  317. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  318. break;
  319. case 8: // 保留,暂时没有用到
  320. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  321. break;
  322. }
  323. EnterCriticalSection( &g_csParadigmReadOneData );
  324. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  325. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  326. {
  327. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  328. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  329. pBaseVar->m_dbData = nValue *
  330. fCoef *
  331. fScale;
  332. }
  333. else
  334. {
  335. pBaseVar->m_dbData = (double)(nValue * fCoef);
  336. }
  337. if( pBaseVar->m_nRearm != 0 )
  338. {
  339. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  340. }
  341. //g_RtuReadOneData.dbData = (double)(nValue * fCoef);
  342. LeaveCriticalSection( &g_csParadigmReadOneData );
  343. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, pBaseVar->m_dbData);
  344. }
  345. }
  346. EnterCriticalSection(&g_csParadigmReadOneData);
  347. pDev->m_dwOnlineTick = GetTickCount();
  348. LeaveCriticalSection(&g_csParadigmReadOneData);
  349. return 0;
  350. }
  351. void UnInitParadigmDll()
  352. {
  353. if (g_hPARADIGMLibModule != NULL && ( NULL != pPARADIGM_DLLUnInit ))
  354. {
  355. pPARADIGM_DLLUnInit();
  356. }
  357. DeleteCriticalSection( &g_csParadigmReadOneData );
  358. if (g_hPARADIGMLibModule!= NULL)
  359. {
  360. AfxFreeLibrary(g_hPARADIGMLibModule);
  361. g_hPARADIGMLibModule = NULL;
  362. }
  363. }
  364. int GetParadigmFromIni(char chCmd[32], int &nAddr,int &nVer, int &nCid2,
  365. char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID], char chDataLen[MAX_ID])
  366. {
  367. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  368. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  369. CHAR strCid2[MAX_ID] ="";
  370. char strAdr[MAX_ID]="";
  371. CHAR strVer[MAX_ID]={0};
  372. CHAR strParaSum[MAX_ID] = {0};
  373. CHAR strParaIndex[MAX_ID] = {0};
  374. wsprintf(strFile, "%s\\config\\upsparadigm.ini", g_strDirectory);
  375. //模块版本
  376. GetPrivateProfileString("VER", "Ver", "", strVer, sizeof(strVer), strFile);
  377. //命令模块
  378. GetPrivateProfileString(chCmd,"cmdModule", "", strValue, sizeof(strValue), strFile);
  379. //命令ID的参数索引
  380. GetPrivateProfileString(chCmd, "Para_Index", "", strParaIndex, sizeof(strParaIndex), strFile);
  381. //命令长度
  382. GetPrivateProfileString(chCmd, "cmdlen", "", chCmdLen, sizeof(chCmdLen), strFile);
  383. //命令的CID2
  384. GetPrivateProfileString(strValue, "Cid2", "", strCid2, sizeof(strCid2), strFile);
  385. //模块设备地址
  386. GetPrivateProfileString(strValue, "Addr", "", strAdr, sizeof(strAdr), strFile);
  387. //模块命令参数的数量
  388. GetPrivateProfileString(strValue, "ParaSum", "", strParaSum, sizeof(strParaSum), strFile);
  389. //数据总长度
  390. GetPrivateProfileString(strValue, "datalen", "", chDataLen, sizeof(chDataLen), strFile);
  391. //读:起始位置,写:设置命令
  392. char ParaIndex[MAX_ID]={0};
  393. strcpy(ParaIndex, "para");
  394. strcat(ParaIndex, strParaIndex);
  395. GetPrivateProfileString(strValue, ParaIndex, "", chAdrBeginPos, sizeof(chAdrBeginPos), strFile);
  396. nCid2 = atoi(strCid2);
  397. nAddr = atoi(strAdr);
  398. nVer = atoi(strVer);
  399. return 0;
  400. }