CarelProcess.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "stdafx.h"
  2. #include "CarelProcess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "NoticeQueue.h"
  6. HINSTANCE g_hCarelLibModule = NULL;
  7. CAREL_DLLInit pCarel_DLLInit = NULL;
  8. CAREL_DLLUnInit pCarel_DLLUnInit = NULL;
  9. CAREL_DLLRequestData pCarelRequestData = NULL;
  10. CAREL_DLLControl pCarelControl = NULL;
  11. CRITICAL_SECTION g_csCarelReadOneData;
  12. char g_szCarelMsg[CAREL_MAX_MSG] = {0};
  13. int CarelCommandSend();
  14. void UnInitCarekDll();
  15. int GetCarelFromIni( char chCmd[32], char szType[MAX_ID], char szIndex[MAX_ID] );
  16. BOOL LoadCarelDll(CString strpath)
  17. {
  18. char strFile[256] = {0};
  19. g_hCarelLibModule = NULL;
  20. sprintf(strFile, "%s\\dll\\Carel.dll", strpath);
  21. InitializeCriticalSection( &g_csCarelReadOneData );
  22. g_hCarelLibModule = AfxLoadLibrary(strFile);
  23. //大金空调动态库初始化
  24. if (NULL != g_hCarelLibModule)
  25. {
  26. pCarel_DLLInit =(CAREL_DLLInit)::GetProcAddress(g_hCarelLibModule, "CAREL_DLLInit");
  27. pCarel_DLLUnInit =(CAREL_DLLUnInit)::GetProcAddress(g_hCarelLibModule, "CAREL_DLLUnInit");
  28. pCarelRequestData = (CAREL_DLLRequestData)::GetProcAddress(g_hCarelLibModule, "CAREL_DLLRequestData");
  29. pCarelControl = (CAREL_DLLControl)::GetProcAddress(g_hCarelLibModule, "CAREL_DLLControl");
  30. return TRUE;
  31. }
  32. else
  33. {
  34. return FALSE;
  35. }
  36. }
  37. BOOL InitCarelComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  38. {
  39. if (pCarel_DLLInit(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  40. return TRUE;
  41. else
  42. return FALSE;
  43. }
  44. int CarelRequestData(
  45. int nPort,
  46. int nDevAddr,
  47. char chDevUid[20],
  48. int iVarID,
  49. char chRs232cmd[32])
  50. {
  51. char chMsg[80] = {0};
  52. char szType[MAX_ID] = {0};
  53. char szIndex[MAX_ID] = {0};
  54. GetCarelFromIni((char*)(LPCTSTR)chRs232cmd,
  55. szType,
  56. szIndex);
  57. int nRet = -1;
  58. nRet = pCarelRequestData( nPort, //端口
  59. nDevAddr, //设备地址
  60. chRs232cmd, //命令
  61. szType, //变量类型
  62. szIndex, //变量索引
  63. g_szCarelMsg //输出缓冲区
  64. );
  65. if( nRet != 0 )
  66. {
  67. return -1;
  68. }
  69. nRet = CarelSingleResponseData(chDevUid, iVarID, szType, g_szCarelMsg );
  70. return nRet;
  71. }
  72. int CarelSingleResponseData(char chDevUid[20], int iVarID, char szType[MAX_ID], char chMsg[80])
  73. {
  74. int nDeviceIndex = -1, nVarIndex = -1;
  75. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  76. if( bFind == FALSE ) return -1;
  77. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  78. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  79. EnterCriticalSection(&g_csCarelReadOneData);
  80. pDev->m_dwOnlineTick = GetTickCount();
  81. LeaveCriticalSection(&g_csCarelReadOneData);
  82. DWORD nValue = 0;
  83. WORD wdValue = 0;
  84. double fCoef = pBaseVar->m_dbCoefficient;
  85. int nDataLen = pBaseVar->m_iDataLen;
  86. int nVarItemID = pBaseVar->m_nVarItemID;
  87. if( strcmp(szType, "A") == 0 )
  88. {
  89. INT16 nValue16;
  90. BYTE *pB = (BYTE*)&nValue16;
  91. pB[1] = TwoByteToByte(chMsg[0], chMsg[1]);
  92. pB[0] = TwoByteToByte(chMsg[2], chMsg[3]);
  93. nValue = nValue16;
  94. }
  95. else if( strcmp(szType, "I") == 0 )
  96. {
  97. }
  98. else if( strcmp(szType, "D") == 0 )
  99. {
  100. nValue = AsciiToBYTE(chMsg[0]);
  101. }
  102. EnterCriticalSection( &g_csCarelReadOneData );
  103. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  104. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  105. {
  106. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  107. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  108. pBaseVar->m_dbData = nValue *
  109. fCoef *
  110. fScale;
  111. }
  112. else
  113. {
  114. pBaseVar->m_dbData = (double)(nValue * fCoef);
  115. }
  116. if( pBaseVar->m_nRearm != 0 )
  117. {
  118. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  119. }
  120. LeaveCriticalSection( &g_csCarelReadOneData );
  121. if( pBaseVar->m_nVarTypeID != SNMP_NPM_TYPE &&
  122. pBaseVar->m_nVarTypeID != SNMP_STRING_ID )
  123. {
  124. if( (int)pBaseVar->m_dbData < pBaseVar->m_nReserved2 ||
  125. (int)pBaseVar->m_dbData > pBaseVar->m_nReserved3 )
  126. {
  127. return -1;
  128. }
  129. }
  130. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_ParadigmReadOneData.dbData);
  131. EnterCriticalSection(&g_csCarelReadOneData);
  132. pDev->m_dwOnlineTick = GetTickCount();
  133. LeaveCriticalSection(&g_csCarelReadOneData);
  134. return 0;
  135. }
  136. void UnInitCarelDll()
  137. {
  138. if (g_hCarelLibModule != NULL && ( NULL != pCarel_DLLUnInit ))
  139. {
  140. pCarel_DLLUnInit();
  141. }
  142. DeleteCriticalSection( &g_csCarelReadOneData );
  143. if (g_hCarelLibModule!= NULL)
  144. {
  145. AfxFreeLibrary(g_hCarelLibModule);
  146. g_hCarelLibModule = NULL;
  147. }
  148. }
  149. int GetCarelFromIni(char chCmd[32], char szType[MAX_ID], char szIndex[MAX_ID])
  150. {
  151. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  152. wsprintf(strFile, "%s\\config\\Carel.ini", g_strDirectory);
  153. //类型
  154. GetPrivateProfileString(chCmd, "type", "", szType, sizeof(szType), strFile);
  155. //索引
  156. GetPrivateProfileString(chCmd, "index", "", szIndex, sizeof(szIndex), strFile);
  157. return 0;
  158. }
  159. int CarelControlRequest(char chDevUid[20], int iVarID, double data, char chRs232cmd[32])//用于请求写命令操作
  160. {
  161. int nDeviceIndex = -1, nVarIndex = -1;
  162. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  163. if( bFind == FALSE ) return -1;
  164. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  165. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  166. char szMsg[MAX_VAR_MSG] = {0};
  167. char szType[MAX_ID] = {0};
  168. char szIndex[MAX_ID] = {0};
  169. if( pCarelControl( pDev->m_iPort,
  170. pDev->m_iDevideaddr,
  171. chRs232cmd,
  172. szType,
  173. szIndex,
  174. szMsg
  175. ) == 0 )
  176. {
  177. return 0;
  178. }
  179. else
  180. {
  181. return -1;
  182. }
  183. }