#include "StdAfx.h" #include ".\fad.h" other_fad::other_fad(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_sz01Msg, 0, sizeof(m_sz01Msg)); memset(m_sz02Msg, 0, sizeof(m_sz02Msg)); memset(m_sz10Msg, 0, sizeof(m_sz10Msg)); memset(m_sz11Msg, 0, sizeof(m_sz11Msg)); m_dwOnlineTick = 0; m_devOnline = TRUE; } other_fad::~other_fad() { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL other_fad::OpenOtherComm(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 other_fad::GetIniInfo(char *szPath,char *szIniName,char *szCmd,int &IniSendCMD, char *nType,int &nIndex,int &nLen) { CHAR szFile[MAX_PATH + 1] = ""; wsprintf(szFile, "%s\\config\\%s", szPath, szIniName); TRACE("szFile =%s\n",szFile); IniSendCMD = GetPrivateProfileInt(szCmd, "SendCmd", 0, szFile); // 返回的字符串是以\0结束的; GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile); nType[strlen(nType)] = '\0'; nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile); nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); return 0; } int other_fad::SendReadRequest( char *szPath, char *szIniName, int nCommPort, int nAddr, char *szCmd, char *szMsg, int nReversed1, int nReversed2, int nReversed3, int nReversed4, int nReversed5, float fReversed1, float fReversed2, float fReversed3, char *szReversed1, char *szReversed2, char *szReversed3, char *szReversed4, char *szReversed5 ) { CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet(-1); int nIndex(0), nLen(0), IniSendCMD(0); char szDataType[CMD_TYPE] = {0}; GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,szDataType,nIndex,nLen); if ( ( strlen(m_sz01Msg) == 0 && (strcmp(szCmd, "cmd-1") == 0 )) || ( strlen(m_sz02Msg) == 0 && (strcmp(szCmd, "cmd-2") == 0 )) || ( strlen(m_sz10Msg) == 0 && (strcmp(szCmd, "cmd-3") == 0 )) || ( strlen(m_sz11Msg) == 0 && (strcmp(szCmd, "cmd-4") == 0 )) || strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ) { nRet = GetDeviceParam(nAddr ,pComm, IniSendCMD, szCmd ); 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 = GetFAD_01Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType); nRet = GetFAD_02Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType); nRet = GetFAD_10Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType); nRet = GetFAD_11Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType); //LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg)); return nRet; } // 发送写设置; int other_fad::SendSetReuest( char *szPath, char *szIniName, int nCommPort, int nAddr, char *szCmd, char *szMsg, int nReversed1, int nReversed2, int nReversed3, int nReversed4, int nReversed5, float fReversed1, float fReversed2, float fReversed3, char *szReversed1, char *szReversed2, char *szReversed3, char *szReversed4, char *szReversed5 ) { return 0; } int other_fad::GetDeviceParam(int naddr, CCommProcess *pComm,const int &IniSendCMD,char *szCmd) { int nRet(-1); nRet = Send_ReadDeviceData(naddr, pComm, IniSendCMD); if( nRet != 0 ) { return nRet; } nRet = Recv_ReadDeviceData(naddr, pComm, szCmd); return nRet; } int other_fad::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,const int &IniSendCMD) { #if DEBUG_FADCTRL int nSLen = sizeof(SEND_DEVICEDATA); SEND_DEVICEDATA SendDeviceData; memset(&SendDeviceData, 0, sizeof(SEND_DEVICEDATA)); SendDeviceData.Start = 0xFF; SendDeviceData.Addr = nAddr; SendDeviceData.Cid = IniSendCMD; memset(&SendDeviceData.Data,0,2); // 结束符; SendDeviceData.End = nAddr ^ IniSendCMD ^ 0 ^ 0; if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { ResetEvent(m_hSemComm); int nResult = pComm->Write((BYTE *)&SendDeviceData, nSLen); if (nResult != nSLen){ SetEvent(m_hSemComm); return EER_CODE_COM_REGNUM; } } else{ return ERR_CODE_COM_BUSY; } #endif return 0; } int other_fad::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd) { #if DEBUG_FADCTRL int nProcessLen(0),nReadLen(0); char *pBuff = new char[MAX_RECE_MSG]; memset(pBuff, 0 , MAX_RECE_MSG); nReadLen = pComm->Read((BYTE *)pBuff,MAX_RECE_MSG); if (nReadLen <= 0){ SetEvent(m_hSemComm); if (pBuff != NULL) { delete [] pBuff; pBuff = NULL; } return ERR_CODE_COM_READ_NO_DATA; } //长度校验出错 ; CopyFAD_01_Msg(nAddr, szCmd, pBuff); CopyFAD_02_Msg(nAddr, szCmd, pBuff); CopyFAD_10_Msg(nAddr, szCmd, pBuff); CopyFAD_11_Msg(nAddr, szCmd, pBuff); m_dwOnlineTick = GetTickCount(); SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(); #endif return 0; } void other_fad::CopyFAD_01_Msg( int naddr, char *szCmd, char *pBuffer) { if ( strcmp(szCmd, "cmd-1") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_sz01Msg, pBuffer, sizeof(m_sz01Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void other_fad::CopyFAD_02_Msg( int naddr, char *szCmd, char *pBuffer) { if (strcmp(szCmd, "cmd-2") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_sz02Msg, pBuffer, sizeof(m_sz02Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void other_fad::CopyFAD_10_Msg( int naddr, char *szCmd, char *pBuffer) { if ( strcmp(szCmd, "cmd-3") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_sz10Msg, pBuffer, sizeof(m_sz10Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void other_fad::CopyFAD_11_Msg( int naddr, char *szCmd, char *pBuffer) { if ( strcmp(szCmd, "cmd-4") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_sz11Msg, pBuffer, sizeof(m_sz11Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int other_fad::GetFAD_01Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType ) { int nRet(0); if (strcmp(szCmd, "cmd-1") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif hl_to_hex(m_sz01Msg[3],m_sz01Msg[4],szMsg); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int other_fad::GetFAD_02Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType ) { int nRet(0); if (strcmp(szCmd, "cmd-2") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif hl_to_hex(m_sz02Msg[3],m_sz02Msg[4],szMsg); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int other_fad::GetFAD_10Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType ) { int nRet(0); if (strcmp(szCmd, "cmd-3") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif hl_to_hex(m_sz10Msg[3],m_sz10Msg[4],szMsg); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int other_fad::GetFAD_11Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType ) { int nRet(0); if (strcmp(szCmd, "cmd-4") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif hl_to_hex(m_sz11Msg[3],m_sz11Msg[4],szMsg); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } void other_fad::SimulationCommData(void) { #if 0 m_sz10Msg[0] = 0xFF; m_sz10Msg[1] = 0x01; m_sz10Msg[2] = 0x10; m_sz10Msg[3] = 0x01; m_sz10Msg[4] = 0x1A; #endif }