Emerson_CM.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. // Emerson_CM.cpp: implementation of the Emerson_CM class.
  2. //////////////////////////////////////////////////////////////////////////////
  3. ////// //////
  4. ////// 文 件: Emerson_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 "Emerson_CM.h"
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[]=__FILE__;
  21. #define new DEBUG_NEW
  22. #endif
  23. Emerson_CM::Emerson_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. Emerson_CM::~Emerson_CM()
  47. {
  48. #if IS_USE_READMSG_CS
  49. DeleteCriticalSection( &m_csReadMsg );
  50. #endif
  51. MTVERIFY( CloseHandle( m_hSemComm ) );
  52. CloseComm();
  53. }
  54. BOOL Emerson_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 Emerson_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 Emerson_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 Emerson_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 Emerson_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 Emerson_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 Emerson_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 Emerson_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 Emerson_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. int Emerson_CM::GetEmersonEnquiryVarMsg(int nAddr, //address
  385. 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. if(
  395. //strcmp(szCmd, "cmd-4") == 0 ||//无bit;
  396. //strcmp(szCmd, "cmd-5") == 0 || //无bit;
  397. strcmp(szCmd, "cmd-6") == 0 ||
  398. //strcmp(szCmd, "cmd-7") == 0 ||
  399. strcmp(szCmd, "cmd-8") == 0 ||
  400. strcmp(szCmd, "cmd-9") == 0 ||
  401. strcmp(szCmd, "cmd-10") == 0 ||
  402. strcmp(szCmd, "cmd-11") == 0 ||
  403. strcmp(szCmd, "cmd-12") == 0 ||
  404. strcmp(szCmd, "cmd-13") == 0 ||
  405. strcmp(szCmd, "cmd-14") == 0 ||
  406. //strcmp(szCmd, "cmd-15") == 0 ||
  407. strcmp(szCmd, "cmd-16") == 0 ||
  408. strcmp(szCmd, "cmd-17") == 0 ||
  409. strcmp(szCmd, "cmd-18") == 0 ||
  410. strcmp(szCmd, "cmd-19") == 0 ||
  411. strcmp(szCmd, "cmd-20") == 0 ||
  412. //strcmp(szCmd, "cmd-21") == 0 || //冷水,4位;
  413. //strcmp(szCmd, "cmd-22") == 0 ||//无bit;
  414. //strcmp(szCmd, "cmd-23") == 0 ||//无bit;
  415. strcmp(szCmd, "cmd-24") == 0 ||//无bit;
  416. strcmp(szCmd, "cmd-25") == 0 ||
  417. strcmp(szCmd, "cmd-26") == 0 ||
  418. strcmp(szCmd, "cmd-27") == 0 ||
  419. strcmp(szCmd, "cmd-28") == 0 ||
  420. //strcmp(szCmd, "cmd-29") == 0 ||
  421. strcmp(szCmd, "cmd-30") == 0 ||
  422. strcmp(szCmd, "cmd-31") == 0 ||
  423. strcmp(szCmd, "cmd-32") == 0 ||
  424. strcmp(szCmd, "cmd-33") == 0 /*||*/
  425. //strcmp(szCmd, "cmd-34") == 0 ||
  426. //strcmp(szCmd, "cmd-35") == 0 ||
  427. //strcmp(szCmd, "cmd-36") == 0 ||
  428. //strcmp(szCmd, "cmd-37") == 0 ||
  429. //strcmp(szCmd, "cmd-38") == 0 ||
  430. //strcmp(szCmd, "cmd-39") == 0 //双bit;
  431. //strcmp(szCmd, "cmd-40") == 0 ||//无bit;
  432. //strcmp(szCmd, "cmd-41") == 0 ||//无bit;
  433. //strcmp(szCmd, "cmd-42") == 0//无bit;
  434. )
  435. {
  436. #if IS_USE_READMSG_CS
  437. EnterCriticalSection( &m_csReadMsg );
  438. #endif
  439. memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
  440. //LOG4C((LOG_NOTICE , "szRevcMsg = %s",szRecvMsg));
  441. GetBitStatu(szRecvMsg,startbit,endbit);
  442. #if IS_USE_READMSG_CS
  443. LeaveCriticalSection(&m_csReadMsg);
  444. #endif
  445. nRet = 0;
  446. }
  447. if (
  448. strcmp(szCmd, "cmd-4") == 0 //温度设定
  449. )
  450. {
  451. #if IS_USE_READMSG_CS
  452. EnterCriticalSection( &m_csReadMsg );
  453. #endif
  454. memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
  455. int Temp = 17 + (szRecvMsg[0] & 0xf0)/16;
  456. sprintf(szRecvMsg,"%d",Temp);
  457. #if IS_USE_READMSG_CS
  458. LeaveCriticalSection(&m_csReadMsg);
  459. #endif
  460. nRet = 0;
  461. }
  462. if (
  463. strcmp(szCmd, "cmd-5") == 0 //湿度设定
  464. )
  465. {
  466. #if IS_USE_READMSG_CS
  467. EnterCriticalSection( &m_csReadMsg );
  468. #endif
  469. memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
  470. int Hum = 42 + (szRecvMsg[0] & 0x0f) * 2;
  471. sprintf(szRecvMsg,"%d",Hum);
  472. #if IS_USE_READMSG_CS
  473. LeaveCriticalSection(&m_csReadMsg);
  474. #endif
  475. nRet = 0;
  476. }
  477. if(
  478. strcmp(szCmd, "cmd-22") == 0 ||//BCD温度 无bit;
  479. strcmp(szCmd, "cmd-23") == 0 //BCD湿度 无bit;
  480. )
  481. {
  482. #if IS_USE_READMSG_CS
  483. EnterCriticalSection( &m_csReadMsg );
  484. #endif
  485. memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
  486. BCDtofloat(szRecvMsg);
  487. //LOG4C((LOG_NOTICE, "BCDtofloat = %",szRecvMsg));
  488. #if IS_USE_READMSG_CS
  489. LeaveCriticalSection(&m_csReadMsg);
  490. #endif
  491. nRet = 0;
  492. }
  493. // if (
  494. // strcmp(szCmd, "cmd-40") == 0 ||//not bit SendCmd
  495. // strcmp(szCmd, "cmd-41") == 0 ||
  496. // strcmp(szCmd, "cmd-42") == 0
  497. // )
  498. // {
  499. //#if IS_USE_READMSG_CS
  500. // EnterCriticalSection( &m_csReadMsg );
  501. //#endif
  502. // memcpy(szRecvMsg, m_emerson_QeuryMsg + Index,nLen);//命令对应的需要的数据szRecvMsg;
  503. //#if IS_USE_READMSG_CS
  504. // LeaveCriticalSection(&m_csReadMsg);
  505. //#endif
  506. // }
  507. return nRet;
  508. }
  509. void Emerson_CM::SetEmersonEnquiryVarMsg( int nAddr,char szCmd[MAX_CMD], char *pBuffer)
  510. {
  511. if (strcmp(szCmd, "cmd-4") == 0 ||
  512. strcmp(szCmd, "cmd-5") == 0 ||
  513. strcmp(szCmd, "cmd-6") == 0 ||
  514. //strcmp(szCmd, "cmd-7") == 0 ||
  515. strcmp(szCmd, "cmd-8") == 0 ||
  516. strcmp(szCmd, "cmd-9") == 0 ||
  517. strcmp(szCmd, "cmd-10") == 0 ||
  518. strcmp(szCmd, "cmd-11") == 0 ||
  519. strcmp(szCmd, "cmd-12") == 0 ||
  520. strcmp(szCmd, "cmd-13") == 0 ||
  521. strcmp(szCmd, "cmd-14") == 0 ||
  522. //strcmp(szCmd, "cmd-15") == 0 ||
  523. strcmp(szCmd, "cmd-16") == 0 ||
  524. strcmp(szCmd, "cmd-17") == 0 ||
  525. strcmp(szCmd, "cmd-18") == 0 ||
  526. strcmp(szCmd, "cmd-19") == 0 ||
  527. strcmp(szCmd, "cmd-20") == 0 ||
  528. //strcmp(szCmd, "cmd-21") == 0 ||
  529. strcmp(szCmd, "cmd-22") == 0 ||
  530. strcmp(szCmd, "cmd-23") == 0 ||
  531. strcmp(szCmd, "cmd-24") == 0 ||
  532. strcmp(szCmd, "cmd-25") == 0 ||
  533. strcmp(szCmd, "cmd-26") == 0 ||
  534. strcmp(szCmd, "cmd-27") == 0 ||
  535. strcmp(szCmd, "cmd-28") == 0 ||
  536. //strcmp(szCmd, "cmd-29") == 0 ||
  537. strcmp(szCmd, "cmd-30") == 0 ||
  538. strcmp(szCmd, "cmd-31") == 0 ||
  539. strcmp(szCmd, "cmd-32") == 0 ||
  540. strcmp(szCmd, "cmd-33") == 0 /*||
  541. strcmp(szCmd, "cmd-34") == 0 ||
  542. strcmp(szCmd, "cmd-35") == 0 ||
  543. strcmp(szCmd, "cmd-36") == 0 ||
  544. strcmp(szCmd, "cmd-37") == 0 ||
  545. strcmp(szCmd, "cmd-38") == 0 ||
  546. strcmp(szCmd, "cmd-39") == 0 ||
  547. strcmp(szCmd, "cmd-40") == 0 ||
  548. strcmp(szCmd, "cmd-41") == 0 */
  549. )
  550. {
  551. #if IS_USE_READMSG_CS
  552. EnterCriticalSection( &m_csReadMsg );
  553. #endif
  554. memcpy(m_emerson_QeuryMsg[nAddr-1], pBuffer, sizeof(m_emerson_QeuryMsg[nAddr-1]));
  555. #if IS_USE_READMSG_CS
  556. LeaveCriticalSection(&m_csReadMsg);
  557. #endif
  558. }
  559. }
  560. BOOL Emerson_CM::ChkSumCheck(char szSendMsg[],int SendCmd/*,int nReadLen*/)
  561. {
  562. //int len = (int)strlen(szSendMsg);
  563. unsigned int nData = 0;
  564. int i = 0;
  565. unsigned int nSumCrc = (unsigned int)szSendMsg[28];
  566. switch (SendCmd)
  567. {
  568. case 0x05:
  569. for (i = 0;i < 28;i++)
  570. {
  571. nData += szSendMsg[i];
  572. }
  573. nData = nData & 0x00ff;
  574. if ( (char)nData != szSendMsg[i] )
  575. {
  576. //LOG4C((LOG_NOTICE, "nData=%d, szSendMsg[28]=%d, sumcrc = %d", nData, (unsigned int)szSendMsg[28], nSumCrc));
  577. LOG4C_HEX_DUMP((LOG_NOTICE, szSendMsg, 29));
  578. return FALSE;
  579. }
  580. break;
  581. case 0x11:
  582. for(i = 0;i < 4;i++)
  583. {
  584. nData +=szSendMsg[i];
  585. }
  586. nData = nData & 0x00ff;
  587. if ((unsigned int)nData != (unsigned int)szSendMsg[i])
  588. {
  589. return FALSE;
  590. }
  591. break;
  592. default:
  593. break;
  594. }
  595. return TRUE;
  596. }
  597. void Emerson_CM:: GetBitStatu( char *bytValue,int startbit,int endbit)
  598. {
  599. int ByteBit = 0;
  600. char tempbyte[9] = {0};
  601. //int Len = sizeof(bytValue);
  602. if (startbit == endbit)
  603. {
  604. switch(startbit)
  605. {
  606. /*
  607. case 7:
  608. ByteBit = bytValue[0] & 0x01;
  609. break;
  610. case 6:
  611. ByteBit = (bytValue[0] & 0x02) / 0x02 ;
  612. break;
  613. case 5:
  614. ByteBit = (bytValue[0] & 0x04) / 0x04 ;
  615. break;
  616. case 4:
  617. ByteBit = (bytValue[0] & 0x08) / 0x08 ;
  618. break;
  619. case 3:
  620. ByteBit = (bytValue[0] & 0x10) / 0x10 ;
  621. break;
  622. case 2:
  623. ByteBit = (bytValue[0] & 0x20) / 0x20 ;
  624. break;
  625. case 1:
  626. ByteBit = (bytValue[0] & 0x40) / 0x40 ;
  627. break;
  628. case 0:
  629. ByteBit = (bytValue[0] & 0x80) / 0x80;
  630. break;
  631. }*/
  632. case 0:
  633. ByteBit = bytValue[0] & 0x01;
  634. break;
  635. case 1:
  636. ByteBit = (bytValue[0] & 0x02) / 0x02 ;
  637. break;
  638. case 2:
  639. ByteBit = (bytValue[0] & 0x04) / 0x04 ;
  640. break;
  641. case 3:
  642. ByteBit = (bytValue[0] & 0x08) / 0x08 ;
  643. break;
  644. case 4:
  645. ByteBit = (bytValue[0] & 0x10) / 0x10 ;
  646. break;
  647. case 5:
  648. ByteBit = (bytValue[0] & 0x20) / 0x20 ;
  649. break;
  650. case 6:
  651. ByteBit = (bytValue[0] & 0x40) / 0x40 ;
  652. break;
  653. case 7:
  654. ByteBit = (bytValue[0] & 0x80) / 0x80;
  655. break;
  656. }
  657. _snprintf(bytValue,sizeof(bytValue),"%d",ByteBit);
  658. //itoa(ByteBit,&bytValue,10);
  659. }
  660. else
  661. {
  662. int tempbit = startbit;
  663. int TiLen = endbit - startbit;
  664. for (int i = 0; i < TiLen;i++)
  665. {
  666. tempbit = tempbit + i;
  667. switch(tempbit)
  668. {
  669. /*case 7:
  670. ByteBit = bytValue[0] & 0x01;
  671. break;
  672. case 6:
  673. ByteBit = (bytValue[0] & 0x02) / 0x02 ;
  674. break;
  675. case 5:
  676. ByteBit = (bytValue[0] & 0x04) / 0x04 ;
  677. break;
  678. case 4:
  679. ByteBit = (bytValue[0] & 0x08) / 0x08 ;
  680. break;
  681. case 3:
  682. ByteBit = (bytValue[0] & 0x10) / 0x10 ;
  683. break;
  684. case 2:
  685. ByteBit = (bytValue[0] & 0x20) / 0x20 ;
  686. break;
  687. case 1:
  688. ByteBit = (bytValue[0] & 0x40) / 0x40 ;
  689. break;
  690. case 0:
  691. ByteBit = (bytValue[0] & 0x80) / 0x80;
  692. break;
  693. }*/
  694. case 0:
  695. ByteBit = bytValue[0] & 0x01;
  696. break;
  697. case 1:
  698. ByteBit = (bytValue[0] & 0x02) / 0x02 ;
  699. break;
  700. case 2:
  701. ByteBit = (bytValue[0] & 0x04) / 0x04 ;
  702. break;
  703. case 3:
  704. ByteBit = (bytValue[0] & 0x08) / 0x08 ;
  705. break;
  706. case 4:
  707. ByteBit = (bytValue[0] & 0x10) / 0x10 ;
  708. break;
  709. case 5:
  710. ByteBit = (bytValue[0] & 0x20) / 0x20 ;
  711. break;
  712. case 6:
  713. ByteBit = (bytValue[0] & 0x40) / 0x40 ;
  714. break;
  715. case 7:
  716. ByteBit = (bytValue[0] & 0x80) / 0x80;
  717. break;
  718. }
  719. _snprintf(tempbyte+i,sizeof(tempbyte),"%d",ByteBit);
  720. //itoa(ByteBit,&bytValue,10);
  721. }
  722. bytValue = tempbyte;
  723. }
  724. }
  725. void Emerson_CM::BCDtofloat(char szRecvbuffer[VAR_MSG])
  726. {
  727. int Lvalue;
  728. char BufferLd[20] = {0};
  729. char BufferHd[20] = {0};
  730. //int Len = sizeof(szRecvbuffer)-1;
  731. BYTE TempChar[2] = {0};
  732. TempChar[0] = szRecvbuffer[0];
  733. TempChar[1] = szRecvbuffer[1];
  734. CString Hstr,Lstr,Sumstr;
  735. Hstr.Format("%0x",TempChar[0]);
  736. if (TempChar[1] < 10)
  737. {
  738. Lstr.Format("0%0x",TempChar[1]);
  739. }
  740. else
  741. Lstr.Format("%0x",TempChar[1]);
  742. Sumstr = Hstr + Lstr;
  743. _snprintf(szRecvbuffer, sizeof(szRecvbuffer), "%s",Sumstr);
  744. }
  745. void Emerson_CM::SimulationCommData()
  746. {
  747. //
  748. //LOG4C((LOG_NOTICE, "模拟数据---------"));
  749. m_emerson_QeuryMsg[0][0] =0xF0;
  750. m_emerson_QeuryMsg[0][1] =0x06;
  751. m_emerson_QeuryMsg[0][2] =0x1A;
  752. m_emerson_QeuryMsg[0][3] =0x00;
  753. m_emerson_QeuryMsg[0][4] =0x02;
  754. m_emerson_QeuryMsg[0][5] =0x09;
  755. m_emerson_QeuryMsg[0][6] =0x04;
  756. m_emerson_QeuryMsg[0][7] =0x49;
  757. m_emerson_QeuryMsg[0][8] =0xFF;
  758. m_emerson_QeuryMsg[0][9] =0xF3;
  759. m_emerson_QeuryMsg[0][10]=0xFF;
  760. m_emerson_QeuryMsg[0][11]=0x44;
  761. m_emerson_QeuryMsg[0][12]=0x00;
  762. m_emerson_QeuryMsg[0][13]=0x08;
  763. m_emerson_QeuryMsg[0][14]=0x00;
  764. m_emerson_QeuryMsg[0][15]=0x00;
  765. m_emerson_QeuryMsg[0][16]=0x63;
  766. m_emerson_QeuryMsg[0][17]=0x08;
  767. m_emerson_QeuryMsg[0][18]=0xf0;
  768. m_emerson_QeuryMsg[0][19]=0x14;
  769. m_emerson_QeuryMsg[0][20]=0x01;
  770. m_emerson_QeuryMsg[0][21]=0x01;
  771. m_emerson_QeuryMsg[0][22]=0x08;
  772. m_emerson_QeuryMsg[0][23]=0x430;
  773. m_emerson_QeuryMsg[0][24]=0x0A;
  774. m_emerson_QeuryMsg[0][25]=0x4B;
  775. m_emerson_QeuryMsg[0][26]=0x10;
  776. m_emerson_QeuryMsg[0][27]=0x05;
  777. m_emerson_QeuryMsg[0][28]=0xDB;
  778. }