Delta.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #include "StdAfx.h"
  2. #include ".\delta.h"
  3. CDelta::CDelta(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));
  9. memset(m_szDelta_STBMsg, 0, sizeof(m_szDelta_STBMsg));
  10. memset(m_szDelta_STIMsg, 0, sizeof(m_szDelta_STIMsg));
  11. memset(m_szDelta_STOMsg, 0, sizeof(m_szDelta_STOMsg));
  12. memset(m_szDelta_STPMsg, 0, sizeof(m_szDelta_STPMsg));
  13. memset(m_szDelta_STAMsg, 0, sizeof(m_szDelta_STAMsg));
  14. m_dwOnlineTick = 0;
  15. m_devOnline = TRUE;
  16. }
  17. CDelta::~CDelta( )
  18. {
  19. #if IS_USE_READMSG_CS
  20. DeleteCriticalSection(& m_csReadMsg);
  21. #endif
  22. MTVERIFY(CloseHandle(m_hSemComm));
  23. CloseComm();
  24. }
  25. BOOL CDelta::DetaUPSOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  26. {
  27. BOOL bResult = FALSE;
  28. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  29. return bResult;
  30. }
  31. int CDelta::SendReadRequest(
  32. char *szPath,
  33. char *szIniName,
  34. int nCommPort,
  35. int nAddr,
  36. char *szCmd,
  37. char *szMsg,
  38. int nReversed1,
  39. int nReversed2,
  40. int nReversed3,
  41. int nReversed4,
  42. int nReversed5,
  43. float fReversed1,
  44. float fReversed2,
  45. float fReversed3,
  46. char *szReversed1,
  47. char *szReversed2,
  48. char *szReversed3,
  49. char *szReversed4,
  50. char *szReversed5
  51. )
  52. {
  53. CCommProcess *pComm = FindComm(nCommPort);
  54. if( pComm == NULL ) return -1;
  55. int nRet = -1;
  56. int nIndex = 0, nLen = 0, IniSendlen = 0;
  57. char IniSendCMD[MAX_SENDCMD] = {0};
  58. char szType[TYPE_LENGTH] = {0};
  59. int startBit = 0;
  60. int endBit = 0;
  61. m_address = nAddr;
  62. GetIniInfo(szPath, szIniName, szCmd, IniSendCMD, IniSendlen ,szType, nIndex, nLen, startBit, endBit);
  63. int iCmd = atoi(szCmd + 4);
  64. if(
  65. (strlen(m_szDelta_STBMsg) == 0 && (iCmd >= 1 && iCmd <= 15)) || iCmd == 1 ||
  66. (strlen(m_szDelta_STIMsg) == 0 && (iCmd >= 16 && iCmd <= 31)) || iCmd == 16 ||
  67. (strlen(m_szDelta_STOMsg) == 0 && (iCmd >= 32 && iCmd <= 49)) || iCmd == 32 ||
  68. (strlen(m_szDelta_STPMsg) == 0 && (iCmd >= 50 && iCmd <= 64)) || iCmd == 50 ||
  69. (strlen(m_szDelta_STAMsg) == 0 && (iCmd >= 65 && iCmd <= 85)) || iCmd == 65
  70. )
  71. {
  72. nRet = GetDeviceParam( pComm, IniSendCMD,IniSendlen, szCmd );
  73. if( nRet != 0 ) return nRet;
  74. }
  75. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  76. m_devOnline = FALSE;
  77. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  78. m_devOnline = TRUE;
  79. if( m_devOnline == FALSE )
  80. return -1;
  81. nRet = GetDeltaStbMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  82. nRet = GetDeltaStiMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  83. nRet = GetDeltaStoMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  84. nRet = GetDeltaStpMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  85. nRet = GetDeltaStaMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  86. LOG4C((LOG_NOTICE, "%s = %s", szCmd, szMsg));
  87. return nRet;
  88. }
  89. int CDelta::GetIniInfo( char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen,int &SBit,int &EBit)
  90. {
  91. CHAR szFile[MAX_PATH + 1] = "";
  92. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  93. IniSendCMD[0] = '~';
  94. IniSendCMD[1] = '0';
  95. IniSendCMD[2] = '0';
  96. IniSendCMD[3] = 'P';
  97. IniSendCMD[4] = '0';
  98. IniSendCMD[5] = '0';
  99. IniSendCMD[6] = '3';
  100. GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD + 7, sizeof(IniSendCMD) - 7, szFile);
  101. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  102. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  103. return 0;
  104. }
  105. int CDelta::GetDeviceParam(CCommProcess *pComm,char *IniSendCMD,const int &IniSendlen ,char *szCmd)
  106. {
  107. int nRet = -1;
  108. nRet = Send_ReadDeviceData(pComm, IniSendCMD, IniSendlen);
  109. if( nRet != 0 )
  110. return nRet;
  111. nRet = Recv_ReadDeviceData(pComm, szCmd);
  112. return nRet;
  113. }
  114. int CDelta::Send_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen)
  115. {
  116. #if DEBUG_DELTAVF
  117. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  118. {
  119. int nDatalen = (int)strlen(IniSendCMD);
  120. ResetEvent(m_hSemComm);
  121. int nResult = pComm->Write((BYTE *)IniSendCMD, nDatalen);
  122. if (nResult != nDatalen)
  123. {
  124. SetEvent(m_hSemComm);
  125. return EER_CODE_COM_REGNUM;
  126. }
  127. }
  128. else
  129. {
  130. return ERR_CODE_COM_BUSY; //串口忙
  131. }
  132. #endif
  133. return 0;
  134. }
  135. int CDelta::Recv_ReadDeviceData(CCommProcess *pComm,char *szCmd)
  136. {
  137. #if DEBUG_DELTAVF
  138. int nReceiveLen = 0, nReadLen = 0;
  139. char *pBuff = new char[MAX_RECE_MSG];
  140. memset(pBuff, 0 , MAX_RECE_MSG);
  141. nReadLen = pComm->Read((BYTE *)pBuff,MAX_RECE_MSG);
  142. if (nReadLen <= 0)
  143. {
  144. SetEvent(m_hSemComm);
  145. if (pBuff != NULL)
  146. {
  147. delete [] pBuff;
  148. pBuff = NULL;
  149. }
  150. return ERR_CODE_COM_READ_NO_DATA;
  151. }
  152. CopyDeltaStbMsg(szCmd, pBuff);
  153. CopyDeltaStiMsg(szCmd, pBuff);
  154. CopyDeltaStoMsg(szCmd, pBuff);
  155. CopyDeltaStpMsg(szCmd, pBuff);
  156. CopyDeltaStaMsg(szCmd, pBuff);
  157. m_dwOnlineTick = GetTickCount();
  158. SetEvent( m_hSemComm );
  159. if( pBuff != NULL)
  160. {
  161. delete[] pBuff;
  162. pBuff = NULL;
  163. }
  164. #else
  165. SimulationCommData();
  166. #endif
  167. return 0;
  168. }
  169. void CDelta::CopyDeltaStbMsg(char *szCmd, char *pBuff)
  170. {
  171. int iCmd = atoi(szCmd + 4);
  172. if ( iCmd >= 1 && iCmd <= 15)
  173. {
  174. #if IS_USE_READMSG_CS
  175. EnterCriticalSection(&m_csReadMsg);
  176. #endif
  177. memcpy(m_szDelta_STBMsg, pBuff, sizeof(m_szDelta_STBMsg));
  178. #if IS_USE_READMSG_CS
  179. LeaveCriticalSection(&m_csReadMsg);
  180. #endif
  181. }
  182. }
  183. void CDelta::CopyDeltaStiMsg(char *szCmd, char *pBuff)
  184. {
  185. int iCmd = atoi(szCmd + 4);
  186. if ( iCmd >= 16 && iCmd <= 31)
  187. {
  188. #if IS_USE_READMSG_CS
  189. EnterCriticalSection(&m_csReadMsg);
  190. #endif
  191. memcpy(m_szDelta_STIMsg, pBuff, sizeof(m_szDelta_STIMsg));
  192. #if IS_USE_READMSG_CS
  193. LeaveCriticalSection(&m_csReadMsg);
  194. #endif
  195. }
  196. }
  197. void CDelta::CopyDeltaStoMsg(char *szCmd, char *pBuff)
  198. {
  199. int iCmd = atoi(szCmd + 4);
  200. if ( iCmd >= 32 && iCmd <= 49)
  201. {
  202. #if IS_USE_READMSG_CS
  203. EnterCriticalSection(&m_csReadMsg);
  204. #endif
  205. memcpy(m_szDelta_STOMsg, pBuff, sizeof(m_szDelta_STOMsg));
  206. #if IS_USE_READMSG_CS
  207. LeaveCriticalSection(&m_csReadMsg);
  208. #endif
  209. }
  210. }
  211. void CDelta::CopyDeltaStpMsg(char *szCmd, char *pBuff)
  212. {
  213. int iCmd = atoi(szCmd + 4);
  214. if ( iCmd >= 50 && iCmd <= 64)
  215. {
  216. #if IS_USE_READMSG_CS
  217. EnterCriticalSection(&m_csReadMsg);
  218. #endif
  219. memcpy(m_szDelta_STPMsg,pBuff,sizeof(m_szDelta_STPMsg));
  220. #if IS_USE_READMSG_CS
  221. LeaveCriticalSection(&m_csReadMsg);
  222. #endif
  223. }
  224. }
  225. void CDelta::CopyDeltaStaMsg(char *szCmd, char *pBuff)
  226. {
  227. int iCmd = atoi(szCmd + 4);
  228. if ( iCmd >= 65 && iCmd <= 85)
  229. {
  230. #if IS_USE_READMSG_CS
  231. EnterCriticalSection(&m_csReadMsg);
  232. #endif
  233. memcpy(m_szDelta_STAMsg,pBuff,sizeof(m_szDelta_STAMsg));
  234. #if IS_USE_READMSG_CS
  235. LeaveCriticalSection(&m_csReadMsg);
  236. #endif
  237. }
  238. }
  239. int CDelta::GetDeltaStbMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
  240. {
  241. int iCmd = atoi(szCmd + 4);
  242. if ( iCmd >= 1 && iCmd <= 15)
  243. {
  244. #if IS_USE_READMSG_CS
  245. EnterCriticalSection(&m_csReadMsg);
  246. #endif
  247. CString strSTB = CString(m_szDelta_STBMsg+7);
  248. CString strTemp = strSTB;
  249. int nTempIndex = -1;
  250. for( int i = 0; i < nIndex - 1; i++ )
  251. {
  252. nTempIndex = strTemp.Find(";");
  253. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  254. }
  255. strTemp = strTemp.Mid(0, nLen);
  256. if (strTemp.Find(";") != -1) strTemp = "";
  257. if (nLen != 1) strTemp.TrimLeft("0");
  258. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  259. #if IS_USE_READMSG_CS
  260. LeaveCriticalSection(&m_csReadMsg);
  261. #endif
  262. }
  263. return 0;
  264. }
  265. int CDelta::GetDeltaStiMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
  266. {
  267. int iCmd = atoi(szCmd + 4);
  268. if ( iCmd >= 16 && iCmd <= 31)
  269. {
  270. #if IS_USE_READMSG_CS
  271. EnterCriticalSection(&m_csReadMsg);
  272. #endif
  273. CString strSTI = CString(m_szDelta_STIMsg+7);
  274. CString strTemp = strSTI;
  275. int nTempIndex = -1;
  276. for( int i = 0; i < nIndex - 1; i++ )
  277. {
  278. nTempIndex = strTemp.Find(";");
  279. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  280. }
  281. strTemp = strTemp.Mid(0, nLen);
  282. if (strTemp.Find(";") != -1) strTemp = "";
  283. if (nLen != 1) strTemp.TrimLeft("0");
  284. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  285. #if IS_USE_READMSG_CS
  286. LeaveCriticalSection(&m_csReadMsg);
  287. #endif
  288. }
  289. return 0;
  290. }
  291. int CDelta::GetDeltaStoMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
  292. {
  293. int iCmd = atoi(szCmd + 4);
  294. if ( iCmd >= 32 && iCmd <= 49)
  295. {
  296. #if IS_USE_READMSG_CS
  297. EnterCriticalSection(&m_csReadMsg);
  298. #endif
  299. CString strSTO = CString(m_szDelta_STOMsg+7);
  300. CString strTemp = strSTO;
  301. int nTempIndex = -1;
  302. for( int i = 0; i < nIndex - 1; i++ )
  303. {
  304. nTempIndex = strTemp.Find(";");
  305. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  306. }
  307. strTemp = strTemp.Mid(0, nLen);
  308. if (strTemp.Find(";") != -1) strTemp = "";
  309. if (nLen != 1) strTemp.TrimLeft("0");
  310. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  311. #if IS_USE_READMSG_CS
  312. LeaveCriticalSection(&m_csReadMsg);
  313. #endif
  314. }
  315. return 0;
  316. }
  317. int CDelta::GetDeltaStpMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
  318. {
  319. int iCmd = atoi(szCmd + 4);
  320. if ( iCmd >= 50 && iCmd <= 64)
  321. {
  322. #if IS_USE_READMSG_CS
  323. EnterCriticalSection(&m_csReadMsg);
  324. #endif
  325. CString strSTP = CString(m_szDelta_STPMsg+7);
  326. CString strTemp = strSTP;
  327. int nTempIndex = -1;
  328. for( int i = 0; i < nIndex - 1; i++ )
  329. {
  330. nTempIndex = strTemp.Find(";");
  331. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  332. }
  333. strTemp = strTemp.Mid(0, nLen);
  334. if (strTemp.Find(";") != -1) strTemp = "";
  335. if (nLen != 1) strTemp.TrimLeft("0");
  336. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  337. #if IS_USE_READMSG_CS
  338. LeaveCriticalSection(&m_csReadMsg);
  339. #endif
  340. }
  341. return 0;
  342. }
  343. int CDelta::GetDeltaStaMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
  344. {
  345. int iCmd = atoi(szCmd + 4);
  346. if ( iCmd >= 65 && iCmd <= 85)
  347. {
  348. #if IS_USE_READMSG_CS
  349. EnterCriticalSection(&m_csReadMsg);
  350. #endif
  351. CString strSTA = CString(m_szDelta_STAMsg+7);
  352. CString strTemp = strSTA;
  353. int nTempIndex = -1;
  354. for( int i = 0; i < nIndex - 1; i++ )
  355. {
  356. nTempIndex = strTemp.Find(";");
  357. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  358. }
  359. strTemp = strTemp.Mid(0, nLen);
  360. if (strTemp.Find(";") != -1) strTemp = "";
  361. if (nLen != 1) strTemp.TrimLeft("0");
  362. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  363. #if IS_USE_READMSG_CS
  364. LeaveCriticalSection(&m_csReadMsg);
  365. #endif
  366. }
  367. return 0;
  368. }
  369. void CDelta::SimulationCommData()
  370. {
  371. CString strSTP, strSTB, strSTI, strSTO, strSTA;
  372. // 旁路;
  373. strSTP = "~00D033500;3;2233;;;2240;;;2227;;;;;;020AB";
  374. // 电池;
  375. strSTB = "~00D0390;0;0;;;;2748;0000;;100;;2759;0000;;100D6";
  376. // 输入;
  377. strSTI = "~00D0453;499;2231;0264;25690;499;2238;0266;25668;499;2226;0224;26836;3880;3540;3360";
  378. // 输出;
  379. strSTO = "~00D0890;499;3;2208;0254;005301;025;2211;0237;004970;023;2209;0246;005196;024;3650;3550;3240;0";
  380. // 告警;
  381. strSTA = "~00D0360;0;0;0;0;0;0;0;0;0;0;;;0;;0;0;0;0;14D";
  382. memset(m_szDelta_STAMsg,0,sizeof(m_szDelta_STAMsg));
  383. memset(m_szDelta_STBMsg,0,sizeof(m_szDelta_STBMsg));
  384. memset(m_szDelta_STIMsg,0,sizeof(m_szDelta_STIMsg));
  385. memset(m_szDelta_STOMsg,0,sizeof(m_szDelta_STOMsg));
  386. memset(m_szDelta_STPMsg,0,sizeof(m_szDelta_STPMsg));
  387. memcpy(m_szDelta_STAMsg, (char *)(LPCTSTR)strSTA, strSTA.GetLength());
  388. memcpy(m_szDelta_STBMsg, (char *)(LPCTSTR)strSTB, strSTB.GetLength());
  389. memcpy(m_szDelta_STIMsg, (char *)(LPCTSTR)strSTI, strSTI.GetLength());
  390. memcpy(m_szDelta_STOMsg, (char *)(LPCTSTR)strSTO, strSTO.GetLength());
  391. memcpy(m_szDelta_STPMsg, (char *)(LPCTSTR)strSTP, strSTP.GetLength());
  392. }