/************************************************* /* Copyright (C), 2010-2011,StoneU. Co., Ltd. /* File name: ST_3C20KS.h /* Author: Zero.t /* Version: Ver1.0.0.1 /* Date: 2012-01-05 /* Description: 山特 UPS,具体型号6C20KS;本类首次使用在项目<福建/第二医院>; /* Others: UPS协议类,不做控制设备操作; /* Function List: ### /* History: 1.Date: ### Author: ### Modification: ### *************************************************/ #include "StdAfx.h" #include ".\st_6c20ks_q1.h" #include "CommProcess.h" CST_6C20KS::CST_6C20KS(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_szQ1Msg, 0, sizeof(m_szQ1Msg)); memset(m_szWAMsg, 0, sizeof(m_szWAMsg)); m_dwOnlineTick = 0; m_devOnline = TRUE; } CST_6C20KS::~CST_6C20KS() { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL CST_6C20KS::SanTakOpenComm(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 CST_6C20KS::SendReadRequest( char *szPath, // 程序所在路径 char *szIniName, // 配置文件名称 int nCommPort, // 串行端口 int nAddr, // 设备地址 char *szCmd, // 请求命令 char *szMsg, // 响应的值 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, // 预留字符数组参数1接口 char *szReversed2, // 预留字符数组参数2接口 char *szReversed3, // 预留字符数组参数3接口 char *szReversed4, // 预留字符数组参数4接口 char *szReversed5 // 预留字符数组参数5接口 ) { CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) { LOG4C((LOG_NOTICE,"6C20KS 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_szQ1Msg) == 0 && (iCmd >= 1 && iCmd <= 14)) || iCmd == 1 || (strlen(m_szWAMsg) == 0 && (iCmd >= 15 && iCmd <= 26)) || iCmd == 15 ) { 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 = GetSanTak_Q1Msg(nAddr,szCmd, szMsg, nIndex, nLen, szDataType, iSBit,iEBit ); nRet = GetSanTak_WAMsg(nAddr,szCmd, szMsg, nIndex, nLen, szDataType, iSBit,iEBit ); //LOG4C((LOG_NOTICE, "%s=%s, return %d", szCmd, szMsg, nRet)); return nRet; } int CST_6C20KS::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 ) { LOG4C((LOG_NOTICE,"山特CST_6C20KS发送命令失败")); return nRet; } nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd); return nRet; } int CST_6C20KS::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, char *IniSendCMD,const int &IniSendlen) { #if DEBUG_ST6C20KS IniSendCMD[IniSendlen] = 0x0D; if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { ResetEvent(m_hSemComm); int nResult = pComm->Write((BYTE *)IniSendCMD, 3); if (nResult != 3){ SetEvent(m_hSemComm); return EER_CODE_SANTAK_COM_REGNUM; } } else{ return ERR_CODE_SANTAK_COM_BUSY; } #endif return 0; } int CST_6C20KS::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd) { #if DEBUG_ST6C20KS 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; } return ERR_CODE_SANTAK_COM_READ_NO_DATA; } //长度校验出错 ; SetSanTakQ1Msg(nAddr, szCmd, pBuff); SetSanTakWAMsg(nAddr, szCmd, pBuff); m_dwOnlineTick = GetTickCount(); SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(); #endif return 0; } void CST_6C20KS::SetSanTakQ1Msg( int nAddr,char *szCmd, char *pBuffer) { int iCmd = atoi(szCmd+4); if ( iCmd >=1 && iCmd <= 14 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szQ1Msg, pBuffer, sizeof(m_szQ1Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CST_6C20KS::SetSanTakWAMsg( int nAddr,char *szCmd, char *pBuffer) { int iCmd = atoi(szCmd+4); if ( iCmd >=15 && iCmd <= 26 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szWAMsg, pBuffer, sizeof(m_szWAMsg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int CST_6C20KS::GetSanTak_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 >=1 && iCmd <= 14 ){ #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szDataType, m_szQ1Msg + nIndex, szRecvMsg, nLen); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CST_6C20KS::GetSanTak_WAMsg(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 >=15 && iCmd <= 26 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szDataType, m_szWAMsg + nIndex, szRecvMsg, nLen); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } void CST_6C20KS::SimulationCommData(void) { memcpy(m_szQ1Msg, "(208.4 140.0 208.4 034 59.9 2.05 35.0 00110000.", sizeof("(208.4 140.0 208.4 034 59.9 2.05 35.0 00110000.")); memcpy(m_szWAMsg, "(215.4 216.8 221.0 235.4 226.8 421.0 616.8 261.0 295.4 276.8 621.0 019 01111100.", sizeof("(215.4 216.8 221.0 215.4 216.8 221.0 216.8 221.0 215.4 216.8 221.0 019 01111100.")); }