| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- #include "StdAfx.h"
- #include ".\delta.h"
- CDelta::CDelta(char *szPath,char *szIniName,int nCommPort,int nAddr,int nRate,int nDataBit,int nStopBit,int nParity,int nInterval)
- {
- #if IS_USE_READMSG_CS
- InitializeCriticalSection(&m_csReadMsg);
- #endif
- MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
- memset(m_szDelta_STBMsg, 0, sizeof(m_szDelta_STBMsg));
- memset(m_szDelta_STIMsg, 0, sizeof(m_szDelta_STIMsg));
- memset(m_szDelta_STOMsg, 0, sizeof(m_szDelta_STOMsg));
- memset(m_szDelta_STPMsg, 0, sizeof(m_szDelta_STPMsg));
- memset(m_szDelta_STAMsg, 0, sizeof(m_szDelta_STAMsg));
- m_dwOnlineTick = 0;
- m_devOnline = TRUE;
- }
- CDelta::~CDelta( )
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL CDelta::DetaUPSOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
- {
- BOOL bResult = FALSE;
- bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
- return bResult;
- }
- int CDelta::SendReadRequest(
- char *szPath,
- char *szIniName,
- int nCommPort,
- int nAddr,
- char *szCmd,
- char *szMsg,
- int nReversed1,
- int nReversed2,
- int nReversed3,
- int nReversed4,
- int nReversed5,
- float fReversed1,
- float fReversed2,
- float fReversed3,
- char *szReversed1,
- char *szReversed2,
- char *szReversed3,
- char *szReversed4,
- char *szReversed5
- )
- {
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- int nIndex = 0, nLen = 0, IniSendlen = 0;
- char IniSendCMD[MAX_SENDCMD] = {0};
- char szType[TYPE_LENGTH] = {0};
- int startBit = 0;
- int endBit = 0;
- m_address = nAddr;
- GetIniInfo(szPath, szIniName, szCmd, IniSendCMD, IniSendlen ,szType, nIndex, nLen, startBit, endBit);
- int iCmd = atoi(szCmd + 4);
- if(
- (strlen(m_szDelta_STBMsg) == 0 && (iCmd >= 1 && iCmd <= 15)) || iCmd == 1 ||
- (strlen(m_szDelta_STIMsg) == 0 && (iCmd >= 16 && iCmd <= 31)) || iCmd == 16 ||
- (strlen(m_szDelta_STOMsg) == 0 && (iCmd >= 32 && iCmd <= 49)) || iCmd == 32 ||
- (strlen(m_szDelta_STPMsg) == 0 && (iCmd >= 50 && iCmd <= 64)) || iCmd == 50 ||
- (strlen(m_szDelta_STAMsg) == 0 && (iCmd >= 65 && iCmd <= 85)) || iCmd == 65
- )
- {
- nRet = GetDeviceParam( pComm, IniSendCMD,IniSendlen, szCmd );
- if( nRet != 0 ) return nRet;
- }
- if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
- m_devOnline = FALSE;
- else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
- m_devOnline = TRUE;
- if( m_devOnline == FALSE )
- return -1;
- nRet = GetDeltaStbMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetDeltaStiMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetDeltaStoMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetDeltaStpMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetDeltaStaMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- LOG4C((LOG_NOTICE, "%s = %s", szCmd, szMsg));
- return nRet;
- }
- int CDelta::GetIniInfo( char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen,int &SBit,int &EBit)
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- IniSendCMD[0] = '~';
- IniSendCMD[1] = '0';
- IniSendCMD[2] = '0';
- IniSendCMD[3] = 'P';
- IniSendCMD[4] = '0';
- IniSendCMD[5] = '0';
- IniSendCMD[6] = '3';
- GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD + 7, sizeof(IniSendCMD) - 7, szFile);
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
- nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
- return 0;
- }
- int CDelta::GetDeviceParam(CCommProcess *pComm,char *IniSendCMD,const int &IniSendlen ,char *szCmd)
- {
- int nRet = -1;
- nRet = Send_ReadDeviceData(pComm, IniSendCMD, IniSendlen);
- if( nRet != 0 )
- return nRet;
- nRet = Recv_ReadDeviceData(pComm, szCmd);
- return nRet;
- }
- int CDelta::Send_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen)
- {
- #if DEBUG_DELTAVF
- if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
- {
- int nDatalen = (int)strlen(IniSendCMD);
- ResetEvent(m_hSemComm);
- int nResult = pComm->Write((BYTE *)IniSendCMD, nDatalen);
- if (nResult != nDatalen)
- {
- SetEvent(m_hSemComm);
- return EER_CODE_COM_REGNUM;
- }
- }
- else
- {
- return ERR_CODE_COM_BUSY; //串口忙
- }
- #endif
- return 0;
- }
- int CDelta::Recv_ReadDeviceData(CCommProcess *pComm,char *szCmd)
- {
- #if DEBUG_DELTAVF
- int nReceiveLen = 0, nReadLen = 0;
- char *pBuff = new char[MAX_RECE_MSG];
- memset(pBuff, 0 , MAX_RECE_MSG);
- nReadLen = pComm->Read((BYTE *)pBuff,MAX_RECE_MSG);
- if (nReadLen <= 0)
- {
- SetEvent(m_hSemComm);
- if (pBuff != NULL)
- {
- delete [] pBuff;
- pBuff = NULL;
- }
- return ERR_CODE_COM_READ_NO_DATA;
- }
- CopyDeltaStbMsg(szCmd, pBuff);
- CopyDeltaStiMsg(szCmd, pBuff);
- CopyDeltaStoMsg(szCmd, pBuff);
- CopyDeltaStpMsg(szCmd, pBuff);
- CopyDeltaStaMsg(szCmd, pBuff);
- m_dwOnlineTick = GetTickCount();
- SetEvent( m_hSemComm );
- if( pBuff != NULL)
- {
- delete[] pBuff;
- pBuff = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- void CDelta::CopyDeltaStbMsg(char *szCmd, char *pBuff)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 1 && iCmd <= 15)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szDelta_STBMsg, pBuff, sizeof(m_szDelta_STBMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CDelta::CopyDeltaStiMsg(char *szCmd, char *pBuff)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 16 && iCmd <= 31)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szDelta_STIMsg, pBuff, sizeof(m_szDelta_STIMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CDelta::CopyDeltaStoMsg(char *szCmd, char *pBuff)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 32 && iCmd <= 49)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szDelta_STOMsg, pBuff, sizeof(m_szDelta_STOMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CDelta::CopyDeltaStpMsg(char *szCmd, char *pBuff)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 50 && iCmd <= 64)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szDelta_STPMsg,pBuff,sizeof(m_szDelta_STPMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CDelta::CopyDeltaStaMsg(char *szCmd, char *pBuff)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 65 && iCmd <= 85)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szDelta_STAMsg,pBuff,sizeof(m_szDelta_STAMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- int CDelta::GetDeltaStbMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 1 && iCmd <= 15)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- CString strSTB = CString(m_szDelta_STBMsg+7);
- CString strTemp = strSTB;
- int nTempIndex = -1;
- for( int i = 0; i < nIndex - 1; i++ )
- {
- nTempIndex = strTemp.Find(";");
- strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
- }
- strTemp = strTemp.Mid(0, nLen);
- if (strTemp.Find(";") != -1) strTemp = "";
- if (nLen != 1) strTemp.TrimLeft("0");
- memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- return 0;
- }
- int CDelta::GetDeltaStiMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 16 && iCmd <= 31)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- CString strSTI = CString(m_szDelta_STIMsg+7);
- CString strTemp = strSTI;
- int nTempIndex = -1;
- for( int i = 0; i < nIndex - 1; i++ )
- {
- nTempIndex = strTemp.Find(";");
- strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
- }
- strTemp = strTemp.Mid(0, nLen);
- if (strTemp.Find(";") != -1) strTemp = "";
- if (nLen != 1) strTemp.TrimLeft("0");
- memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- return 0;
- }
- int CDelta::GetDeltaStoMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 32 && iCmd <= 49)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- CString strSTO = CString(m_szDelta_STOMsg+7);
- CString strTemp = strSTO;
- int nTempIndex = -1;
- for( int i = 0; i < nIndex - 1; i++ )
- {
- nTempIndex = strTemp.Find(";");
- strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
- }
- strTemp = strTemp.Mid(0, nLen);
- if (strTemp.Find(";") != -1) strTemp = "";
- if (nLen != 1) strTemp.TrimLeft("0");
- memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- return 0;
- }
- int CDelta::GetDeltaStpMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 50 && iCmd <= 64)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- CString strSTP = CString(m_szDelta_STPMsg+7);
- CString strTemp = strSTP;
- int nTempIndex = -1;
- for( int i = 0; i < nIndex - 1; i++ )
- {
- nTempIndex = strTemp.Find(";");
- strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
- }
- strTemp = strTemp.Mid(0, nLen);
- if (strTemp.Find(";") != -1) strTemp = "";
- if (nLen != 1) strTemp.TrimLeft("0");
- memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- return 0;
- }
- int CDelta::GetDeltaStaMsg(char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szDataType,int SBit ,int EBit)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 65 && iCmd <= 85)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- CString strSTA = CString(m_szDelta_STAMsg+7);
- CString strTemp = strSTA;
- int nTempIndex = -1;
- for( int i = 0; i < nIndex - 1; i++ )
- {
- nTempIndex = strTemp.Find(";");
- strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
- }
- strTemp = strTemp.Mid(0, nLen);
- if (strTemp.Find(";") != -1) strTemp = "";
- if (nLen != 1) strTemp.TrimLeft("0");
- memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- return 0;
- }
- void CDelta::SimulationCommData()
- {
- CString strSTP, strSTB, strSTI, strSTO, strSTA;
- // 旁路;
- strSTP = "~00D033500;3;2233;;;2240;;;2227;;;;;;020AB";
- // 电池;
- strSTB = "~00D0390;0;0;;;;2748;0000;;100;;2759;0000;;100D6";
- // 输入;
- strSTI = "~00D0453;499;2231;0264;25690;499;2238;0266;25668;499;2226;0224;26836;3880;3540;3360";
- // 输出;
- strSTO = "~00D0890;499;3;2208;0254;005301;025;2211;0237;004970;023;2209;0246;005196;024;3650;3550;3240;0";
- // 告警;
- strSTA = "~00D0360;0;0;0;0;0;0;0;0;0;0;;;0;;0;0;0;0;14D";
- memset(m_szDelta_STAMsg,0,sizeof(m_szDelta_STAMsg));
- memset(m_szDelta_STBMsg,0,sizeof(m_szDelta_STBMsg));
- memset(m_szDelta_STIMsg,0,sizeof(m_szDelta_STIMsg));
- memset(m_szDelta_STOMsg,0,sizeof(m_szDelta_STOMsg));
- memset(m_szDelta_STPMsg,0,sizeof(m_szDelta_STPMsg));
- memcpy(m_szDelta_STAMsg, (char *)(LPCTSTR)strSTA, strSTA.GetLength());
- memcpy(m_szDelta_STBMsg, (char *)(LPCTSTR)strSTB, strSTB.GetLength());
- memcpy(m_szDelta_STIMsg, (char *)(LPCTSTR)strSTI, strSTI.GetLength());
- memcpy(m_szDelta_STOMsg, (char *)(LPCTSTR)strSTO, strSTO.GetLength());
- memcpy(m_szDelta_STPMsg, (char *)(LPCTSTR)strSTP, strSTP.GetLength());
- }
|