DataMate3000.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. #include "stdafx.h"
  2. #include "DataMate3000.h"
  3. CDataMate3000::CDataMate3000(char *szPath,char *szIniName,int nCommPort,int nAddr,int nRate,int nDataBit,int nStopBit,int nParity,int nInterval)
  4. {
  5. #if IS_USE_READMSG_CS
  6. InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
  7. #endif
  8. MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  9. for( int i = 0; i < MAX_ADDR; i++ )
  10. {
  11. memset(m_szDataMate3000_42Msg[i], 0, sizeof(m_szDataMate3000_42Msg[i]));
  12. memset(m_szDataMate3000_43Msg[i], 0, sizeof(m_szDataMate3000_43Msg[i]));
  13. memset(m_szDataMate3000_47Msg[i], 0, sizeof(m_szDataMate3000_47Msg[i]));
  14. memset(m_szDataMate3000_4DMsg[i], 0, sizeof(m_szDataMate3000_4DMsg[i]));
  15. memset(m_szDataMate3000_82Msg[i], 0, sizeof(m_szDataMate3000_82Msg[i]));
  16. memset(m_szDataMate3000_45Msg[i], 0, sizeof(m_szDataMate3000_45Msg[i]));
  17. m_devOnline[i] = TRUE;
  18. m_dwOnlineTick[i] = 0;
  19. }
  20. }
  21. CDataMate3000::~CDataMate3000()
  22. {
  23. #if IS_USE_READMSG_CS
  24. DeleteCriticalSection( &m_csReadMsg );
  25. #endif
  26. MTVERIFY( CloseHandle( m_hSemComm ) );
  27. CloseComm();
  28. }
  29. BOOL CDataMate3000::DataMate3000OpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  30. {
  31. BOOL bResult = FALSE;
  32. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  33. //LOG4C((LOG_NOTICE,"DME3000 DataMate3000OpenComm::%d",bResult));
  34. return bResult;
  35. }
  36. int CDataMate3000::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
  37. {
  38. CHAR szFile[MAX_PATH + 1] = "";
  39. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  40. IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  41. GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile);
  42. szDataType[strlen(szDataType)] = '\0';
  43. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  44. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  45. iSBit = GetPrivateProfileInt(szCmd, "StaBit", 0, szFile);//从配置文件中取值
  46. iEBit = GetPrivateProfileInt(szCmd, "EndBit", 0, szFile);
  47. return 0;
  48. }
  49. void lowcase2uppcase(BYTE &btSrc)
  50. {
  51. if( btSrc >= 'a' && btSrc <= 'z' )
  52. btSrc = btSrc - 'a' + 'A';
  53. }
  54. void CDataMate3000::GetDME3000Check(char *szData, char *szCheck ,int nlen /* = 12 */)
  55. {
  56. DWORD dwSum(0);
  57. for (int i(0); i < nlen; i++)
  58. dwSum += szData[i] ;
  59. WORD iCompliment = dwSum;
  60. iCompliment = ~iCompliment;//取反
  61. iCompliment++;
  62. itoa(iCompliment, szCheck, 16);
  63. for (int i(0); i < 5 ; i++)
  64. lowcase2uppcase((BYTE &)szCheck[i]);
  65. }
  66. int CDataMate3000::SendReadRequest(
  67. char *szPath,
  68. char *szIniName,
  69. int nCommPort,
  70. int nAddr,
  71. char *szCmd,
  72. char *szMsg,
  73. int nReversed1,
  74. int nReversed2,
  75. int nReversed3,
  76. int nReversed4,
  77. int nReversed5,
  78. float fReversed1,
  79. float fReversed2,
  80. float fReversed3,
  81. char *szReversed1,
  82. char *szReversed2,
  83. char *szReversed3,
  84. char *szReversed4,
  85. char *szReversed5 )
  86. {
  87. CCommProcess *pComm = FindComm(nCommPort);
  88. if( pComm == NULL ) return -1;
  89. int nRet = -1;
  90. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  91. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  92. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  93. int iCmd = atoi(szCmd+4);
  94. if( nAddr < 1 ) return -1;
  95. if(
  96. (strlen(m_szDataMate3000_42Msg[nAddr -1]) == 0 && (iCmd >= 1 && iCmd <= 3)) || iCmd == 1 ||
  97. (strlen(m_szDataMate3000_43Msg[nAddr -1]) == 0 && (iCmd ==4)) || iCmd == 4 ||
  98. (strlen(m_szDataMate3000_47Msg[nAddr -1]) == 0 && (iCmd >= 5 && iCmd <= 10)) || iCmd == 5 ||
  99. (strlen(m_szDataMate3000_4DMsg[nAddr -1]) == 0 && (iCmd >= 11 && iCmd <= 16)) || iCmd == 11 ||
  100. (strlen(m_szDataMate3000_82Msg[nAddr -1]) == 0 && (iCmd >= 17 && iCmd <= 43)) || iCmd == 17
  101. )
  102. {
  103. nRet = GetDeviceParam( nAddr,pComm,szCmd,IniSendCMD,IniSendlen );
  104. if( nRet != 0 )
  105. {
  106. LOG4C((LOG_NOTICE,"DME3000 GetDeviceParam::Error"));
  107. return nRet;
  108. }
  109. }
  110. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  111. {
  112. m_devOnline[nAddr - 1] = FALSE;
  113. }
  114. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  115. {
  116. m_devOnline[nAddr - 1] = TRUE;
  117. }
  118. if( m_devOnline[nAddr - 1] == FALSE )
  119. {
  120. return -1;
  121. }
  122. nRet = GetDataMate3000_42VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit, iEBit);
  123. nRet = GetDataMate3000_43VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit, iEBit);
  124. nRet = GetDataMate3000_47VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit, iEBit);
  125. nRet = GetDataMate3000_4DVarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit, iEBit);
  126. nRet = GetDataMate3000_82VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit, iEBit);
  127. //LOG4C((LOG_NOTICE, "DME3000 szCmd= %s, szMsg = %s", szCmd, szMsg));
  128. return nRet;
  129. }
  130. int CDataMate3000::GetDataMate3000_42VarMsg(int nAddr,char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szType,int iSBit,int iEBit)
  131. {
  132. int iCmd = atoi(szCmd + 4);
  133. if( iCmd >= 1 && iCmd <=3 )
  134. {
  135. #if IS_USE_READMSG_CS
  136. EnterCriticalSection( &m_csReadMsg );
  137. #endif
  138. DataConversion(szType, m_szDataMate3000_42Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
  139. #if IS_USE_READMSG_CS
  140. LeaveCriticalSection(&m_csReadMsg);
  141. #endif
  142. }
  143. return 0;
  144. }
  145. int CDataMate3000::GetDataMate3000_43VarMsg(int nAddr,char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szType,int iSBit,int iEBit)
  146. {
  147. if(strcmp(szCmd, "cmd-4") == 0 )
  148. {
  149. #if IS_USE_READMSG_CS
  150. EnterCriticalSection( &m_csReadMsg );
  151. #endif
  152. DataConversion(szType, m_szDataMate3000_43Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
  153. #if IS_USE_READMSG_CS
  154. LeaveCriticalSection(&m_csReadMsg);
  155. #endif
  156. }
  157. return 0;
  158. }
  159. int CDataMate3000::GetDataMate3000_47VarMsg(int nAddr,char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szType,int iSBit,int iEBit)
  160. {
  161. int iCmd = atoi(szCmd + 4);
  162. if( iCmd >= 5 && iCmd <= 10 )
  163. {
  164. #if IS_USE_READMSG_CS
  165. EnterCriticalSection( &m_csReadMsg );
  166. #endif
  167. DataConversion(szType, m_szDataMate3000_47Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
  168. #if IS_USE_READMSG_CS
  169. LeaveCriticalSection(&m_csReadMsg);
  170. #endif
  171. }
  172. return 0;
  173. }
  174. int CDataMate3000::GetDataMate3000_4DVarMsg(int nAddr,char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szType,int iSBit,int iEBit)
  175. {
  176. int iCmd = atoi(szCmd + 4);
  177. if( iCmd >= 11 && iCmd <= 16 )
  178. {
  179. #if IS_USE_READMSG_CS
  180. EnterCriticalSection( &m_csReadMsg );
  181. #endif
  182. DataConversion(szType, m_szDataMate3000_4DMsg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
  183. #if IS_USE_READMSG_CS
  184. LeaveCriticalSection(&m_csReadMsg);
  185. #endif
  186. }
  187. return 0;
  188. }
  189. int CDataMate3000::GetDataMate3000_82VarMsg(int nAddr,char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szType,int iSBit,int iEBit)
  190. {
  191. int iCmd = atoi(szCmd + 4);
  192. if( iCmd >= 17 && iCmd <= 43 )
  193. {
  194. #if IS_USE_READMSG_CS
  195. EnterCriticalSection( &m_csReadMsg );
  196. #endif
  197. DataConversion(szType, m_szDataMate3000_82Msg[nAddr - 1] + nIndex, szMsg, nLen, iSBit, iEBit);
  198. #if IS_USE_READMSG_CS
  199. LeaveCriticalSection(&m_csReadMsg);
  200. #endif
  201. }
  202. return 0;
  203. }
  204. int CDataMate3000::GetDataMate3000_45VarMsg(int nAddr,char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szType,int iSBit,int iEBit)
  205. {
  206. if( strcmp(szCmd, "cmd-44") == 0)
  207. {
  208. #if IS_USE_READMSG_CS
  209. EnterCriticalSection( &m_csReadMsg );
  210. #endif
  211. memcpy(szMsg, m_szDataMate3000_45Msg[nAddr - 1] + nIndex, nLen);
  212. #if IS_USE_READMSG_CS
  213. LeaveCriticalSection(&m_csReadMsg);
  214. #endif
  215. }
  216. return 0;
  217. }
  218. int CDataMate3000::GetDeviceParam( int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD, const int &IniSendlen)
  219. {
  220. int nRet = -1;
  221. nRet = Send_ReadDeviceData(nAddr,pComm,szCmd,IniSendCMD,IniSendlen);
  222. if( nRet != 0 )
  223. {
  224. LOG4C((LOG_NOTICE,"DME3000 RequestStatus::Error"));
  225. return nRet; // 串口忙
  226. }
  227. nRet = Recv_ReadDeviceData(nAddr,pComm,szCmd);
  228. return nRet;
  229. }
  230. int CDataMate3000::SetDeviceParam( int nAddr,CCommProcess *pComm,char *szCmd,char *szSetMsg,char *szRecvMsg,char *IniSendCMD, const int &IniSendlen)
  231. {
  232. int nRet = -1;
  233. nRet = Send_WriteDeviceData(nAddr,pComm,szCmd,szSetMsg,IniSendCMD,IniSendlen);
  234. if( nRet != 0 )
  235. {
  236. LOG4C((LOG_NOTICE,"DME3000 SetDeviceParam::Error"));
  237. return nRet; // 串口忙
  238. }
  239. nRet = Recv_WriteDeviceData(nAddr,pComm,szCmd,szRecvMsg);
  240. return nRet;
  241. }
  242. void CDataMate3000::SetDataMate3000_42VarMsg( int nAddr, char *szCmd, char *pBuffer)
  243. {
  244. int iCmd = atoi(szCmd + 4);
  245. if( iCmd >= 1 && iCmd <= 3 )
  246. {
  247. #if IS_USE_READMSG_CS
  248. EnterCriticalSection( &m_csReadMsg );
  249. #endif
  250. memcpy(m_szDataMate3000_42Msg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_42Msg[nAddr - 1]));
  251. #if IS_USE_READMSG_CS
  252. LeaveCriticalSection(&m_csReadMsg);
  253. #endif
  254. }
  255. }
  256. void CDataMate3000::SetDataMate3000_43VarMsg( int nAddr, char *szCmd, char *pBuffer)
  257. {
  258. if( strcmp(szCmd, "cmd-4") == 0)
  259. {
  260. #if IS_USE_READMSG_CS
  261. EnterCriticalSection( &m_csReadMsg );
  262. #endif
  263. memcpy(m_szDataMate3000_43Msg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_43Msg[nAddr - 1]));
  264. #if IS_USE_READMSG_CS
  265. LeaveCriticalSection(&m_csReadMsg);
  266. #endif
  267. }
  268. }
  269. void CDataMate3000::SetDataMate3000_47VarMsg( int nAddr, char *szCmd, char *pBuffer)
  270. {
  271. int iCmd = atoi(szCmd + 4);
  272. if( iCmd >= 5 && iCmd <= 10 )
  273. {
  274. #if IS_USE_READMSG_CS
  275. EnterCriticalSection( &m_csReadMsg );
  276. #endif
  277. memcpy(m_szDataMate3000_47Msg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_47Msg[nAddr - 1]));
  278. #if IS_USE_READMSG_CS
  279. LeaveCriticalSection(&m_csReadMsg);
  280. #endif
  281. }
  282. }
  283. void CDataMate3000::SetDataMate3000_4DVarMsg( int nAddr, char *szCmd, char *pBuffer)
  284. {
  285. int iCmd = atoi(szCmd + 4);
  286. if( iCmd >= 11 && iCmd <= 16 )
  287. {
  288. #if IS_USE_READMSG_CS
  289. EnterCriticalSection( &m_csReadMsg );
  290. #endif
  291. memcpy(m_szDataMate3000_4DMsg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_4DMsg[nAddr - 1]));
  292. #if IS_USE_READMSG_CS
  293. LeaveCriticalSection(&m_csReadMsg);
  294. #endif
  295. }
  296. }
  297. void CDataMate3000::SetDataMate3000_82VarMsg( int nAddr, char *szCmd, char *pBuffer)
  298. {
  299. int iCmd = atoi(szCmd + 4);
  300. if( iCmd >= 17 && iCmd <= 43 )
  301. {
  302. #if IS_USE_READMSG_CS
  303. EnterCriticalSection( &m_csReadMsg );
  304. #endif
  305. memcpy(m_szDataMate3000_82Msg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_82Msg[nAddr - 1]));
  306. #if IS_USE_READMSG_CS
  307. LeaveCriticalSection(&m_csReadMsg);
  308. #endif
  309. }
  310. }
  311. WORD CDataMate3000::GetDataLength(const WORD wLENID, char chLength[4])
  312. {
  313. char szLenID[3] = {0};
  314. //int nLen = 10;
  315. DigitToBinary(wLENID, szLenID, sizeof(szLenID));
  316. char chCheckSum = GetLCheckSum(szLenID, sizeof(szLenID));
  317. chLength[0] = chCheckSum;
  318. chLength[1] = szLenID[0];
  319. chLength[2] = szLenID[1];
  320. chLength[3] = szLenID[2];
  321. return 0;
  322. }
  323. char CDataMate3000::GetLCheckSum(char *pBuf, int len)
  324. {
  325. //WORD iSum = 0;
  326. char chCompliment = 0;
  327. //unsigned char chCompliment[2] = {0};
  328. for(int i=0; i<len; i++)//求和
  329. {
  330. chCompliment += AsciiToBYTE(pBuf[i]);
  331. //chCompliment += pBuf[i];
  332. }
  333. chCompliment = ~chCompliment;//取反
  334. chCompliment++;
  335. return chCompliment;
  336. }
  337. // 发送设置设备参数请求
  338. int CDataMate3000::SendSetReuest(
  339. char *szPath,
  340. char *szIniName,
  341. int nCommPort,
  342. int nAddr,
  343. char *szCmd,
  344. char *szSetMsg,
  345. int nReversed1,
  346. int nReversed2,
  347. int nReversed3,
  348. int nReversed4,
  349. int nReversed5,
  350. float fReversed1,
  351. float fReversed2,
  352. float fReversed3,
  353. char *szReversed1,
  354. char *szReversed2,
  355. char *szReversed3,
  356. char *szReversed4,
  357. char *szReversed5
  358. )
  359. {
  360. CCommProcess *pComm = FindComm(nCommPort);
  361. if( pComm == NULL ) return -1;
  362. int nRet = -1;
  363. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  364. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  365. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  366. char szRecvMsg[10] = {0};
  367. SetDeviceParam(nAddr, pComm, szCmd, szSetMsg,szRecvMsg,IniSendCMD,IniSendlen);
  368. return 0;
  369. }
  370. int CDataMate3000::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen)
  371. {
  372. int nRet = -1;
  373. #if DEBUG_DataMate3000
  374. /************************************************************************/
  375. char chChkSum[5] = {0};
  376. BYTE byArySend[120] = {0};
  377. byArySend[0] = 0x7E;
  378. byArySend[1] = 0x32;
  379. byArySend[2] = 0x31;
  380. byArySend[3] = ByteToAscii((nAddr>>4) & 0x0f);
  381. byArySend[4] = ByteToAscii(nAddr & 0x0f);
  382. //Cid1
  383. byArySend[5] = 0x36;
  384. byArySend[6] = 0x30;
  385. memcpy(byArySend + 7, IniSendCMD, 2);
  386. byArySend[9] = 0x30;
  387. byArySend[10] = 0x30;
  388. byArySend[11] = 0x30;
  389. byArySend[12] = 0x30;
  390. char cAryCheck[5] = {0};
  391. GetDME3000Check((char*)byArySend + 1, cAryCheck, 12);
  392. memcpy(byArySend + 13,cAryCheck,4);
  393. byArySend[17] = 0x0D;
  394. CString strSendCommand = "";
  395. strSendCommand.Format("%s",byArySend);
  396. //LOG4C((LOG_NOTICE,"SendCommand = %s",strSendCommand));
  397. /************************************************************************/
  398. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  399. {
  400. ResetEvent( m_hSemComm );
  401. int nResult = pComm->Write(byArySend, 18);
  402. if( nResult == 18 )
  403. {
  404. nRet = 0;
  405. }
  406. else
  407. {
  408. SetEvent( m_hSemComm );
  409. LOG4C((LOG_NOTICE,"DME3000 RequestStatus:写长度与返回值不等"));
  410. return ERR_CODE_DATAMATE3000_COM_INVALIDRES;
  411. }
  412. }
  413. else
  414. {
  415. LOG4C((LOG_NOTICE,"DME3000 RequestStatus:未获取事件信号权"));
  416. return ERR_CODE_DATAMATE3000_COM_BUSY;
  417. }
  418. #endif
  419. Sleep(150);
  420. return nRet;
  421. }
  422. int CDataMate3000::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
  423. {
  424. #if DEBUG_DataMate3000
  425. int nReceiveLen = 60;
  426. BYTE *pBuffer = new BYTE[nReceiveLen];
  427. ZeroMemory(pBuffer,60);
  428. int nReadLen = pComm->Read( pBuffer, nReceiveLen);
  429. //LOG4C((LOG_NOTICE,"DME3000 pBuffer = %s, nReadLen = %d",pBuffer,nReadLen));
  430. if( nReadLen <= 0)
  431. {
  432. SetEvent( m_hSemComm );
  433. if( pBuffer != NULL)
  434. {
  435. delete[] pBuffer;
  436. pBuffer = NULL;
  437. }
  438. LOG4C((LOG_NOTICE,"DME3000 ResponseStatus:读取长度 <= 0"));
  439. return ERR_CODE_DATAMATE3000_COM_READ_NO_DATA;
  440. }
  441. char cAryCheck[5] = {0};
  442. GetDME3000Check((char*)pBuffer + 1, cAryCheck, nReadLen -6);
  443. //LOG4C((LOG_NOTICE,"cAryCheck = %s",cAryCheck));
  444. char cTemp[5] = {0};
  445. memcpy(cTemp, pBuffer + nReadLen - 5, 4);
  446. //LOG4C((LOG_NOTICE,"DME3000 cTemp = %s",cTemp));
  447. if( strcmp( cAryCheck,cTemp) != 0 )
  448. {
  449. SetEvent( m_hSemComm );
  450. if( pBuffer != NULL)
  451. {
  452. delete[] pBuffer;
  453. pBuffer = NULL;
  454. }
  455. LOG4C((LOG_NOTICE,"DME3000 ResponseStatus:总校检错误"));
  456. return ERR_CODE_DATAMATE3000_COM_CHKSUM_LOST;
  457. }
  458. if (!CheckLength((char*)pBuffer))
  459. {
  460. SetEvent( m_hSemComm );
  461. if( pBuffer != NULL)
  462. {
  463. delete[] pBuffer;
  464. pBuffer = NULL;
  465. }
  466. LOG4C((LOG_NOTICE,"DME3000 ResponseStatus:长度校检错误"));
  467. return ERR_CODE_DATAMATE3000_COM_VARLEN;
  468. }
  469. int nRet = RtnCheck((char*)pBuffer);
  470. if(0 != nRet)
  471. {
  472. SetEvent( m_hSemComm );
  473. if( pBuffer != NULL)
  474. {
  475. delete[] pBuffer;
  476. pBuffer = NULL;
  477. }
  478. LOG4C((LOG_NOTICE,"DME3000 ResponseStatus:返回码错误"));
  479. return nRet;
  480. }
  481. SetDataMate3000_42VarMsg(nAddr, szCmd, (char*)pBuffer);
  482. SetDataMate3000_43VarMsg(nAddr, szCmd, (char*)pBuffer);
  483. SetDataMate3000_47VarMsg(nAddr, szCmd, (char*)pBuffer);
  484. SetDataMate3000_4DVarMsg(nAddr, szCmd, (char*)pBuffer);
  485. SetDataMate3000_82VarMsg(nAddr, szCmd, (char*)pBuffer);
  486. m_dwOnlineTick[nAddr - 1] = GetTickCount();
  487. // 设置串口等待事件为有信号;
  488. SetEvent( m_hSemComm );
  489. if( pBuffer != NULL)
  490. {
  491. delete[] pBuffer;
  492. pBuffer = NULL;
  493. }
  494. #else
  495. SimulationCommData();
  496. #endif
  497. Sleep(150);
  498. return 0;
  499. }
  500. int CDataMate3000::Send_WriteDeviceData(int naddr, CCommProcess *pComm, char *szCmd, char *szSetMsg, char *IniSendCMD, const int &IniSendlen )
  501. {
  502. #if DEBUG_DataMate3000
  503. char szSendMsg[DATAMATE3000_SEND_MSG];
  504. int iSendlen = sizeof(REQUEST_STRUCT);
  505. REQUEST_STRUCT szSend;
  506. ZeroMemory(&szSend,sizeof(REQUEST_STRUCT));
  507. if ( strcmp(szCmd,"cmd-44") == 0 )
  508. {
  509. //sol
  510. szSend.Sol = 0x7E;
  511. //ver
  512. szSend.Ver[0] = '2';
  513. szSend.Ver[1] = '1';
  514. //adr
  515. szSend.Adr[0] = ByteToAscii((naddr>>4) & 0x0f);
  516. szSend.Adr[1] = ByteToAscii(naddr & 0x0f);
  517. //cid1
  518. szSend.Cid1[0] = '6';
  519. szSend.Cid1[1] = '0';
  520. //cid2
  521. memcpy(szSend.Cid2, IniSendCMD, 2);
  522. memset(szSend.Length, 0, 4);
  523. //length
  524. szSend.Length[1] = '0';
  525. szSend.Length[2] = '0';
  526. szSend.Length[3] = '2';
  527. szSend.Length[0] = 'E';
  528. //szInfo
  529. memcpy(szSend.szInfo,szSetMsg, sizeof(szSend.szInfo));
  530. memset(szSend.Chksum, 0,sizeof(szSend.Chksum));
  531. szSend.Eol = 0x0D;
  532. memcpy(szSendMsg, &szSend, sizeof(REQUEST_STRUCT));
  533. GetChkSum(szSendMsg, (char *)szSend.Chksum, sizeof(REQUEST_STRUCT));
  534. szSend.Eol = 0x0D;
  535. memcpy(szSendMsg, &szSend, sizeof(REQUEST_STRUCT));
  536. }
  537. ResetEvent( m_hSemComm );
  538. int nResult = pComm->Write((unsigned char *)szSendMsg, iSendlen);
  539. if( nResult == iSendlen )
  540. {
  541. }
  542. else
  543. {
  544. SetEvent( m_hSemComm );
  545. return EER_CODE_DATAMATE3000_COM_WRITE_DATA;
  546. }
  547. #endif
  548. return 0;
  549. }
  550. int CDataMate3000::Recv_WriteDeviceData(int nAddr, CCommProcess *pComm, char *szCmd, char *szRecvMsg )
  551. {
  552. #if DEBUG_DataMate3000
  553. int nReceiveLen = 0;
  554. int nProcessLen = 0;
  555. int nReadLen = 0;
  556. nReceiveLen = DATEMATE3000_RECEIVE_MSG;
  557. char *pBuffer = new char[ nReceiveLen ];
  558. memset(pBuffer, 0, nReceiveLen);
  559. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
  560. if( nReadLen <= 0)
  561. {
  562. SetEvent( m_hSemComm );
  563. if( pBuffer != NULL)
  564. {
  565. delete[] pBuffer;
  566. pBuffer = NULL;
  567. }
  568. return ERR_CODE_DATAMATE3000_COM_READ_NO_DATA;
  569. }
  570. #if 0
  571. if (!ChkSumCheck(pBuffer, strlen(pBuffer)))
  572. {
  573. SetEvent( m_hSemComm );
  574. if( pBuffer != NULL)
  575. {
  576. delete[] pBuffer;
  577. pBuffer = NULL;
  578. }
  579. return ERR_CODE_DATAMATE3000_COM_CHKSUM_LOST;
  580. }
  581. if (!CheckLength(pBuffer))
  582. {
  583. SetEvent( m_hSemComm );
  584. if( pBuffer != NULL)
  585. {
  586. delete[] pBuffer;
  587. pBuffer = NULL;
  588. }
  589. return ERR_CODE_DATAMATE3000_COM_VARLEN;
  590. }
  591. int nRet = RtnCheck(pBuffer);
  592. if(0 != nRet)
  593. {
  594. SetEvent( m_hSemComm );
  595. if( pBuffer != NULL)
  596. {
  597. delete[] pBuffer;
  598. pBuffer = NULL;
  599. }
  600. return nRet;
  601. }
  602. #endif
  603. // 设置串口等待事件为有信号
  604. SetEvent( m_hSemComm );
  605. if( pBuffer != NULL)
  606. {
  607. delete[] pBuffer;
  608. pBuffer = NULL;
  609. }
  610. #else
  611. SimulationCommData();
  612. #endif
  613. return 0;
  614. }
  615. void CDataMate3000::SimulationCommData(void)
  616. {
  617. }
  618. WORD CDataMate3000::RtnCheck(char Msg[VAR_MSG])
  619. {
  620. int len ,index;
  621. len = 2;
  622. index = 7;
  623. char ch[2];
  624. ch[0] = Msg[index];
  625. ch[1] = Msg[index+1];
  626. if (atoi(ch)==ERR_CID_RTN_VAR)
  627. {
  628. return ERR_CID_RTN_VAR;
  629. }
  630. else if (atoi(ch)==ERR_CID_RTN_CHKSUM)
  631. {
  632. return ERR_CID_RTN_CHKSUM;
  633. }
  634. else if (atoi(ch)==ERR_CID_RTN_LCHKSUM)
  635. {
  636. return ERR_CID_RTN_LCHKSUM;
  637. }
  638. else if (atoi(ch)==ERR_CID_RTN_CID2)
  639. {
  640. return ERR_CID_RTN_CID2;
  641. }
  642. else if (atoi(ch)==ERR_CID_RTN_FORMAT)
  643. {
  644. return ERR_CID_RTN_FORMAT;
  645. }
  646. else if (atoi(ch)==ERR_CID_RTN_INVALI_DATA)
  647. {
  648. return ERR_CID_RTN_INVALI_DATA;
  649. }
  650. else /*if (atoi(ch)==ERR_CID_RTN_NORMAL)*/
  651. {
  652. return ERR_CID_RTN_NORMAL;
  653. }
  654. }