Tmaster.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: Tmaster.cpp //////
  4. ////// 作 者: Wangjianfeng //////
  5. ////// 创建时间: //////
  6. ////// 说 明: Tmaster约顿空调协议 //////
  7. ////// //////
  8. ////// 修改时间:2011-07-23 //////
  9. ////// 修改说明:数据转换 //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. //////////////////////////////////////////////////////////////////////
  13. #include "stdafx.h"
  14. #include "CommProcess.h"
  15. #include "Global.h"
  16. #include ".\tmaster.h"
  17. CTmaster::CTmaster(
  18. char szPath[MAX_PATH], // 程序所在路径 ;
  19. char szIniName[MAX_PATH], // 配置文件名称;
  20. int nCommPort, // 串行端口;
  21. int nAddr, // 设备地址;
  22. int nRate, // 波特率 ;
  23. int nDataBit, // 数据位;
  24. int nStopBit, // 停止位;
  25. int nParity, // 校验位;
  26. int nInterval // 时间间隔 ;
  27. )
  28. {
  29. #if IS_USE_READMSG_CS
  30. InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
  31. #endif
  32. MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  33. for(int i = 0;i < MAX_ADDR;i++)
  34. {
  35. memset(m_szE0Msg[i], 0, sizeof(m_szE0Msg[i]));
  36. memset(m_szSend49Msg[i], 0, sizeof(m_szSend49Msg[i]));
  37. memset(m_szSend45Msg[i], 0, sizeof(m_szSend45Msg[i]));
  38. m_devOnline[i] = TRUE;
  39. m_dwOnlineTick[i] = 0;
  40. }
  41. }
  42. CTmaster::~CTmaster()
  43. {
  44. #if IS_USE_READMSG_CS
  45. DeleteCriticalSection( &m_csReadMsg );
  46. #endif
  47. MTVERIFY( CloseHandle( m_hSemComm ) );
  48. CloseComm();
  49. }
  50. int CTmaster::GetULFromIni(
  51. char szPath[MAX_PATH], //服务器程序所在目录
  52. char szIniName[MAX_PATH], //配置文件名
  53. char szCmd[MAX_CMD], //命令
  54. char szSendMsg[MAX_CMD], //发送Buffer
  55. char nType[MAX_CMD],
  56. int &nIndex,
  57. int &nLen,
  58. int &startbit,
  59. int &endbit,
  60. char szLength[5],
  61. int &nCDT)
  62. {
  63. CHAR szFile[MAX_PATH + 1] = "";
  64. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  65. GetPrivateProfileString(szCmd, "SendCmd", "", szSendMsg, 10, szFile);
  66. szSendMsg[strlen(szSendMsg)] = '\0';
  67. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  68. nType[strlen(nType)] = '\0';
  69. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  70. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  71. startbit = GetPrivateProfileInt(szCmd, "startbit", 0, szFile);
  72. endbit = GetPrivateProfileInt(szCmd, "endbit", 0, szFile);
  73. GetPrivateProfileString(szCmd, "LENGTH", "", szLength, 10, szFile);
  74. szLength[strlen(szLength)] = '\0';
  75. nCDT = GetPrivateProfileInt(szCmd, "CDT", 0, szFile);
  76. return 0;
  77. }
  78. BOOL CTmaster::AirTmasterOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  79. {
  80. BOOL bResult = FALSE;
  81. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  82. return bResult;
  83. }
  84. int CTmaster::SendReadRequest(
  85. char szPath[MAX_PATH],
  86. char szIniName[MAX_PATH],
  87. int nCommPort,
  88. int nAddr,
  89. char szCmd[MAX_CMD],
  90. char szMsg[VAR_MSG],
  91. int nReversed1,
  92. int nReversed2,
  93. int nReversed3,
  94. int nReversed4,
  95. int nReversed5,
  96. float fReversed1,
  97. float fReversed2,
  98. float fReversed3,
  99. char szReversed1[MAX_RESERVED1],
  100. char szReversed2[MAX_RESERVED2],
  101. char szReversed3[MAX_RESERVED3],
  102. char szReversed4[MAX_RESERVED4],
  103. char szReversed5[MAX_RESERVED5]
  104. )
  105. {
  106. int nIndex = 0, nLen = 0;
  107. int startbit = 0,endbit = 0;
  108. char szSendMsg[_VAR_MSG] = {0};
  109. char szType[TYPE_LENGTH] = {0};
  110. char szLength[5] = {0};
  111. int nCDT = 0;
  112. GetULFromIni(szPath, szIniName, szCmd,szSendMsg, szType, nIndex, nLen,startbit,endbit,szLength,nCDT);
  113. CCommProcess *pComm = FindComm(nCommPort);
  114. if( pComm == NULL ) return -1;
  115. int nRet = -1;
  116. if (
  117. (strlen(m_szE0Msg[nAddr-1]) ==0 &&
  118. (
  119. strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  120. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  121. strcmp(szCmd, "cmd-5") == 0 || strcmp(szCmd, "cmd-6") == 0 ||
  122. strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 ||
  123. strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 ||
  124. strcmp(szCmd, "cmd-11") == 0 ||
  125. strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
  126. strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  127. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
  128. strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
  129. strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
  130. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
  131. strcmp(szCmd, "cmd-24") == 0
  132. )) || strcmp(szCmd, "cmd-1") == 0
  133. )
  134. {
  135. nRet = GetDeviceParam( nAddr,pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType,startbit,endbit );
  136. if( nRet != 0 ) return nRet;
  137. }
  138. if( GetTickCount() - m_dwOnlineTick[nAddr-1] > 60 *1000 && m_dwOnlineTick[nAddr-1] > 0 )
  139. {
  140. m_devOnline[nAddr-1] = FALSE;
  141. }
  142. else if( GetTickCount() - m_dwOnlineTick[nAddr-1] < 60 *1000 && m_dwOnlineTick[nAddr-1] > 0 )
  143. {
  144. m_devOnline[nAddr-1] = TRUE;
  145. }
  146. if( m_devOnline[nAddr-1] == FALSE )
  147. {
  148. return -1;
  149. }
  150. nRet = GetE0VarMsg(szCmd, szMsg, nAddr, nIndex, nLen, szType,startbit,endbit);
  151. LOG4C((LOG_NOTICE, "szCmd = %s, szMsg = %s ,nIndex = %d , startbit = %d, endbit = %d ,return nRet = %d", szCmd, szMsg,nIndex,startbit,endbit, nRet));
  152. return nRet;
  153. }
  154. int CTmaster::GetDeviceParam(
  155. int nAddr,
  156. CCommProcess *pComm, // 串口对象指针
  157. char szSendMsg[MAX_INFO], // 发送Buffer
  158. char szCmd[MAX_CMD], // 命令
  159. char szRecvMsg[VAR_MSG], // 接收Buffer
  160. int &nIndex, // 变量索引,针对接收Buffer而言
  161. int &nLen, // 变量长度
  162. char szType[TYPE_LENGTH], // 变量数据类型
  163. int &startbit,
  164. int &endbit
  165. )
  166. {
  167. int nRet = -1;
  168. nRet = RequestReadStatus(nAddr,pComm, szSendMsg);
  169. if( nRet != 0 )
  170. {
  171. return nRet; // 串口忙
  172. }
  173. nRet = ResponseReadStatus(pComm, szCmd, szRecvMsg, nAddr, nIndex, nLen, szType,startbit,endbit);
  174. return nRet;
  175. }
  176. int CTmaster::ResponseReadStatus(
  177. CCommProcess *pComm,
  178. char szCmd[MAX_CMD],
  179. char szRecvMsg[VAR_MSG],
  180. int nAddr,
  181. int &nIndex,
  182. int &nLen,
  183. char szType[TYPE_LENGTH],
  184. int &startbit,
  185. int &endbit)
  186. {
  187. #if ___DEBUG
  188. REPONSEPARAM structResponse;
  189. memset( &structResponse, 0, sizeof(REPONSEPARAM) );
  190. int nReceiveLen = 0;
  191. nReceiveLen = sizeof(REPONSEPARAM);
  192. char *pBuffer = new char[ nReceiveLen ];
  193. memset(pBuffer, 0, nLen);
  194. int nReadLen = 0;
  195. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
  196. if (!CheckLength(pBuffer))
  197. {
  198. SetEvent(m_hSemComm);
  199. if (pBuffer !=NULL)
  200. {
  201. delete[] pBuffer;
  202. pBuffer = NULL;
  203. }
  204. return ERR_CODE_AIRTMASTER_COM_LRC_LOST; //长度校验出错
  205. }
  206. if (!ChkSumCheck(pBuffer,nReadLen))
  207. {
  208. SetEvent(m_hSemComm);
  209. if (pBuffer !=NULL)
  210. {
  211. delete[] pBuffer;
  212. pBuffer = NULL;
  213. }
  214. return ERR_CODE_AIRTMASTER_COM_LRC_LOST; //校验码校验出错
  215. }
  216. int nRet = RtnCheck(pBuffer, nReadLen);
  217. if (nRet != 0)
  218. {
  219. SetEvent( m_hSemComm );
  220. if( pBuffer != NULL)
  221. {
  222. delete[] pBuffer;
  223. pBuffer = NULL;
  224. }
  225. return nRet; //RTN校验出错
  226. }
  227. if( nReadLen <= 0)
  228. {
  229. // 串口没有读到数据
  230. TRACE("串口没有读到数据!\r\n");
  231. SetEvent( m_hSemComm );
  232. if( pBuffer != NULL)
  233. {
  234. delete[] pBuffer;
  235. pBuffer = NULL;
  236. }
  237. return ERR_CODE_AIRTMASTER_COM_READ_NO_DATA;
  238. }
  239. SetE0VarMsg( nAddr,szCmd, pBuffer);
  240. m_dwOnlineTick[nAddr-1] = GetTickCount();
  241. SetEvent( m_hSemComm );
  242. if( pBuffer != NULL)
  243. {
  244. delete[] pBuffer;
  245. pBuffer = NULL;
  246. }
  247. #else
  248. SimulationCommData();
  249. LOG4C((LOG_NOTICE,"%s",m_szE0Msg[0]));
  250. #endif
  251. return 0;
  252. }
  253. int CTmaster::RequestReadStatus(int nAddr,CCommProcess *pComm,char chSendMsg[_VAR_MSG])
  254. {
  255. #if ___DEBUG
  256. char chLength[4] = {0};
  257. char chChkSum[5] = {0};
  258. REQUESTPARAM RequestPara;
  259. int iLen = sizeof(RequestPara);
  260. memset( &RequestPara, 0, iLen );
  261. //起始位;
  262. RequestPara.RequestChild.bySoi = 0x7E;
  263. //通讯协议版本;
  264. RequestPara.RequestChild.byVer[0] = 0x32;
  265. RequestPara.RequestChild.byVer[1] = 0x30;
  266. //设备地址描述;
  267. RequestPara.RequestChild.byAdr[0] = ByteToAscii((nAddr >> 4) & 0x0f);
  268. RequestPara.RequestChild.byAdr[1] = ByteToAscii(nAddr & 0x0f);
  269. //Cid1
  270. RequestPara.RequestChild.byCid1[0] = 0x36;
  271. RequestPara.RequestChild.byCid1[1] = 0x30;
  272. //Cid2
  273. memcpy(RequestPara.RequestChild.byCid2, chSendMsg, 2);
  274. RequestPara.RequestChild.byLength[0] = 0x30;
  275. RequestPara.RequestChild.byLength[1] = 0x30;
  276. RequestPara.RequestChild.byLength[2] = 0x30;
  277. RequestPara.RequestChild.byLength[3] = 0x30;
  278. int nLen = sizeof(RequestPara.byCheckSum) -1;
  279. //校验码;
  280. BYTE *pDataBuf = new BYTE[ iLen - nLen];
  281. memset(pDataBuf, 0, iLen -nLen );
  282. memcpy(pDataBuf, &RequestPara, iLen - nLen);
  283. GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.byCheckSum) - 2 );
  284. delete []pDataBuf;
  285. pDataBuf = NULL;
  286. RequestPara.byCheckSum[0] = chChkSum[0];
  287. RequestPara.byCheckSum[1] = chChkSum[1];
  288. RequestPara.byCheckSum[2] = chChkSum[2];
  289. RequestPara.byCheckSum[3] = chChkSum[3];
  290. //结束符
  291. RequestPara.byEoi = 0x0D;
  292. //WaitForSingObject();等待有信号;
  293. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  294. {
  295. //求得数组字符实际长度;
  296. int nDataLen = (int)sizeof(RequestPara);
  297. //ResetEvent()设置无信号状态
  298. ResetEvent( m_hSemComm );
  299. int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
  300. if( nResult != nDataLen )
  301. {
  302. //SetEvent设置有信号;
  303. SetEvent( m_hSemComm );
  304. //返回 错误值;
  305. return EER_CODE_AIRTMASTER_COM_REGNUM;
  306. }
  307. }
  308. else
  309. {
  310. //如果m_hSemCom无信号,返回错误;
  311. return ERR_CODE_AIRTMASTER_COM_BUSY;
  312. }
  313. #endif
  314. return 0;
  315. }
  316. int CTmaster::GetE0VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int nAddr, int &nIndex, int &nLen, char szType[TYPE_LENGTH],int &startbit,int &endbit)
  317. {
  318. int nRet = 0;
  319. if( strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  320. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  321. strcmp(szCmd, "cmd-5") == 0 || strcmp(szCmd, "cmd-6") == 0 ||
  322. strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 ||
  323. strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 ||
  324. strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  325. strcmp(szCmd, "cmd-13") == 0 ||
  326. strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  327. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
  328. strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
  329. strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
  330. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
  331. strcmp(szCmd, "cmd-24") == 0
  332. )
  333. {
  334. #if IS_USE_READMSG_CS
  335. EnterCriticalSection( &m_csReadMsg );
  336. #endif
  337. DataConversion(szType, m_szE0Msg[nAddr-1] + nIndex, szRecvMsg, nLen,startbit,endbit );
  338. #if IS_USE_READMSG_CS
  339. LeaveCriticalSection(&m_csReadMsg);
  340. #endif
  341. nRet = 0;
  342. }
  343. return nRet;
  344. }
  345. // 发送设置设备参数请求
  346. int CTmaster::SendSetReuest(
  347. char szPath[MAX_PATH], // 程序所在路径
  348. char szIniName[MAX_PATH], // 配置文件名称
  349. int nCommPort, // 串行端口
  350. int nAddr, // 设备地址
  351. char szCmd[MAX_CMD], // 请求命令
  352. char szSetMsg[VAR_MSG], // 写入设备里的值
  353. int nReversed1, // 预留整形参数1接口
  354. int nReversed2, // 预留整形参数2接口
  355. int nReversed3, // 预留整形参数3接口
  356. int nReversed4, // 预留整形参数4接口
  357. int nReversed5, // 预留整形参数5接口
  358. float fReversed1, // 预留float参数1接口
  359. float fReversed2, // 预留float参数2接口
  360. float fReversed3, // 预留float参数3接口
  361. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  362. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  363. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  364. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  365. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  366. )
  367. {
  368. int nIndex = 0, nLen = 0;
  369. int startbit = 0,endbit = 0;
  370. char szSendCmd[_VAR_MSG] = {0};
  371. char szType[TYPE_LENGTH] = {0};
  372. char szLength[5] = {0};
  373. int nCDT = 0;
  374. GetULFromIni(
  375. szPath,
  376. szIniName,
  377. szCmd,
  378. szSendCmd, //ini SendCmd命令;
  379. szType, //ini Type键值;
  380. nIndex,
  381. nLen,
  382. startbit,
  383. endbit,
  384. szLength, // ini LENGTH;
  385. nCDT); // int CDT;
  386. CCommProcess *pComm = FindComm(nCommPort);
  387. if( pComm == NULL ) return -1;
  388. int nRet = -1;
  389. if (
  390. strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 || //-25空调开机;-26空调关机;
  391. strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-28") == 0 ||
  392. strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 ||
  393. strcmp(szCmd, "cmd-31") == 0 || strcmp(szCmd, "cmd-32") == 0 ||
  394. strcmp(szCmd, "cmd-33") == 0 || strcmp(szCmd, "cmd-34") == 0 ||
  395. strcmp(szCmd, "cmd-35") == 0
  396. )
  397. {
  398. nRet = SetDeviceParam( nAddr,pComm, szSendCmd, szCmd, szSetMsg/*输入设备的设置值*/, szLength, nCDT );
  399. if( nRet != 0 ) return nRet;
  400. }
  401. // if( GetTickCount() - m_dwOnlineTick[nAddr-1] > 60 *1000 && m_dwOnlineTick[nAddr-1] > 0 )
  402. // {
  403. // m_devOnline[nAddr-1] = FALSE;
  404. // }
  405. // else if( GetTickCount() - m_dwOnlineTick[nAddr-1] < 60 *1000 && m_dwOnlineTick[nAddr-1] > 0 )
  406. // {
  407. // m_devOnline[nAddr-1] = TRUE;
  408. // }
  409. //
  410. //
  411. // if( m_devOnline[nAddr-1] == FALSE )
  412. // {
  413. // return -1;
  414. // }
  415. return nRet;
  416. }
  417. // 设置传入设备的值;
  418. int CTmaster::SetDeviceParam(
  419. int nAddr,
  420. CCommProcess *pComm, //串口对象指针
  421. char szSendCmd[_VAR_MSG], //发送Buffer
  422. char szCmd[MAX_CMD], // 命令
  423. char szSetMsg[VAR_MSG], // 接收Buffer
  424. char szLength[5], // 变量索引,针对接收Buffer而言
  425. int &nCDT // 变量长度
  426. )
  427. {
  428. LOG4C((LOG_NOTICE,"nAddr = %d,szSendCmd = %s,szCmd = %s,szSetMsg = %s,szLength = %s,nCDT = %d",nAddr,szSendCmd,szCmd,szSetMsg,szLength,nCDT));
  429. int nRet = -1;
  430. nRet = RequestWriteStatus(pComm, nAddr, szCmd, szSendCmd, szSetMsg, szLength, nCDT);
  431. if( nRet != 0 )
  432. {
  433. return nRet; // 串口忙
  434. }
  435. nRet = ResponseWriteStatus(pComm, nAddr);
  436. return nRet;
  437. }
  438. // 写优先,设置事件为无信号;
  439. int CTmaster::RequestWriteStatus( CCommProcess *pComm,int nAddr,char szCmd[MAX_CMD],char szSendCmd[VAR_MSG] ,char szSetMsg[VAR_MSG],char szLength[5], int &nCDT)
  440. {
  441. char chLength[4] = {0};
  442. char chChkSum[5] = {0};
  443. char szSendBuf[100] = {0};
  444. #if 1
  445. int nDataLen = 0;
  446. szSendBuf[0] = 0x7E;
  447. szSendBuf[1] = 0x32;
  448. szSendBuf[2] = 0x30;
  449. szSendBuf[3] = ByteToAscii((nAddr >> 4) & 0x0f);
  450. szSendBuf[4] = ByteToAscii(nAddr & 0x0f);
  451. szSendBuf[5] = 0x36;
  452. szSendBuf[6] = 0x30;
  453. memcpy(&szSendBuf[7], szSendCmd, 2);
  454. memcpy(&szSendBuf[9],szLength,4);
  455. if (strcmp(szCmd,"cmd-25") == 0)
  456. {
  457. memcpy(&szSendBuf[13],szSetMsg,2);
  458. GetCheckSum((char *)szSendBuf + 1, chChkSum, 14 );
  459. memcpy(&szSendBuf[15],chChkSum,4);
  460. szSendBuf[19] = 0x0D;
  461. nDataLen = 20;
  462. }
  463. else
  464. {
  465. szSendBuf[13] = ByteToAscii((nCDT >> 4) & 0x0f);
  466. szSendBuf[14] = ByteToAscii(nCDT & 0x0f);
  467. memcpy(&szSendBuf[15],szSetMsg,4);
  468. GetCheckSum((char *)szSendBuf + 1, chChkSum, 17 );
  469. memcpy(&szSendBuf[19],chChkSum,4);
  470. szSendBuf[23] = 0x0D;
  471. nDataLen = 24;
  472. }
  473. LOG4C_HEX_DUMP((LOG_NOTICE, szSendBuf, nDataLen));
  474. ResetEvent( m_hSemComm );//设置为无信号(写设备优先级高);
  475. int iResult = pComm->Write((BYTE *)&szSendBuf, nDataLen);
  476. if (iResult == nDataLen)
  477. {
  478. SetEvent( m_hSemComm );//设置为有信号;
  479. return 0;
  480. }
  481. else
  482. {
  483. SetEvent( m_hSemComm );//设置为有信号;
  484. return EER_CODE_AIRTMASTER_COM_WRITE_DATA;
  485. }
  486. #endif
  487. #if 0
  488. WRITEPARAM RequestPara;
  489. int iLen = sizeof(RequestPara);
  490. memset( &RequestPara, 0, iLen );
  491. //起始位;
  492. RequestPara.ReposeChild.bySoi = 0x7E;
  493. //通讯协议版本;
  494. RequestPara.ReposeChild.byVer[0] = 0x32;
  495. RequestPara.ReposeChild.byVer[1] = 0x30;
  496. //设备地址描述;
  497. RequestPara.ReposeChild.byAdr[0] = ByteToAscii((nAddr >> 4) & 0x0f);
  498. RequestPara.ReposeChild.byAdr[1] = ByteToAscii(nAddr & 0x0f);
  499. //Cid1
  500. RequestPara.ReposeChild.byCid1[0] = 0x36;
  501. RequestPara.ReposeChild.byCid1[1] = 0x30;
  502. //Cid2
  503. memcpy(RequestPara.ReposeChild.byCid2, szSendCmd, 2);
  504. //Length;
  505. memcpy(RequestPara.ReposeChild.byLength,szLength,4);
  506. if (strcmp(szCmd,"cmd-25") == 0)
  507. {
  508. memcpy(RequestPara.ReposeChild.byInfo,szSetMsg,strlen(szSetMsg));
  509. }
  510. else
  511. {
  512. char szSet[10] ={0};
  513. szSet[0] = nCDT;
  514. memcpy(&szSet[1],szSetMsg,strlen(szSetMsg));
  515. memcpy(RequestPara.ReposeChild.byInfo,szSet,strlen(szSet));
  516. }
  517. int nLen = sizeof(RequestPara.byCheckSum) -1;
  518. //校验码;
  519. BYTE *pDataBuf = new BYTE[ iLen - nLen];
  520. memset(pDataBuf, 0, iLen -nLen );
  521. memcpy(pDataBuf, &RequestPara, iLen - nLen);
  522. GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.byCheckSum) - 2 );
  523. delete []pDataBuf;
  524. pDataBuf = NULL;
  525. RequestPara.byCheckSum[0] = chChkSum[0];
  526. RequestPara.byCheckSum[1] = chChkSum[1];
  527. RequestPara.byCheckSum[2] = chChkSum[2];
  528. RequestPara.byCheckSum[3] = chChkSum[3];
  529. //结束符
  530. RequestPara.byEoi = 0x0D;
  531. ResetEvent( m_hSemComm );//设置为无信号(写设备优先级高);
  532. int nDataLen = (int)sizeof(RequestPara);
  533. //WriteMessage写命令入串口,返回实际写入的字节数;
  534. int iResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
  535. if (iResult == nDataLen)
  536. {
  537. SetEvent( m_hSemComm );//设置为有信号;
  538. return 0;
  539. }
  540. else
  541. {
  542. SetEvent( m_hSemComm );//设置为有信号;
  543. return EER_CODE_AIRTMASTER_COM_WRITE_DATA;
  544. }
  545. #endif
  546. return 0;
  547. }
  548. int CTmaster::ResponseWriteStatus( CCommProcess *pComm,int nAddr)
  549. {
  550. char chBuf[30] = {0};
  551. //读取请求写入的命令;
  552. int iReadLen = pComm->Read((BYTE *)chBuf, sizeof(chBuf));
  553. if( iReadLen <= 0) // 串口没有读到数据
  554. {
  555. SetEvent( m_hSemComm );
  556. return ERR_CODE_AIRTMASTER_COM_READ_NO_DATA;
  557. }
  558. SetEvent( m_hSemComm );
  559. if (chBuf[7] == 0x00 && chBuf[8] == 0x00)//RTN =00H 成功接收;
  560. {
  561. return 0;
  562. }
  563. else
  564. return ERR_CODE_AIRTMASTER_COM_INVALIDRES;
  565. return 0;
  566. }
  567. void CTmaster::SetE0VarMsg(int nAddr, char szCmd[MAX_CMD], char *pBuff)
  568. {
  569. if( strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  570. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  571. strcmp(szCmd, "cmd-5") == 0 ||
  572. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
  573. strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
  574. strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
  575. strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
  576. strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  577. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
  578. strcmp(szCmd, "cmd-18") == 0 ||strcmp(szCmd, "cmd-19") == 0 ||
  579. strcmp(szCmd, "cmd-20") == 0|| strcmp(szCmd, "cmd-21") == 0 ||
  580. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
  581. strcmp(szCmd, "cmd-24") == 0
  582. )
  583. {
  584. #if IS_USE_READMSG_CS
  585. EnterCriticalSection( &m_csReadMsg );
  586. #endif
  587. memcpy(m_szE0Msg[nAddr-1], pBuff, sizeof(m_szE0Msg));
  588. #if IS_USE_READMSG_CS
  589. LeaveCriticalSection(&m_csReadMsg);
  590. #endif
  591. }
  592. }
  593. UINT CTmaster::GetCheckSum(char *pBuf, char chDest[5], int len)
  594. {
  595. LOG4C((LOG_NOTICE,"pBuf = %s, len = %d",pBuf,len));
  596. WORD iSum = 0;;
  597. for(int i=0; i<len; i++) //求和;
  598. {
  599. iSum += pBuf[i];
  600. }
  601. WORD iCompliment = iSum;
  602. iCompliment = ~iCompliment; //取反;
  603. iCompliment++;
  604. itoa(iCompliment, chDest, 16);
  605. chDest[0] = lowercase2uppercase(chDest[0]);
  606. chDest[1] = lowercase2uppercase(chDest[1]);
  607. chDest[2] = lowercase2uppercase(chDest[2]);
  608. chDest[3] = lowercase2uppercase(chDest[3]);
  609. return atoi(chDest);
  610. }
  611. WORD CTmaster::RtnCheck(char szRecvdMsg[],int len)
  612. {
  613. char ch[2] = {0};
  614. ch[0] = szRecvdMsg[7];
  615. ch[1] = szRecvdMsg[8];
  616. if (atoi(ch) == 0)
  617. {
  618. return ERR_CODE_RTN_NATURAL;
  619. }
  620. else if (atoi(ch) ==1)
  621. {
  622. return ERR_CODE_RTN_VER_ERROR;
  623. }
  624. else if (atoi(ch) ==2)
  625. {
  626. return ERR_CODE_RTN_CHKSUM_ERROR;
  627. }
  628. else if (atoi(ch) ==3)
  629. {
  630. return ERR_CODE_RTN_LCHKSUM_ERROR;
  631. }
  632. else if (atoi(ch) ==4)
  633. {
  634. return ERR_CODE_RTN_CID_ERROR;
  635. }
  636. else if (atoi(ch) ==5)
  637. {
  638. return ERR_CODE_RTN_COMMAND_FORMAT;
  639. }
  640. else if (atoi(ch) ==6)
  641. {
  642. return ERR_CODE_RTN_INVALID_DATA;
  643. }
  644. return -1;
  645. }
  646. void CTmaster::SimulationCommData(void)
  647. {
  648. ///
  649. // 获取系统模拟量量化数据
  650. m_szE0Msg[0][0]=0x7E;
  651. m_szE0Msg[0][1]=0x32;
  652. m_szE0Msg[0][2]=0x30;
  653. m_szE0Msg[0][3]=0x30;
  654. m_szE0Msg[0][4]=0x31;
  655. m_szE0Msg[0][5]=0x36;
  656. m_szE0Msg[0][6]=0x30;
  657. m_szE0Msg[0][7]=0x30;
  658. m_szE0Msg[0][8]=0x30;
  659. m_szE0Msg[0][9]=0x33;
  660. m_szE0Msg[0][10]=0x30;
  661. m_szE0Msg[0][11]=0x33;
  662. m_szE0Msg[0][12]=0x41;
  663. m_szE0Msg[0][13]=0x30;
  664. m_szE0Msg[0][14]=0x31;
  665. m_szE0Msg[0][15]=0x30;
  666. m_szE0Msg[0][16]=0x39;
  667. m_szE0Msg[0][17]=0x30;
  668. m_szE0Msg[0][18]=0x32;
  669. m_szE0Msg[0][19]=0x32;
  670. m_szE0Msg[0][20]=0x36;
  671. m_szE0Msg[0][21]=0x30;
  672. m_szE0Msg[0][22]=0x31;
  673. m_szE0Msg[0][23]=0x32;
  674. m_szE0Msg[0][24]=0x35;
  675. m_szE0Msg[0][25]=0x30;
  676. m_szE0Msg[0][26]=0x38;
  677. m_szE0Msg[0][27]=0x31;
  678. m_szE0Msg[0][28]=0x32;
  679. m_szE0Msg[0][29]=0x30;
  680. m_szE0Msg[0][30]=0x42;
  681. m_szE0Msg[0][31]=0x34;
  682. m_szE0Msg[0][32]=0x42;
  683. m_szE0Msg[0][33]=0x46;
  684. m_szE0Msg[0][34]=0x46;
  685. m_szE0Msg[0][35]=0x30;
  686. m_szE0Msg[0][36]=0x30;
  687. m_szE0Msg[0][37]=0x31;
  688. m_szE0Msg[0][38]=0x42;
  689. m_szE0Msg[0][39]=0x30;
  690. m_szE0Msg[0][40]=0x30;
  691. m_szE0Msg[0][41]=0x31;
  692. m_szE0Msg[0][42]=0x32;
  693. m_szE0Msg[0][43]=0x30;
  694. m_szE0Msg[0][44]=0x30;
  695. m_szE0Msg[0][45]=0x34;
  696. m_szE0Msg[0][46]=0x36;
  697. m_szE0Msg[0][47]=0x30;
  698. m_szE0Msg[0][48]=0x30;
  699. m_szE0Msg[0][49]=0x32;
  700. m_szE0Msg[0][50]=0x33;
  701. m_szE0Msg[0][51]=0x30;
  702. m_szE0Msg[0][52]=0x30;
  703. m_szE0Msg[0][53]=0x33;
  704. m_szE0Msg[0][54]=0x32;
  705. m_szE0Msg[0][55]=0x30;
  706. m_szE0Msg[0][56]=0x30;
  707. m_szE0Msg[0][57]=0x30;
  708. m_szE0Msg[0][58]=0x41;
  709. m_szE0Msg[0][59]=0x30;
  710. m_szE0Msg[0][60]=0x31;
  711. m_szE0Msg[0][61]=0x30;
  712. m_szE0Msg[0][62]=0x34;
  713. m_szE0Msg[0][63]=0x30;
  714. m_szE0Msg[0][64]=0x30;
  715. m_szE0Msg[0][65]=0x31;
  716. m_szE0Msg[0][66]=0x45;
  717. m_szE0Msg[0][67]=0x30;
  718. m_szE0Msg[0][68]=0x30;
  719. m_szE0Msg[0][69]=0x38;
  720. m_szE0Msg[0][70]=0x42;
  721. m_szE0Msg[0][71]=0x46;
  722. m_szE0Msg[0][72]=0x31;
  723. m_szE0Msg[0][73]=0x44;
  724. m_szE0Msg[0][74]=0x35;
  725. m_szE0Msg[0][75]=0x0D;
  726. }