greeprocess.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #include "stdafx.h"
  2. #include "greeprocess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "NoticeQueue.h"
  6. HANDLE g_GREERequestThread = NULL;
  7. HANDLE g_GREEResponseThread = NULL;
  8. DWORD g_dwGreeVariantTick = 0;
  9. CRITICAL_SECTION g_csGreeReadOneData;
  10. //GREE动态库输出函数
  11. HINSTANCE g_hGREELibModule = NULL;
  12. GREE_DLLInitCom pGREE_DLLInitCom = NULL;
  13. GREE_DLLInit pGREE_DLLInit = NULL;
  14. GREE_DLLUnInit pGREE_DLLUnInit = NULL;
  15. GREE_DLLWrCom pGREE_DLLWrCom = NULL;
  16. void UnInitGreeDll();
  17. int GetGreeFromIni(char chCmd[32], char *pPosBegin);
  18. int GetGreeFromIni(char chCmd[32], char *pPosBegin)
  19. {
  20. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  21. CHAR strValue[8] = "";
  22. wsprintf(strFile, "%s\\config\\gree.ini", g_strDirectory);
  23. //Database
  24. GetPrivateProfileString(chCmd, "PosIndex", "", strValue, sizeof(strValue), strFile);
  25. memcpy(pPosBegin, strValue, strlen(strValue));
  26. return 0;
  27. }
  28. BOOL LoadGreeDll(CString strpath)
  29. {
  30. char strFile[256] = {0};
  31. g_hGREELibModule = NULL;
  32. sprintf(strFile, "%s\\dll\\gree.dll", strpath);
  33. InitializeCriticalSection( &g_csGreeReadOneData );
  34. g_hGREELibModule = AfxLoadLibrary(strFile);
  35. //GREE 动态库初始化
  36. if (NULL != g_hGREELibModule)
  37. {
  38. pGREE_DLLInitCom =( GREE_DLLInitCom)::GetProcAddress(g_hGREELibModule, "GREE_DLLInitCom");
  39. pGREE_DLLInit = (GREE_DLLInit)::GetProcAddress(g_hGREELibModule, "GREE_DLLInit");
  40. pGREE_DLLUnInit = (GREE_DLLUnInit)::GetProcAddress(g_hGREELibModule, "GREE_DLLUnInit");
  41. pGREE_DLLWrCom = (GREE_DLLWrCom)::GetProcAddress(g_hGREELibModule, "GREE_DLLWrCom");
  42. return TRUE;
  43. }
  44. else
  45. {
  46. return FALSE;
  47. }
  48. }
  49. BOOL InitGreeComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  50. {
  51. if (pGREE_DLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  52. return TRUE;
  53. else
  54. return FALSE;
  55. }
  56. int GreeRequestData( SETBASEPARAM SetBasePara,
  57. int nDataLen,
  58. int nPort,
  59. int nDevAddr,
  60. char chDevUid[20],
  61. int iVarID,
  62. char chRs232cmd[32])
  63. {
  64. char chMsg[80] = {0};
  65. int nReadPos = 0;
  66. char pPosBegin[8]={0};
  67. GetGreeFromIni(chRs232cmd, pPosBegin);
  68. nReadPos = atoi(pPosBegin);
  69. int nRet = pGREE_DLLInit(nPort, nDevAddr, nDataLen, nReadPos,chMsg);
  70. if(nRet==0)
  71. {
  72. nRet = GreeSingleResponseData(chDevUid, iVarID, chMsg );
  73. }
  74. return nRet;
  75. }
  76. int GreeSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80])
  77. {
  78. int nDeviceIndex = -1, nVarIndex = -1;
  79. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  80. if( bFind == FALSE ) return -1;
  81. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  82. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  83. EnterCriticalSection(&g_csGreeReadOneData);
  84. pDev->m_dwOnlineTick = GetTickCount();
  85. LeaveCriticalSection(&g_csGreeReadOneData);
  86. int nVarItemID = pBaseVar->m_nVarItemID;
  87. int nDataLen = pBaseVar->m_iDataLen;
  88. int nValue;
  89. double fCoef = pBaseVar->m_dbCoefficient;
  90. if( nVarItemID > 0 )//BIT位变量
  91. {
  92. int nStartBit = pBaseVar->m_nStartBit;
  93. int nEndBit = pBaseVar->m_nEndBit;
  94. char szValue[4];
  95. // Gree空调特殊处理
  96. char chBuffer[8] = {0};
  97. memset(chBuffer, 0, sizeof(chBuffer));
  98. BYTE byValue = chMsg[0];
  99. DigitToBinary(byValue, chBuffer, 8) ;
  100. CString strTemp;
  101. switch( nEndBit - nStartBit )
  102. {
  103. case 0:
  104. strTemp.Format("%c", chBuffer[nStartBit]);
  105. nValue = atoi(strTemp);
  106. break;
  107. case 1:
  108. strTemp.Format("00%c%c", chBuffer[nStartBit + 1], chBuffer[nStartBit]);
  109. szValue[0] = AsciiToBYTE( strTemp.GetAt(0) );
  110. szValue[1] = AsciiToBYTE( strTemp.GetAt(1) );
  111. szValue[2] = AsciiToBYTE( strTemp.GetAt(2) );
  112. szValue[3] = AsciiToBYTE( strTemp.GetAt(3) );
  113. nValue = Hex16(szValue);
  114. break;
  115. case 2:
  116. strTemp.Format("0%c%c%c", chBuffer[nStartBit + 2], chBuffer[nStartBit + 1], chBuffer[nStartBit]);
  117. szValue[0] = AsciiToBYTE( strTemp.GetAt(0) );
  118. szValue[1] = AsciiToBYTE( strTemp.GetAt(1) );
  119. szValue[2] = AsciiToBYTE( strTemp.GetAt(2) );
  120. szValue[3] = AsciiToBYTE( strTemp.GetAt(3) );
  121. nValue = Hex16(szValue);
  122. break;
  123. case 3:
  124. strTemp.Format("%c%c%c%c", chBuffer[nStartBit + 2], chBuffer[nStartBit + 1], chBuffer[nStartBit], chBuffer[nStartBit +3]);
  125. szValue[0] = AsciiToBYTE( strTemp.GetAt(0) );
  126. szValue[1] = AsciiToBYTE( strTemp.GetAt(1) );
  127. szValue[2] = AsciiToBYTE( strTemp.GetAt(2) );
  128. szValue[3] = AsciiToBYTE( strTemp.GetAt(3) );
  129. nValue = Hex16(szValue);
  130. break;
  131. }
  132. //strTemp.Format("%d",
  133. EnterCriticalSection( &g_csGreeReadOneData );
  134. pBaseVar->m_dbData = (double)(nValue * fCoef);
  135. if( pBaseVar->m_nRearm != 0 )
  136. {
  137. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  138. }
  139. LeaveCriticalSection( &g_csGreeReadOneData );
  140. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  141. }
  142. else
  143. {
  144. // 联合类型变量做特殊处理
  145. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  146. {
  147. if( nDataLen == 2 )
  148. {
  149. //int类型
  150. union __UNION_VAR_INT{
  151. char ch[2];
  152. int value;
  153. }unionVarInt;
  154. unionVarInt.ch[0] = chMsg[1];
  155. unionVarInt.ch[1] = chMsg[0];
  156. EnterCriticalSection( &g_csGreeReadOneData );
  157. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  158. if( pBaseVar->m_nRearm != 0 )
  159. {
  160. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  161. }
  162. LeaveCriticalSection( &g_csGreeReadOneData );
  163. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  164. }
  165. else if( nDataLen == 4 )
  166. {
  167. //float类型
  168. union __UNION_VAR_FLOAT{
  169. char ch[4];
  170. float value;
  171. }unionVarFloat;
  172. unionVarFloat.ch[0] = chMsg[3];
  173. unionVarFloat.ch[1] = chMsg[2];
  174. unionVarFloat.ch[2] = chMsg[1];
  175. unionVarFloat.ch[3] = chMsg[0];
  176. EnterCriticalSection( &g_csGreeReadOneData );
  177. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  178. if( pBaseVar->m_nRearm != 0 )
  179. {
  180. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  181. }
  182. LeaveCriticalSection( &g_csGreeReadOneData );
  183. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  184. }
  185. else if( nDataLen == 8 )
  186. {
  187. //double类型
  188. union __UNION_VAR_DOUBLE{
  189. char ch[8];
  190. double value;
  191. }unionVarDouble;
  192. unionVarDouble.ch[0] = chMsg[7];
  193. unionVarDouble.ch[1] = chMsg[6];
  194. unionVarDouble.ch[2] = chMsg[5];
  195. unionVarDouble.ch[3] = chMsg[4];
  196. unionVarDouble.ch[4] = chMsg[3];
  197. unionVarDouble.ch[5] = chMsg[2];
  198. unionVarDouble.ch[6] = chMsg[1];
  199. unionVarDouble.ch[7] = chMsg[0];
  200. EnterCriticalSection( &g_csGreeReadOneData );
  201. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  202. if( pBaseVar->m_nRearm != 0 )
  203. {
  204. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  205. }
  206. LeaveCriticalSection( &g_csGreeReadOneData );
  207. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  208. }
  209. }
  210. else
  211. {
  212. CString strTemp;
  213. switch( nDataLen )
  214. {
  215. case 1:
  216. nValue = chMsg[0];
  217. break;
  218. case 2:
  219. //short int wdValue;
  220. strTemp.Format("%d%d", chMsg[0] - '0', chMsg[1] - '0');
  221. nValue = atoi(strTemp);
  222. //memcpy(&wdValue, chMsg, sizeof(short int));
  223. //nValue = ntohs(wdValue);
  224. //nValue = wdValue;
  225. break;
  226. case 3: // 一般为字符串类型,不作处理
  227. break;
  228. case 4: // 保留,暂时不知道什么处理
  229. break;
  230. case 5: // 一般为字符串类型,不作处理
  231. break;
  232. case 6: // 一般为字符串类型,不作处理
  233. break;
  234. case 7: // 一般为字符串类型,不作处理
  235. break;
  236. case 8: // 保留,暂时没有用到
  237. break;
  238. }
  239. EnterCriticalSection( &g_csGreeReadOneData );
  240. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  241. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  242. {
  243. double fScale = 1.0;
  244. if( pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues > 0 )
  245. {
  246. fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  247. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  248. }
  249. pBaseVar->m_dbData = nValue *
  250. fCoef *
  251. fScale;
  252. }
  253. else
  254. {
  255. pBaseVar->m_dbData = (double)(nValue * fCoef);
  256. }
  257. if( pBaseVar->m_nRearm != 0 )
  258. {
  259. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  260. }
  261. //g_RtuReadOneData.dbData = (double)(nValue * fCoef);
  262. LeaveCriticalSection( &g_csGreeReadOneData );
  263. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_RtuReadOneData.dbData);
  264. }
  265. }
  266. EnterCriticalSection(&g_csGreeReadOneData);
  267. pDev->m_dwOnlineTick = GetTickCount();
  268. LeaveCriticalSection(&g_csGreeReadOneData);
  269. return 0;
  270. }
  271. int RequestGreeWrData(char chDevUid[20], int iVarID, double data, char chRs232cmd[32])//用于请求写命令操作
  272. {
  273. int nDeviceIndex = -1, nVarIndex = -1;
  274. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  275. if( bFind == FALSE ) return -1;
  276. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  277. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  278. int nWorkStatus, nWorkMode = 0, nFanSpeed = 0;
  279. int nPutWind = 1, nAirTrade, nSleep, nLight;
  280. int nTempSetPoint;
  281. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  282. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  283. wsprintf(strFile, "%s\\config\\Gree.ini", g_strDirectory);
  284. nAirTrade = GetPrivateProfileInt(chRs232cmd, "AirTrade", 0, strFile);
  285. nSleep = GetPrivateProfileInt(chRs232cmd, "Sleep", 0, strFile);
  286. nLight = GetPrivateProfileInt(chRs232cmd, "Light", 0, strFile);
  287. nTempSetPoint = GetPrivateProfileInt("Other", "SetCoolTemp", 0, strFile);
  288. nWorkStatus = 1;
  289. if( strcmp( chRs232cmd, "cmd-7" ) == 0 )
  290. {
  291. nWorkStatus = 0;
  292. }
  293. else if( strcmp(chRs232cmd, "cmd-8") == 0 ) //开机(模式:自动,风量:自动)
  294. {
  295. nWorkMode = 0;
  296. nFanSpeed = 0;
  297. }
  298. else if( strcmp(chRs232cmd, "cmd-9") == 0 ) //开机(模式:制冷,风量:自动)
  299. {
  300. nWorkMode = 1;
  301. nFanSpeed = 0;
  302. }
  303. else if( strcmp(chRs232cmd, "cmd-10") == 0 ) //开机(模式:抽湿,风量:自动)
  304. {
  305. nWorkMode = 2;
  306. nFanSpeed = 0;
  307. }
  308. else if( strcmp(chRs232cmd, "cmd-11") == 0 ) //开机(模式:送风,风量:自动)
  309. {
  310. nWorkMode = 3;
  311. nFanSpeed = 0;
  312. }
  313. else if( strcmp(chRs232cmd, "cmd-12") == 0 ) //开机(模式:制热,风量:自动)
  314. {
  315. nWorkMode = 4;
  316. nFanSpeed = 0;
  317. nTempSetPoint = GetPrivateProfileInt("Other", "SetHeartTemp", 0, strFile);
  318. }
  319. else if( strcmp(chRs232cmd, "cmd-13") == 0 ) //开机(模式:自动,风量:低风)
  320. {
  321. nWorkMode = 0;
  322. nFanSpeed = 1;
  323. }
  324. else if( strcmp(chRs232cmd, "cmd-14") == 0 ) //开机(模式:制冷,风量:低风)
  325. {
  326. nWorkMode = 1;
  327. nFanSpeed = 1;
  328. }
  329. else if( strcmp(chRs232cmd, "cmd-15") == 0 ) //开机(模式:抽湿,风量:低风)
  330. {
  331. nWorkMode = 2;
  332. nFanSpeed = 1;
  333. }
  334. else if( strcmp(chRs232cmd, "cmd-16") == 0 ) //开机(模式:送风,风量:低风)
  335. {
  336. nWorkMode = 3;
  337. nFanSpeed = 1;
  338. }
  339. else if( strcmp(chRs232cmd, "cmd-17") == 0 ) //开机(模式:制热,风量:低风)
  340. {
  341. nWorkMode = 4;
  342. nFanSpeed = 1;
  343. nTempSetPoint = GetPrivateProfileInt("Other", "SetHeartTemp", 0, strFile);
  344. }
  345. else if( strcmp(chRs232cmd, "cmd-18") == 0 ) //开机(模式:自动,风量:中风)
  346. {
  347. nWorkMode = 0;
  348. nFanSpeed = 2;
  349. }
  350. else if( strcmp(chRs232cmd, "cmd-19") == 0 ) //开机(模式:制冷,风量:中风)
  351. {
  352. nWorkMode = 1;
  353. nFanSpeed = 2;
  354. }
  355. else if( strcmp(chRs232cmd, "cmd-20") == 0 ) //开机(模式:抽湿,风量:中风)
  356. {
  357. nWorkMode = 2;
  358. nFanSpeed = 2;
  359. }
  360. else if( strcmp(chRs232cmd, "cmd-21") == 0 ) //开机(模式:送风,风量:中风)
  361. {
  362. nWorkMode = 3;
  363. nFanSpeed = 2;
  364. }
  365. else if( strcmp(chRs232cmd, "cmd-22") == 0 ) //开机(模式:制热,风量:中风)
  366. {
  367. nWorkMode = 4;
  368. nFanSpeed = 2;
  369. nTempSetPoint = GetPrivateProfileInt("Other", "SetHeartTemp", 0, strFile);
  370. }
  371. else if( strcmp(chRs232cmd, "cmd-23") == 0 ) //开机(模式:自动,风量:高风)
  372. {
  373. nWorkMode = 0;
  374. nFanSpeed = 3;
  375. }
  376. else if( strcmp(chRs232cmd, "cmd-24") == 0 ) //开机(模式:制冷,风量:高风)
  377. {
  378. nWorkMode = 1;
  379. nFanSpeed = 3;
  380. }
  381. else if( strcmp(chRs232cmd, "cmd-25") == 0 ) //开机(模式:抽湿,风量:高风)
  382. {
  383. nWorkMode = 2;
  384. nFanSpeed = 3;
  385. }
  386. else if( strcmp(chRs232cmd, "cmd-26") == 0 ) //开机(模式:送风,风量:高风)
  387. {
  388. nWorkMode = 3;
  389. nFanSpeed = 3;
  390. }
  391. else if( strcmp(chRs232cmd, "cmd-27") == 0 ) //开机(模式:制热,风量:高风)
  392. {
  393. nWorkMode = 4;
  394. nFanSpeed = 3;
  395. nTempSetPoint = GetPrivateProfileInt("Other", "SetHeartTemp", 0, strFile);
  396. }
  397. if( pGREE_DLLWrCom( pDev->m_iPort,
  398. pDev->m_iDevideaddr,
  399. nWorkStatus,
  400. nWorkMode,
  401. nFanSpeed,
  402. nPutWind,
  403. nAirTrade,
  404. nSleep,
  405. nLight,
  406. nTempSetPoint) == 0 )
  407. {
  408. return 0;
  409. }
  410. else
  411. {
  412. return -1;
  413. }
  414. }
  415. void UnInitGreeDll()
  416. {
  417. if (g_hGREELibModule != NULL && ( NULL != pGREE_DLLUnInit ))
  418. {
  419. pGREE_DLLUnInit();
  420. }
  421. DeleteCriticalSection( &g_csGreeReadOneData );
  422. if (g_hGREELibModule!= NULL)
  423. {
  424. AfxFreeLibrary(g_hGREELibModule);
  425. g_hGREELibModule = NULL;
  426. }
  427. }