/************************************************* /* Copyright (C), 2010-2011,StoneU. Co., Ltd. /* File name: EA800.h /* Author: Zero.t /* Version: Ver1.0.0.1 /* Date: 2011-12-05 /* Description: 易事特EA系列 UPS,EA800; /* Others: RS232,2400/8/1,无校验; /* Function List: ### /* History: *************************************************/ #include "StdAfx.h" #include ".\icp_i7017.h" ICP_I7017::ICP_I7017(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)); for( int i = 0; i < MAX_ADDR; i++ ) { memset(m_szAI_Msg, 0, sizeof(m_szAI_Msg)); m_devOnline[i] = TRUE; m_dwOnlineTick[i] = 0; } } ICP_I7017::~ICP_I7017(void) { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL ICP_I7017::ICP_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 ICP_I7017::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] ) { CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) { LOG4C((LOG_NOTICE,"7017 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); int iCmd = atoi(szCmd+4); if( strlen(m_szAI_Msg[nAddr - 1]) == 0 && ( iCmd >= 1 && iCmd <= 8) || iCmd == 1 ) { nRet = GetDeviceParam(nAddr, pComm, IniSendCMD, IniSendlen, szCmd ); if( nRet != 0 ) { LOG4C((LOG_NOTICE,"收发数据失败")); return nRet; } } if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 * 1000 && m_dwOnlineTick[nAddr - 1] > 0 ) { m_devOnline[nAddr - 1] = FALSE; } else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 * 1000 && m_dwOnlineTick[nAddr - 1] > 0 ){ m_devOnline[nAddr - 1] = TRUE; } if( m_devOnline[nAddr - 1] == FALSE ) { return -1; } nRet = GetICP_AI_Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType ); //LOG4C((LOG_NOTICE, "nCommPort = %d,%s = %s",nCommPort, szCmd, szMsg)); return nRet; } int ICP_I7017::GetDeviceParam( int nAddr,CCommProcess *pComm,char *IniSendCMD, const int &IniSendlen, char *szCmd) { int nRet(-1); nRet = Send_ReadDeviceData(nAddr, pComm, IniSendCMD, IniSendlen); if( nRet != 0 ) { return nRet; } nRet = Recv_ReadDeviceData(nAddr, pComm, szCmd); return nRet; } // IniSendCMD 2个byte; int ICP_I7017::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen) { #if DEBUG_ICP_I7017 BYTE bySend[6] = {0}; memcpy(bySend,IniSendCMD,IniSendlen); // 设备地址描述; bySend[IniSendlen] = ByteToAscii((nAddr>>4) & 0x0f); bySend[IniSendlen + 1] = ByteToAscii(nAddr & 0x0f); bySend[IniSendlen + 2] = 0x0D; int nDatalen = (int)strlen((char *)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_COM_REGNUM; } } else{ return ERR_CODE_COM_BUSY; } #endif return 0; } int ICP_I7017::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd) { #if DEBUG_ICP_I7017 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_COM_READ_NO_DATA; } //长度校验出错 ; CopyICP_AI_Msg(nAddr, szCmd, pBuff); m_dwOnlineTick[nAddr -1] = GetTickCount(); SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(nAddr); #endif return 0; } void ICP_I7017::CopyICP_AI_Msg( int nAddr, char *szCmd, char *pBuffer) { int iCmd = atoi(szCmd+4); if ( iCmd >=1 && iCmd <= 8 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szAI_Msg[nAddr -1], pBuffer, sizeof(m_szAI_Msg[nAddr -1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int ICP_I7017::GetICP_AI_Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType ) { int nRet = 0; int iCmd = atoi(szCmd+4); if ( iCmd >=1 && iCmd <= 8 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szAI_Msg[nAddr -1] + nIndex, szMsg, nLen, 0, 0); {// 系数转换; float fNum = atof(szMsg); fNum -= 4; sprintf(szMsg,"%f",fNum); } #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } void ICP_I7017::SimulationCommData( int nAddr) { memcpy(m_szAI_Msg[nAddr -1], ">-00.012-00.013-00.014-00.014-00.012-00.011-00.011-00.011.", sizeof(">-00.012-00.013-00.014-00.014-00.012-00.011-00.011-00.011.")); }