#include "StdAfx.h" #include ".\ea99.h" EA99::EA99(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_szG1_Msg, 0, sizeof(m_szG1_Msg)); memset(m_szG2_Msg, 0, sizeof(m_szG2_Msg)); memset(m_szG3_Msg, 0, sizeof(m_szG3_Msg)); memset(m_szQ1_Msg, 0, sizeof(m_szQ1_Msg)); memset(m_szF_Msg, 0, sizeof(m_szF_Msg)); memset(m_szGF_Msg, 0, sizeof(m_szGF_Msg)); m_dwOnlineTick = 0; m_devOnline = TRUE; } EA99::~EA99() { #if IS_USE_READMSG_CS DeleteCriticalSection( &m_csReadMsg ); #endif MTVERIFY( CloseHandle( m_hSemComm ) ); CloseComm(); } BOOL EA99::EA99_OpenComm(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 EA99::SendReadRequest( char szPath[MAX_PATH], // 程序所在路径 char szIniName[MAX_PATH], // 配置文件名称 int nCommPort, // 串行端口 int nAddr, // 设备地址 char szCmd[MAX_CMD], // 请求命令 char szMsg[VAR_MSG], // 响应的值 int nReversed1, // 预留整形参数1接口 int nReversed2, // 预留整形参数2接口 int nReversed3, // 预留整形参数3接口 int nReversed4, // 预留整形参数4接口 int nReversed5, // 预留整形参数5接口 float fReversed1, // 预留float参数1接口 float fReversed2, // 预留float参数2接口 float fReversed3, // 预留float参数3接口 char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口 char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口 char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口 char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口 char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口 ) { CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0); char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0}; GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit); if( nAddr < 1 ) return -1; int iCmd = atoi(szCmd + 4); if( ( strlen(m_szG1_Msg) == 0 && (iCmd >= 1 && iCmd <= 8) ) || iCmd == 1 || ( strlen(m_szG2_Msg) == 0 && (iCmd >= 9 && iCmd <= 27) ) || iCmd == 9 || ( strlen(m_szG3_Msg) == 0 && (iCmd >= 28 && iCmd <= 39) ) || iCmd == 28 || ( strlen(m_szQ1_Msg) == 0 && (iCmd >= 40 && iCmd <= 54) ) || iCmd == 40 || ( strlen(m_szF_Msg) == 0 && (iCmd >= 55 && iCmd <= 58) ) || iCmd == 55 || ( strlen(m_szGF_Msg) == 0 && (iCmd >= 59 && iCmd <= 99) ) || iCmd == 59 ) { nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen ); 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 = GetEA800_G1Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType ); nRet = GetEA800_G2Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType ); nRet = GetEA800_G3Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType ); nRet = GetEA800_Q1Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType ); nRet = GetEA800_FMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType ); nRet = GetEA800_GFMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType ); //LOG4C((LOG_NOTICE,"%s = %s",szCmd,szMsg)); return nRet; } int EA99::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen) { int nRet = -1; nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen); if( nRet != 0 ) return nRet; nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd); return nRet; } int EA99::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, char *IniSendCMD,const int &IniSendlen) { #if DEBUG_EA99 BYTE byArySend[5] = {0}; memcpy(byArySend,IniSendCMD,IniSendlen); byArySend[IniSendlen] = 0x0D; if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口 { ResetEvent( m_hSemComm ); int nResult = pComm->Write(byArySend, IniSendlen +1 ); if( nResult != IniSendlen+1 ) { SetEvent( m_hSemComm ); return ERR_CODE_COM_FAULT; } } else { return ERR_CODE_COM_BUSY; } #endif return 0; } int EA99::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd) { #if DEBUG_EA99 BYTE *byAryRecv = new BYTE[255]; memset(byAryRecv,0, 255); int iRecvLen = pComm->Read(byAryRecv, 255); if( iRecvLen <= 0) { SetEvent( m_hSemComm ); if( iRecvLen != NULL) { delete[] byAryRecv; byAryRecv = NULL; } return ERR_CODE_COM_READ_NO_DATA; } SetEA800_G1Msg(nAddr, szCmd, byAryRecv); SetEA800_G2Msg(nAddr, szCmd, byAryRecv); SetEA800_G3Msg(nAddr, szCmd, byAryRecv); SetEA800_Q1Msg(nAddr, szCmd, byAryRecv); SetEA800_FMsg(nAddr, szCmd, byAryRecv); SetEA800_GFMsg(nAddr, szCmd, byAryRecv); m_dwOnlineTick = GetTickCount(); SetEvent( m_hSemComm ); if( byAryRecv != NULL) { delete[] byAryRecv; byAryRecv = NULL; } #else SimulationCommData(); #endif return 0; } void EA99::SetEA800_G1Msg( int nAddr,char *szCmd, BYTE *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 1 && iCmd <=8) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szG1_Msg, pBuffer, sizeof(m_szG1_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void EA99::SetEA800_G2Msg( int nAddr,char *szCmd, BYTE *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 9 && iCmd <= 27) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szG2_Msg, pBuffer, sizeof(m_szG2_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void EA99::SetEA800_G3Msg( int nAddr,char *szCmd, BYTE *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 28 && iCmd <= 39) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szG3_Msg, pBuffer, sizeof(m_szG3_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void EA99::SetEA800_Q1Msg( int nAddr,char *szCmd, BYTE *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 40 && iCmd <= 54) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szQ1_Msg, pBuffer, sizeof(m_szQ1_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void EA99::SetEA800_FMsg( int nAddr,char *szCmd, BYTE *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 55 && iCmd <= 58) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szF_Msg, pBuffer, sizeof(m_szF_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void EA99::SetEA800_GFMsg( int nAddr,char *szCmd, BYTE *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 59 && iCmd <= 99) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szGF_Msg, pBuffer, sizeof(m_szGF_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int EA99::GetEA800_G1Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit) { int nRet = 0; int iCmd = atoi(szCmd + 4); if ( iCmd >= 1 && iCmd <= 8) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szDataType, m_szG1_Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); nRet = 0; #endif } return nRet; } int EA99::GetEA800_G2Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit) { int nRet = 0; int iCmd = atoi(szCmd + 4); if ( iCmd >= 9 && iCmd <= 27) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szDataType, m_szG2_Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); nRet = 0; #endif } return nRet; } int EA99::GetEA800_G3Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit) { int nRet = 0; int iCmd = atoi(szCmd + 4); if ( iCmd >= 28 && iCmd <= 39) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szDataType, m_szG3_Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); nRet = 0; #endif } return nRet; } int EA99::GetEA800_Q1Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit) { int nRet = 0; int iCmd = atoi(szCmd + 4); if ( iCmd >= 40 && iCmd <= 54) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szDataType, m_szQ1_Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); nRet = 0; #endif } return nRet; } int EA99::GetEA800_FMsg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit) { int nRet = 0; int iCmd = atoi(szCmd + 4); if ( iCmd >= 55 && iCmd <= 58) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szDataType, m_szF_Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); nRet = 0; #endif } return nRet; } int EA99::GetEA800_GFMsg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit) { int nRet = 0; int iCmd = atoi(szCmd + 4); if ( iCmd >= 59 && iCmd <= 99) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szDataType, m_szGF_Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); nRet = 0; #endif } return nRet; } void EA99::SimulationCommData(void) { //GF:"!220V/381V 3P4W 050 220V/381V 3P4W 050 220V/381V 3P4W 050 480 60kVA ." //F:"#220.0 091 480.0 50.0." //Q1:"(228.4 228.4 219.3 000 50.0 D.65 00.0 00000000." //G1:"!546 099 0000 000.3 +00.0 49.9 49.9 49.9." //G2:"!00000001 00001111 00000000." //G3:"!228.2/227.1/228.6 226.6/225.2/227.6 219.1/219.1/218.7 000.0/000.0/000.0." //LOG4C((LOG_NOTICE,"进行数据模拟操作")); memcpy(m_szQ1_Msg, "(000.0 000.0 219.0 000 00.0 2.14 16.0 10000000.", sizeof("(000.0 000.0 219.0 000 00.0 2.14 16.0 10000000.")); //memcpy(m_szF_Msg, "#235.1 012 231.1 50.2 000.", sizeof("#235.1 012 231.1 50.2 000.")); memcpy(m_szG1_Msg, "!546 099 0000 000.3 +00.0 49.9 49.9 49.9.", sizeof("!546 099 0000 000.3 +00.0 49.9 49.9 49.9.")); memcpy(m_szG2_Msg, "!01000100 00011011 00000000.", sizeof("!01000100 00011011 00000000.")); memcpy(m_szG3_Msg, "!000.0/000.0/000.0 000.0/000.0/000.0 219.0/219.0/219.0 000.0/000.0/009.0.", sizeof("!000.0/000.0/000.0 000.0/000.0/000.0 219.0/219.0/219.0 000.0/000.0/009.0.")); }