////////////////////////////////////////////////////////////////////////////// ////// ////// ////// 文 件: UpsMonitorLan.cpp ////// ////// 作 者: Suguobing ////// ////// 创建时间: ////// ////// 说 明: Monitor Lan协议 ////// ////// ////// ////// 修改时间:2010-11-24 ////// ////// 修改说明: ////// ////// ////// ////////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CommProcess.h" #include "UpsMonitorLan.h" CUpsMonitorLan::CUpsMonitorLan( 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_szRD_Msg, 0, sizeof(m_szRD_Msg)); memset(m_szRS_Msg, 0, sizeof(m_szRD_Msg)); m_dwOnlineTick = 0; m_devOnline = TRUE; } CUpsMonitorLan::~CUpsMonitorLan() { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL CUpsMonitorLan::UpsMonitorOpenComm(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 CUpsMonitorLan::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_MONITOR_SEND_MSG] = {0}; char szType[UPS_TYPE_LENGTH] = {0}; int startBit = 0; int endBit = 0; m_address = nAddr; GetMonitorFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; if( ( strlen(m_szRD_Msg) == 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 ) ) || //获取系统模拟量量化数据(浮点数) ( strlen(m_szRS_Msg) == 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-1") == 0 || strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-14") == 0)//UPS工作状态及传送顺序 { nRet = GetDeviceParam( 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 = GetMonitorRDMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit); nRet = GetMonitorRSMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit); // if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0) // { // //LOG4C((LOG_NOTICE, "szCmd=%s, szMsg = %s,nIndex = %d, nLen = %d, szType =%s, nRet =%d",szCmd, szMsg,nIndex, nLen, szType, nRet)); // LOG4C_HEX_DUMP((LOG_NOTICE, szMsg, sizeof(szMsg))); // } //LOG4C((LOG_NOTICE, "nRet=%d", nRet)); return nRet; } int CUpsMonitorLan::GetMonitorRDMsg(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 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szRD_Msg + nIndex, szMsg, nLen, startBit, endBit); //LOG4C((LOG_NOTICE, "szcmd = %s, m_szRD_Msg=%s, szMsg = %s",szCmd, m_szRD_Msg, szMsg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUpsMonitorLan::GetMonitorRSMsg(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-14") == 0 || strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szRS_Msg + nIndex, szMsg, nLen, startBit, endBit); //LOG4C((LOG_NOTICE, "szcmd = %s, m_szRs_Msg=%s, szMsg = %s",szCmd, m_szRS_Msg, szMsg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUpsMonitorLan::GetDeviceParam( CCommProcess *pComm, //串口对象指针 char szSendMsg[UPS_MONITOR_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(pComm, szSendMsg); if( nRet != 0 ) { return nRet; // 串口忙 } nRet = ResponseStatus(pComm, szCmd, szMsg, nIndex, nLen, szType); return nRet; } int CUpsMonitorLan::GetMonitorFromIni( char szPath[MAX_PATH], //服务器程序所在目录 char szIniName[MAX_PATH], //配置文件名 char szCmd[MAX_CMD], //命令 char szSanTakSendMsg[MAX_CMD], //发送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", "", szSanTakSendMsg, 10, szFile); szSanTakSendMsg[strlen(szSanTakSendMsg)] = 0x0D; GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile); nType[strlen(nType)] = '\0'; nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile); nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); //LOG4C((LOG_NOTICE, "szCmd=%s, szSanTakSendMsg = %s", szCmd, szSanTakSendMsg)); return 0; } int CUpsMonitorLan::RequestStatus(CCommProcess *pComm, char chSendMsg[UPS_MONITOR_SEND_MSG] ) { #if DEBUG_MONITOR //开关量,用于测试模拟数据 if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { int nDatalen = (int)strlen(chSendMsg); ResetEvent(m_hSemComm); int nResult = pComm->Write((BYTE *)chSendMsg, nDatalen); if (nResult == nDatalen) { } else { SetEvent(m_hSemComm); return EER_CODE_MONITOR_COM_WRITE_DATA; } } else { return ERR_CODE_MONITOR_COM_BUSY; //串口忙 } #endif return 0; } int CUpsMonitorLan::ResponseStatus( CCommProcess *pComm, char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] ) { #if DEBUG_MONITOR int nReceiveLen = 0; int nProcessLen = 0; int nReadLen = 0; RESPONSE_STRUCT strcutResponse; memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT)); nReceiveLen = sizeof(RESPONSE_STRUCT); char *pBuff = new char[nReceiveLen]; memset(pBuff, 0 , nReceiveLen); nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen); if (nReadLen <= 0) { SetEvent(m_hSemComm); if (pBuff != NULL) { delete [] pBuff; pBuff = NULL; } //LOG4C((LOG_NOTICE, "CUpsMonitorLan::ResponseStatus pBuff = %s, nReadLen = %d, nReceiveLen = %d", pBuff, nReadLen, nReceiveLen)); return ERR_CODE_MONITOR_COM_READ_NO_DATA; } //长度校验出错 SetMonitorRDMsg(szCmd, pBuff); SetMonitorRSMsg(szCmd, pBuff); //LOG4C((LOG_NOTICE, "CUpsMonitorLan::ResponseStatus pBuff = %s, nReadLen = %d, nReceiveLen = %d", pBuff, nReadLen, nReceiveLen)); m_dwOnlineTick = GetTickCount(); // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(); #endif return 0; } void CUpsMonitorLan::SetMonitorRDMsg(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 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szRD_Msg, pBuff, sizeof(m_szRD_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUpsMonitorLan::SetMonitorRSMsg(char szCmd[MAX_CMD], char *pBuff) { if ( 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szRS_Msg, pBuff, sizeof(m_szRS_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUpsMonitorLan::SimulationCommData(void) { memcpy(m_szRD_Msg, "DVIA228..DVIB233..DVIC235..DVOA191..DVOB192..DVOC197..DLOA032..DLOB030..DLOC028..DTMP025..DHZF050..DVBT428..DTMB191..", sizeof("DVIA228..DVIB233..DVIC235..DVOA191..DVOB192..DVOC197..DLOA032..DLOB030..DLOC001..DTMP025..DHZF050..DVBT428..DTMB191..")); memcpy(m_szRS_Msg, "SOK1..SUF1..SBP1..SBM1..SBL1..", sizeof("SOK0..SUF0..SBP0..SBM0..SBL0..")); } int CUpsMonitorLan::SendSetReuest( 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接口 ) { return 0; }