DaiKinProcess.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. default:
  342. LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
  343. }
  344. nValue = atoi(strTemp);
  345. }
  346. else if( nDataLen == 2 )
  347. {
  348. char chBuffer[16] = {0};
  349. memset(chBuffer, 0, sizeof(chBuffer));
  350. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  351. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  352. itoa(nValue, chBuffer, 2);
  353. CString strTemp;
  354. switch( nEndBit - nStartBit )
  355. {
  356. case 0:
  357. strTemp.Format("%c", chBuffer[nStartBit]);
  358. break;
  359. case 1:
  360. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  361. break;
  362. case 2:
  363. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  364. break;
  365. case 3:
  366. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  367. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  368. break;
  369. case 4:
  370. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  371. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  372. break;
  373. case 5:
  374. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  375. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  376. break;
  377. case 6:
  378. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  379. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  380. break;
  381. default:
  382. LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
  383. }
  384. nValue = atoi(strTemp);
  385. }
  386. }
  387. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  388. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  389. {
  390. nStartReg = (int)nStartBit / 8;
  391. nEndReg = (int)(nEndBit + 1) / 8;
  392. nValue = (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF );
  393. }
  394. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  395. {
  396. nStartReg = (int)nStartBit / 8;
  397. nEndReg = (int)(nEndBit + 1) / 8;
  398. nValue = ( ( (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF ) ) << 8 ) |
  399. ( ( (AsciiToBYTE(chMsg[nStartReg + 2]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 3]) & 0x00FF ) ) & 0x0000FFFF );
  400. }
  401. EnterCriticalSection( &g_csDaiKinReadOneData );
  402. pBaseVar->m_dbData = (double)(nValue * fCoef);
  403. if( pBaseVar->m_nRearm != 0 )
  404. {
  405. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  406. }
  407. LeaveCriticalSection( &g_csDaiKinReadOneData );
  408. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  409. }
  410. else
  411. {
  412. // 联合类型变量做特殊处理
  413. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  414. {
  415. if( nDataLen == 2 )
  416. {
  417. //int类型
  418. union __UNION_VAR_INT{
  419. char ch[2];
  420. int value;
  421. }unionVarInt;
  422. unionVarInt.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  423. unionVarInt.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  424. EnterCriticalSection( &g_csDaiKinReadOneData );
  425. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  426. if( pBaseVar->m_nRearm != 0 )
  427. {
  428. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  429. }
  430. LeaveCriticalSection( &g_csDaiKinReadOneData );
  431. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  432. }
  433. else if( nDataLen == 4 )
  434. {
  435. //float类型
  436. union __UNION_VAR_FLOAT{
  437. char ch[4];
  438. float value;
  439. }unionVarFloat;
  440. unionVarFloat.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  441. unionVarFloat.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  442. unionVarFloat.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  443. unionVarFloat.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  444. EnterCriticalSection( &g_csDaiKinReadOneData );
  445. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  446. if( pBaseVar->m_nRearm != 0 )
  447. {
  448. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  449. }
  450. LeaveCriticalSection( &g_csDaiKinReadOneData );
  451. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  452. }
  453. else if( nDataLen == 8 )
  454. {
  455. //double类型
  456. union __UNION_VAR_DOUBLE{
  457. char ch[8];
  458. double value;
  459. }unionVarDouble;
  460. unionVarDouble.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  461. unionVarDouble.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  462. unionVarDouble.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  463. unionVarDouble.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  464. unionVarDouble.ch[4] = (AsciiToBYTE(chMsg[8]) << 4) | (AsciiToBYTE(chMsg[9]) & 0x00FF);
  465. unionVarDouble.ch[5] = (AsciiToBYTE(chMsg[10]) << 4) | (AsciiToBYTE(chMsg[11]) & 0x00FF);
  466. unionVarDouble.ch[6] = (AsciiToBYTE(chMsg[12]) << 4) | (AsciiToBYTE(chMsg[13]) & 0x00FF);
  467. unionVarDouble.ch[7] = (AsciiToBYTE(chMsg[14]) << 4) | (AsciiToBYTE(chMsg[15]) & 0x00FF);
  468. EnterCriticalSection( &g_csDaiKinReadOneData );
  469. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  470. if( pBaseVar->m_nRearm != 0 )
  471. {
  472. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  473. }
  474. LeaveCriticalSection( &g_csDaiKinReadOneData );
  475. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  476. }
  477. }
  478. else
  479. {
  480. switch( nDataLen )
  481. {
  482. case 1:
  483. nValue = AsciiToBYTE(chMsg[0]) << 4;
  484. break;
  485. case 2:
  486. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  487. break;
  488. case 3: // 一般为字符串类型,不作处理
  489. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  490. break;
  491. case 4: // 保留,暂时不知道什么处理
  492. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  493. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  494. break;
  495. case 5: // 一般为字符串类型,不作处理
  496. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  497. break;
  498. case 6: // 一般为字符串类型,不作处理
  499. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  500. break;
  501. case 7: // 一般为字符串类型,不作处理
  502. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  503. break;
  504. case 8: // 保留,暂时没有用到
  505. LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
  506. break;
  507. }
  508. EnterCriticalSection( &g_csDaiKinReadOneData );
  509. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  510. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  511. {
  512. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  513. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  514. pBaseVar->m_dbData = nValue *
  515. fCoef *
  516. fScale;
  517. }
  518. else
  519. {
  520. pBaseVar->m_dbData = (double)(nValue * fCoef);
  521. }
  522. if( pBaseVar->m_nRearm != 0 )
  523. {
  524. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  525. }
  526. //g_RtuReadOneData.dbData = (double)(nValue * fCoef);
  527. LeaveCriticalSection( &g_csDaiKinReadOneData );
  528. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_ParadigmReadOneData.dbData);
  529. }
  530. }
  531. // g_RtuReadOneData.iStatus = GetVarstatus(g_RtuReadOneData.dbData, g_RtuReadOneData.iUpperlimit, g_RtuReadOneData.iLowerlimit, g_RtuReadOneData.iNormalstate);
  532. EnterCriticalSection(&g_csDaiKinReadOneData);
  533. pDev->m_dwOnlineTick = GetTickCount();
  534. LeaveCriticalSection(&g_csDaiKinReadOneData);
  535. return 0;
  536. }
  537. void UnInitDaiKinDll()
  538. {
  539. if (g_hDaiKinLibModule != NULL && ( NULL != pDaiKin_DLLUnInit ))
  540. {
  541. pDaiKin_DLLUnInit();
  542. }
  543. DeleteCriticalSection( &g_csDaiKinReadOneData );
  544. if (g_hDaiKinLibModule!= NULL)
  545. {
  546. AfxFreeLibrary(g_hDaiKinLibModule);
  547. g_hDaiKinLibModule = NULL;
  548. }
  549. }
  550. int GetDaiKinFromIni(char chCmd[32], int &nVer, int &nCid2,
  551. char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID], char chDataLen[MAX_ID])
  552. {
  553. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  554. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  555. CHAR strCid2[4] ="";
  556. CHAR strVer[10]={0};
  557. CHAR strParaSum[4] = {0};
  558. CHAR strParaIndex[4] = {0};
  559. wsprintf(strFile, "%s\\config\\DaiKin.ini", g_strDirectory);
  560. //模块版本
  561. GetPrivateProfileString("VER", "Ver", "", strVer, sizeof(strVer), strFile);
  562. //命令模块
  563. GetPrivateProfileString(chCmd,"cmdModule", "", strValue, sizeof(strValue), strFile);
  564. //命令ID的参数索引
  565. GetPrivateProfileString(chCmd, "Para_Index", "", strParaIndex, sizeof(strParaIndex), strFile);
  566. //命令长度
  567. GetPrivateProfileString(chCmd, "cmdlen", "", chCmdLen, sizeof(chCmdLen), strFile);
  568. //命令的CID2
  569. GetPrivateProfileString(chCmd, "Cid2", "", strCid2, sizeof(strCid2), strFile);
  570. //模块命令参数的数量
  571. GetPrivateProfileString(strValue, "ParaSum", "", strParaSum, sizeof(strParaSum), strFile);
  572. //数据总长度
  573. GetPrivateProfileString(strValue, "datalen", "", chDataLen, sizeof(chDataLen), strFile);
  574. //读:起始位置,写:设置命令
  575. char ParaIndex[12]={0};
  576. strcpy(ParaIndex, "para");
  577. strcat(ParaIndex, strParaIndex);
  578. GetPrivateProfileString(strValue, ParaIndex, "", chAdrBeginPos, sizeof(chAdrBeginPos), strFile);
  579. nCid2 = atoi(strCid2);
  580. nVer = atoi(strVer);
  581. return 0;
  582. }
  583. int RequestDaiKinWrData(char chDevUid[20], int iVarID, double data, char chRs232cmd[32])//用于请求写命令操作
  584. {
  585. int nDeviceIndex = -1, nVarIndex = -1;
  586. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  587. if( bFind == FALSE ) return -1;
  588. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  589. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  590. if( strcmp(chRs232cmd, "cmd-15") == 0 ||
  591. strcmp(chRs232cmd, "cmd-16") == 0 ||
  592. strcmp(chRs232cmd, "cmd-17") == 0 ||
  593. strcmp(chRs232cmd, "cmd-18") == 0 ||
  594. strcmp(chRs232cmd, "cmd-19") == 0 ||
  595. strcmp(chRs232cmd, "cmd-20") == 0 ) //设置参数
  596. {
  597. char chMsg[80] = {0};
  598. int nCid1 = 0x60;
  599. int nCid2;
  600. WORD wLenId = 0x00;//INFO字节长度
  601. BYTE byCmdType = 0x00;//命令类型
  602. BYTE byCmdId = 0x00;
  603. char byDataFlag = 0;
  604. char chAdrBeginPos[MAX_ID] = {0};
  605. char chCmdLen[MAX_ID] = {0};
  606. char chDataLen[MAX_ID] = {0};
  607. int nVer;
  608. GetDaiKinFromIni((char*)(LPCTSTR)chRs232cmd,
  609. nVer,
  610. nCid2,
  611. chAdrBeginPos,
  612. chCmdLen,
  613. chDataLen);
  614. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  615. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  616. wsprintf(strFile, "%s\\config\\DaiKin.ini", g_strDirectory);
  617. //模块版本
  618. GetPrivateProfileString(chRs232cmd,"cmdType", "", strValue, sizeof(strValue), strFile);
  619. if( pDaiKin_DLLSetParam( pDev->m_iPort,
  620. pDev->m_iDevideaddr,
  621. nVer,
  622. nCid1,
  623. nCid2,
  624. atoi(chCmdLen),
  625. atoi(strValue),
  626. (int)data ) == 0 )
  627. {
  628. return 0;
  629. }
  630. else
  631. {
  632. return -1;
  633. }
  634. }
  635. else if( strcmp(chRs232cmd, "cmd-21") == 0 ) //控制空调运转
  636. {
  637. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  638. pDev->m_iDevideaddr, SET_RUN, 0, (int)data ) == 0 )
  639. {
  640. return 0;
  641. }
  642. else
  643. {
  644. return -1;
  645. }
  646. }
  647. else if( strcmp(chRs232cmd, "cmd-22") == 0 ) //控制运转模式
  648. {
  649. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  650. pDev->m_iDevideaddr, SET_RUN_MODE, 0, (int)data ) == 0 )
  651. {
  652. return 0;
  653. }
  654. else
  655. {
  656. return -1;
  657. }
  658. }
  659. else if( strcmp(chRs232cmd, "cmd-23") == 0 ) //设定制冷温度
  660. {
  661. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  662. pDev->m_iDevideaddr, SET_TEMP, 0, (int)data ) == 0 )
  663. {
  664. return 0;
  665. }
  666. else
  667. {
  668. return -1;
  669. }
  670. }
  671. else if( strcmp(chRs232cmd, "cmd-24") == 0 ) //设定制热温度
  672. {
  673. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  674. pDev->m_iDevideaddr, SET_TEMP, 1, (int)data ) == 0 )
  675. {
  676. return 0;
  677. }
  678. else
  679. {
  680. return -1;
  681. }
  682. }
  683. else if( strcmp(chRs232cmd, "cmd-25") == 0 ) //设定制热风扇1风量
  684. {
  685. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  686. pDev->m_iDevideaddr, SET_FAN, 0, (int)data ) == 0 )
  687. {
  688. return 0;
  689. }
  690. else
  691. {
  692. return -1;
  693. }
  694. }
  695. else if( strcmp(chRs232cmd, "cmd-26") == 0 ) //设定制热风扇2风量
  696. {
  697. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  698. pDev->m_iDevideaddr, SET_FAN, 1, (int)data ) == 0 )
  699. {
  700. return 0;
  701. }
  702. else
  703. {
  704. return -1;
  705. }
  706. }
  707. else if( strcmp(chRs232cmd, "cmd-27") == 0 ) //设定制冷风扇1风量
  708. {
  709. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  710. pDev->m_iDevideaddr, SET_FAN, 2, (int)data ) == 0 )
  711. {
  712. return 0;
  713. }
  714. else
  715. {
  716. return -1;
  717. }
  718. }
  719. else if( strcmp(chRs232cmd, "cmd-28") == 0 ) //设定制冷风扇2风量
  720. {
  721. if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
  722. pDev->m_iDevideaddr, SET_FAN, 3, (int)data ) == 0 )
  723. {
  724. return 0;
  725. }
  726. else
  727. {
  728. return -1;
  729. }
  730. }
  731. return 0;
  732. }