cm.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. // cm.cpp: implementation of the cm class.
  2. //////////////////////////////////////////////////////////////////////////////
  3. ////// //////
  4. ////// 文 件: cm.cpp //////
  5. ////// 作 者: Wangjianfeng //////
  6. ////// 创建时间: //////
  7. ////// 说 明: EmersonCM+协议 //////
  8. ////// //////
  9. ////// 修改时间:2010-04-08 //////
  10. ////// 修改说明:数据转换 //////
  11. ////// //////
  12. //////////////////////////////////////////////////////////////////////////////
  13. //////////////////////////////////////////////////////////////////////
  14. #include "stdafx.h"
  15. #include "CommProcess.h"
  16. #include "Global.h"
  17. #include "cm.h"
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[]=__FILE__;
  21. #define new DEBUG_NEW
  22. #endif
  23. cm::cm(char szPath[MAX_PATH], // 程序所在路径
  24. char szIniName[MAX_PATH], // 配置文件名称
  25. int nCommPort, // 串行端口
  26. int nAddr, // 设备地址
  27. int nRate, // 波特率
  28. int nDataBit, // 数据位
  29. int nStopBit, // 停止位
  30. int nParity, // 校验位
  31. int nInterval // 时间间隔
  32. )
  33. {
  34. #if IS_USE_READMSG_CS
  35. InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
  36. #endif
  37. MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  38. for(int i = 0;i < MAX_ADDR;i++)
  39. {
  40. memset(m_emerson_ControlMsg[i], 0, sizeof(m_emerson_ControlMsg[i]));
  41. memset(m_emerson_QeuryMsg[i], 0, sizeof(m_emerson_QeuryMsg[i]));
  42. m_devOnline[i] = TRUE;
  43. m_dwOnlineTick[i] = 0;
  44. }
  45. }
  46. cm::~cm()
  47. {
  48. #if IS_USE_READMSG_CS
  49. DeleteCriticalSection( &m_csReadMsg );
  50. #endif
  51. MTVERIFY( CloseHandle( m_hSemComm ) );
  52. CloseComm();
  53. }
  54. BOOL cm::AirEmersonOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  55. {
  56. BOOL bResult = FALSE;
  57. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  58. return bResult;
  59. }
  60. int cm::SendReadRequest(
  61. char szPath[MAX_PATH], // 程序所在路径
  62. char szIniName[MAX_PATH], // 配置文件名称
  63. int nCommPort, // 串行端口
  64. int nAddr, // 设备地址
  65. char szCmd[MAX_CMD], // 请求命令
  66. char szRecvMsg[VAR_MSG], // 响应的值
  67. int nReversed1, // 预留整形参数1接口
  68. int nReversed2, // 预留整形参数2接口
  69. int nReversed3, // 预留整形参数3接口
  70. int nReversed4, // 预留整形参数4接口
  71. int nReversed5, // 预留整形参数5接口
  72. float fReversed1, // 预留float参数1接口
  73. float fReversed2, // 预留float参数2接口
  74. float fReversed3, // 预留float参数3接口
  75. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  76. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  77. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  78. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  79. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  80. )
  81. {
  82. int LBCount = 0;
  83. int SendCmd = 0;
  84. int Function = 0;
  85. int Index = 0;
  86. int nLen = 0;
  87. char Dtype[10] = {0};
  88. int startbit =0;
  89. int endbit = 0;
  90. GetFromIni(szPath, szIniName, szCmd, SendCmd,Function, LBCount,Index,nLen,Dtype,startbit,endbit);
  91. CCommProcess *pComm = FindComm(nCommPort);
  92. if( pComm == NULL ) return -1;
  93. if( nAddr < 1 ) return -1;
  94. int nRet = -1;
  95. /*if( strlen((char *)m_emerson_QeuryMsg) == 0 &&
  96. (
  97. strcmp(szCmd, "cmd-1") == 0 ||
  98. strcmp(szCmd, "cmd-2") == 0 ||
  99. strcmp(szCmd, "cmd-3") == 0
  100. )
  101. ||*/
  102. if((strlen((char *)m_emerson_QeuryMsg[nAddr-1]) == 0 &&
  103. (
  104. strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  105. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
  106. strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
  107. strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
  108. strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
  109. strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  110. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
  111. strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
  112. strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
  113. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
  114. strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  115. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0 ||
  116. strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
  117. strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
  118. strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  119. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
  120. strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
  121. strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
  122. strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0
  123. ))
  124. ||(strcmp(szCmd, "cmd-4"))||strcmp(szCmd, "cmd-33")||strcmp(szCmd, "cmd-41")
  125. )
  126. {
  127. int nRet = GetDeviceParam( pComm,nAddr,szCmd,SendCmd,Function,LBCount,Index,nLen, Dtype,startbit,endbit,szRecvMsg);
  128. if( nRet != 0 ) return nRet;
  129. }
  130. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 * 1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  131. {
  132. m_devOnline[nAddr - 1] = FALSE;
  133. //LOG4C((LOG_NOTICE , "m_devOnline[%d] = FALSE",nAddr-1));
  134. }
  135. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 * 1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  136. {
  137. m_devOnline[nAddr - 1] = TRUE;
  138. //LOG4C((LOG_NOTICE , "m_devOnline[%d] = TRUE",nAddr-1));
  139. }
  140. if( m_devOnline[nAddr - 1] == FALSE )
  141. {
  142. return -1;
  143. }
  144. nRet = GetEmersonEnquiryVarMsg(nAddr,szCmd, szRecvMsg, Index, nLen, Dtype,startbit,endbit);
  145. //LOG4C((LOG_NOTICE, "szCmd = %s, szMsg = %s ,nIndex = %d , startbit = %d, endbit = %d ,return nRet = %d", szCmd, szRecvMsg,Index,startbit,endbit, nRet));
  146. return nRet;
  147. }
  148. // 发送设置设备参数请求
  149. int cm::SendSetReuest(
  150. char szPath[MAX_PATH], // 程序所在路径
  151. char szIniName[MAX_PATH], // 配置文件名称
  152. int nCommPort, // 串行端口
  153. int nAddr, // 设备地址
  154. char szCmd[MAX_CMD], // 请求命令
  155. char szRecvMsg[VAR_MSG], // 响应的值
  156. int nReversed1, // 预留整形参数1接口
  157. int nReversed2, // 预留整形参数2接口
  158. int nReversed3, // 预留整形参数3接口
  159. int nReversed4, // 预留整形参数4接口
  160. int nReversed5, // 预留整形参数5接口
  161. float fReversed1, // 预留float参数1接口
  162. float fReversed2, // 预留float参数2接口
  163. float fReversed3, // 预留float参数3接口
  164. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  165. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  166. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  167. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  168. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  169. )
  170. {
  171. int LBCount = 0;
  172. int SendCmd = 0;
  173. int Function = 0;
  174. int Index = 0;
  175. int nLen = 0;
  176. char Dtype[10] = {0};
  177. int startbit =0;
  178. int endbit = 0;
  179. GetFromIni(szPath, szIniName, szCmd, SendCmd,Function, LBCount,Index,nLen,Dtype,startbit,endbit);
  180. CCommProcess *pComm = FindComm(nCommPort);
  181. if( pComm == NULL ) return -1;
  182. if( strlen((char *)m_emerson_ControlMsg) == 0 ||
  183. strcmp(szCmd, "cmd-1") == 0 ||
  184. strcmp(szCmd, "cmd-2") == 0 ||
  185. strcmp(szCmd, "cmd-3") == 0
  186. )
  187. {
  188. GetDeviceParam( pComm,nAddr,szCmd,SendCmd,Function,LBCount,Index,nLen, Dtype,startbit,endbit,szRecvMsg);
  189. }
  190. return 0;
  191. }
  192. int cm::RequestReadStatus(CCommProcess *pComm,int SendCmd,int Function,int LBCount,int addr)
  193. {
  194. #if DEBUG_EMERSON
  195. int iLen = sizeof(REQUESTPARAM);
  196. REQUESTPARAM RequestPara;
  197. memset( &RequestPara, 0, iLen );
  198. RequestPara.UnitId = (addr - 1) ^ 0xf0;//
  199. RequestPara.ControlId = Function;
  200. RequestPara.Len = LBCount;
  201. RequestPara.Request = SendCmd;
  202. RequestPara.Chksum = (RequestPara.UnitId + RequestPara.ControlId + RequestPara.Len + RequestPara.Request) & 0xff;
  203. //WaitForSingObject();等待有信号;
  204. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  205. {
  206. //ResetEvent()设置无信号状态
  207. ResetEvent( m_hSemComm );
  208. int nResult = pComm->Write((unsigned char *)&RequestPara, iLen);
  209. if( nResult != iLen )
  210. {
  211. //SetEvent设置有信号;
  212. SetEvent( m_hSemComm );
  213. //返回 错误值;
  214. return EER_CODE_AIREMERSONCM_COM_REGNUM;
  215. }
  216. }
  217. else
  218. {
  219. //如果m_hSemCom无信号,返回错误;
  220. return ERR_CODE_AIREMERSONCM_COM_BUSY;
  221. }
  222. #endif
  223. return 0;
  224. }
  225. int cm::ResponseReadStatus( int nAddr,
  226. CCommProcess *pComm, // 串口对象
  227. char szCmd[MAX_CMD], // 命令
  228. int &SendCmd, // 控制码
  229. int &Function, // 功能码
  230. int &LBCount // 变量类型长度
  231. )
  232. {
  233. #if DEBUG_EMERSON
  234. int nReceiveLen = sizeof(RESPONSE_STRUCT);
  235. char *pBuffer = new char[nReceiveLen];
  236. memset(pBuffer, 0, nReceiveLen);
  237. int nReadLen = 0;
  238. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);//pBuffer整串协议 数据
  239. if( nReadLen <= 0)
  240. {
  241. LOG4C((LOG_NOTICE, "EmersonCM串口没有读到数据!"));
  242. SetEvent( m_hSemComm );
  243. if( pBuffer != NULL)
  244. {
  245. delete[] pBuffer;
  246. pBuffer = NULL;
  247. }
  248. return ERR_CODE_AIREMERSONCM_COM_READ_NO_DATA;
  249. }
  250. #if 1
  251. if (!ChkSumCheck(pBuffer,SendCmd))
  252. {
  253. LOG4C((LOG_NOTICE, "EmersonCM ChkSumCheck Lost SendCmd = %d!", SendCmd));
  254. SetEvent(m_hSemComm);
  255. if (pBuffer !=NULL)
  256. {
  257. delete[] pBuffer;
  258. pBuffer = NULL;
  259. }
  260. return ERR_CODE_AIREMERSONCM_COM_LRC_LOST; //校验码校验出错
  261. }
  262. #endif
  263. //SetEmersonContrlVarMsg(szCmd, pBuffer);
  264. SetEmersonEnquiryVarMsg(nAddr,szCmd, pBuffer);
  265. m_dwOnlineTick[nAddr - 1] = GetTickCount();
  266. // 设置串口等待事件为有信号
  267. SetEvent( m_hSemComm );
  268. if( pBuffer != NULL)
  269. {
  270. delete[] pBuffer;
  271. pBuffer = NULL;
  272. }
  273. #else
  274. SimulationCommData();
  275. #endif
  276. return 0;
  277. }
  278. int cm::RequestWriteStatus( CCommProcess *pComm,int Function ,int SendCmd,int LBCount,int nAddr )
  279. {
  280. REQUESTPARAM requsetparam;
  281. int nDataLen = sizeof(REQUESTPARAM);
  282. memset(&requsetparam,0,nDataLen);
  283. requsetparam.UnitId = (nAddr-1) ^ 0xf0; //机组-in
  284. requsetparam.ControlId = SendCmd; //0x11//控制
  285. requsetparam.Len = LBCount; //后续数
  286. requsetparam.Request = Function; //szcmd//功能码-in
  287. requsetparam.Chksum = (requsetparam.UnitId + requsetparam.ControlId + requsetparam.Len + requsetparam.Request) & 0xff; //校验
  288. ResetEvent( m_hSemComm );//设置为无信号
  289. //WriteMessage写命令入串口,返回实际写入的字节数;
  290. int iResult = pComm->Write((BYTE *)&requsetparam, nDataLen);
  291. if (iResult == nDataLen)
  292. {
  293. SetEvent( m_hSemComm );//设置为有信号;
  294. return 0;
  295. }
  296. else
  297. {
  298. SetEvent( m_hSemComm );//设置为有信号;
  299. return EER_CODE_AIREMERSONCM_COM_WRITE_DATA;
  300. }
  301. }
  302. int cm::ResponseWriteStatus(int nAddr,CCommProcess *pComm)
  303. {
  304. char chBuf[30] = {0};
  305. //读取请求写入的命令;
  306. int iReadLen = pComm->Read((BYTE *)chBuf, sizeof(chBuf));
  307. if( iReadLen <= 0) // 串口没有读到数据
  308. {
  309. SetEvent( m_hSemComm );
  310. return ERR_CODE_AIREMERSONCM_COM_READ_NO_DATA;
  311. }
  312. SetEvent( m_hSemComm );
  313. if (chBuf[1] == 0x06)//ack字符 成功接收;
  314. {
  315. LOG4C((LOG_NOTICE,"设置成功"));
  316. return 0;
  317. }
  318. else
  319. return ERR_CODE_AIREMERSONCM_COM_INVALIDRES;
  320. }
  321. int cm::GetFromIni(
  322. char szPath[MAX_PATH], //服务器程序所在目录
  323. char szIniName[MAX_PATH], //配置文件名
  324. char szCmd[MAX_CMD], //功能码
  325. int &SendCmd,
  326. int &Function,
  327. int &LBCount,
  328. int &Index,
  329. int &nLen,
  330. char Dtype[10],
  331. int &startbit,
  332. int &endbit
  333. )
  334. {
  335. CHAR szFile[MAX_PATH + 1] = "";
  336. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  337. TRACE("szFile =%s\n",szFile);
  338. SendCmd = GetPrivateProfileInt(szCmd,"SendCmd",0,szFile);
  339. Function = GetPrivateProfileInt(szCmd,"Function",0,szFile);
  340. LBCount = GetPrivateProfileInt(szCmd,"LBCount",0,szFile);
  341. Index = GetPrivateProfileInt(szCmd,"Index",0,szFile);
  342. nLen = GetPrivateProfileInt(szCmd,"nLen",0,szFile);
  343. DWORD ks = GetPrivateProfileString(szCmd, "Type" , "" , Dtype ,10, szFile);
  344. Dtype[strlen(Dtype)] = '\0';
  345. startbit = GetPrivateProfileInt(szCmd,"startbit",0,szFile);
  346. endbit = GetPrivateProfileInt(szCmd,"endbit",0,szFile);
  347. return 0;
  348. }
  349. int cm::GetDeviceParam( CCommProcess *pComm, //串口对象指针
  350. int nAddr, //设备地址
  351. char szCmd[MAX_CMD], //功能码
  352. int &SendCmd, //
  353. int &Function, //
  354. int &LBCount, //
  355. int &Index, //
  356. int &nLen, //
  357. char Dtype[10], //
  358. int &startbit, //
  359. int &endbit, //
  360. char szRecvMsg[VAR_MSG] // 接收Buffer
  361. )
  362. {
  363. int nRet = -1;
  364. if(SendCmd == 0x05)
  365. {
  366. nRet = RequestReadStatus(pComm, SendCmd, Function, LBCount, nAddr);
  367. if( nRet != 0 )
  368. {
  369. return nRet; // 串口忙
  370. }
  371. nRet = ResponseReadStatus( nAddr,pComm, szCmd,SendCmd,Function, LBCount);
  372. }
  373. else if (SendCmd = 0x11)
  374. {
  375. nRet = RequestWriteStatus(pComm, SendCmd, Function, LBCount, nAddr);
  376. if( nRet != 0 )
  377. {
  378. return nRet; // 串口忙
  379. }
  380. nRet = ResponseWriteStatus(nAddr,pComm);
  381. }
  382. return nRet;
  383. }
  384. //
  385. //int cm::GetEmersonContrlVarMsg( char szCmd[MAX_CMD], // 命令
  386. // char szRecvMsg[VAR_MSG], // 接收Buffer
  387. // int &Index, // 变量索引,针对接收Buffer而言
  388. // int &nLen, // 变量长度
  389. // char szSendCmd[TYPE_LENGTH],
  390. // int &startbit,
  391. // int &endbit) // 变量数据类型
  392. //{
  393. // int nRet = 0;
  394. //
  395. // if( strcmp(szCmd, "cmd-1") == 0 ||
  396. // strcmp(szCmd, "cmd-2") == 0 /*||
  397. // strcmp(szCmd, "cmd-3") == 0*/
  398. // )
  399. // {
  400. //#if IS_USE_READMSG_CS
  401. // EnterCriticalSection( &m_csReadMsg );
  402. //#endif
  403. // memcpy(szRecvMsg, m_emerson_ControlMsg + Index, nLen);//命令对应的需要的数据szRecvMsg;
  404. //#if IS_USE_READMSG_CS
  405. // LeaveCriticalSection(&m_csReadMsg);
  406. //#endif
  407. //
  408. // nRet = 0;
  409. // }
  410. //
  411. // return nRet;
  412. //}
  413. int cm::GetEmersonEnquiryVarMsg(int nAddr, //address
  414. char szCmd[MAX_CMD], // 命令
  415. char szRecvMsg[VAR_MSG], // 接收Buffer
  416. int &Index, // 变量索引,针对接收Buffer而言
  417. int &nLen, // 变量长度
  418. char szSendCmd[TYPE_LENGTH] ,
  419. int &startbit,
  420. int &endbit) // 变量数据类型
  421. {
  422. int nRet = 0;
  423. if(
  424. //strcmp(szCmd, "cmd-4") == 0 ||//无bit;
  425. //strcmp(szCmd, "cmd-5") == 0 || //无bit;
  426. strcmp(szCmd, "cmd-6") == 0 ||
  427. //strcmp(szCmd, "cmd-7") == 0 ||
  428. strcmp(szCmd, "cmd-8") == 0 ||
  429. strcmp(szCmd, "cmd-9") == 0 ||
  430. strcmp(szCmd, "cmd-10") == 0 ||
  431. strcmp(szCmd, "cmd-11") == 0 ||
  432. strcmp(szCmd, "cmd-12") == 0 ||
  433. strcmp(szCmd, "cmd-13") == 0 ||
  434. strcmp(szCmd, "cmd-14") == 0 ||
  435. //strcmp(szCmd, "cmd-15") == 0 ||
  436. strcmp(szCmd, "cmd-16") == 0 ||
  437. strcmp(szCmd, "cmd-17") == 0 ||
  438. strcmp(szCmd, "cmd-18") == 0 ||
  439. strcmp(szCmd, "cmd-19") == 0 ||
  440. strcmp(szCmd, "cmd-20") == 0 ||
  441. //strcmp(szCmd, "cmd-21") == 0 || //冷水,4位;
  442. //strcmp(szCmd, "cmd-22") == 0 ||//无bit;
  443. //strcmp(szCmd, "cmd-23") == 0 ||//无bit;
  444. strcmp(szCmd, "cmd-24") == 0 ||//无bit;
  445. strcmp(szCmd, "cmd-25") == 0 ||
  446. strcmp(szCmd, "cmd-26") == 0 ||
  447. strcmp(szCmd, "cmd-27") == 0 ||
  448. strcmp(szCmd, "cmd-28") == 0 ||
  449. //strcmp(szCmd, "cmd-29") == 0 ||
  450. strcmp(szCmd, "cmd-30") == 0 ||
  451. strcmp(szCmd, "cmd-31") == 0 ||
  452. strcmp(szCmd, "cmd-32") == 0 ||
  453. strcmp(szCmd, "cmd-33") == 0 /*||*/
  454. //strcmp(szCmd, "cmd-34") == 0 ||
  455. //strcmp(szCmd, "cmd-35") == 0 ||
  456. //strcmp(szCmd, "cmd-36") == 0 ||
  457. //strcmp(szCmd, "cmd-37") == 0 ||
  458. //strcmp(szCmd, "cmd-38") == 0 ||
  459. //strcmp(szCmd, "cmd-39") == 0 //双bit;
  460. //strcmp(szCmd, "cmd-40") == 0 ||//无bit;
  461. //strcmp(szCmd, "cmd-41") == 0 ||//无bit;
  462. //strcmp(szCmd, "cmd-42") == 0//无bit;
  463. )
  464. {
  465. #if IS_USE_READMSG_CS
  466. EnterCriticalSection( &m_csReadMsg );
  467. #endif
  468. memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
  469. //LOG4C((LOG_NOTICE , "szRevcMsg = %s",szRecvMsg));
  470. GetBitStatu(szRecvMsg,startbit,endbit);
  471. #if IS_USE_READMSG_CS
  472. LeaveCriticalSection(&m_csReadMsg);
  473. #endif
  474. nRet = 0;
  475. }
  476. if (
  477. strcmp(szCmd, "cmd-4") == 0 //温度设定
  478. )
  479. {
  480. #if IS_USE_READMSG_CS
  481. EnterCriticalSection( &m_csReadMsg );
  482. #endif
  483. memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
  484. int Temp = 17 + (szRecvMsg[0] & 0xf0)/16;
  485. sprintf(szRecvMsg,"%d",Temp);
  486. #if IS_USE_READMSG_CS
  487. LeaveCriticalSection(&m_csReadMsg);
  488. #endif
  489. nRet = 0;
  490. }
  491. if (
  492. strcmp(szCmd, "cmd-5") == 0 //湿度设定
  493. )
  494. {
  495. #if IS_USE_READMSG_CS
  496. EnterCriticalSection( &m_csReadMsg );
  497. #endif
  498. memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
  499. int Hum = 42 + (szRecvMsg[0] & 0x0f) * 2;
  500. sprintf(szRecvMsg,"%d",Hum);
  501. #if IS_USE_READMSG_CS
  502. LeaveCriticalSection(&m_csReadMsg);
  503. #endif
  504. nRet = 0;
  505. }
  506. if(
  507. strcmp(szCmd, "cmd-22") == 0 ||//BCD温度 无bit;
  508. strcmp(szCmd, "cmd-23") == 0 //BCD湿度 无bit;
  509. )
  510. {
  511. #if IS_USE_READMSG_CS
  512. EnterCriticalSection( &m_csReadMsg );
  513. #endif
  514. memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
  515. BCDtofloat(szRecvMsg);
  516. //LOG4C((LOG_NOTICE, "BCDtofloat = %",szRecvMsg));
  517. #if IS_USE_READMSG_CS
  518. LeaveCriticalSection(&m_csReadMsg);
  519. #endif
  520. nRet = 0;
  521. }
  522. // if (
  523. // strcmp(szCmd, "cmd-40") == 0 ||//not bit SendCmd
  524. // strcmp(szCmd, "cmd-41") == 0 ||
  525. // strcmp(szCmd, "cmd-42") == 0
  526. // )
  527. // {
  528. //#if IS_USE_READMSG_CS
  529. // EnterCriticalSection( &m_csReadMsg );
  530. //#endif
  531. // memcpy(szRecvMsg, m_emerson_QeuryMsg + Index,nLen);//命令对应的需要的数据szRecvMsg;
  532. //#if IS_USE_READMSG_CS
  533. // LeaveCriticalSection(&m_csReadMsg);
  534. //#endif
  535. // }
  536. return nRet;
  537. }
  538. //void cm::SetEmersonContrlVarMsg( char szCmd[MAX_CMD], char *pBuffer)
  539. //{
  540. // if( strcmp(szCmd, "cmd-1") == 0 ||
  541. // strcmp(szCmd, "cmd-2") == 0 /*||
  542. // strcmp(szCmd, "cmd-3") == 0 */
  543. // )
  544. // {
  545. //#if IS_USE_READMSG_CS
  546. // EnterCriticalSection( &m_csReadMsg );
  547. //#endif
  548. // memcpy(m_emerson_ControlMsg, pBuffer, sizeof(m_emerson_ControlMsg));
  549. //#if IS_USE_READMSG_CS
  550. // LeaveCriticalSection(&m_csReadMsg);
  551. //#endif
  552. // }
  553. //}
  554. void cm::SetEmersonEnquiryVarMsg( int nAddr,char szCmd[MAX_CMD], char *pBuffer)
  555. {
  556. if (strcmp(szCmd, "cmd-4") == 0 ||
  557. strcmp(szCmd, "cmd-5") == 0 ||
  558. strcmp(szCmd, "cmd-6") == 0 ||
  559. //strcmp(szCmd, "cmd-7") == 0 ||
  560. strcmp(szCmd, "cmd-8") == 0 ||
  561. strcmp(szCmd, "cmd-9") == 0 ||
  562. strcmp(szCmd, "cmd-10") == 0 ||
  563. strcmp(szCmd, "cmd-11") == 0 ||
  564. strcmp(szCmd, "cmd-12") == 0 ||
  565. strcmp(szCmd, "cmd-13") == 0 ||
  566. strcmp(szCmd, "cmd-14") == 0 ||
  567. //strcmp(szCmd, "cmd-15") == 0 ||
  568. strcmp(szCmd, "cmd-16") == 0 ||
  569. strcmp(szCmd, "cmd-17") == 0 ||
  570. strcmp(szCmd, "cmd-18") == 0 ||
  571. strcmp(szCmd, "cmd-19") == 0 ||
  572. strcmp(szCmd, "cmd-20") == 0 ||
  573. //strcmp(szCmd, "cmd-21") == 0 ||
  574. strcmp(szCmd, "cmd-22") == 0 ||
  575. strcmp(szCmd, "cmd-23") == 0 ||
  576. strcmp(szCmd, "cmd-24") == 0 ||
  577. strcmp(szCmd, "cmd-25") == 0 ||
  578. strcmp(szCmd, "cmd-26") == 0 ||
  579. strcmp(szCmd, "cmd-27") == 0 ||
  580. strcmp(szCmd, "cmd-28") == 0 ||
  581. //strcmp(szCmd, "cmd-29") == 0 ||
  582. strcmp(szCmd, "cmd-30") == 0 ||
  583. strcmp(szCmd, "cmd-31") == 0 ||
  584. strcmp(szCmd, "cmd-32") == 0 ||
  585. strcmp(szCmd, "cmd-33") == 0 /*||
  586. strcmp(szCmd, "cmd-34") == 0 ||
  587. strcmp(szCmd, "cmd-35") == 0 ||
  588. strcmp(szCmd, "cmd-36") == 0 ||
  589. strcmp(szCmd, "cmd-37") == 0 ||
  590. strcmp(szCmd, "cmd-38") == 0 ||
  591. strcmp(szCmd, "cmd-39") == 0 ||
  592. strcmp(szCmd, "cmd-40") == 0 ||
  593. strcmp(szCmd, "cmd-41") == 0 */
  594. )
  595. {
  596. #if IS_USE_READMSG_CS
  597. EnterCriticalSection( &m_csReadMsg );
  598. #endif
  599. memcpy(m_emerson_QeuryMsg[nAddr-1], pBuffer, sizeof(m_emerson_QeuryMsg[nAddr-1]));
  600. #if IS_USE_READMSG_CS
  601. LeaveCriticalSection(&m_csReadMsg);
  602. #endif
  603. }
  604. }
  605. BOOL cm::ChkSumCheck(char szSendMsg[],int SendCmd/*,int nReadLen*/)
  606. {
  607. //int len = (int)strlen(szSendMsg);
  608. unsigned int nData = 0;
  609. int i = 0;
  610. unsigned int nSumCrc = (unsigned int)szSendMsg[28];
  611. switch (SendCmd)
  612. {
  613. case 0x05:
  614. for (i = 0;i < 28;i++)
  615. {
  616. nData += szSendMsg[i];
  617. }
  618. nData = nData & 0x00ff;
  619. if ( (char)nData != szSendMsg[i] )
  620. {
  621. //LOG4C((LOG_NOTICE, "nData=%d, szSendMsg[28]=%d, sumcrc = %d", nData, (unsigned int)szSendMsg[28], nSumCrc));
  622. LOG4C_HEX_DUMP((LOG_NOTICE, szSendMsg, 29));
  623. return FALSE;
  624. }
  625. break;
  626. case 0x11:
  627. for(i = 0;i < 4;i++)
  628. {
  629. nData +=szSendMsg[i];
  630. }
  631. nData = nData & 0x00ff;
  632. if ((unsigned int)nData != (unsigned int)szSendMsg[i])
  633. {
  634. return FALSE;
  635. }
  636. break;
  637. default:
  638. break;
  639. }
  640. return TRUE;
  641. }
  642. void cm:: GetBitStatu( char *bytValue,int startbit,int endbit)
  643. {
  644. int ByteBit = 0;
  645. char tempbyte[9] = {0};
  646. //int Len = sizeof(bytValue);
  647. if (startbit == endbit)
  648. {
  649. switch(startbit)
  650. {
  651. case 0:
  652. ByteBit = bytValue[0] & 0x01;
  653. break;
  654. case 1:
  655. ByteBit = (bytValue[0] & 0x02) / 0x02 ;
  656. break;
  657. case 2:
  658. ByteBit = (bytValue[0] & 0x04) / 0x04 ;
  659. break;
  660. case 3:
  661. ByteBit = (bytValue[0] & 0x08) / 0x08 ;
  662. break;
  663. case 4:
  664. ByteBit = (bytValue[0] & 0x10) / 0x10 ;
  665. break;
  666. case 5:
  667. ByteBit = (bytValue[0] & 0x20) / 0x20 ;
  668. break;
  669. case 6:
  670. ByteBit = (bytValue[0] & 0x40) / 0x40 ;
  671. break;
  672. case 7:
  673. ByteBit = (bytValue[0] & 0x80) / 0x80;
  674. break;
  675. }
  676. _snprintf(bytValue,sizeof(bytValue),"%d",ByteBit);
  677. //itoa(ByteBit,&bytValue,10);
  678. }
  679. else
  680. {
  681. int tempbit = startbit;
  682. int TiLen = endbit - startbit;
  683. for (int i = 0; i < TiLen;i++)
  684. {
  685. tempbit = tempbit + i;
  686. switch(tempbit)
  687. {
  688. case 0:
  689. ByteBit = bytValue[0] & 0x01;
  690. break;
  691. case 1:
  692. ByteBit = (bytValue[0] & 0x02) / 0x02 ;
  693. break;
  694. case 2:
  695. ByteBit = (bytValue[0] & 0x04) / 0x04 ;
  696. break;
  697. case 3:
  698. ByteBit = (bytValue[0] & 0x08) / 0x08 ;
  699. break;
  700. case 4:
  701. ByteBit = (bytValue[0] & 0x10) / 0x10 ;
  702. break;
  703. case 5:
  704. ByteBit = (bytValue[0] & 0x20) / 0x20 ;
  705. break;
  706. case 6:
  707. ByteBit = (bytValue[0] & 0x40) / 0x40 ;
  708. break;
  709. case 7:
  710. ByteBit = (bytValue[0] & 0x80) / 0x80;
  711. break;
  712. }
  713. _snprintf(tempbyte+i,sizeof(tempbyte),"%d",ByteBit);
  714. //itoa(ByteBit,&bytValue,10);
  715. }
  716. bytValue = tempbyte;
  717. }
  718. }
  719. void cm::BCDtofloat(char szRecvbuffer[VAR_MSG])
  720. {
  721. int Lvalue;
  722. char BufferLd[20] = {0};
  723. char BufferHd[20] = {0};
  724. //int Len = sizeof(szRecvbuffer)-1;
  725. BYTE TempChar[2] = {0};
  726. TempChar[0] = szRecvbuffer[0];
  727. TempChar[1] = szRecvbuffer[1];
  728. CString Hstr,Lstr,Sumstr;
  729. Hstr.Format("%0x",TempChar[0]);
  730. if (TempChar[1] < 10)
  731. {
  732. Lstr.Format("0%0x",TempChar[1]);
  733. }
  734. else
  735. Lstr.Format("%0x",TempChar[1]);
  736. Sumstr = Hstr + Lstr;
  737. _snprintf(szRecvbuffer, sizeof(szRecvbuffer), "%s",Sumstr);
  738. }
  739. void cm::SimulationCommData()
  740. {
  741. //
  742. //LOG4C((LOG_NOTICE, "模拟数据---------"));
  743. m_emerson_QeuryMsg[0][0] =0xF0;
  744. m_emerson_QeuryMsg[0][1] =0x06;
  745. m_emerson_QeuryMsg[0][2] =0x1A;
  746. m_emerson_QeuryMsg[0][3] =0x00;
  747. m_emerson_QeuryMsg[0][4] =0x02;
  748. m_emerson_QeuryMsg[0][5] =0x09;
  749. m_emerson_QeuryMsg[0][6] =0x04;
  750. m_emerson_QeuryMsg[0][7] =0x49;
  751. m_emerson_QeuryMsg[0][8] =0xFF;
  752. m_emerson_QeuryMsg[0][9] =0xF3;
  753. m_emerson_QeuryMsg[0][10]=0xFF;
  754. m_emerson_QeuryMsg[0][11]=0x44;
  755. m_emerson_QeuryMsg[0][12]=0x00;
  756. m_emerson_QeuryMsg[0][13]=0x08;
  757. m_emerson_QeuryMsg[0][14]=0x00;
  758. m_emerson_QeuryMsg[0][15]=0x00;
  759. m_emerson_QeuryMsg[0][16]=0x63;
  760. m_emerson_QeuryMsg[0][17]=0x08;
  761. m_emerson_QeuryMsg[0][18]=0xf0;
  762. m_emerson_QeuryMsg[0][19]=0x14;
  763. m_emerson_QeuryMsg[0][20]=0x01;
  764. m_emerson_QeuryMsg[0][21]=0x01;
  765. m_emerson_QeuryMsg[0][22]=0x08;
  766. m_emerson_QeuryMsg[0][23]=0x430;
  767. m_emerson_QeuryMsg[0][24]=0x0A;
  768. m_emerson_QeuryMsg[0][25]=0x4B;
  769. m_emerson_QeuryMsg[0][26]=0x10;
  770. m_emerson_QeuryMsg[0][27]=0x05;
  771. m_emerson_QeuryMsg[0][28]=0xDB;
  772. }