Daikin_Modify.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include "stdafx.h"
  2. #include "Daikin_Modify.h"
  3. DaiKin_Modify::DaiKin_Modify(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. DaiKin_Modify::~DaiKin_Modify()
  17. {
  18. #if IS_USE_READMSG_CS
  19. DeleteCriticalSection( &m_csReadMsg );
  20. #endif
  21. MTVERIFY( CloseHandle( m_hSemComm ) );
  22. CloseComm();
  23. }
  24. BOOL DaiKin_Modify::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 DaiKin_Modify::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", "-1", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  35. GetPrivateProfileString(szCmd, "type", "-1", szDataType, 10, szFile);
  36. szDataType[strlen(szDataType)] = '\0';
  37. nIndex = GetPrivateProfileInt(szCmd, "Index", -1, szFile);
  38. nLen = GetPrivateProfileInt(szCmd, "Len", -1, szFile);
  39. iSBit = GetPrivateProfileInt(szCmd, "StaBit", -1, szFile);//从配置文件中取值
  40. iEBit = GetPrivateProfileInt(szCmd, "EndBit", -1, szFile);
  41. return 0;
  42. }
  43. // 发送读取设备参数请求
  44. int DaiKin_Modify::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 DaiKin_Modify::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 DaiKin_Modify::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. //DataConversion(szType, m_szDaKin_400Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit);
  126. _BYTE_1_Decimal(szMsg,(BYTE*)m_szDaKin_400Msg[nAddr - 1] +nIndex);
  127. #if IS_USE_READMSG_CS
  128. LeaveCriticalSection(&m_csReadMsg);
  129. #endif
  130. return 0;
  131. }
  132. int DaiKin_Modify::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD, const int &IniSendlen)
  133. {
  134. int nRet = -1;
  135. nRet = Send_ReadDeviceData(nAddr,pComm,szCmd,IniSendCMD,IniSendlen);
  136. if( nRet != 0 )
  137. return nRet;
  138. nRet = Recv_ReadDeviceData(nAddr, pComm, szCmd);
  139. return nRet;
  140. }
  141. int DaiKin_Modify::SetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *szSetMsg,char *szRecvMsg,char *IniSendCMD, const int &IniSendlen)
  142. {
  143. int nRet = -1;
  144. nRet = Send_WriteDeviceData(nAddr, pComm, szCmd, szSetMsg,IniSendCMD, IniSendlen );
  145. if( nRet != 0 )
  146. return nRet;
  147. nRet = Recv_WriteDeviceData(nAddr, pComm, szCmd, szRecvMsg);
  148. return nRet;
  149. }
  150. // 发送设置设备参数请求
  151. int DaiKin_Modify::SendSetReuest(
  152. char *szPath,
  153. char *szIniName,
  154. int nCommPort,
  155. int nAddr,
  156. char *szCmd,
  157. char *szSetMsg,
  158. int nReversed1,
  159. int nReversed2,
  160. int nReversed3,
  161. int nReversed4,
  162. int nReversed5,
  163. float fReversed1,
  164. float fReversed2,
  165. float fReversed3,
  166. char *szReversed1,
  167. char *szReversed2,
  168. char *szReversed3,
  169. char *szReversed4,
  170. char *szReversed5
  171. )
  172. {
  173. CCommProcess *pComm = FindComm(nCommPort);
  174. if( pComm == NULL ) return -1;
  175. int nRet = -1;
  176. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  177. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  178. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  179. char szRecvMsg[10] = {0};
  180. SetDeviceParam(nAddr, pComm, szCmd, szSetMsg,szRecvMsg,IniSendCMD,IniSendlen);
  181. return 0;
  182. }
  183. int DaiKin_Modify::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd,const char *IniSendCMD,const int &IniSendlen)
  184. {
  185. int nRet = -1;
  186. #if DEBUG_DAIKIN_MODIFY
  187. int iSendLen = 0;
  188. BYTE byArySend[30] = {0};
  189. byArySend[0] = 0x7E;
  190. byArySend[1] = nAddr;
  191. byArySend[2] = 0x01;
  192. byArySend[3] = 0x05;
  193. byArySend[4] = byArySend[1] +
  194. byArySend[2] +
  195. byArySend[3];
  196. // EOI;
  197. byArySend[5] = 0x0D;
  198. //LOG4C_HEX_DUMP((LOG_NOTICE,(char *)byArySend,30));
  199. iSendLen = 6;
  200. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  201. {
  202. ResetEvent( m_hSemComm );
  203. int nResult = pComm->Write(byArySend, iSendLen);
  204. if( nResult != iSendLen )
  205. {
  206. SetEvent( m_hSemComm );
  207. LOG4C((LOG_NOTICE,"DAKIN Write长度错误"));
  208. return EER_CODE_COM_WRITE_DATA;
  209. }
  210. }
  211. else
  212. {
  213. LOG4C((LOG_NOTICE,"DAKIN 无法获取信号控制"));
  214. return ERR_CODE_COM_BUSY;
  215. }
  216. #endif
  217. return 0;
  218. }
  219. int DaiKin_Modify::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
  220. {
  221. #if DEBUG_DAIKIN_MODIFY
  222. BYTE *byAryRecv = new BYTE[90];
  223. ZeroMemory(byAryRecv, 90);
  224. int iRecvLen = pComm->Read(byAryRecv,90);
  225. if( iRecvLen <= 0)
  226. {
  227. SetEvent( m_hSemComm );
  228. if( byAryRecv != NULL)
  229. {
  230. delete[] byAryRecv;
  231. byAryRecv = NULL;
  232. }
  233. LOG4C((LOG_NOTICE, "DAKIN 串口没有读到数据"));
  234. return ERR_CODE_COM_READ_NO_DATA;
  235. }
  236. // copy to the array ;
  237. memcpy(m_szDaKin_400Msg[nAddr -1], byAryRecv, iRecvLen);
  238. m_dwOnlineTick[nAddr - 1] = GetTickCount();
  239. //LOG4C_HEX_DUMP((LOG_NOTICE,(char *)m_szDaKin_400Msg[nAddr -1],iRecvLen));
  240. SetEvent( m_hSemComm );
  241. if( byAryRecv != NULL)
  242. {
  243. delete[] byAryRecv;
  244. byAryRecv = NULL;
  245. }
  246. #else
  247. SimulationCommData(nAddr);
  248. #endif
  249. return 0;
  250. }
  251. int DaiKin_Modify::Send_WriteDeviceData( int nAddr,CCommProcess *pComm, char *szCmd, char *szSetMsg,char *IniSendCMD, const int &IniSendlen)
  252. {
  253. #if DEBUG_DAIKIN_MODIFY
  254. /************************************************************************/
  255. int iSendLen = 0;
  256. char chLength[4] = {0};
  257. char chChkSum[5] = {0};
  258. BYTE byArySend[120] = {0};
  259. // SOI;
  260. byArySend[0] = 0x7E;
  261. byArySend[1] = nAddr;
  262. byArySend[2] = 0x03;
  263. byArySend[3] = 0x04;
  264. byArySend[4] = atoi(IniSendCMD);
  265. if ( byArySend[4] == 9)
  266. {
  267. byArySend[5] = TwoHexCharToChar(szSetMsg[0] ,szSetMsg[1]);
  268. //LOG4C((LOG_NOTICE,"Set = %s,%d",szSetMsg,byArySend[5]));
  269. }
  270. else
  271. byArySend[5] = atoi(szSetMsg);
  272. byArySend[6] = byArySend[1] +
  273. byArySend[2] +
  274. byArySend[3] +
  275. byArySend[4] +
  276. byArySend[5] ;
  277. byArySend[7] = 0x0D;
  278. iSendLen = 8;
  279. //LOG4C_HEX_DUMP((LOG_NOTICE,(char *)byArySend,8));
  280. /************************************************************************/
  281. ResetEvent( m_hSemComm );
  282. // 读取上次未读的数据,确保空调缓存已无数据;
  283. BYTE byRecv[250] = {0};
  284. pComm->Read(byRecv, 250);
  285. Sleep(100);
  286. int nResult = pComm->Write(byArySend, iSendLen);
  287. if( nResult != iSendLen )
  288. {
  289. SetEvent( m_hSemComm );
  290. LOG4C((LOG_NOTICE,"DAIKIN 写长度不对"));
  291. return EER_CODE_COM_WRITE_DATA;
  292. }
  293. Sleep(200);
  294. #endif
  295. return 0;
  296. }
  297. int DaiKin_Modify::Recv_WriteDeviceData(int nAddr, CCommProcess *pComm, char *szCmd, char *szRecvMsg)
  298. {
  299. #if DEBUG_DAIKIN_MODIFY
  300. int nReceiveLen = 250;
  301. int nProcessLen = 0;
  302. int nReadLen = 0;
  303. char *pBuffer = new char[ nReceiveLen ];
  304. memset(pBuffer, 0, nReceiveLen);
  305. //Sleep(200);
  306. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
  307. if( nReadLen <= 0)
  308. {
  309. SetEvent( m_hSemComm );
  310. if( pBuffer != NULL)
  311. {
  312. delete[] pBuffer;
  313. pBuffer = NULL;
  314. }
  315. LOG4C((LOG_NOTICE,"DAKIN 写 串口没有读到数据"));
  316. return ERR_CODE_COM_READ_NO_DATA;
  317. }
  318. // 设置串口等待事件为有信号;
  319. SetEvent( m_hSemComm );
  320. if( pBuffer != NULL)
  321. {
  322. delete[] pBuffer;
  323. pBuffer = NULL;
  324. }
  325. #else
  326. SimulationCommData(nAddr);
  327. #endif
  328. return 0;
  329. }
  330. void DaiKin_Modify::SimulationCommData(int nAddr)
  331. {
  332. m_szDaKin_400Msg[nAddr -1][0] = 0x7E;
  333. m_szDaKin_400Msg[nAddr -1][1] = 0x01;
  334. m_szDaKin_400Msg[nAddr -1][2] = 0x11;
  335. m_szDaKin_400Msg[nAddr -1][3] = 0x05;
  336. m_szDaKin_400Msg[nAddr -1][4] = 0x16;
  337. m_szDaKin_400Msg[nAddr -1][5] = 0x16;
  338. m_szDaKin_400Msg[nAddr -1][6] = 0x20;
  339. m_szDaKin_400Msg[nAddr -1][7] = 0x10;
  340. m_szDaKin_400Msg[nAddr -1][8] = 0x20;
  341. m_szDaKin_400Msg[nAddr -1][9] = 0x10;
  342. m_szDaKin_400Msg[nAddr -1][10] = 0x01;
  343. m_szDaKin_400Msg[nAddr -1][11] = 0x62;
  344. m_szDaKin_400Msg[nAddr -1][12] = 0x17;
  345. m_szDaKin_400Msg[nAddr -1][13] = 0x02;
  346. m_szDaKin_400Msg[nAddr -1][14] = 0x00;
  347. m_szDaKin_400Msg[nAddr -1][15] = 0x1A;
  348. m_szDaKin_400Msg[nAddr -1][16] = 0x00;
  349. m_szDaKin_400Msg[nAddr -1][17] = 0x00;
  350. m_szDaKin_400Msg[nAddr -1][18] = 0x20;
  351. m_szDaKin_400Msg[nAddr -1][19] = 0x20;
  352. m_szDaKin_400Msg[nAddr -1][20] = 0x79;
  353. m_szDaKin_400Msg[nAddr -1][21] = 0x0D;
  354. }
  355. void DaiKin_Modify::lowcase2uppcase(BYTE &btSrc)
  356. {
  357. if( btSrc >= 'a' && btSrc <= 'z' )
  358. btSrc = btSrc - 'a' + 'A';
  359. }
  360. void DaiKin_Modify::GetCheckSum(char *szData, char *szCheck ,int nlen)
  361. {
  362. DWORD dwSum(0);
  363. for (int i = 0; i < nlen; i++)
  364. dwSum += szData[i];
  365. WORD iCompliment = dwSum;
  366. iCompliment = ~iCompliment;//取反
  367. iCompliment++;
  368. itoa(iCompliment, szCheck, 16);
  369. for (int i(0); i < 5 ; i++)
  370. lowcase2uppcase((BYTE &)szCheck[i]);
  371. }