PowerwareProcess.cpp 13 KB

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