#include "StdAfx.h" #include ".\ea800.h" CEA800::CEA800(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_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)); memset(m_szF_Msg, 0, sizeof(m_szF_Msg)); memset(m_szGF_Msg, 0, sizeof(m_szGF_Msg)); m_dwOnlineTick = 0; m_devOnline = TRUE; } CEA800::~CEA800(void) { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL CEA800::EA800OpenComm(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 CEA800::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 ) 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); if( ( strlen(m_szG1_Msg) == 0 && strcmp(IniSendCMD,"G1") == 0 ) || strcmp(szCmd, "cmd-1") == 0 || ( strlen(m_szG2_Msg) == 0 && strcmp(IniSendCMD,"G2") == 0 ) || strcmp(szCmd, "cmd-9") == 0 || ( strlen(m_szG3_Msg) == 0 && strcmp(IniSendCMD,"G3") == 0 ) || strcmp(szCmd, "cmd-28") == 0 || ( strlen(m_szQ1_Msg) == 0 && strcmp(IniSendCMD,"Q1") == 0 ) || strcmp(szCmd,"cmd-40") == 0 || ( strlen(m_szF_Msg) == 0 && strcmp(IniSendCMD,"F") == 0 ) || strcmp(szCmd, "cmd-55") == 0 ) { nRet = GetDeviceParam(nAddr,pComm,IniSendCMD,IniSendlen); if( nRet != 0 ) { LOG4C((LOG_NOTICE,"收发数据失败")); 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(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit ); nRet = GetEA800_G2Msg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit ); nRet = GetEA800_G3Msg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit ); nRet = GetEA800_Q1Msg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit ); nRet = GetEA800_FMsg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit ); nRet = GetEA800_GFMsg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit ); //LOG4C((LOG_NOTICE, "%s = %s", szCmd, szMsg)); return nRet; } int CEA800::GetDeviceParam( int nAddr,CCommProcess *pComm, char *IniSendCMD,const int &IniSendlen) { int nRet = -1; nRet = Send_ReadDeviceData(nAddr,pComm,IniSendCMD,IniSendlen); if( nRet != 0 ) return nRet; nRet = Recv_ReadDeviceData(nAddr,pComm,IniSendCMD); return nRet; } // IniSendCMD 2个byte; int CEA800::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen) { #if DEBUG_EA800 BYTE bySend[4] = {0}; memcpy(bySend,IniSendCMD,IniSendlen); bySend[IniSendlen] = 0x0D; int nDatalen = IniSendlen+1; if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { ResetEvent(m_hSemComm); int nResult = pComm->Write(bySend, nDatalen); if (nResult != nDatalen) { SetEvent(m_hSemComm); LOG4C((LOG_NOTICE,"实际写长度不一致")); return EER_CODE_EA800_COM_REGNUM; } } else { LOG4C((LOG_NOTICE,"线程信号外")); return ERR_CODE_EA800_COM_BUSY; } #endif return 0; } int CEA800::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szSendCMD) { #if DEBUG_EA800 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; } LOG4C((LOG_NOTICE,"接收数据长度 = 0")); return ERR_CODE_EA800_COM_READ_NO_DATA; } //长度校验出错 ; SetEA800_G1Msg(nAddr, szSendCMD, pBuff); SetEA800_G2Msg(nAddr, szSendCMD, pBuff); SetEA800_G3Msg(nAddr, szSendCMD, pBuff); SetEA800_Q1Msg(nAddr, szSendCMD, pBuff); SetEA800_FMsg( nAddr, szSendCMD, pBuff); SetEA800_GFMsg(nAddr, szSendCMD, pBuff); m_dwOnlineTick = GetTickCount(); SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(); #endif return 0; } void CEA800::SetEA800_G1Msg( int nAddr, char *szSendCMD,char *pBuffer) { if ( strcmp(szSendCMD,"G1") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szG1_Msg, pBuffer, sizeof(m_szG1_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CEA800::SetEA800_G2Msg( int nAddr, char *szSendCMD, char *pBuffer) { if ( strcmp(szSendCMD,"G2") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szG2_Msg, pBuffer, sizeof(m_szG2_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CEA800::SetEA800_G3Msg( int nAddr, char *szSendCMD, char *pBuffer) { if ( strcmp(szSendCMD,"G3") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szG3_Msg, pBuffer, sizeof(m_szG3_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CEA800::SetEA800_Q1Msg( int nAddr, char *szSendCMD,char *pBuffer) { if ( strcmp(szSendCMD,"Q1") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szQ1_Msg, pBuffer, sizeof(m_szQ1_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CEA800::SetEA800_FMsg( int nAddr, char *szSendCMD,char *pBuffer) { if ( strcmp(szSendCMD,"F") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szF_Msg, pBuffer, sizeof(m_szF_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CEA800::SetEA800_GFMsg( int nAddr, char *szSendCMD, char *pBuffer) { if ( strcmp(szSendCMD,"GF") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szGF_Msg, pBuffer, sizeof(m_szGF_Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int CEA800::GetEA800_G1Msg( int nAddr,char *szSendCMD, char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit ) { int nRet = 0; if ( strcmp(szSendCMD,"G1") == 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 CEA800::GetEA800_G2Msg( int nAddr,char *szSendCMD, char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit ) { int nRet = 0; if ( strcmp(szSendCMD,"G2") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szG2_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CEA800::GetEA800_G3Msg( int nAddr,char *szSendCMD,char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit ) { int nRet = 0; if ( strcmp(szSendCMD,"G3") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szG3_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CEA800::GetEA800_Q1Msg( int nAddr,char *szSendCMD,char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit ) { int nRet = 0; if ( strcmp(szSendCMD,"Q1") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szQ1_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CEA800::GetEA800_FMsg( int nAddr,char *szSendCMD, char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit ) { int nRet = 0; if ( strcmp(szSendCMD,"F") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szF_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CEA800::GetEA800_GFMsg( int nAddr,char *szSendCMD,char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit ) { int nRet = 0; if ( strcmp(szSendCMD,"GF") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szGF_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } void CEA800::SimulationCommData(void) { //LOG4C((LOG_NOTICE,"进行数据模拟操作")); memcpy(m_szQ1_Msg, "(000.0 000.0 219.0 000 00.0 2.14 16.0 10000000.", sizeof("(000.0 000.0 219.0 000 00.0 2.14 16.0 10000000.")); //memcpy(m_szF_Msg, "#235.1 012 231.1 50.2 000.", sizeof("#235.1 012 231.1 50.2 000.")); memcpy(m_szG1_Msg, "!372 067 0752 006.5 000.0 00.0 00.0 49.9.", sizeof("!372 067 0752 006.5 000.0 00.0 00.0 49.9.")); memcpy(m_szG2_Msg, "!01000100 00011011 00000000.", sizeof("!01000100 00011011 00000000.")); memcpy(m_szG3_Msg, "!000.0/000.0/000.0 000.0/000.0/000.0 219.0/219.0/219.0 000.0/000.0/009.0.", sizeof("!000.0/000.0/000.0 000.0/000.0/000.0 219.0/219.0/219.0 000.0/000.0/009.0.")); } //F: #235.1 012 231.1 50.2 000. //Q1:(235.2 000.0 245.3 015 49.9 5.66 26.3 10001001 000.