Daikin.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. #include "stdafx.h"
  2. #include "Daikin.h"
  3. CDaiKin::CDaiKin(char *szPath,char *szIniName,int nCommPort,int nAddr,int nRate,int nDataBit,int nStopBit,int nParity,int nInterval)
  4. {
  5. #if IS_USE_READMSG_CS
  6. InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
  7. #endif
  8. MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  9. for( int i = 0; i < 9; i++ )
  10. {
  11. memset(m_szDaKin_400Msg[i], 0, sizeof(m_szDaKin_400Msg[i]));
  12. m_devOnline[i] = TRUE;
  13. m_dwOnlineTick[i] = 0;
  14. }
  15. }
  16. CDaiKin::~CDaiKin()
  17. {
  18. #if IS_USE_READMSG_CS
  19. DeleteCriticalSection( &m_csReadMsg );
  20. #endif
  21. MTVERIFY( CloseHandle( m_hSemComm ) );
  22. CloseComm();
  23. }
  24. BOOL CDaiKin::DaiKinOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  25. {
  26. BOOL bResult = FALSE;
  27. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  28. return bResult;
  29. }
  30. int CDaiKin::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
  31. {
  32. CHAR szFile[MAX_PATH + 1] = "";
  33. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  34. IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  35. GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile);
  36. szDataType[strlen(szDataType)] = '\0';
  37. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  38. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  39. iSBit = GetPrivateProfileInt(szCmd, "StaBit", 0, szFile);//从配置文件中取值
  40. iEBit = GetPrivateProfileInt(szCmd, "EndBit", 0, szFile);
  41. return 0;
  42. }
  43. // 发送读取设备参数请求
  44. int CDaiKin::SendReadRequest(
  45. char *szPath,
  46. char *szIniName,
  47. int nCommPort,
  48. int nAddr,
  49. char *szCmd,
  50. char *szMsg,
  51. int nReversed1,
  52. int nReversed2,
  53. int nReversed3,
  54. int nReversed4,
  55. int nReversed5,
  56. float fReversed1,
  57. float fReversed2,
  58. float fReversed3,
  59. char *szReversed1,
  60. char *szReversed2,
  61. char *szReversed3,
  62. char *szReversed4,
  63. char *szReversed5 )
  64. {
  65. CCommProcess *pComm = FindComm(nCommPort);
  66. if( pComm == NULL ) return -1;
  67. int nRet = -1;
  68. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  69. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  70. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  71. int iCmd = atoi(szCmd + 4);
  72. if( (strlen(m_szDaKin_400Msg[nAddr -1]) == 0 && (iCmd >= 5 && iCmd <= 15)) || iCmd == 5)
  73. {
  74. nRet = GetDeviceParam(nAddr, pComm,szCmd, IniSendCMD, IniSendlen);
  75. if( nRet != 0 ) return nRet;
  76. }
  77. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  78. m_devOnline[nAddr - 1] = FALSE;
  79. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  80. m_devOnline[nAddr - 1] = TRUE;
  81. if( m_devOnline[nAddr - 1] == FALSE )
  82. return -1;
  83. nRet = GetDaiKin_400VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType, iSBit, iEBit);
  84. //LOG4C((LOG_NOTICE, "CDaiKin::SendSetReuest szCmd= %s, szMsg = %s", szCmd, szMsg));
  85. return nRet;
  86. }
  87. void CDaiKin::BitDataProcess(const BYTE &bySour, char *szRecv,int &StaBit,int &EndBit)
  88. {
  89. int ByteBit = 0;
  90. int sum = bySour - 48;
  91. switch(StaBit)
  92. {
  93. case 0:
  94. ByteBit = sum & 0x01;
  95. break;
  96. case 1:
  97. ByteBit = (sum & 0x02) / 0x02 ;
  98. break;
  99. case 2:
  100. ByteBit = (sum & 0x04) / 0x04 ;
  101. break;
  102. case 3:
  103. ByteBit = (sum & 0x08) / 0x08 ;
  104. break;
  105. case 4:
  106. ByteBit = (sum & 0x10) / 0x10 ;
  107. break;
  108. case 5:
  109. ByteBit = (sum & 0x20) / 0x20 ;
  110. break;
  111. case 6:
  112. ByteBit = (sum & 0x40) / 0x40 ;
  113. break;
  114. case 7:
  115. ByteBit = (sum & 0x80) / 0x80;
  116. break;
  117. }
  118. sprintf(szRecv,"%d",ByteBit);
  119. }
  120. int CDaiKin::GetDaiKin_400VarMsg( int nAddr,char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
  121. {
  122. #if IS_USE_READMSG_CS
  123. EnterCriticalSection( &m_csReadMsg );
  124. #endif
  125. int i = 0;
  126. if ( strcmp(szType, "BIT") == 0 )
  127. {
  128. char szConvMsg[2] = {0};
  129. memcpy(szMsg, m_szDaKin_400Msg[nAddr -1] + nIndex,nLen);
  130. BitDataProcess((BYTE)szMsg[0],szConvMsg,startBit,endBit);
  131. memset(szMsg,0,strlen(szMsg));
  132. memcpy(szMsg,szConvMsg,2);
  133. }else
  134. DataConversion(szType, m_szDaKin_400Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit);
  135. #if IS_USE_READMSG_CS
  136. LeaveCriticalSection(&m_csReadMsg);
  137. #endif
  138. return 0;
  139. }
  140. int CDaiKin::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD, const int &IniSendlen)
  141. {
  142. int nRet = -1;
  143. nRet = Send_ReadDeviceData(nAddr,pComm,szCmd,IniSendCMD,IniSendlen);
  144. if( nRet != 0 )
  145. {
  146. LOG4C((LOG_NOTICE,"DAKIN Write Error"));
  147. return nRet; // 串口忙
  148. }
  149. nRet = Recv_ReadDeviceData(nAddr, pComm, szCmd);
  150. return nRet;
  151. }
  152. int CDaiKin::SetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *szSetMsg,char *szRecvMsg,char *IniSendCMD, const int &IniSendlen)
  153. {
  154. int nRet = -1;
  155. nRet = Send_WriteDeviceData(nAddr, pComm, szCmd, szSetMsg,IniSendCMD, IniSendlen );
  156. if( nRet != 0 )
  157. {
  158. LOG4C((LOG_NOTICE,"DAKIN Write Error"));
  159. return nRet; // 串口忙
  160. }
  161. nRet = Recv_WriteDeviceData(nAddr, pComm, szCmd, szRecvMsg);
  162. return nRet;
  163. }
  164. // 发送设置设备参数请求
  165. int CDaiKin::SendSetReuest(
  166. char *szPath,
  167. char *szIniName,
  168. int nCommPort,
  169. int nAddr,
  170. char *szCmd,
  171. char *szSetMsg,
  172. int nReversed1,
  173. int nReversed2,
  174. int nReversed3,
  175. int nReversed4,
  176. int nReversed5,
  177. float fReversed1,
  178. float fReversed2,
  179. float fReversed3,
  180. char *szReversed1,
  181. char *szReversed2,
  182. char *szReversed3,
  183. char *szReversed4,
  184. char *szReversed5
  185. )
  186. {
  187. CCommProcess *pComm = FindComm(nCommPort);
  188. if( pComm == NULL ) return -1;
  189. int nRet = -1;
  190. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  191. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  192. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  193. char szRecvMsg[10] = {0};
  194. SetDeviceParam(nAddr, pComm, szCmd, szSetMsg,szRecvMsg,IniSendCMD,IniSendlen);
  195. return 0;
  196. }
  197. int CDaiKin::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd,const char *IniSendCMD,const int &IniSendlen)
  198. {
  199. int nRet = -1;
  200. #if DEBUG_DAIKIN
  201. int iSendLen = 0;
  202. BYTE byArySend[60] = {0};
  203. byArySend[0] = 0x7E;
  204. byArySend[1] = 0x32;
  205. byArySend[2] = 0x30;
  206. byArySend[3] = ByteToAscii((nAddr>>4) & 0x0f);
  207. byArySend[4] = ByteToAscii(nAddr & 0x0f);
  208. //Cid1
  209. byArySend[5] = 0x36;
  210. byArySend[6] = 0x30;
  211. //Cid2
  212. byArySend[7] = 0x45;
  213. byArySend[8] = 0x30;
  214. // length;
  215. byArySend[9] = 0x41;
  216. byArySend[10] = 0x30;
  217. byArySend[11] = 0x30;
  218. byArySend[12] = 0x36;
  219. // STX;
  220. byArySend[13] = 0x34;
  221. byArySend[14] = 0x30;
  222. byArySend[15] = 0x30;
  223. // Line id;
  224. byArySend[16] = nAddr + 47;
  225. // Unit No;
  226. #if 0
  227. int UnitNo = nAddr - 1;
  228. byArySend[17] = ByteToAscii((UnitNo>>4) & 0x0f);
  229. byArySend[18] = ByteToAscii(UnitNo & 0x0f);
  230. #else
  231. byArySend[17] = 0x30;
  232. byArySend[18] = 0x30;
  233. #endif
  234. // CHECK;
  235. char chChkSum[5] = {0};
  236. GetCheckSum((char *)byArySend + 1, chChkSum, 18 );
  237. memcpy(byArySend + 19, chChkSum, 4);
  238. // EOI;
  239. byArySend[23] = 0x0D;
  240. iSendLen = 24;
  241. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  242. {
  243. ResetEvent( m_hSemComm );
  244. int nResult = pComm->Write(byArySend, iSendLen);
  245. if( nResult != iSendLen )
  246. {
  247. SetEvent( m_hSemComm );
  248. LOG4C((LOG_NOTICE,"DAKIN Write长度错误"));
  249. return EER_CODE_COM_WRITE_DATA;
  250. }
  251. }
  252. else
  253. {
  254. LOG4C((LOG_NOTICE,"DAKIN 无法获取信号控制"));
  255. return ERR_CODE_COM_BUSY;
  256. }
  257. #endif
  258. return 0;
  259. }
  260. int CDaiKin::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
  261. {
  262. #if DEBUG_DAIKIN
  263. BYTE *byAryRecv = new BYTE[90];
  264. ZeroMemory(byAryRecv, 90);
  265. int iRecvLen = pComm->Read(byAryRecv,90);
  266. if( iRecvLen <= 0)
  267. {
  268. SetEvent( m_hSemComm );
  269. if( byAryRecv != NULL)
  270. {
  271. delete[] byAryRecv;
  272. byAryRecv = NULL;
  273. }
  274. LOG4C((LOG_NOTICE, "DAKIN 串口没有读到数据"));
  275. return ERR_CODE_COM_READ_NO_DATA;
  276. }
  277. if (!ChkSumCheck( (char*)byAryRecv, iRecvLen))
  278. {
  279. SetEvent(m_hSemComm);
  280. if (byAryRecv != NULL)
  281. {
  282. delete [] byAryRecv;
  283. byAryRecv = NULL;
  284. }
  285. LOG4C((LOG_NOTICE, "DAKIN 校验码校验出错"));
  286. return ERR_CODE_RTN_CHKSUM_ERROR;
  287. }
  288. if (RtnCheck( (char*)byAryRecv) != 0)
  289. {
  290. SetEvent(m_hSemComm);
  291. if (byAryRecv != NULL)
  292. {
  293. delete [] byAryRecv;
  294. byAryRecv = NULL;
  295. }
  296. LOG4C((LOG_NOTICE, "DAKIN RTN校验出错"));
  297. return ERR_CODE_RTN_CHKSUM;
  298. }
  299. // copy ;
  300. memcpy(m_szDaKin_400Msg[nAddr -1], byAryRecv, iRecvLen);
  301. m_dwOnlineTick[nAddr - 1] = GetTickCount();
  302. SetEvent( m_hSemComm );
  303. if( byAryRecv != NULL)
  304. {
  305. delete[] byAryRecv;
  306. byAryRecv = NULL;
  307. }
  308. #else
  309. SimulationCommData(nAddr);
  310. #endif
  311. return 0;
  312. }
  313. int CDaiKin::Send_WriteDeviceData( int nAddr,CCommProcess *pComm, char *szCmd, char *szSetMsg,char *IniSendCMD, const int &IniSendlen)
  314. {
  315. #if DEBUG_DAIKIN
  316. /************************************************************************/
  317. int iSendLen = 0;
  318. char chLength[4] = {0};
  319. char chChkSum[5] = {0};
  320. BYTE byArySend[120] = {0};
  321. // SOI;
  322. byArySend[0] = 0x7E;
  323. // Ver;
  324. byArySend[1] = 0x32;
  325. byArySend[2] = 0x30;
  326. // Addr;
  327. byArySend[3] = ByteToAscii((nAddr>>4) & 0x0f);
  328. byArySend[4] = ByteToAscii(nAddr & 0x0f);
  329. // Cid1;
  330. byArySend[5] = 0x36;
  331. byArySend[6] = 0x30;
  332. // Cid2;
  333. memcpy(byArySend + 7, IniSendCMD, IniSendlen);
  334. if ( (strcmp(szCmd, "cmd-1") == 0)) // 开关机;
  335. {
  336. // Length;
  337. byArySend[9] = 0x45;
  338. byArySend[10] = 0x30;
  339. byArySend[11] = 0x30;
  340. byArySend[12] = 0x32;
  341. int iCtrl = atoi(szSetMsg);
  342. memset(szSetMsg,0,strlen(szSetMsg));
  343. itoa(iCtrl,szSetMsg,16);
  344. for (int i(0); i < 2 ; i++)
  345. lowcase2uppcase((BYTE &)szSetMsg[i]);
  346. memcpy(byArySend + 13, szSetMsg, 2);
  347. GetCheckSum((char *)byArySend + 1, chChkSum, 14 );
  348. memcpy(byArySend + 15, chChkSum, 4);
  349. byArySend[19] = 0x0D;
  350. iSendLen = 20;
  351. LOG4C((LOG_NOTICE,"DAKIN cmd-1=空调开关控制:%s",szSetMsg));
  352. }
  353. else if ( (strcmp(szCmd, "cmd-2") == 0)) // 设置风量;
  354. {
  355. char szTemp[10] = {0};
  356. int iRealLen = strlen(szSetMsg);
  357. memcpy(szTemp,szSetMsg,iRealLen);
  358. if (iRealLen > 4)
  359. {
  360. LOG4C((LOG_NOTICE,"DAKIN 非法指令长度:%s",szSetMsg));
  361. return -1;
  362. }
  363. // 制热模式下,制冷为00;
  364. if (iRealLen == 2)
  365. {
  366. memcpy(szSetMsg + 2, szTemp,iRealLen);
  367. szSetMsg[0] = 48;
  368. szSetMsg[1] = 48;
  369. }else if (iRealLen == 1)
  370. {
  371. memcpy(szSetMsg + 3, szTemp,iRealLen);
  372. szSetMsg[0] = 48;
  373. szSetMsg[1] = 48;
  374. szSetMsg[2] = 48;
  375. }
  376. // Length;
  377. byArySend[9] = 0x38;
  378. byArySend[10] = 0x30;
  379. byArySend[11] = 0x30;
  380. byArySend[12] = 0x38;
  381. // STX;
  382. byArySend[13] = 0x36;
  383. byArySend[14] = 0x34;
  384. byArySend[15] = 0x30;
  385. // 接口卡LINEID;
  386. byArySend[16] = nAddr -1 + 48;
  387. // 设置值;
  388. memcpy(byArySend + 17, szSetMsg, 4);
  389. GetCheckSum((char *)byArySend + 1, chChkSum, 20 );
  390. memcpy(byArySend + 21, chChkSum, 4);
  391. // EOL;
  392. byArySend[25] = 0x0D;
  393. iSendLen = 26;
  394. LOG4C((LOG_NOTICE,"DAKIN cmd-2=空调风量设置:%s",szSetMsg));
  395. }
  396. else if ( (strcmp(szCmd, "cmd-3") == 0)) // 设置温度;
  397. {
  398. // Length;
  399. byArySend[9] = 0x36;
  400. byArySend[10] = 0x30;
  401. byArySend[11] = 0x30;
  402. byArySend[12] = 0x41;
  403. // STX;
  404. byArySend[13] = 0x36;
  405. byArySend[14] = 0x33;
  406. byArySend[15] = 0x30;
  407. // Line No;
  408. byArySend[16] = nAddr -1 + 48;
  409. // 设置的值;
  410. char szTemp[10] = {0};
  411. int iRealLen = strlen(szSetMsg);
  412. memcpy(szTemp,szSetMsg,iRealLen);
  413. if (iRealLen > 6)
  414. {
  415. LOG4C((LOG_NOTICE,"DAKIN 非法指令长度:%s",szSetMsg));
  416. return -1;
  417. }
  418. if (iRealLen == 5)
  419. {
  420. memcpy(szSetMsg + 1, szTemp,iRealLen);
  421. szSetMsg[0] = 48;
  422. }else if (iRealLen == 4)
  423. {
  424. memcpy(szSetMsg + 2, szTemp,iRealLen);
  425. szSetMsg[0] = 48;
  426. szSetMsg[1] = 48;
  427. }
  428. else if ( iRealLen == 3)
  429. {
  430. memcpy(szSetMsg + 3, szTemp,iRealLen);
  431. szSetMsg[0] = 48;
  432. szSetMsg[1] = 48;
  433. szSetMsg[2] = 48;
  434. }
  435. else if ( iRealLen == 2)
  436. {
  437. memcpy(szSetMsg + 4, szTemp,iRealLen);
  438. szSetMsg[0] = 48;
  439. szSetMsg[1] = 48;
  440. szSetMsg[2] = 48;
  441. szSetMsg[3] = 48;
  442. }
  443. else if ( iRealLen == 1)
  444. {
  445. memcpy(szSetMsg + 5, szTemp,iRealLen);
  446. szSetMsg[0] = 48;
  447. szSetMsg[1] = 48;
  448. szSetMsg[2] = 48;
  449. szSetMsg[3] = 48;
  450. szSetMsg[4] = 48;
  451. }
  452. else if ( iRealLen == 0)
  453. memcpy(szSetMsg , "000000", 6);
  454. memcpy(byArySend + 17, szSetMsg, 6);
  455. GetCheckSum((char *)byArySend + 1, chChkSum, 22 );
  456. memcpy(byArySend + 23, chChkSum, 4);
  457. byArySend[27] = 0x0D;
  458. iSendLen = 28;
  459. LOG4C((LOG_NOTICE,"DAKIN cmd-3=空调温度设置:%s",szSetMsg));
  460. }else if ( strcmp(szCmd, "cmd-4") == 0) // 设置运转模式;
  461. {
  462. // Length;
  463. byArySend[9] = 0x42;
  464. byArySend[10] = 0x30;
  465. byArySend[11] = 0x30;
  466. byArySend[12] = 0x35;
  467. // STX;
  468. byArySend[13] = 0x36;
  469. byArySend[14] = 0x32;
  470. byArySend[15] = 0x30;
  471. // 接口卡LINEID; 范围:0~2
  472. byArySend[16] = nAddr -1 + 48;
  473. // 设置值; 范围:0~7
  474. memcpy(byArySend + 17, szSetMsg, 1);
  475. GetCheckSum((char *)byArySend + 1, chChkSum, 17 );
  476. memcpy(byArySend + 18, chChkSum, 4);
  477. // EOI;
  478. byArySend[22] = 0x0D;
  479. iSendLen = 23;
  480. LOG4C((LOG_NOTICE,"DAKIN cmd-4=空调运转模式设置:%s",szSetMsg));
  481. }
  482. /************************************************************************/
  483. ResetEvent( m_hSemComm );
  484. // 读取上次未读的数据,确保空调缓存已无数据;
  485. BYTE byRecv[250] = {0};
  486. pComm->Read(byRecv, 250);
  487. Sleep(500);
  488. int nResult = pComm->Write(byArySend, iSendLen);
  489. if( nResult != iSendLen )
  490. {
  491. SetEvent( m_hSemComm );
  492. return EER_CODE_COM_WRITE_DATA;
  493. }
  494. Sleep(300);
  495. #endif
  496. return 0;
  497. }
  498. int CDaiKin::Recv_WriteDeviceData(int nAddr, CCommProcess *pComm, char *szCmd, char *szRecvMsg)
  499. {
  500. #if DEBUG_DAIKIN
  501. int nReceiveLen = 250;
  502. int nProcessLen = 0;
  503. int nReadLen = 0;
  504. char *pBuffer = new char[ nReceiveLen ];
  505. memset(pBuffer, 0, nReceiveLen);
  506. Sleep(200);
  507. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
  508. if( nReadLen <= 0)
  509. {
  510. SetEvent( m_hSemComm );
  511. if( pBuffer != NULL)
  512. {
  513. delete[] pBuffer;
  514. pBuffer = NULL;
  515. }
  516. LOG4C((LOG_NOTICE,"DAKIN 写 串口没有读到数据"));
  517. return ERR_CODE_COM_READ_NO_DATA;
  518. }
  519. #if 0
  520. if (!ChkSumCheck(pBuffer, strlen(pBuffer)))
  521. {
  522. SetEvent( m_hSemComm );
  523. if( pBuffer != NULL)
  524. {
  525. delete[] pBuffer;
  526. pBuffer = NULL;
  527. }
  528. LOG4C((LOG_NOTICE,"DAKIN 写 校检错误"));
  529. return ERR_CODE_DAIKIN_COM_CHKSUM_LOST;
  530. }
  531. if (!CheckLength(pBuffer))
  532. {
  533. SetEvent( m_hSemComm );
  534. if( pBuffer != NULL)
  535. {
  536. delete[] pBuffer;
  537. pBuffer = NULL;
  538. }
  539. LOG4C((LOG_NOTICE,"DAKIN 写 数据长度校检错误"));
  540. return ERR_CODE_DAIKIN_COM_VARLEN;
  541. }
  542. int nRet = RtnCheck(pBuffer);
  543. if(0 != nRet)
  544. {
  545. SetEvent( m_hSemComm );
  546. if( pBuffer != NULL)
  547. {
  548. delete[] pBuffer;
  549. pBuffer = NULL;
  550. }
  551. LOG4C((LOG_NOTICE,"DAKIN 写 返回码出错"));
  552. return nRet;
  553. }
  554. #endif
  555. // 设置串口等待事件为有信号;
  556. SetEvent( m_hSemComm );
  557. if( pBuffer != NULL)
  558. {
  559. delete[] pBuffer;
  560. pBuffer = NULL;
  561. }
  562. #else
  563. SimulationCommData(nAddr);
  564. #endif
  565. return 0;
  566. }
  567. void CDaiKin::SimulationCommData(int nAddr)
  568. {
  569. }
  570. WORD CDaiKin::RtnCheck(char Msg[VAR_MSG])
  571. {
  572. int len ,index;
  573. len = 2;
  574. index = 7;
  575. char ch[2];
  576. ch[0] = Msg[index];
  577. ch[1] = Msg[index+1];
  578. int RTN = atoi(ch);
  579. if ( RTN == 0)
  580. return 0;
  581. if (RTN==ERR_CODE_RTN_VER_ERROR)
  582. {
  583. return ERR_CODE_RTN_VER_ERROR;
  584. }
  585. else if (RTN==ERR_CODE_RTN_CHKSUM_ERROR)
  586. {
  587. return ERR_CODE_RTN_CHKSUM_ERROR;
  588. }
  589. else if (RTN==ERR_CODE_RTN_LCHKSUM_ERROR)
  590. {
  591. return ERR_CODE_RTN_LCHKSUM_ERROR;
  592. }
  593. else if (RTN==ERR_CODE_RTN_CID_ERROR)
  594. {
  595. return ERR_CODE_RTN_CID_ERROR;
  596. }
  597. else if (RTN==ERR_CODE_RTN_COMMAND_FORMAT)
  598. {
  599. return ERR_CODE_RTN_COMMAND_FORMAT;
  600. }
  601. else if (RTN==ERR_CODE_RTN_INVALID_DATA)
  602. {
  603. return ERR_CODE_RTN_INVALID_DATA;
  604. }
  605. else /*if (atoi(ch)==ERR_CID_RTN_NORMAL)*/
  606. {
  607. return ERR_CODE_RTN_OPERATE_FAIL;
  608. }
  609. }
  610. void CDaiKin::lowcase2uppcase(BYTE &btSrc)
  611. {
  612. if( btSrc >= 'a' && btSrc <= 'z' )
  613. btSrc = btSrc - 'a' + 'A';
  614. }
  615. void CDaiKin::GetCheckSum(char *szData, char *szCheck ,int nlen)
  616. {
  617. DWORD dwSum(0);
  618. for (int i = 0; i < nlen; i++)
  619. dwSum += szData[i];
  620. WORD iCompliment = dwSum;
  621. iCompliment = ~iCompliment;//取反
  622. iCompliment++;
  623. itoa(iCompliment, szCheck, 16);
  624. for (int i(0); i < 5 ; i++)
  625. lowcase2uppcase((BYTE &)szCheck[i]);
  626. }