Daikin1111.cpp 29 KB

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