Daikin - 副本.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. // DataMate3000.cpp: implementation of the CDataMate3000 class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Daikin.h"
  6. #include "CommProcess.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CDaiKin::CDaiKin( char szPath[MAX_PATH], // 程序所在路径
  16. char szIniName[MAX_PATH], // 配置文件名称
  17. int nCommPort, // 串行端口
  18. int nAddr, // 设备地址
  19. int nRate, // 波特率
  20. int nDataBit, // 数据位
  21. int nStopBit, // 停止位
  22. int nParity, // 校验位
  23. int nInterval // 时间间隔
  24. )
  25. {
  26. #if IS_USE_READMSG_CS
  27. InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
  28. #endif
  29. MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  30. for( int i = 0; i < MAX_ADDR; i++ )
  31. {
  32. memset(m_szDaKin_42Msg[i], 0, sizeof(m_szDaKin_42Msg[i]));
  33. memset(m_szDaKin_43Msg[i], 0, sizeof(m_szDaKin_43Msg[i]));
  34. memset(m_szDaKin_44Msg[i], 0, sizeof(m_szDaKin_44Msg[i]));
  35. m_devOnline[i] = TRUE;
  36. m_dwOnlineTick[i] = 0;
  37. }
  38. }
  39. CDaiKin::~CDaiKin()
  40. {
  41. #if IS_USE_READMSG_CS
  42. DeleteCriticalSection( &m_csReadMsg );
  43. #endif
  44. MTVERIFY( CloseHandle( m_hSemComm ) );
  45. CloseComm();
  46. }
  47. BOOL CDaiKin::DaiKinOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  48. {
  49. BOOL bResult = FALSE;
  50. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  51. return bResult;
  52. }
  53. // 发送读取设备参数请求
  54. int CDaiKin::SendReadRequest(
  55. char szPath[MAX_PATH], // 程序所在路径
  56. char szIniName[MAX_PATH], // 配置文件名称
  57. int nCommPort, // 串行端口
  58. int nAddr, // 设备地址
  59. char szCmd[MAX_CMD], // 请求命令
  60. char szMsg[VAR_MSG], // 响应的值
  61. int nReversed1, // 预留整形参数1接口
  62. int nReversed2, // 预留整形参数2接口
  63. int nReversed3, // 预留整形参数3接口
  64. int nReversed4, // 预留整形参数4接口
  65. int nReversed5, // 预留整形参数5接口
  66. float fReversed1, // 预留float参数1接口
  67. float fReversed2, // 预留float参数2接口
  68. float fReversed3, // 预留float参数3接口
  69. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  70. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  71. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  72. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  73. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  74. )
  75. {
  76. int nIndex = 0, nLen = 0;
  77. char szSendMsg[DAIKIN_SEND_MSG] = {0};
  78. char szType[DK_TYPE_LENGTH] = {0};
  79. int startBit = 0;
  80. int endBit = 0;
  81. GetFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
  82. CCommProcess *pComm = FindComm(nCommPort);
  83. if( pComm == NULL ) return -1;
  84. int nRet = -1;
  85. if( (strlen(m_szDaKin_42Msg[nAddr - 1]) == 0 &&
  86. (strcmp(szCmd, "cmd-1") == 0 ) )|| //获取系统模拟量量化数据(浮点数)
  87. ( strlen(m_szDaKin_43Msg[nAddr - 1]) == 0 &&
  88. (strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 ||
  89. strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  90. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ) )|| //获取开关输入状态
  91. ( strlen(m_szDaKin_44Msg[nAddr - 1]) == 0 &&
  92. (strcmp(szCmd, "cmd-8") == 0 ) ) ||
  93. strcmp(szCmd, "cmd-1") == 0 ||
  94. strcmp(szCmd, "cmd-2") ==0 ||
  95. strcmp(szCmd, "cmd-8") == 0
  96. ) //获取系统参数(定点数)
  97. {
  98. nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
  99. if( nRet != 0 ) return nRet;
  100. }
  101. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  102. {
  103. m_devOnline[nAddr - 1] = FALSE;
  104. }
  105. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  106. {
  107. m_devOnline[nAddr - 1] = TRUE;
  108. }
  109. if( m_devOnline[nAddr - 1] == FALSE )
  110. {
  111. return -1;
  112. }
  113. nRet = GetDaiKin_42VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  114. nRet = GetDaiKin_43VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  115. nRet = GetDaiKin_44VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  116. //LOG4C((LOG_NOTICE, "CDaiKin::SendSetReuest szCmd= %s, szMsg = %s", szCmd, szMsg));
  117. return nRet;
  118. }
  119. int CDaiKin::GetDaiKin_42VarMsg( int nAddr, //设备地址
  120. char szCmd[MAX_CMD], // 命令
  121. char szMsg[VAR_MSG], // 接收Buffer
  122. int &nIndex, // 变量索引,针对接收Buffer而言
  123. int &nLen, // 变量长度
  124. char szType[TYPE_LENGTH], // 变量数据类型
  125. int startBit,
  126. int endBit )
  127. {
  128. int nRet = 0;
  129. if( strcmp(szCmd, "cmd-1") == 0 )
  130. {
  131. #if IS_USE_READMSG_CS
  132. EnterCriticalSection( &m_csReadMsg );
  133. #endif
  134. DataConversion(szType, m_szDaKin_42Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit);
  135. //LOG4C_HEX_DUMP((LOG_INFO, szMsg, 5));
  136. #if IS_USE_READMSG_CS
  137. LeaveCriticalSection(&m_csReadMsg);
  138. #endif
  139. nRet = 0;
  140. }
  141. return nRet;
  142. }
  143. int CDaiKin::GetDaiKin_43VarMsg( int nAddr, //设备地址
  144. char szCmd[MAX_CMD], // 命令
  145. char szMsg[VAR_MSG], // 接收Buffer
  146. int &nIndex, // 变量索引,针对接收Buffer而言
  147. int &nLen, // 变量长度
  148. char szType[TYPE_LENGTH],// 变量数据类型
  149. int startBit,
  150. int endBit )
  151. {
  152. int nRet = 0;
  153. if( strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 ||
  154. strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  155. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 )
  156. {
  157. #if IS_USE_READMSG_CS
  158. EnterCriticalSection( &m_csReadMsg );
  159. #endif
  160. DataConversion(szType, m_szDaKin_43Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
  161. #if IS_USE_READMSG_CS
  162. LeaveCriticalSection(&m_csReadMsg);
  163. #endif
  164. nRet = 0;
  165. }
  166. return nRet;
  167. }
  168. int CDaiKin::GetDaiKin_44VarMsg(int nAddr, //设备地址
  169. char szCmd[MAX_CMD], // 命令
  170. char szMsg[VAR_MSG], // 接收Buffer
  171. int &nIndex, // 变量索引,针对接收Buffer而言
  172. int &nLen, // 变量长度
  173. char szType[TYPE_LENGTH],// 变量数据类型
  174. int startBit,
  175. int endBit )
  176. {
  177. int nRet = 0;
  178. if( strcmp(szCmd, "cmd-8") == 0 )
  179. {
  180. #if IS_USE_READMSG_CS
  181. EnterCriticalSection( &m_csReadMsg );
  182. #endif
  183. DataConversion(szType, m_szDaKin_44Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
  184. #if IS_USE_READMSG_CS
  185. LeaveCriticalSection(&m_csReadMsg);
  186. #endif
  187. nRet = 0;
  188. }
  189. return nRet;
  190. }
  191. int CDaiKin::GetDeviceParam(int nAddr,
  192. CCommProcess *pComm, //串口对象指针
  193. char szSendMsg[DAIKIN_SEND_MSG], //发送Buffer
  194. char szCmd[MAX_CMD], // 命令
  195. char szMsg[VAR_MSG], // 接收Buffer
  196. int &nIndex, // 变量索引,针对接收Buffer而言
  197. int &nLen, // 变量长度
  198. char szType[TYPE_LENGTH]) // 变量数据类型
  199. {
  200. int nRet = -1;
  201. nRet = RequestStatus(nAddr, szCmd, pComm, szSendMsg);
  202. if( nRet != 0 )
  203. {
  204. return nRet; // 串口忙
  205. }
  206. nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
  207. return nRet;
  208. }
  209. int CDaiKin::SetDeviceParam(int nAddr,
  210. CCommProcess *pComm, //串口对象指针
  211. char szSendMsg[DAIKIN_SEND_MSG], //发送Buffer
  212. char szCmd[MAX_CMD], // 命令
  213. char szMsg[VAR_MSG], // 接收Buffer
  214. int &nIndex, // 变量索引,针对接收Buffer而言
  215. int &nLen, // 变量长度
  216. char szType[TYPE_LENGTH],
  217. int &iSendLen) // 变量数据类型
  218. {
  219. int nRet = -1;
  220. nRet = WriteRequestStatus(nAddr, szCmd, pComm, szSendMsg,iSendLen);
  221. if( nRet != 0 )
  222. {
  223. LOG4C((LOG_NOTICE,"Write Error"));
  224. return nRet; // 串口忙
  225. }
  226. nRet = WriteResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
  227. return nRet;
  228. }
  229. void CDaiKin::SetDaKin_42VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
  230. {
  231. if( strcmp(szCmd, "cmd-1") == 0 )
  232. {
  233. #if IS_USE_READMSG_CS
  234. EnterCriticalSection( &m_csReadMsg );
  235. #endif
  236. memcpy(m_szDaKin_42Msg[nAddr - 1], pBuffer, sizeof(m_szDaKin_42Msg[nAddr - 1]));
  237. #if IS_USE_READMSG_CS
  238. LeaveCriticalSection(&m_csReadMsg);
  239. #endif
  240. }
  241. }
  242. void CDaiKin::SetDaKin_43VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
  243. {
  244. if( strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 ||
  245. strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  246. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 )
  247. {
  248. #if IS_USE_READMSG_CS
  249. EnterCriticalSection( &m_csReadMsg );
  250. #endif
  251. memcpy(m_szDaKin_43Msg[nAddr - 1], pBuffer, sizeof(m_szDaKin_43Msg[nAddr - 1]));
  252. #if IS_USE_READMSG_CS
  253. LeaveCriticalSection(&m_csReadMsg);
  254. #endif
  255. }
  256. }
  257. void CDaiKin::SetDaKin_44VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
  258. {
  259. if( strcmp(szCmd, "cmd-8") == 0 )
  260. {
  261. #if IS_USE_READMSG_CS
  262. EnterCriticalSection( &m_csReadMsg );
  263. #endif
  264. memcpy(m_szDaKin_44Msg[nAddr - 1], pBuffer, sizeof(m_szDaKin_44Msg[nAddr - 1]));
  265. #if IS_USE_READMSG_CS
  266. LeaveCriticalSection(&m_csReadMsg);
  267. #endif
  268. }
  269. }
  270. // WORD CDaiKin::GetDataLength(const WORD wLENID, char chLength[4])
  271. // {
  272. // //char szLenID[3] = {0};
  273. // ////int nLen = 10;
  274. // //DigitToBinary(wLENID, szLenID, sizeof(szLenID));
  275. //
  276. // //char chCheckSum = GetLCheckSum(szLenID, sizeof(szLenID));
  277. //
  278. // //chLength[0] = chCheckSum;
  279. // //chLength[1] = szLenID[0];
  280. // //chLength[2] = szLenID[1];
  281. // //chLength[3] = szLenID[2];
  282. //
  283. // return 0;
  284. // }
  285. // 发送设置设备参数请求
  286. int CDaiKin::SendSetReuest(
  287. char szPath[MAX_PATH], // 程序所在路径
  288. char szIniName[MAX_PATH], // 配置文件名称
  289. int nCommPort, // 串行端口
  290. int nAddr, // 设备地址
  291. char szCmd[MAX_CMD], // 请求命令
  292. char szMsg[VAR_MSG], // 响应的值
  293. int nReversed1, // 预留整形参数1接口
  294. int nReversed2, // 预留整形参数2接口
  295. int nReversed3, // 预留整形参数3接口
  296. int nReversed4, // 预留整形参数4接口
  297. int nReversed5, // 预留整形参数5接口
  298. float fReversed1, // 预留float参数1接口
  299. float fReversed2, // 预留float参数2接口
  300. float fReversed3, // 预留float参数3接口
  301. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  302. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  303. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  304. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  305. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  306. )
  307. {
  308. LOG4C((LOG_NOTICE,"CtrlMsg = %s",szMsg));
  309. int nIndex = 0, nLen = 0, StaBit = 0, EndBit = 0;
  310. char szSendMsg[DAIKIN_SEND_MSG];
  311. char szType[TYPE_LENGTH] = {0};
  312. char szCid2[CID2_LENGTH] = {0};
  313. memset(szSendMsg, 0, DAIKIN_SEND_MSG);
  314. GetFromIni(szPath, szIniName, szCmd, szCid2, szType, nIndex, nLen, StaBit, EndBit);
  315. int iSendLen = 0;
  316. int iLen = sizeof(REQUESTSET_STRCUT);
  317. char chLength[4] = {0};
  318. char chChkSum[5] = {0};
  319. BYTE byArySend[120] = {0};
  320. byArySend[0] = 0x7E;
  321. byArySend[1] = 0x32;
  322. byArySend[2] = 0x30;
  323. byArySend[3] = ByteToAscii((nAddr>>4) & 0x0f);
  324. byArySend[4] = ByteToAscii(nAddr & 0x0f);
  325. //Cid1
  326. byArySend[5] = 0x36;
  327. byArySend[6] = 0x30;
  328. memcpy(byArySend + 7, szCid2, 2);
  329. if ( (strcmp(szCmd, "cmd-9") == 0)) // 开关机;
  330. {
  331. // lengchck;
  332. byArySend[9] = 0x45;
  333. byArySend[10] = 0x30;
  334. byArySend[11] = 0x30;
  335. byArySend[12] = 0x32;
  336. int iCtrl = atoi(szMsg);
  337. memset(szMsg,0,strlen(szMsg));
  338. itoa(iCtrl,szMsg,16);
  339. for (int i(0); i < 2 ; i++)
  340. lowcase2uppcase((BYTE &)szMsg[i]);
  341. memcpy(byArySend + 13, szMsg, 2);
  342. GetCheckSum((char *)byArySend + 1, chChkSum, 14 );
  343. memcpy(byArySend + 15, chChkSum, 4);
  344. byArySend[19] = 0x0D;
  345. iSendLen = 20;
  346. LOG4C((LOG_NOTICE,"cmd-9=空调开关控制:%s",szMsg));
  347. }
  348. else if ( (strcmp(szCmd, "cmd-10") == 0)) // 设置风量;
  349. {
  350. int iRealLen = strlen(szMsg);
  351. if ( iRealLen != 4)
  352. {
  353. LOG4C((LOG_NOTICE,"非法指令长度:%s",szMsg));
  354. return -1;
  355. }
  356. // lengchck;
  357. byArySend[9] = 0x38;
  358. byArySend[10] = 0x30;
  359. byArySend[11] = 0x30;
  360. byArySend[12] = 0x38;
  361. // STX;
  362. byArySend[13] = 0x36;
  363. byArySend[14] = 0x34;
  364. byArySend[15] = 0x30;
  365. // 接口卡LINEID;
  366. byArySend[16] = nAddr -1 + 48;
  367. // 设置值;
  368. memcpy(byArySend + 17, szMsg, 4);
  369. GetCheckSum((char *)byArySend + 1, chChkSum, 20 );
  370. memcpy(byArySend + 21, chChkSum, 4);
  371. // EOL;
  372. byArySend[25] = 0x0D;
  373. iSendLen = 26;
  374. LOG4C((LOG_NOTICE,"cmd-10=空调风量设置:%s",szMsg));
  375. }
  376. else if ( (strcmp(szCmd, "cmd-11") == 0)) // 设置温度;
  377. {
  378. byArySend[9] = 0x36;
  379. byArySend[10] = 0x30;
  380. byArySend[11] = 0x30;
  381. byArySend[12] = 0x41;
  382. byArySend[13] = 0x36;
  383. byArySend[14] = 0x33;
  384. byArySend[15] = 0x30;
  385. byArySend[16] = nAddr -1 + 48;
  386. char szTemp[10] = {0};
  387. int iRealLen = strlen(szMsg);
  388. memcpy(szTemp,szMsg,iRealLen);
  389. if (iRealLen > 6)
  390. {
  391. LOG4C((LOG_NOTICE,"非法指令长度:%s",szMsg));
  392. return -1;
  393. }
  394. if (iRealLen == 5)
  395. {
  396. memcpy(szMsg + 1, szTemp,iRealLen);
  397. szMsg[0] = 48;
  398. }else if (iRealLen == 4)
  399. {
  400. memcpy(szMsg + 2, szTemp,iRealLen);
  401. szMsg[0] = 48;
  402. szMsg[1] = 48;
  403. }
  404. else if ( iRealLen == 3)
  405. {
  406. memcpy(szMsg + 3, szTemp,iRealLen);
  407. szMsg[0] = 48;
  408. szMsg[1] = 48;
  409. szMsg[2] = 48;
  410. }
  411. else if ( iRealLen == 2)
  412. {
  413. memcpy(szMsg + 4, szTemp,iRealLen);
  414. szMsg[0] = 48;
  415. szMsg[1] = 48;
  416. szMsg[2] = 48;
  417. szMsg[3] = 48;
  418. }
  419. else if ( iRealLen == 1)
  420. {
  421. memcpy(szMsg + 5, szTemp,iRealLen);
  422. szMsg[0] = 48;
  423. szMsg[1] = 48;
  424. szMsg[2] = 48;
  425. szMsg[3] = 48;
  426. szMsg[4] = 48;
  427. }
  428. else if ( iRealLen == 0)
  429. memcpy(szMsg , "000000", 6);
  430. memcpy(byArySend + 17, szMsg, 6);
  431. GetCheckSum((char *)byArySend + 1, chChkSum, 22 );
  432. memcpy(byArySend + 23, chChkSum, 4);
  433. byArySend[27] = 0x0D;
  434. iSendLen = 28;
  435. LOG4C((LOG_NOTICE,"cmd-11=空调温度设置:%s",szMsg));
  436. }else if ( strcmp(szCmd, "cmd-12") == 0) // 设置运转模式;
  437. {
  438. // lengchck;
  439. byArySend[9] = 0x42;
  440. byArySend[10] = 0x30;
  441. byArySend[11] = 0x30;
  442. byArySend[12] = 0x35;
  443. // STX;
  444. byArySend[13] = 0x36;
  445. byArySend[14] = 0x32;
  446. byArySend[15] = 0x30;
  447. // 接口卡LINEID; 范围:0~2
  448. byArySend[16] = nAddr -1 + 48;
  449. // 设置值; 范围:0~7
  450. memcpy(byArySend + 17, szMsg, 1);
  451. GetCheckSum((char *)byArySend + 1, chChkSum, 17 );
  452. memcpy(byArySend + 18, chChkSum, 4);
  453. // EOL;
  454. byArySend[22] = 0x0D;
  455. iSendLen = 23;
  456. LOG4C((LOG_NOTICE,"cmd-12=空调运转模式设置:%s",szMsg));
  457. }
  458. memcpy(szSendMsg, byArySend, sizeof(byArySend));
  459. CCommProcess *pComm = FindComm(nCommPort);
  460. if( pComm == NULL ) return -1;
  461. if( nAddr < 1 ) return -1;
  462. SetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType, iSendLen );
  463. return 0;
  464. }
  465. //读取配置文件
  466. int CDaiKin::GetFromIni(
  467. char szPath[MAX_PATH], //服务器程序所在目录
  468. char szIniName[MAX_PATH], //配置文件名
  469. char szCmd[MAX_CMD], //命令
  470. char szCid2[CID2_LENGTH], //发送Buffer
  471. char szType[TYPE_LENGTH],
  472. int &nIndex,
  473. int &nLen,
  474. int &nStaBit,
  475. int &nEndBit
  476. )
  477. {
  478. char szFile[MAX_PATH + 1] = "";
  479. memset(szType, 0, TYPE_LENGTH);
  480. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);//只读config下面的ini
  481. GetPrivateProfileString(szCmd, "SendCmd", "", szCid2, CID2_LENGTH, szFile);//读取配置文件中的一段字符串
  482. GetPrivateProfileString(szCmd, "Type", "", szType, TYPE_LENGTH, szFile);
  483. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);//从配置文件中取值
  484. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  485. nStaBit = GetPrivateProfileInt(szCmd, "StaBit", 0, szFile);//从配置文件中取值
  486. nEndBit = GetPrivateProfileInt(szCmd, "EndBit", 0, szFile);
  487. return 0;
  488. }
  489. int CDaiKin::RequestStatus( int nAddr,
  490. char szCmd[MAX_CMD], // 命令
  491. CCommProcess *pComm, //串口对象指针
  492. char chSendMsg[DAIKIN_SEND_MSG] //发送Buffer
  493. )
  494. {
  495. int nRet = -1;
  496. #if DEBUG_DAIKIN
  497. int iLen = sizeof(REQUEST_STRUCT);
  498. char chLength[4] = {0};
  499. char chChkSum[5] = {0};
  500. REQUEST_STRUCT RequestPara;
  501. memset( &RequestPara, 0, iLen );
  502. //起始位
  503. RequestPara.headMes.Start = 0x7E;
  504. //通讯协议版本
  505. RequestPara.headMes.Version[0] = 0x32;
  506. RequestPara.headMes.Version[1] = 0x30;
  507. //设备地址描述
  508. RequestPara.headMes.Address[0] = ByteToAscii((nAddr>>4) & 0x0f);
  509. RequestPara.headMes.Address[1] = ByteToAscii(nAddr & 0x0f);
  510. //Cid1
  511. RequestPara.headMes.Cid1[0] = 0x36;
  512. RequestPara.headMes.Cid1[1] = 0x30;
  513. //Cid2
  514. RequestPara.headMes.Cid2[0] = ByteToAscii(chSendMsg[0] & 0x0f);
  515. RequestPara.headMes.Cid2[1] = ByteToAscii(chSendMsg[1] & 0x0f);
  516. RequestPara.headMes.Lenth[0] = 0x30;
  517. RequestPara.headMes.Lenth[1] = 0x30;
  518. RequestPara.headMes.Lenth[2] = 0x30;
  519. RequestPara.headMes.Lenth[3] = 0x30;
  520. //校验码
  521. BYTE *pDataBuf = new BYTE[ iLen - sizeof(RequestPara.chkSum) - 1 ];
  522. memset(pDataBuf, 0, iLen - sizeof(RequestPara.chkSum) - 1 );
  523. memcpy(pDataBuf, &RequestPara, iLen - sizeof(RequestPara.chkSum) - 1 );
  524. GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.chkSum) - 2 );
  525. RequestPara.chkSum[0] = chChkSum[0];
  526. RequestPara.chkSum[1] = chChkSum[1];
  527. RequestPara.chkSum[2] = chChkSum[2];
  528. RequestPara.chkSum[3] = chChkSum[3];
  529. //结束符
  530. RequestPara.End = 0x0D;
  531. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  532. {
  533. int nDataLen = (int)sizeof(RequestPara);
  534. ResetEvent( m_hSemComm );
  535. int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
  536. if( nResult == iLen )
  537. {
  538. }
  539. else
  540. {
  541. delete []pDataBuf;
  542. SetEvent( m_hSemComm );
  543. return EER_CODE_DAIKIN_COM_WRITE_DATA;
  544. }
  545. }
  546. else
  547. {
  548. delete []pDataBuf;
  549. return ERR_CODE_DAIKIN_COM_BUSY;
  550. }
  551. delete[]pDataBuf;
  552. #endif
  553. return 0;
  554. }
  555. int CDaiKin::ResponseStatus(
  556. int nAddr, // 设备地址
  557. CCommProcess *pComm, //串口对象指针
  558. char szCmd[MAX_CMD], // 命令
  559. char szMsg[VAR_MSG], // 接收Buffer
  560. int &nIndex, // 变量索引,针对接收Buffer而言
  561. int &nLen, // 变量长度
  562. char szType[TYPE_LENGTH] // 变量数据类型
  563. )
  564. {
  565. #if DEBUG_DAIKIN
  566. int nReceiveLen = 0;
  567. int nProcessLen = 0;
  568. int nReadLen = 0;
  569. RESPONSE_STRUCT structResponse;
  570. memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) );
  571. nReceiveLen = sizeof(RESPONSE_STRUCT);
  572. char *pBuffer = new char[ nReceiveLen ];
  573. memset(pBuffer, 0, nReceiveLen);
  574. nReadLen = pComm->Read((BYTE *)pBuffer,nReceiveLen);
  575. //LOG4C((LOG_NOTICE, " nLen= %d, nReadLen = %d", nLen, nReadLen));
  576. if( nReadLen <= 0)
  577. {
  578. // 串口没有读到数据
  579. //TRACE("串口没有读到数据!\r\n");
  580. SetEvent( m_hSemComm );
  581. if( pBuffer != NULL)
  582. {
  583. delete[] pBuffer;
  584. pBuffer = NULL;
  585. }
  586. LOG4C((LOG_NOTICE, "串口没有读到数据"));
  587. return ERR_CODE_DAIKIN_COM_READ_NO_DATA;
  588. }
  589. if (!ChkSumCheck(pBuffer, nReadLen))
  590. {
  591. TRACE("校验码校验出错");
  592. SetEvent(m_hSemComm);
  593. if (pBuffer != NULL)
  594. {
  595. delete [] pBuffer;
  596. pBuffer = NULL;
  597. }
  598. LOG4C((LOG_NOTICE, "校验码校验出错"));
  599. return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
  600. }
  601. if (RtnCheck(pBuffer) != 0)
  602. {
  603. SetEvent(m_hSemComm);
  604. TRACE("RTN校验出错");
  605. if (pBuffer != NULL)
  606. {
  607. delete [] pBuffer;
  608. pBuffer = NULL;
  609. }
  610. LOG4C((LOG_NOTICE, "RTN校验出错"));
  611. return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错
  612. }
  613. SetDaKin_42VarMsg(nAddr, szCmd, pBuffer);
  614. SetDaKin_43VarMsg(nAddr, szCmd, pBuffer);
  615. SetDaKin_44VarMsg(nAddr, szCmd, pBuffer);
  616. //SetDaKin_45VarMsg(nAddr, szCmd, pBuffer);
  617. //SetDaKin_47VarMsg(nAddr, szCmd, pBuffer);
  618. m_dwOnlineTick[nAddr - 1] = GetTickCount();
  619. // 设置串口等待事件为有信号
  620. SetEvent( m_hSemComm );
  621. if( pBuffer != NULL)
  622. {
  623. delete[] pBuffer;
  624. pBuffer = NULL;
  625. }
  626. #else
  627. SimulationCommData(nAddr);
  628. #endif
  629. return 0;
  630. }
  631. int CDaiKin::WriteRequestStatus( int nAddr,
  632. char *szCmd, // 命令
  633. CCommProcess *pComm, //串口对象指针
  634. char *chSendMsg , //发送Buffer
  635. int &iSendLen
  636. )
  637. {
  638. #if DEBUG_DAIKIN
  639. ResetEvent( m_hSemComm );
  640. // 读取上次未读的数据,确保空调缓存已无数据;
  641. BYTE byRecv[250] = {0};
  642. pComm->Read(byRecv, 250);
  643. Sleep(1000);
  644. int nResult = pComm->Write((unsigned char *)chSendMsg, iSendLen);
  645. LOG4C((LOG_NOTICE,"%s %d",chSendMsg,iSendLen));
  646. if( nResult == iSendLen )
  647. {
  648. }
  649. else
  650. {
  651. SetEvent( m_hSemComm );
  652. return EER_CODE_DAIKIN_COM_WRITE_DATA;
  653. }
  654. Sleep(300);
  655. #endif
  656. return 0;
  657. }
  658. int CDaiKin::WriteResponseStatus( int nAddr, // 设备地址
  659. CCommProcess *pComm, //串口对象指针
  660. char szCmd[MAX_CMD], // 命令
  661. char szMsg[VAR_MSG], // 接收Buffer
  662. int &nIndex, // 变量索引,针对接收Buffer而言
  663. int &nLen, // 变量长度
  664. char szType[TYPE_LENGTH] // 变量数据类型
  665. )
  666. {
  667. #if DEBUG_DAIKIN
  668. int nReceiveLen = 250;
  669. int nProcessLen = 0;
  670. int nReadLen = 0;
  671. char *pBuffer = new char[ nReceiveLen ];
  672. memset(pBuffer, 0, nReceiveLen);
  673. Sleep(200);
  674. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
  675. LOG4C((LOG_NOTICE,"写 nReadLen = %d, nReceiveLen = %d",nReadLen, nReceiveLen));
  676. if( nReadLen <= 0)
  677. {
  678. SetEvent( m_hSemComm );
  679. if( pBuffer != NULL)
  680. {
  681. delete[] pBuffer;
  682. pBuffer = NULL;
  683. }
  684. LOG4C((LOG_NOTICE,"写 串口没有读到数据"));
  685. return ERR_CODE_DAIKIN_COM_READ_NO_DATA;
  686. }
  687. #if 0
  688. //int nTempLen = strlen(pBuffer);
  689. //CString str;
  690. //str.Format("%d", nTempLen);
  691. //AfxMessageBox(str);
  692. if (!ChkSumCheck(pBuffer, strlen(pBuffer)))
  693. {
  694. SetEvent( m_hSemComm );
  695. if( pBuffer != NULL)
  696. {
  697. delete[] pBuffer;
  698. pBuffer = NULL;
  699. }
  700. LOG4C((LOG_NOTICE,"写 校检错误"));
  701. return ERR_CODE_DAIKIN_COM_CHKSUM_LOST;
  702. }
  703. if (!CheckLength(pBuffer))
  704. {
  705. SetEvent( m_hSemComm );
  706. if( pBuffer != NULL)
  707. {
  708. delete[] pBuffer;
  709. pBuffer = NULL;
  710. }
  711. LOG4C((LOG_NOTICE,"写 数据长度校检错误"));
  712. return ERR_CODE_DAIKIN_COM_VARLEN;
  713. }
  714. int nRet = RtnCheck(pBuffer);
  715. if(0 != nRet)
  716. {
  717. SetEvent( m_hSemComm );
  718. if( pBuffer != NULL)
  719. {
  720. delete[] pBuffer;
  721. pBuffer = NULL;
  722. }
  723. LOG4C((LOG_NOTICE,"写 返回码出错"));
  724. return nRet;
  725. }
  726. #endif
  727. // 设置串口等待事件为有信号
  728. SetEvent( m_hSemComm );
  729. if( pBuffer != NULL)
  730. {
  731. delete[] pBuffer;
  732. pBuffer = NULL;
  733. }
  734. #else
  735. SimulationCommData(nAddr);
  736. #endif
  737. return 0;
  738. }
  739. void CDaiKin::SimulationCommData(int nAddr)
  740. {
  741. }
  742. WORD CDaiKin::RtnCheck(char Msg[VAR_MSG])
  743. {
  744. int len ,index;
  745. len = 2;
  746. index = 7;
  747. char ch[2];
  748. ch[0] = Msg[index];
  749. ch[1] = Msg[index+1];
  750. if (atoi(ch)==ERR_CID_RTN_VAR)
  751. {
  752. return ERR_CID_RTN_VAR;
  753. }
  754. else if (atoi(ch)==ERR_CID_RTN_CHKSUM)
  755. {
  756. return ERR_CID_RTN_CHKSUM;
  757. }
  758. else if (atoi(ch)==ERR_CID_RTN_LCHKSUM)
  759. {
  760. return ERR_CID_RTN_LCHKSUM;
  761. }
  762. else if (atoi(ch)==ERR_CID_RTN_CID2)
  763. {
  764. return ERR_CID_RTN_CID2;
  765. }
  766. else if (atoi(ch)==ERR_CID_RTN_FORMAT)
  767. {
  768. return ERR_CID_RTN_FORMAT;
  769. }
  770. else if (atoi(ch)==ERR_CID_RTN_INVALI_DATA)
  771. {
  772. return ERR_CID_RTN_INVALI_DATA;
  773. }
  774. else /*if (atoi(ch)==ERR_CID_RTN_NORMAL)*/
  775. {
  776. return ERR_CID_RTN_NORMAL;
  777. }
  778. }
  779. void CDaiKin::lowcase2uppcase(BYTE &btSrc)
  780. {
  781. if( btSrc >= 'a' && btSrc <= 'z' )
  782. {
  783. btSrc = btSrc - 'a' + 'A';
  784. }
  785. }
  786. void CDaiKin::GetCheckSum(char *szData, char *szCheck ,int nlen /* = 12 */)
  787. {
  788. DWORD dwSum(0);
  789. nlen = strlen(szData);
  790. for (int i = 0; i < nlen; i++)
  791. {
  792. dwSum += szData[i] ;
  793. }
  794. WORD iCompliment = dwSum;
  795. iCompliment = ~iCompliment;//取反
  796. iCompliment++;
  797. itoa(iCompliment, szCheck, 16);
  798. for (int i(0); i < 5 ; i++)
  799. {
  800. lowcase2uppcase((BYTE &)szCheck[i]);
  801. }
  802. }
  803. #if 0
  804. UINT CDaiKin::GetCheckSum(char *pBuf, char chDest[5], int len)
  805. {
  806. WORD iSum = 0;
  807. //unsigned char chCompliment[2] = {0};
  808. for(int i=0; i<len; i++)//求和
  809. {
  810. //TRACE("%x\r\n", pBuf[i]);
  811. iSum += pBuf[i];
  812. }
  813. WORD iCompliment = iSum;
  814. iCompliment = ~iCompliment;//取反
  815. iCompliment++;
  816. //DigitToBinary(iCompliment, chDest, sizeof(chDest));
  817. itoa(iCompliment, chDest, 16);
  818. chDest[0] = lowercase2uppercase(chDest[0]);
  819. chDest[1] = lowercase2uppercase(chDest[1]);
  820. chDest[2] = lowercase2uppercase(chDest[2]);
  821. chDest[3] = lowercase2uppercase(chDest[3]);
  822. return atoi(chDest);
  823. }
  824. #endif