greeprocess.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. default:
  132. LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
  133. }
  134. //strTemp.Format("%d",
  135. EnterCriticalSection( &g_csGreeReadOneData );
  136. pBaseVar->m_dbData = (double)(nValue * fCoef);
  137. if( pBaseVar->m_nRearm != 0 )
  138. {
  139. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  140. }
  141. LeaveCriticalSection( &g_csGreeReadOneData );
  142. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  143. }
  144. else
  145. {
  146. // 联合类型变量做特殊处理
  147. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  148. {
  149. if( nDataLen == 2 )
  150. {
  151. //int类型
  152. union __UNION_VAR_INT{
  153. char ch[2];
  154. int value;
  155. }unionVarInt;
  156. unionVarInt.ch[0] = chMsg[1];
  157. unionVarInt.ch[1] = chMsg[0];
  158. EnterCriticalSection( &g_csGreeReadOneData );
  159. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  160. if( pBaseVar->m_nRearm != 0 )
  161. {
  162. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  163. }
  164. LeaveCriticalSection( &g_csGreeReadOneData );
  165. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  166. }
  167. else if( nDataLen == 4 )
  168. {
  169. //float类型
  170. union __UNION_VAR_FLOAT{
  171. char ch[4];
  172. float value;
  173. }unionVarFloat;
  174. unionVarFloat.ch[0] = chMsg[3];
  175. unionVarFloat.ch[1] = chMsg[2];
  176. unionVarFloat.ch[2] = chMsg[1];
  177. unionVarFloat.ch[3] = chMsg[0];
  178. EnterCriticalSection( &g_csGreeReadOneData );
  179. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  180. if( pBaseVar->m_nRearm != 0 )
  181. {
  182. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  183. }
  184. LeaveCriticalSection( &g_csGreeReadOneData );
  185. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  186. }
  187. else if( nDataLen == 8 )
  188. {
  189. //double类型
  190. union __UNION_VAR_DOUBLE{
  191. char ch[8];
  192. double value;
  193. }unionVarDouble;
  194. unionVarDouble.ch[0] = chMsg[7];
  195. unionVarDouble.ch[1] = chMsg[6];
  196. unionVarDouble.ch[2] = chMsg[5];
  197. unionVarDouble.ch[3] = chMsg[4];
  198. unionVarDouble.ch[4] = chMsg[3];
  199. unionVarDouble.ch[5] = chMsg[2];
  200. unionVarDouble.ch[6] = chMsg[1];
  201. unionVarDouble.ch[7] = chMsg[0];
  202. EnterCriticalSection( &g_csGreeReadOneData );
  203. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  204. if( pBaseVar->m_nRearm != 0 )
  205. {
  206. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  207. }
  208. LeaveCriticalSection( &g_csGreeReadOneData );
  209. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  210. }
  211. }
  212. else
  213. {
  214. CString strTemp;
  215. switch( nDataLen )
  216. {
  217. case 1:
  218. nValue = chMsg[0];
  219. break;
  220. case 2:
  221. //short int wdValue;
  222. strTemp.Format("%d%d", chMsg[0] - '0', chMsg[1] - '0');
  223. nValue = atoi(strTemp);
  224. //memcpy(&wdValue, chMsg, sizeof(short int));
  225. //nValue = ntohs(wdValue);
  226. //nValue = wdValue;
  227. break;
  228. case 3: // 一般为字符串类型,不作处理
  229. LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
  230. break;
  231. case 4: // 保留,暂时不知道什么处理
  232. LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
  233. break;
  234. case 5: // 一般为字符串类型,不作处理
  235. LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
  236. break;
  237. case 6: // 一般为字符串类型,不作处理
  238. LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
  239. break;
  240. case 7: // 一般为字符串类型,不作处理
  241. LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
  242. break;
  243. case 8: // 保留,暂时没有用到
  244. LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
  245. break;
  246. }
  247. EnterCriticalSection( &g_csGreeReadOneData );
  248. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  249. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  250. {
  251. double fScale = 1.0;
  252. if( pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues > 0 )
  253. {
  254. fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  255. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  256. }
  257. pBaseVar->m_dbData = nValue *
  258. fCoef *
  259. fScale;
  260. }
  261. else
  262. {
  263. pBaseVar->m_dbData = (double)(nValue * fCoef);
  264. }
  265. if( pBaseVar->m_nRearm != 0 )
  266. {
  267. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  268. }
  269. //g_RtuReadOneData.dbData = (double)(nValue * fCoef);
  270. LeaveCriticalSection( &g_csGreeReadOneData );
  271. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_RtuReadOneData.dbData);
  272. }
  273. }
  274. EnterCriticalSection(&g_csGreeReadOneData);
  275. pDev->m_dwOnlineTick = GetTickCount();
  276. LeaveCriticalSection(&g_csGreeReadOneData);
  277. return 0;
  278. }
  279. int RequestGreeWrData(char chDevUid[20], int iVarID, double data, char chRs232cmd[32])//用于请求写命令操作
  280. {
  281. int nDeviceIndex = -1, nVarIndex = -1;
  282. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  283. if( bFind == FALSE ) return -1;
  284. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  285. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  286. int nWorkStatus, nWorkMode = 0, nFanSpeed = 0;
  287. int nPutWind = 1, nAirTrade, nSleep, nLight;
  288. int nTempSetPoint;
  289. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  290. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  291. wsprintf(strFile, "%s\\config\\Gree.ini", g_strDirectory);
  292. nAirTrade = GetPrivateProfileInt(chRs232cmd, "AirTrade", 0, strFile);
  293. nSleep = GetPrivateProfileInt(chRs232cmd, "Sleep", 0, strFile);
  294. nLight = GetPrivateProfileInt(chRs232cmd, "Light", 0, strFile);
  295. nTempSetPoint = GetPrivateProfileInt("Other", "SetCoolTemp", 0, strFile);
  296. nWorkStatus = 1;
  297. if( strcmp( chRs232cmd, "cmd-7" ) == 0 )
  298. {
  299. nWorkStatus = 0;
  300. }
  301. else if( strcmp(chRs232cmd, "cmd-8") == 0 ) //开机(模式:自动,风量:自动)
  302. {
  303. nWorkMode = 0;
  304. nFanSpeed = 0;
  305. }
  306. else if( strcmp(chRs232cmd, "cmd-9") == 0 ) //开机(模式:制冷,风量:自动)
  307. {
  308. nWorkMode = 1;
  309. nFanSpeed = 0;
  310. }
  311. else if( strcmp(chRs232cmd, "cmd-10") == 0 ) //开机(模式:抽湿,风量:自动)
  312. {
  313. nWorkMode = 2;
  314. nFanSpeed = 0;
  315. }
  316. else if( strcmp(chRs232cmd, "cmd-11") == 0 ) //开机(模式:送风,风量:自动)
  317. {
  318. nWorkMode = 3;
  319. nFanSpeed = 0;
  320. }
  321. else if( strcmp(chRs232cmd, "cmd-12") == 0 ) //开机(模式:制热,风量:自动)
  322. {
  323. nWorkMode = 4;
  324. nFanSpeed = 0;
  325. nTempSetPoint = GetPrivateProfileInt("Other", "SetHeartTemp", 0, strFile);
  326. }
  327. else if( strcmp(chRs232cmd, "cmd-13") == 0 ) //开机(模式:自动,风量:低风)
  328. {
  329. nWorkMode = 0;
  330. nFanSpeed = 1;
  331. }
  332. else if( strcmp(chRs232cmd, "cmd-14") == 0 ) //开机(模式:制冷,风量:低风)
  333. {
  334. nWorkMode = 1;
  335. nFanSpeed = 1;
  336. }
  337. else if( strcmp(chRs232cmd, "cmd-15") == 0 ) //开机(模式:抽湿,风量:低风)
  338. {
  339. nWorkMode = 2;
  340. nFanSpeed = 1;
  341. }
  342. else if( strcmp(chRs232cmd, "cmd-16") == 0 ) //开机(模式:送风,风量:低风)
  343. {
  344. nWorkMode = 3;
  345. nFanSpeed = 1;
  346. }
  347. else if( strcmp(chRs232cmd, "cmd-17") == 0 ) //开机(模式:制热,风量:低风)
  348. {
  349. nWorkMode = 4;
  350. nFanSpeed = 1;
  351. nTempSetPoint = GetPrivateProfileInt("Other", "SetHeartTemp", 0, strFile);
  352. }
  353. else if( strcmp(chRs232cmd, "cmd-18") == 0 ) //开机(模式:自动,风量:中风)
  354. {
  355. nWorkMode = 0;
  356. nFanSpeed = 2;
  357. }
  358. else if( strcmp(chRs232cmd, "cmd-19") == 0 ) //开机(模式:制冷,风量:中风)
  359. {
  360. nWorkMode = 1;
  361. nFanSpeed = 2;
  362. }
  363. else if( strcmp(chRs232cmd, "cmd-20") == 0 ) //开机(模式:抽湿,风量:中风)
  364. {
  365. nWorkMode = 2;
  366. nFanSpeed = 2;
  367. }
  368. else if( strcmp(chRs232cmd, "cmd-21") == 0 ) //开机(模式:送风,风量:中风)
  369. {
  370. nWorkMode = 3;
  371. nFanSpeed = 2;
  372. }
  373. else if( strcmp(chRs232cmd, "cmd-22") == 0 ) //开机(模式:制热,风量:中风)
  374. {
  375. nWorkMode = 4;
  376. nFanSpeed = 2;
  377. nTempSetPoint = GetPrivateProfileInt("Other", "SetHeartTemp", 0, strFile);
  378. }
  379. else if( strcmp(chRs232cmd, "cmd-23") == 0 ) //开机(模式:自动,风量:高风)
  380. {
  381. nWorkMode = 0;
  382. nFanSpeed = 3;
  383. }
  384. else if( strcmp(chRs232cmd, "cmd-24") == 0 ) //开机(模式:制冷,风量:高风)
  385. {
  386. nWorkMode = 1;
  387. nFanSpeed = 3;
  388. }
  389. else if( strcmp(chRs232cmd, "cmd-25") == 0 ) //开机(模式:抽湿,风量:高风)
  390. {
  391. nWorkMode = 2;
  392. nFanSpeed = 3;
  393. }
  394. else if( strcmp(chRs232cmd, "cmd-26") == 0 ) //开机(模式:送风,风量:高风)
  395. {
  396. nWorkMode = 3;
  397. nFanSpeed = 3;
  398. }
  399. else if( strcmp(chRs232cmd, "cmd-27") == 0 ) //开机(模式:制热,风量:高风)
  400. {
  401. nWorkMode = 4;
  402. nFanSpeed = 3;
  403. nTempSetPoint = GetPrivateProfileInt("Other", "SetHeartTemp", 0, strFile);
  404. }
  405. if( pGREE_DLLWrCom( pDev->m_iPort,
  406. pDev->m_iDevideaddr,
  407. nWorkStatus,
  408. nWorkMode,
  409. nFanSpeed,
  410. nPutWind,
  411. nAirTrade,
  412. nSleep,
  413. nLight,
  414. nTempSetPoint) == 0 )
  415. {
  416. return 0;
  417. }
  418. else
  419. {
  420. return -1;
  421. }
  422. }
  423. void UnInitGreeDll()
  424. {
  425. if (g_hGREELibModule != NULL && ( NULL != pGREE_DLLUnInit ))
  426. {
  427. pGREE_DLLUnInit();
  428. }
  429. DeleteCriticalSection( &g_csGreeReadOneData );
  430. if (g_hGREELibModule!= NULL)
  431. {
  432. AfxFreeLibrary(g_hGREELibModule);
  433. g_hGREELibModule = NULL;
  434. }
  435. }