复件 Emerson_CM.cpp 22 KB

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