/************************************************* /* Copyright (C), 2010-2011,StoneU. Co., Ltd. /* File name: EA810H.cpp /* Author: Zero.t /* Version: Ver1.0.0.1 /* Date: 2011-12-08 /* Description: 易事特EA系列 UPS,EA810H; /* Others: RS232,2400/8/1,无校验; /* Function List: ### /* History: *************************************************/ #include "StdAfx.h" #include ".\ea810h.h" CEA810H::CEA810H( 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_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)); m_dwOnlineTick = 0; m_devOnline = TRUE; } CEA810H::~CEA810H(void) { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } int CEA810H::GetIniInfo( char szPath[MAX_PATH], char szIniName[MAX_PATH], char szCmd[MAX_CMD], char IniSendCMD[MAX_SENDCMD], char nType[MAX_CMD], int &nIndex, int &nLen) { CHAR szFile[MAX_PATH + 1] = ""; wsprintf(szFile, "%s\\config\\%s", szPath, szIniName); TRACE("szFile =%s\n",szFile); GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); IniSendCMD[strlen(IniSendCMD)] = 0x0D; GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile); nType[strlen(nType)] = '\0'; nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile); nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); return 0; } BOOL CEA810H::EA810HOpenComm(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 CEA810H::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 IniSendCMD[MAX_SENDCMD] = {0}; char szType[TYPE_LENGTH] = {0}; GetIniInfo(szPath, szIniName, szCmd, IniSendCMD, szType, nIndex, nLen); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; if( ( strlen(m_szG1_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 ))|| (strlen(m_szG2_Msg) == 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 || 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 )) || (strlen(m_szG3_Msg) == 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-33") == 0 || strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 )) || (strlen(m_szQ1_Msg) == 0 && ( strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 )) || strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-9") == 0 || strcmp(szCmd,"cmd-28") || strcmp(szCmd,"cmd-40") ) { nRet = GetDeviceParam( pComm, IniSendCMD, 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 = GetEA800_G1Msg(szCmd, szMsg, nIndex, nLen, szType ); nRet = GetEA800_G2Msg(szCmd, szMsg, nIndex, nLen, szType ); nRet = GetEA800_G3Msg(szCmd, szMsg, nIndex, nLen, szType ); nRet = GetEA800_Q1Msg(szCmd, szMsg, nIndex, nLen, szType ); return nRet; } int CEA810H::GetDeviceParam( CCommProcess *pComm,char IniSendCMD[MAX_SENDCMD],char szCmd[MAX_CMD],char szMsg[VAR_MSG],int &nIndex,int &nLen, char szType[TYPE_LENGTH]) { int nRet = -1; nRet = RequestStatus(pComm, IniSendCMD); if( nRet != 0 ) { return nRet; } nRet = ResponseStatus(pComm, szCmd, szMsg, nIndex, nLen, szType); return nRet; } // IniSendCMD 2个byte; int CEA810H::RequestStatus( CCommProcess *pComm,const char IniSendCMD[MAX_SENDCMD] ) { #if DEBUG_EA810H BYTE bySend[4] = {0}; memcpy(bySend,IniSendCMD,2); bySend[2] = 0x0D; int nDatalen = 3;//(int)sizeof(bySend); if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { ResetEvent(m_hSemComm); int nResult = pComm->Write(bySend, nDatalen); if (nResult != nDatalen){ SetEvent(m_hSemComm); return EER_CODE_EA800_COM_REGNUM; } } else{ return ERR_CODE_EA800_COM_BUSY; } #endif return 0; } int CEA810H::ResponseStatus(CCommProcess *pComm, char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] ) { #if DEBUG_EA810H int nReceiveLen = 260; int nReadLen = 0; 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; } return ERR_CODE_EA800_COM_READ_NO_DATA; } //长度校验出错 ; SetEA800_G1Msg(szCmd, pBuff); SetEA800_G2Msg(szCmd, pBuff); SetEA800_G3Msg(szCmd, pBuff); SetEA800_Q1Msg(szCmd, pBuff); m_dwOnlineTick = GetTickCount(); SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(); #endif return 0; } void CEA810H::SetEA800_G1Msg(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 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szG1_Msg, pBuff, sizeof(m_szG1_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CEA810H::SetEA800_G2Msg(char szCmd[MAX_CMD], char *pBuff) { if ( 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 || 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 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szG1_Msg, pBuff, sizeof(m_szG1_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CEA810H::SetEA800_G3Msg(char szCmd[MAX_CMD], char *pBuff) { if ( 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-33") == 0 || strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szG1_Msg, pBuff, sizeof(m_szG1_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CEA810H::SetEA800_Q1Msg(char szCmd[MAX_CMD], char *pBuff) { if ( strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szG1_Msg, pBuff, sizeof(m_szG1_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int CEA810H::GetEA800_G1Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] ) { 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 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szG1_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CEA810H::GetEA800_G2Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] ) { int nRet = 0; if (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 || 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 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szG1_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CEA810H::GetEA800_G3Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] ) { int nRet = 0; if (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-33") == 0 || strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szG1_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CEA810H::GetEA800_Q1Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] ) { int nRet = 0; if (strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szG1_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } void CEA810H::SimulationCommData(void) { memcpy(m_szG1_Msg, "(215.4 216.8 221.0 056 50.0 2.29 44.5 01111100.", sizeof("(215.4 216.8 221.0 056 50.0 2.29 44.5 00000000.")); }