⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comport.cpp

📁 一个电力系统方面的ISA通讯规约
💻 CPP
字号:
// ComPort.cpp: implementation of the CComPort class.
//
//////////////////////////////////////////////////////////////////////
// create : Benny 2001.7.25

#include "stdafx.h"
#include "ISA.h"
#include "Serial.h"
#include <math.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern void FAR PASCAL EXPORT CommCallBack( UINT uMsg, HANDLE handle, WPARAM wParam, LPARAM lParam );
extern CISAApp theApp;

CComPort::CComPort()
{
	m_uTimeCount = 0;
	memset( m_btBuffer, 0, sizeof(m_btBuffer) );
	m_szSetting = "";
	m_pCurDevice = NULL;
	m_hComPort = NULL;

}

CComPort::~CComPort()
{
	::CloseComm( m_hComPort );

	m_ListDevice.RemoveAll();
}

BOOL CComPort::Open(CString szSetting)
{
	m_szSetting = szSetting;
	m_hComPort = ::OpenComm( szSetting, (COMMCALLBACK)CommCallBack, CHANNEL_IDLE_TIME );

	return m_hComPort != NULL;
}

UINT CComPort::GetPortNO()
{
	return ::StringToPort( m_szSetting );
}

void CComPort::AddDevice(CDevice *pDevice)
{
	if ( pDevice )
		m_ListDevice.AddTail( pDevice );
}

CDevice* CComPort::GetNextDevice(CDevice* pCurDevice)
{
	BOOL flag = true;
	if ( m_ListDevice.IsEmpty() )
		return NULL;
	POSITION pos;
   if(pCurDevice == NULL)
   {
	 pos = m_ListDevice.GetHeadPosition();
	while( pos )
	{
		CDevice* pDevice = m_ListDevice.GetNext( pos );
		if ( STATE_FREE == pDevice->m_nState  )
			return pDevice;
	}
   }
   /*
   STATE_ASKVALUE,
	STATE_SIGNALRESET,
	STATE_ASKSIMULATION,
	STATE_CHECKTIME,
	STATE_CHANGEVALUE,
	STATE_ACKCHANGEVALUE,
	STATE_NAKCHANGEVALUE,
	STATE_ASKTYPE,
	STATE_ACK_R,
	*/
	pos = m_ListDevice.Find( pCurDevice );
	if ( pos  )
		m_ListDevice.GetNext( pos );
	while(pos )
	{
	 //if ( pos )
		 CDevice* pDevice = m_ListDevice.GetNext( pos );
		 if(pDevice->m_nCommand  == STATE_ASKVALUE || pDevice->m_nCommand  == STATE_ACKCHANGEVALUE
			  ||pDevice->m_nCommand  == STATE_ASKSIMULATION ||pDevice->m_nCommand  == STATE_CHECKTIME 
			  || pDevice->m_nCommand == STATE_SIGNALRESET || pDevice->m_nCommand == STATE_NAKCHANGEVALUE || pDevice->m_nCommand == STATE_ACK_R
			  || pDevice->m_nCommand ==STATE_CHANGEVALUE)
		 {
             return pDevice;
		 }
		 
	}
   pos = m_ListDevice.Find( pCurDevice );
    if ( pos )
		  m_ListDevice.GetNext( pos );
	
    while(pos )
	{
	 //if ( pos )
		 CDevice* pDevice = m_ListDevice.GetNext( pos );
		 if(pDevice->m_wParent == pCurDevice->m_wParent )
			 continue;
		 return pDevice;
	}
	
	return m_ListDevice.GetHead();
}

void CComPort::OnTimer()
{
	if ( !m_hComPort )
		return;
    
	if ( m_pCurDevice && STATE_FREE != m_pCurDevice->m_nState )
	{
		m_uTimeCount++;

		if ( m_uTimeCount >= theApp.m_uTimeOut )
		{
			::ResetCommReceive( m_hComPort, NULL );

			m_uTimeCount = 0;
			
			m_pCurDevice->OnTimeOut();

			if ( m_pCurDevice->m_uTimeOutCount < theApp.m_uTimes )
			{
				m_pCurDevice->m_uTimeOutCount++;
//				if ( m_pCurDevice->m_uTimeOutCount == theApp.m_uTimes )
//					m_pCurDevice->OnCommError();
			}
		}
		else
			return;
	}
/*	STATE_ASKVALUE,
	STATE_SIGNALRESET,
	STATE_ASKSIMULATION,
	STATE_CHECKTIME,
	STATE_CHANGEVALUE,
	STATE_ACKCHANGEVALUE,
	STATE_NAKCHANGEVALUE,
	STATE_ASKTYPE,
	STATE_ACK_R,
	*/
    if(m_pCurDevice && (m_pCurDevice->m_nState == STATE_ASKVALUE 
		|| m_pCurDevice->m_nState ==STATE_ASKSIMULATION
		|| m_pCurDevice->m_nState == STATE_CHANGEVALUE  ) )
		return;
	m_pCurDevice = GetNextDevice( m_pCurDevice );
	if ( !m_pCurDevice )
		return;

	if (SENDIDLE == m_pCurDevice->m_btSendState)
		m_pCurDevice->ProcessSend();

	m_uTimeCount = 0;
}

void CComPort::Recv()
{
	int nReceive = ::GetCommReceiveDataLength( m_hComPort );
	if ( nReceive < MINFRAMELEN )
		return;
    BOOL flag = FALSE;
	BYTE btBufferRow[512] = "";
	BYTE* pbtBuffer = btBufferRow;
	::CommPseudoRead( m_hComPort, (char*)btBufferRow, MINFRAMELEN );
	if (ISA_ACK == pbtBuffer[ISA_CODE] || ISA_NAK == pbtBuffer[ISA_CODE])
	{
		::CommRead( m_hComPort, (char*)btBufferRow, nReceive );
		m_pCurDevice->ProcessReceive( pbtBuffer,  MINFRAMELEN);
		memcpy( m_btBuffer, btBufferRow , MINFRAMELEN );
	}
	else
	{
		if ( nReceive < ISA_LENGTH+1 )
			return;
		::CommPseudoRead( m_hComPort, (char*)btBufferRow, ISA_LENGTH +1);
		BYTE btLen = pbtBuffer[ISA_LENGTH] + 9;

		if ( nReceive < pbtBuffer[ISA_LENGTH] + 9 )
			return;

		::CommRead( m_hComPort, (char*)btBufferRow, nReceive );

		memcpy( m_btBuffer, btBufferRow , btLen );
		BYTE btArea = 0;
		BYTE btFatherArea = 10;
		switch (pbtBuffer[ISA_CODE])
		{
			case ISA_R1:
			case ISA_R4:
				btArea = pbtBuffer[ISA_DATA+9];
				btFatherArea = pbtBuffer[ISA_ADDR];
				flag = true;
				break;
			case ISA_R2:
			case ISA_R3:
			case ISA_R5:
				btArea = pbtBuffer[ISA_DATA];
				btFatherArea = pbtBuffer[ISA_ADDR];
				break;

		}
				
		CDevice* pDevice = GetDeviceByArea( btArea, btFatherArea);
		if ( !pDevice )
			return;
		else
		{
			if (pDevice!=m_pCurDevice)
			
				m_pCurDevice = pDevice;
			
			if(flag)
			{
				m_pCurDevice->m_nCommand = STATE_ACK_R;
			}
			    m_pCurDevice->m_btSendState = SENDIDLE;
		}

		m_pCurDevice->ProcessReceive( pbtBuffer, pbtBuffer[ISA_LENGTH] + 9 );
	}
}

CDevice* CComPort::GetDevice(BYTE btAddr)
{
	POSITION pos = m_ListDevice.GetHeadPosition();
	while ( pos )
	{
		CDevice* pDevice = m_ListDevice.GetNext( pos );
		if ( pDevice->m_btAddr == btAddr )
			return pDevice;
	}

	return NULL;
}

CDevice* CComPort::GetDeviceByArea(BYTE btArea,BYTE btFatherArea)
{
	POSITION pos = m_ListDevice.GetHeadPosition();
	while ( pos )
	{
		CDevice* pDevice = m_ListDevice.GetNext( pos );
		if ( pDevice->m_btArea == btArea && pDevice->m_wParent == btFatherArea )
			return pDevice;
	}

	return NULL;
}


BOOL CComPort::ResetComm()
{
	::CloseComm( m_hComPort );
	m_hComPort = ::OpenComm( m_szSetting, (COMMCALLBACK)CommCallBack, CHANNEL_IDLE_TIME );

	return m_hComPort != NULL;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -