#include "StdAfx.h" #include ".\cpdu.h" cpdu::cpdu(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) ); //CreateEvent()创建或打开一个命名的或无名的事件对象 for( int i = 0; i < CPDU_ADDR; i++ ) { memset(m_szMsg[i], 0, sizeof(m_szMsg[i])); m_devOnline[i] = TRUE; m_dwOnlineTick[i] = 0; } } cpdu::~cpdu() { #if IS_USE_READMSG_CS DeleteCriticalSection( &m_csReadMsg ); #endif MTVERIFY( CloseHandle( m_hSemComm ) ); CloseComm(); } BOOL cpdu::_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 ); //LOG4C((LOG_NOTICE , "PDU::OpenComm Com%d = %d",nCommPort,bResult)); return bResult; } BYTE cpdu::GetCheckCode(const BYTE *pSendBuf, const int &ilen) { BYTE byLrc = 0; for(int i=2; i= 1 && iCmd <= 17)) || iCmd == 1 ) { nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen ); if ( nRet != 0) 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 ) { LOG4C((LOG_NOTICE,"CPDU::m_devOnline=FALSE")); return -1; } nRet = GetCPDUVarMsg( nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit,iEBit); //LOG4C((LOG_NOTICE,"CPDU->cmd = %s,值 = %s",szCmd,szMsg)); return nRet; } int cpdu::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen) { //LOG4C((LOG_NOTICE,"PDU::GetDeviceParam")); int nRet = -1; nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen); if( nRet != 0 ) { LOG4C((LOG_NOTICE,"PDU发送命令失败")); return nRet; } nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd); return nRet; } int cpdu::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen) { #if DEBUG_CPDU //LOG4C((LOG_NOTICE,"PDU::Send_ReadDeviceData,nAddr = %d",nAddr)); int iSendLen = 0; BYTE byArySend[16] = {0}; byArySend[0] = 0x69; // 地址; byArySend[1] = nAddr; // 12个XX; byArySend[2] = 0x00; byArySend[3] = 0x00; byArySend[4] = 0x00; byArySend[5] = 0x00; byArySend[6] = 0x00; byArySend[7] = 0x00; byArySend[8] = 0x00; byArySend[9] = 0x00; byArySend[10] = 0x00; byArySend[11] = 0x00; byArySend[12] = 0x00; byArySend[13] = 0x00; // 结束; byArySend[14] = byArySend[0] ^ byArySend[1]; iSendLen = 15; if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口 { ResetEvent( m_hSemComm ); int nResult = pComm->Write(byArySend, iSendLen); if( nResult != iSendLen ) { SetEvent( m_hSemComm ); LOG4C((LOG_NOTICE,"PDU::Send_ReadDeviceData=ERR_CODE_COM_FAULT")); return ERR_CODE_COM_FAULT; } } else { LOG4C((LOG_NOTICE,"PDU::Send_ReadDeviceData=ERR_CODE_COM_FAULT")); return ERR_CODE_COM_BUSY; } #endif return 0; } int cpdu::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd) { #if DEBUG_CPDU BYTE *byAryRecv = new BYTE[16]; ZeroMemory(byAryRecv, 16); int iRecvLen = pComm->Read(byAryRecv, 15); if( iRecvLen != 15) { LOG4C((LOG_NOTICE,"CPDU丢包")); SetEvent( m_hSemComm ); if( iRecvLen != NULL) { delete[] byAryRecv; byAryRecv = NULL; } return ERR_CODE_COM_READ_NO_DATA; } SetCPDUVarMsg( nAddr,szCmd, byAryRecv); m_dwOnlineTick[nAddr -1] = GetTickCount(); SetEvent( m_hSemComm ); if( byAryRecv != NULL) { delete[] byAryRecv; byAryRecv = NULL; } #else SimulationCommData(); #endif return 0; } void cpdu::SetCPDUVarMsg( int nAddr,char *szCmd, BYTE *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 1 && iCmd <=17) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szMsg[nAddr-1], pBuffer, sizeof(m_szMsg[nAddr-1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int cpdu::GetCPDUVarMsg(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 <=17) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif switch( iCmd) { case 1: { int iCur = m_szMsg[nAddr -1][2]*16 + m_szMsg[nAddr -1][3]; itoa(iCur,szRecvMsg,10); } break; case 2: itoa(m_szMsg[nAddr -1][4] ,szRecvMsg,10); break; case 3: itoa(m_szMsg[nAddr -1][5] + 40,szRecvMsg,10); break; case 4: itoa(m_szMsg[nAddr -1][6],szRecvMsg,10); break; case 5: itoa(m_szMsg[nAddr -1][7] + 40,szRecvMsg,10); break; case 6: itoa(m_szMsg[nAddr -1][8],szRecvMsg,10); break; case 7: itoa(m_szMsg[nAddr -1][9] + 40,szRecvMsg,10); break; case 8: itoa(m_szMsg[nAddr -1][10],szRecvMsg,10); break; default: { int iSour = m_szMsg[nAddr - 1][nIndex]; //LOG4C((LOG_NOTICE,"iSour = %d,nIndex = %d,iSBit = %d,iEBit = %d",iSour,nIndex,iSBit,iEBit)); ONEBYTE_BIT2(szRecvMsg,iSour,iSBit,iEBit); } break; } #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); nRet = 0; #endif } return nRet; } void cpdu::SimulationCommData(void) { }