PowerwareProcess.cpp 13 KB

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