////////////////////////////////////////////////////////////////////////////// ////// ////// ////// 文 件: UpsEaton.cpp ////// ////// 作 者: xt ////// ////// 创建时间: 2012-05-08 10:45 ////// ////// 说 明: Eaton UPS协议 ////// ////// ////// ////// 修改时间: ////// ////// 修改说明: ////// ////// ////// ////////////////////////////////////////////////////////////////////////////// #include"stdafx.h" #include "UpsEaton.h" #include "CommProcess.h" CUpsEaton::CUpsEaton( char szPath[MAX_PATH], char szIniName[MAX_PATH], 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_szQ2Msg, 0, sizeof(m_szQ2Msg)); memset(m_szWAMsg, 0, sizeof(m_szWAMsg)); m_dwOnlineTick = 0; m_devOnline = TRUE; } CUpsEaton::~CUpsEaton() { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL CUpsEaton::UpsEatonOpenComm(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 CUpsEaton::SendReadRequest( char szPath[MAX_PATH], char szIniName[MAX_PATH], int nCommPort, int nAddr, char szCmd[MAX_CMD], char szMsg[VAR_MSG], int nReversed1, int nReversed2, int nReversed3, int nReversed4, int nReversed5, float fReversed1, float fReversed2, float fReversed3, char szReversed1[MAX_RESERVED1], char szReversed2[MAX_RESERVED2], char szReversed3[MAX_RESERVED3], char szReversed4[MAX_RESERVED4], char szReversed5[MAX_RESERVED5] ) { int nIndex = 0, nLen = 0; char szSendMsg[UPS_EATON_SEND_MSG] = {0}; char szType[UPS_TYPE_LENGTH] = {0}; int startBit = 0; int endBit = 0; m_address = nAddr; GetParFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit); LOG4C((LOG_NOTICE, " szSendMsg = %s", szSendMsg)); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; if(( strlen(m_szQ2Msg) == 0 && ( strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 || strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 || strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0 || strcmp(szCmd,"cmd-9") == 0 || strcmp(szCmd,"cmd-10") == 0 || strcmp(szCmd,"cmd-11") == 0 || strcmp(szCmd,"cmd-12") == 0 || strcmp(szCmd,"cmd-13") == 0 || strcmp(szCmd,"cmd-14") == 0 || strcmp(szCmd,"cmd-15") == 0 || strcmp(szCmd,"cmd-16") == 0 || strcmp(szCmd,"cmd-17") == 0 || strcmp(szCmd,"cmd-18") == 0 || strcmp(szCmd,"cmd-19") == 0 || strcmp(szCmd,"cmd-20") == 0 ) ) || (strlen(m_szWAMsg) == 0 && ( strcmp(szCmd,"cmd-21") == 0 || strcmp(szCmd,"cmd-22") == 0 || strcmp(szCmd,"cmd-23") == 0 || strcmp(szCmd,"cmd-24") == 0 || strcmp(szCmd,"cmd-25") == 0 || strcmp(szCmd,"cmd-26") == 0 || strcmp(szCmd,"cmd-27") == 0 || strcmp(szCmd,"cmd-28") == 0 || strcmp(szCmd,"cmd-29") == 0 || strcmp(szCmd,"cmd-30") == 0 || strcmp(szCmd,"cmd-31") == 0 || strcmp(szCmd,"cmd-32") == 0 )) || strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-21") == 0 ) { nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType ); 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 = GetUpsEaton_Q2Msg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit); nRet = GetUpsEaton_WAMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit); //LOG4C_HEX_DUMP((LOG_NOTICE, szMsg, sizeof(szMsg))); //LOG4C((LOG_NOTICE, "cmd=%s, sizeof = %d, result %s", szCmd, sizeof(szMsg), szMsg)); return nRet; } int CUpsEaton::GetUpsEaton_Q2Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit ) { int nRet = 0; if (strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 || strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 || strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0 || strcmp(szCmd,"cmd-9") == 0 || strcmp(szCmd,"cmd-10") == 0 || strcmp(szCmd,"cmd-11") == 0 || strcmp(szCmd,"cmd-12") == 0 || strcmp(szCmd,"cmd-13") == 0 || strcmp(szCmd,"cmd-14") == 0 || strcmp(szCmd,"cmd-15") == 0 || strcmp(szCmd,"cmd-16") == 0 || strcmp(szCmd,"cmd-17") == 0 || strcmp(szCmd,"cmd-18") == 0 || strcmp(szCmd,"cmd-19") == 0 || strcmp(szCmd,"cmd-20") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szQ2Msg + nIndex, szMsg, nLen, startBit, endBit); //LOG4C((LOG_NOTICE, "GetSanTak_3C_Q4Msg")); //LOG4C_HEX_DUMP((LOG_INFO, m_szSanTak_3C_Q4Msg,sizeof(m_szSanTak_3C_Q4Msg))); //LOG4C_HEX_DUMP((LOG_INFO, szMsg, 5 )); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUpsEaton::GetUpsEaton_WAMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit ) { int nRet = 0; if ( strcmp(szCmd,"cmd-21") == 0 || strcmp(szCmd,"cmd-22") == 0 || strcmp(szCmd,"cmd-23") == 0 || strcmp(szCmd,"cmd-24") == 0 || strcmp(szCmd,"cmd-25") == 0 || strcmp(szCmd,"cmd-26") == 0 || strcmp(szCmd,"cmd-27") == 0 || strcmp(szCmd,"cmd-28") == 0 || strcmp(szCmd,"cmd-29") == 0 || strcmp(szCmd,"cmd-30") == 0 || strcmp(szCmd,"cmd-31") == 0 || strcmp(szCmd,"cmd-32") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif LOG4C((LOG_NOTICE, "szType = %s, m_szWAMsg = %s, nIndex = %d ,nLen = %d, startBit = %d, endBit = %d", szType,m_szWAMsg, nIndex,nLen,startBit,endBit)); DataConversion(szType, m_szWAMsg + nIndex, szMsg, nLen, startBit, endBit); LOG4C((LOG_NOTICE, "szMsg = %s",szMsg)); //LOG4C_HEX_DUMP((LOG_INFO, m_szSanTak_3C_Q4Msg,sizeof(m_szSanTak_3C_Q4Msg))); //LOG4C_HEX_DUMP((LOG_INFO, szMsg, 5 )); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUpsEaton::GetDeviceParam( int nAddr, CCommProcess *pComm, // 串口对象指针 char szSendMsg[UPS_EATON_SEND_MSG], // 发送Buffer char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH]) // 变量数据类型 { int nRet = -1; nRet = RequestStatus(nAddr, pComm, szSendMsg); if( nRet != 0 ) { return nRet; // 串口忙 } nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType); return nRet; } int CUpsEaton::GetParFromIni( char szPath[MAX_PATH], //服务器程序所在目录 char szIniName[MAX_PATH], //配置文件名 char szCmd[MAX_CMD], //命令 char *szUpsEatonSendMsg, //发送Buffer char nType[MAX_CMD], int &nIndex, int &nLen, int &startBit, int &endBit) { CHAR szFile[MAX_PATH + 1] = ""; wsprintf(szFile, "%s\\config\\%s", szPath, szIniName); //TRACE("szFile =%s\n",szFile); GetPrivateProfileString(szCmd, "SendCmd", "", szUpsEatonSendMsg, 10, szFile); szUpsEatonSendMsg[strlen(szUpsEatonSendMsg)] = 0x0D; GetPrivateProfileString(szCmd, "Type", "", nType, 10, szFile); nType[strlen(nType)] = '\0'; nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile); nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); startBit = GetPrivateProfileInt(szCmd,"staBit",0,szFile); endBit = GetPrivateProfileInt(szCmd,"EndBit",0,szFile); return 0; } int CUpsEaton::RequestStatus(int nAddr, CCommProcess *pComm,const char chSendMsg[UPS_EATON_SEND_MSG] ) { #if DEBUG_UPS_EATON_120 //开关量,用于测试模拟数据 if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { int nDatalen = (int)sizeof(chSendMsg)-1; ResetEvent(m_hSemComm); int nResult = pComm->Write((BYTE *)chSendMsg, nDatalen); //LOG4C_HEX_DUMP((LOG_NOTICE, chSendMsg, 3)); //LOG4C((LOG_NOTICE, "CSanTak::RequestStatus chSendMsg = %s, nResult = %d, nDatalen = %d", chSendMsg,nResult, nDatalen)); if (nResult == nDatalen) { } else { SetEvent(m_hSemComm); return EER_CODE_UPS_EATON_COM_REGNUM; } } else { return ERR_CODE_UPS_EATON_COM_BUSY; //串口忙 } #endif return 0; } int CUpsEaton::ResponseStatus( int nAddr, CCommProcess *pComm, char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] ) { #if DEBUG_UPS_EATON_120 int nReceiveLen = MAX_PATH; int nProcessLen = 0; int nReadLen = 0; char *pBuff = new char[nReceiveLen]; memset(pBuff, 0 , nReceiveLen); nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen); //LOG4C((LOG_NOTICE, "CSanTak::ResponseStatus pBuff = %s, nReadLen = %d, nReceiveLen = %d", pBuff, nReadLen, nReceiveLen)); if (nReadLen <= 0) { SetEvent(m_hSemComm); if (pBuff != NULL) { delete [] pBuff; pBuff = NULL; } return ERR_CODE_UPS_EATON_COM_READ_NO_DATA; } //长度校验出错 SetUpsEaton_Q2Msg(szCmd, pBuff); SetUpsEaton_WAMsg(szCmd, pBuff); m_dwOnlineTick = GetTickCount(); // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(); #endif //LOG4C((LOG_NOTICE, "SimulationCommData")); return 0; } void CUpsEaton::SetUpsEaton_Q2Msg(char szCmd[MAX_CMD], char *pBuff) { if ( strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 || strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 || strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0 || strcmp(szCmd,"cmd-9") == 0 || strcmp(szCmd,"cmd-10") == 0 || strcmp(szCmd,"cmd-11") == 0 || strcmp(szCmd,"cmd-12") == 0 || strcmp(szCmd,"cmd-13") == 0 || strcmp(szCmd,"cmd-14") == 0 || strcmp(szCmd,"cmd-15") == 0 || strcmp(szCmd,"cmd-16") == 0 || strcmp(szCmd,"cmd-17") == 0 || strcmp(szCmd,"cmd-18") == 0 || strcmp(szCmd,"cmd-19") == 0 || strcmp(szCmd,"cmd-20") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szQ2Msg, pBuff, sizeof(m_szQ2Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUpsEaton::SetUpsEaton_WAMsg(char szCmd[MAX_CMD], char *pBuff) { if ( strcmp(szCmd,"cmd-21") == 0 || strcmp(szCmd,"cmd-22") == 0 || strcmp(szCmd,"cmd-23") == 0 || strcmp(szCmd,"cmd-24") == 0 || strcmp(szCmd,"cmd-25") == 0 || strcmp(szCmd,"cmd-26") == 0 || strcmp(szCmd,"cmd-27") == 0 || strcmp(szCmd,"cmd-28") == 0 || strcmp(szCmd,"cmd-29") == 0 || strcmp(szCmd,"cmd-30") == 0 || strcmp(szCmd,"cmd-31") == 0 || strcmp(szCmd,"cmd-32") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szWAMsg, pBuff, sizeof(m_szWAMsg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUpsEaton::SimulationCommData(void) { }