DaiKinProcess.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. #include "stdafx.h"
  2. #include "DaiKinProcess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "NoticeQueue.h"
  6. #define SET_RUN 1 // 设置空调运转(开/关)
  7. #define SET_RUN_MODE 2 // 设置空调运转模式(0:送风,1:制热,2:制冷,3:自动,4:换气清洗,7:除湿
  8. #define SET_TEMP 3 // 设定温度
  9. #define SET_FAN 4 // 设置风量
  10. CRITICAL_SECTION g_csDaiKinReadOneData;
  11. char g_szDaiKinAnlogMsg[DAIKIN_MAX_MSG] = {0};
  12. char g_szDaiKinDigitalMsg[DAIKIN_MAX_MSG] = {0};
  13. char g_szDaiKinAlarmMsg[DAIKIN_MAX_MSG] = {0};
  14. char g_szDaiKinGetParamMsg[DAIKIN_MAX_MSG] = {0};
  15. //大金空调动态库输出函数
  16. HINSTANCE g_hDaiKinLibModule = NULL;
  17. DAIKIN_DLLInitCom pDaiKin_DLLInitCom = NULL;
  18. DAIKIN_DLLInit pDaiKin_DLLInit = NULL;
  19. DAIKIN_DLLUnInit pDaiKin_DLLUnInit = NULL;
  20. DAIKIN_DLLSetParam pDaiKin_DLLSetParam = NULL;
  21. DAIKIN_DLLRemoteCtrl pDaiKin_DLLRemoteCtrl = NULL;
  22. int DaiKinCommandSend();
  23. void UnInitDaiKinDll();
  24. int GetDaiKinFromIni(char chCmd[32], int &nVer, int &nCid2,
  25. char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID], char chDataLen[MAX_ID]);
  26. BOOL LoadDaiKinDll(CString strpath)
  27. {
  28. char strFile[256] = {0};
  29. g_hDaiKinLibModule = NULL;
  30. sprintf(strFile, "%s\\dll\\Daikin.dll", strpath);
  31. InitializeCriticalSection( &g_csDaiKinReadOneData );
  32. g_hDaiKinLibModule = AfxLoadLibrary(strFile);
  33. //大金空调动态库初始化
  34. if (NULL != g_hDaiKinLibModule)
  35. {
  36. pDaiKin_DLLInitCom =( DAIKIN_DLLInitCom)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLInitCom");
  37. pDaiKin_DLLInit =(DAIKIN_DLLInit)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLInit");
  38. pDaiKin_DLLUnInit =(DAIKIN_DLLUnInit)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLUnInit");
  39. pDaiKin_DLLSetParam = (DAIKIN_DLLSetParam)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLSetParam");
  40. pDaiKin_DLLRemoteCtrl = (DAIKIN_DLLRemoteCtrl)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLRemoteCtrl");
  41. return TRUE;
  42. }
  43. else
  44. {
  45. return FALSE;
  46. }
  47. }
  48. BOOL InitDaiKinComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  49. {
  50. if (pDaiKin_DLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  51. return TRUE;
  52. else
  53. return FALSE;
  54. }
  55. int DaiKinRequestData(
  56. int nPort,
  57. int nDevAddr,
  58. char chDevUid[20],
  59. int iVarID,
  60. char chRs232cmd[32])
  61. {
  62. char chMsg[80] = {0};
  63. int nCid1 = 0x60;
  64. int nCid2;
  65. WORD wLenId = 0x00;//INFO字节长度
  66. BYTE byCmdType = 0x00;//命令类型
  67. BYTE byCmdId = 0x00;
  68. char byDataFlag = 0;
  69. char chAdrBeginPos[MAX_ID] = {0};
  70. char chCmdLen[MAX_ID] = {0};
  71. char chDataLen[MAX_ID] = {0};
  72. int nVer;
  73. GetDaiKinFromIni((char*)(LPCTSTR)chRs232cmd,
  74. nVer,
  75. nCid2,
  76. chAdrBeginPos,
  77. chCmdLen,
  78. chDataLen);
  79. int nRet = -1;
  80. if( strlen(g_szDaiKinAnlogMsg) == 0 )
  81. {
  82. int nRet = pDaiKin_DLLInit( nPort, //端口
  83. nDevAddr, //设备地址
  84. nVer, //版本号
  85. nCid1, //控制标识码
  86. nCid2, //命令信息
  87. wLenId, //INFO字节长度
  88. byCmdId, //命令ID
  89. atoi(chDataLen), //请求数据长度
  90. atoi(chAdrBeginPos), //变量索引
  91. atoi(chCmdLen), //变量长度
  92. g_szDaiKinAnlogMsg, //读到变量值
  93. &byDataFlag); //保留未用
  94. if( nRet != 0 )
  95. {
  96. return -1;
  97. }
  98. }
  99. if( strlen(g_szDaiKinDigitalMsg) == 0 )
  100. {
  101. int nRet = pDaiKin_DLLInit( nPort, //端口
  102. nDevAddr, //设备地址
  103. nVer, //版本号
  104. nCid1, //控制标识码
  105. nCid2, //命令信息
  106. wLenId, //INFO字节长度
  107. byCmdId, //命令ID
  108. atoi(chDataLen), //请求数据长度
  109. atoi(chAdrBeginPos), //变量索引
  110. atoi(chCmdLen), //变量长度
  111. g_szDaiKinDigitalMsg, //读到变量值
  112. &byDataFlag); //保留未用
  113. if( nRet != 0 )
  114. {
  115. return -1;
  116. }
  117. }
  118. if( strlen(g_szDaiKinAlarmMsg) == 0 )
  119. {
  120. int nRet = pDaiKin_DLLInit( nPort, //端口
  121. nDevAddr, //设备地址
  122. nVer, //版本号
  123. nCid1, //控制标识码
  124. nCid2, //命令信息
  125. wLenId, //INFO字节长度
  126. byCmdId, //命令ID
  127. atoi(chDataLen), //请求数据长度
  128. atoi(chAdrBeginPos), //变量索引
  129. atoi(chCmdLen), //变量长度
  130. g_szDaiKinAlarmMsg, //读到变量值
  131. &byDataFlag); //保留未用
  132. if( nRet != 0 )
  133. {
  134. return -1;
  135. }
  136. }
  137. if( strlen(g_szDaiKinGetParamMsg) == 0 )
  138. {
  139. int nRet = pDaiKin_DLLInit( nPort, //端口
  140. nDevAddr, //设备地址
  141. nVer, //版本号
  142. nCid1, //控制标识码
  143. nCid2, //命令信息
  144. wLenId, //INFO字节长度
  145. byCmdId, //命令ID
  146. atoi(chDataLen), //请求数据长度
  147. atoi(chAdrBeginPos), //变量索引
  148. atoi(chCmdLen), //变量长度
  149. g_szDaiKinGetParamMsg, //读到变量值
  150. &byDataFlag); //保留未用
  151. if( nRet != 0 )
  152. {
  153. return -1;
  154. }
  155. }
  156. if( strcmp(chRs232cmd, "cmd-1") == 0 && strlen(g_szDaiKinAnlogMsg) > 0 )
  157. {
  158. int nRet = pDaiKin_DLLInit( nPort, //端口
  159. nDevAddr, //设备地址
  160. nVer, //版本号
  161. nCid1, //控制标识码
  162. nCid2, //命令信息
  163. wLenId, //INFO字节长度
  164. byCmdId, //命令ID
  165. atoi(chDataLen), //请求数据长度
  166. atoi(chAdrBeginPos), //变量索引
  167. atoi(chCmdLen), //变量长度
  168. g_szDaiKinAnlogMsg, //读到变量值
  169. &byDataFlag); //保留未用
  170. if( nRet != 0 )
  171. {
  172. return -1;
  173. }
  174. }
  175. else if( strcmp(chRs232cmd, "cmd-2") == 0 && strlen(g_szDaiKinDigitalMsg) > 0 )
  176. {
  177. int nRet = pDaiKin_DLLInit( nPort, //端口
  178. nDevAddr, //设备地址
  179. nVer, //版本号
  180. nCid1, //控制标识码
  181. nCid2, //命令信息
  182. wLenId, //INFO字节长度
  183. byCmdId, //命令ID
  184. atoi(chDataLen), //请求数据长度
  185. atoi(chAdrBeginPos), //变量索引
  186. atoi(chCmdLen), //变量长度
  187. g_szDaiKinDigitalMsg, //读到变量值
  188. &byDataFlag); //保留未用
  189. if( nRet != 0 )
  190. {
  191. return -1;
  192. }
  193. }
  194. else if( strcmp(chRs232cmd, "cmd-9") == 0 && strlen(g_szDaiKinAlarmMsg) > 0 )
  195. {
  196. int nRet = pDaiKin_DLLInit( nPort, //端口
  197. nDevAddr, //设备地址
  198. nVer, //版本号
  199. nCid1, //控制标识码
  200. nCid2, //命令信息
  201. wLenId, //INFO字节长度
  202. byCmdId, //命令ID
  203. atoi(chDataLen), //请求数据长度
  204. atoi(chAdrBeginPos), //变量索引
  205. atoi(chCmdLen), //变量长度
  206. g_szDaiKinAlarmMsg, //读到变量值
  207. &byDataFlag); //保留未用
  208. if( nRet != 0 )
  209. {
  210. return -1;
  211. }
  212. }
  213. else if( strcmp(chRs232cmd, "cmd-11") == 0 && strlen(g_szDaiKinGetParamMsg) > 0 )
  214. {
  215. int nRet = pDaiKin_DLLInit( nPort, //端口
  216. nDevAddr, //设备地址
  217. nVer, //版本号
  218. nCid1, //控制标识码
  219. nCid2, //命令信息
  220. wLenId, //INFO字节长度
  221. byCmdId, //命令ID
  222. atoi(chDataLen), //请求数据长度
  223. atoi(chAdrBeginPos), //变量索引
  224. atoi(chCmdLen), //变量长度
  225. g_szDaiKinGetParamMsg, //读到变量值
  226. &byDataFlag); //保留未用
  227. if( nRet != 0 )
  228. {
  229. return -1;
  230. }
  231. }
  232. if( strcmp(chRs232cmd, "cmd-1") == 0 )
  233. {
  234. if( strlen(g_szDaiKinAnlogMsg) > 0 )
  235. {
  236. for( int i = 0; i < atoi(chCmdLen); i++ )
  237. {
  238. chMsg[i] = g_szDaiKinAnlogMsg[sizeof(CHILD) + atoi(chAdrBeginPos) + i];
  239. }
  240. }
  241. }
  242. else if( strcmp(chRs232cmd, "cmd-2") == 0 ||
  243. strcmp(chRs232cmd, "cmd-3") == 0 ||
  244. strcmp(chRs232cmd, "cmd-4") == 0 ||
  245. strcmp(chRs232cmd, "cmd-5") == 0 ||
  246. strcmp(chRs232cmd, "cmd-6") == 0 ||
  247. strcmp(chRs232cmd, "cmd-7") == 0 ||
  248. strcmp(chRs232cmd, "cmd-8") == 0 )
  249. {
  250. if( strlen(g_szDaiKinAlarmMsg) > 0 )
  251. {
  252. for( int i = 0; i < atoi(chCmdLen); i++ )
  253. {
  254. chMsg[i] = g_szDaiKinDigitalMsg[sizeof(CHILD) + atoi(chAdrBeginPos) + i];
  255. }
  256. }
  257. }
  258. else if( strcmp(chRs232cmd, "cmd-9") == 0 )
  259. {
  260. if( strlen(g_szDaiKinDigitalMsg) > 0 )
  261. {
  262. for( int i = 0; i < atoi(chCmdLen); i++ )
  263. {
  264. chMsg[i] = g_szDaiKinAlarmMsg[sizeof(CHILD) + atoi(chAdrBeginPos) + i];
  265. }
  266. }
  267. }
  268. else if( strcmp(chRs232cmd, "cmd-11") == 0 ||
  269. strcmp(chRs232cmd, "cmd-12") == 0 ||
  270. strcmp(chRs232cmd, "cmd-13") == 0 ||
  271. strcmp(chRs232cmd, "cmd-14") == 0 )
  272. {
  273. if( strlen(g_szDaiKinGetParamMsg) > 0 )
  274. {
  275. for( int i = 0; i < atoi(chCmdLen); i++ )
  276. {
  277. chMsg[i] = g_szDaiKinGetParamMsg[sizeof(CHILD) + atoi(chAdrBeginPos) + i];
  278. }
  279. }
  280. }
  281. if( strlen(chMsg) == 0 ) return -1;
  282. nRet = DaiKinSingleResponseData(chDevUid, iVarID, chMsg, byDataFlag, nCid2 );
  283. return nRet;
  284. }
  285. int DaiKinSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80], char byDataFlag, int nCid2)
  286. {
  287. int nDeviceIndex = -1, nVarIndex = -1;
  288. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  289. if( bFind == FALSE ) return -1;
  290. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  291. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  292. EnterCriticalSection(&g_csDaiKinReadOneData);
  293. pDev->m_dwOnlineTick = GetTickCount();
  294. LeaveCriticalSection(&g_csDaiKinReadOneData);
  295. DWORD nValue = 0;
  296. WORD wdValue = 0;
  297. double fCoef = pBaseVar->m_dbCoefficient;
  298. int nDataLen = pBaseVar->m_iDataLen;
  299. int nVarItemID = pBaseVar->m_nVarItemID;
  300. if( nVarItemID > 0 )//BIT位变量
  301. {
  302. int nStartBit = pBaseVar->m_nStartBit;
  303. int nEndBit = pBaseVar->m_nEndBit;
  304. int nStartReg, nEndReg;
  305. if( nEndBit - nStartBit < 7 )// 一般告警状态会这样子定义,Modbus Rtu 码暂时没有碰到,没有经过调试,待测试
  306. {
  307. if( nDataLen == 1 )
  308. {
  309. char chBuffer[8] = {0};
  310. memset(chBuffer, 0, sizeof(chBuffer));
  311. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[0 + 1]) & 0x00FF );
  312. itoa(nValue, chBuffer, 2);
  313. CString strTemp;
  314. switch( nEndBit - nStartBit )
  315. {
  316. case 0:
  317. strTemp.Format("%c", chBuffer[nStartBit]);
  318. break;
  319. case 1:
  320. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  321. break;
  322. case 2:
  323. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  324. break;
  325. case 3:
  326. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  327. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  328. break;
  329. case 4:
  330. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  331. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  332. break;
  333. case 5:
  334. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  335. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  336. break;
  337. case 6:
  338. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  339. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  340. break;
  341. }
  342. nValue = atoi(strTemp);
  343. }
  344. else if( nDataLen == 2 )
  345. {
  346. char chBuffer[16] = {0};
  347. memset(chBuffer, 0, sizeof(chBuffer));
  348. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  349. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  350. itoa(nValue, chBuffer, 2);
  351. CString strTemp;
  352. switch( nEndBit - nStartBit )
  353. {
  354. case 0:
  355. strTemp.Format("%c", chBuffer[nStartBit]);
  356. break;
  357. case 1:
  358. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  359. break;
  360. case 2:
  361. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  362. break;
  363. case 3:
  364. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  365. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  366. break;
  367. case 4:
  368. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  369. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  370. break;
  371. case 5:
  372. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  373. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  374. break;
  375. case 6:
  376. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  377. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  378. break;
  379. }
  380. nValue = atoi(strTemp);
  381. }
  382. }
  383. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  384. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  385. {
  386. nStartReg = (int)nStartBit / 8;
  387. nEndReg = (int)(nEndBit + 1) / 8;
  388. nValue = (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF );
  389. }
  390. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  391. {
  392. nStartReg = (int)nStartBit / 8;
  393. nEndReg = (int)(nEndBit + 1) / 8;
  394. nValue = ( ( (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF ) ) << 8 ) |
  395. ( ( (AsciiToBYTE(chMsg[nStartReg + 2]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 3]) & 0x00FF ) ) & 0x0000FFFF );
  396. }
  397. EnterCriticalSection( &g_csDaiKinReadOneData );
  398. pBaseVar->m_dbData = (double)(nValue * fCoef);
  399. if( pBaseVar->m_nRearm != 0 )
  400. {
  401. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  402. }
  403. LeaveCriticalSection( &g_csDaiKinReadOneData );
  404. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  405. }
  406. else
  407. {
  408. // 联合类型变量做特殊处理
  409. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  410. {
  411. if( nDataLen == 2 )
  412. {
  413. //int类型
  414. union __UNION_VAR_INT{
  415. char ch[2];
  416. int value;
  417. }unionVarInt;
  418. unionVarInt.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  419. unionVarInt.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  420. EnterCriticalSection( &g_csDaiKinReadOneData );
  421. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  422. if( pBaseVar->m_nRearm != 0 )
  423. {
  424. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  425. }
  426. LeaveCriticalSection( &g_csDaiKinReadOneData );
  427. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  428. }
  429. else if( nDataLen == 4 )
  430. {
  431. //float类型
  432. union __UNION_VAR_FLOAT{
  433. char ch[4];
  434. float value;
  435. }unionVarFloat;
  436. unionVarFloat.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  437. unionVarFloat.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  438. unionVarFloat.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  439. unionVarFloat.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  440. EnterCriticalSection( &g_csDaiKinReadOneData );
  441. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  442. if( pBaseVar->m_nRearm != 0 )
  443. {
  444. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  445. }
  446. LeaveCriticalSection( &g_csDaiKinReadOneData );
  447. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  448. }
  449. else if( nDataLen == 8 )
  450. {
  451. //double类型
  452. union __UNION_VAR_DOUBLE{
  453. char ch[8];
  454. double value;
  455. }unionVarDouble;
  456. unionVarDouble.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  457. unionVarDouble.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  458. unionVarDouble.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  459. unionVarDouble.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  460. unionVarDouble.ch[4] = (AsciiToBYTE(chMsg[8]) << 4) | (AsciiToBYTE(chMsg[9]) & 0x00FF);
  461. unionVarDouble.ch[5] = (AsciiToBYTE(chMsg[10]) << 4) | (AsciiToBYTE(chMsg[11]) & 0x00FF);
  462. unionVarDouble.ch[6] = (AsciiToBYTE(chMsg[12]) << 4) | (AsciiToBYTE(chMsg[13]) & 0x00FF);
  463. unionVarDouble.ch[7] = (AsciiToBYTE(chMsg[14]) << 4) | (AsciiToBYTE(chMsg[15]) & 0x00FF);
  464. EnterCriticalSection( &g_csDaiKinReadOneData );
  465. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  466. if( pBaseVar->m_nRearm != 0 )
  467. {
  468. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  469. }
  470. LeaveCriticalSection( &g_csDaiKinReadOneData );
  471. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  472. }
  473. }
  474. else
  475. {
  476. switch( nDataLen )
  477. {
  478. case 1:
  479. nValue = AsciiToBYTE(chMsg[0]) << 4;
  480. break;
  481. case 2:
  482. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  483. break;
  484. case 3: // 一般为字符串类型,不作处理
  485. break;
  486. case 4: // 保留,暂时不知道什么处理
  487. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  488. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  489. break;
  490. case 5: // 一般为字符串类型,不作处理
  491. break;
  492. case 6: // 一般为字符串类型,不作处理
  493. break;
  494. case 7: // 一般为字符串类型,不作处理
  495. break;
  496. case 8: // 保留,暂时没有用到
  497. break;
  498. }
  499. EnterCriticalSection( &g_csDaiKinReadOneData );
  500. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  501. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  502. {
  503. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  504. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  505. pBaseVar->m_dbData = nValue *
  506. fCoef *
  507. fScale;
  508. }
  509. else
  510. {
  511. pBaseVar->m_dbData = (double)(nValue * fCoef);
  512. }
  513. if( pBaseVar->m_nRearm != 0 )
  514. {
  515. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  516. }
  517. //g_RtuReadOneData.dbData = (double)(nValue * fCoef);
  518. LeaveCriticalSection( &g_csDaiKinReadOneData );
  519. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_ParadigmReadOneData.dbData);
  520. }
  521. }
  522. // g_RtuReadOneData.iStatus = GetVarstatus(g_RtuReadOneData.dbData, g_RtuReadOneData.iUpperlimit, g_RtuReadOneData.iLowerlimit, g_RtuReadOneData.iNormalstate);
  523. EnterCriticalSection(&g_csDaiKinReadOneData);
  524. pDev->m_dwOnlineTick = GetTickCount();
  525. LeaveCriticalSection(&g_csDaiKinReadOneData);
  526. return 0;
  527. }
  528. void UnInitDaiKinDll()
  529. {
  530. if (g_hDaiKinLibModule != NULL && ( NULL != pDaiKin_DLLUnInit ))
  531. {
  532. pDaiKin_DLLUnInit();
  533. }
  534. DeleteCriticalSection( &g_csDaiKinReadOneData );
  535. if (g_hDaiKinLibModule!= NULL)
  536. {
  537. AfxFreeLibrary(g_hDaiKinLibModule);
  538. g_hDaiKinLibModule = NULL;
  539. }
  540. }
  541. int GetDaiKinFromIni(char chCmd[32], int &nVer, int &nCid2,
  542. char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID], char chDataLen[MAX_ID])
  543. {
  544. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  545. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  546. CHAR strCid2[4] ="";
  547. CHAR strVer[10]={0};
  548. CHAR strParaSum[4] = {0};
  549. CHAR strParaIndex[4] = {0};
  550. wsprintf(strFile, "%s\\config\\DaiKin.ini", g_strDirectory);
  551. //模块版本
  552. GetPrivateProfileString("VER", "Ver", "", strVer, sizeof(strVer), strFile);
  553. //命令模块
  554. GetPrivateProfileString(chCmd,"cmdModule", "", strValue, sizeof(strValue), strFile);
  555. //命令ID的参数索引
  556. GetPrivateProfileString(chCmd, "Para_Index", "", strParaIndex, sizeof(strParaIndex), strFile);
  557. //命令长度
  558. GetPrivateProfileString(chCmd, "cmdlen", "", chCmdLen, sizeof(chCmdLen), strFile);
  559. //命令的CID2
  560. GetPrivateProfileString(chCmd, "Cid2", "", strCid2, sizeof(strCid2), strFile);
  561. //模块命令参数的数量
  562. GetPrivateProfileString(strValue, "ParaSum", "", strParaSum, sizeof(strParaSum), strFile);
  563. //数据总长度
  564. GetPrivateProfileString(strValue, "datalen", "", chDataLen, sizeof(chDataLen), strFile);
  565. //读:起始位置,写:设置命令
  566. char ParaIndex[12]={0};
  567. strcpy(ParaIndex, "para");
  568. strcat(ParaIndex, strParaIndex);
  569. GetPrivateProfileString(strValue, ParaIndex, "", chAdrBeginPos, sizeof(chAdrBeginPos), strFile);
  570. nCid2 = atoi(strCid2);
  571. nVer = atoi(strVer);
  572. return 0;
  573. }
  574. int RequestDaiKinWrData(char chDevUid[20], int iVarID, double data, char chRs232cmd[32])//用于请求写命令操作
  575. {
  576. int nDeviceIndex = -1, nVarIndex = -1;
  577. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  578. if( bFind == FALSE ) return -1;
  579. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  580. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  581. if( strcmp(chRs232cmd, "cmd-15") == 0 ||
  582. strcmp(chRs232cmd, "cmd-16") == 0 ||
  583. strcmp(chRs232cmd, "cmd-17") == 0 ||
  584. strcmp(chRs232cmd, "cmd-18") == 0 ||
  585. strcmp(chRs232cmd, "cmd-19") == 0 ||
  586. strcmp(chRs232cmd, "cmd-20") == 0 ) //设置参数
  587. {
  588. char chMsg[80] = {0};
  589. int nCid1 = 0x60;
  590. int nCid2;
  591. WORD wLenId = 0x00;//INFO字节长度
  592. BYTE byCmdType = 0x00;//命令类型
  593. BYTE byCmdId = 0x00;
  594. char byDataFlag = 0;
  595. char chAdrBeginPos[MAX_ID] = {0};
  596. char chCmdLen[MAX_ID] = {0};
  597. char chDataLen[MAX_ID] = {0};
  598. int nVer;
  599. GetDaiKinFromIni((char*)(LPCTSTR)chRs232cmd,
  600. nVer,
  601. nCid2,
  602. chAdrBeginPos,
  603. chCmdLen,
  604. chDataLen);
  605. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  606. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  607. wsprintf(strFile, "%s\\config\\DaiKin.ini", g_strDirectory);
  608. //模块版本
  609. GetPrivateProfileString(chRs232cmd,"cmdType", "", strValue, sizeof(strValue), strFile);
  610. if( pDaiKin_DLLSetParam( pDev->m_iPort,
  611. pDev->m_iDevideaddr,
  612. nVer,
  613. nCid1,
  614. nCid2,
  615. atoi(chCmdLen),
  616. atoi(strValue),
  617. (int)data ) == 0 )
  618. {
  619. return 0;
  620. }
  621. else
  622. {
  623. return -1;
  624. }
  625. }
  626. else if( strcmp(chRs232cmd, "cmd-21") == 0 ) //控制空调运转
  627. {
  628. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  629. pDev->m_iDevideaddr, SET_RUN, 0, (int)data ) == 0 )
  630. {
  631. return 0;
  632. }
  633. else
  634. {
  635. return -1;
  636. }
  637. }
  638. else if( strcmp(chRs232cmd, "cmd-22") == 0 ) //控制运转模式
  639. {
  640. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  641. pDev->m_iDevideaddr, SET_RUN_MODE, 0, (int)data ) == 0 )
  642. {
  643. return 0;
  644. }
  645. else
  646. {
  647. return -1;
  648. }
  649. }
  650. else if( strcmp(chRs232cmd, "cmd-23") == 0 ) //设定制冷温度
  651. {
  652. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  653. pDev->m_iDevideaddr, SET_TEMP, 0, (int)data ) == 0 )
  654. {
  655. return 0;
  656. }
  657. else
  658. {
  659. return -1;
  660. }
  661. }
  662. else if( strcmp(chRs232cmd, "cmd-24") == 0 ) //设定制热温度
  663. {
  664. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  665. pDev->m_iDevideaddr, SET_TEMP, 1, (int)data ) == 0 )
  666. {
  667. return 0;
  668. }
  669. else
  670. {
  671. return -1;
  672. }
  673. }
  674. else if( strcmp(chRs232cmd, "cmd-25") == 0 ) //设定制热风扇1风量
  675. {
  676. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  677. pDev->m_iDevideaddr, SET_FAN, 0, (int)data ) == 0 )
  678. {
  679. return 0;
  680. }
  681. else
  682. {
  683. return -1;
  684. }
  685. }
  686. else if( strcmp(chRs232cmd, "cmd-26") == 0 ) //设定制热风扇2风量
  687. {
  688. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  689. pDev->m_iDevideaddr, SET_FAN, 1, (int)data ) == 0 )
  690. {
  691. return 0;
  692. }
  693. else
  694. {
  695. return -1;
  696. }
  697. }
  698. else if( strcmp(chRs232cmd, "cmd-27") == 0 ) //设定制冷风扇1风量
  699. {
  700. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  701. pDev->m_iDevideaddr, SET_FAN, 2, (int)data ) == 0 )
  702. {
  703. return 0;
  704. }
  705. else
  706. {
  707. return -1;
  708. }
  709. }
  710. else if( strcmp(chRs232cmd, "cmd-28") == 0 ) //设定制冷风扇2风量
  711. {
  712. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  713. pDev->m_iDevideaddr, SET_FAN, 3, (int)data ) == 0 )
  714. {
  715. return 0;
  716. }
  717. else
  718. {
  719. return -1;
  720. }
  721. }
  722. return 0;
  723. }