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

📄 iec103dlg.cpp

📁 IEC60870-5-103规约测试工具。 目前只能打开COM1口
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// IEC103Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "IEC103.h"
#include "IEC103Dlg.h"
#include "IEC103ProtectEx.h"

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

/////////////////////////////////////////////////////////////////////////////
// CIEC103Dlg dialog
CIEC103ProtectEx* Protectdlg;
CIEC103Dlg* dlg;
CIEC103Dlg::CIEC103Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIEC103Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIEC103Dlg)
	m_bShowMSG = TRUE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CIEC103Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIEC103Dlg)
	DDX_Control(pDX, IDC_RICHEDIT, m_msg);
	DDX_Check(pDX, IDC_CHECK_SHOW, m_bShowMSG);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIEC103Dlg, CDialog)
	//{{AFX_MSG_MAP(CIEC103Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
	ON_BN_CLICKED(IDC_BTN_OPEN, OnBtnOpen)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BTN_PROTECT, OnBtnProtect)
	ON_BN_CLICKED(IDC_CHECK_SHOW, OnCheckShow)
	ON_BN_CLICKED(IDC_BTN_SET, OnBtnSet)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIEC103Dlg message handlers

BOOL CIEC103Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_nPollFrame = 0;
	CRect rect;
	GetClientRect( rect );
	rect.top = 35; rect.bottom -= 10;
	rect.left = 10; rect.right -= 10;
	m_msg.MoveWindow( rect );
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CIEC103Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CIEC103Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CIEC103Dlg::Init()
{
	memset( &RxBuf, 0, sizeof( strucomData ) );
	memset( &system, FALSE, sizeof( struSYSTEM ) );
	IEC103data.fcv = LPCI_FCV;
	IEC103data.fcb = LPCI_FCB;
	IEC103data.acd = 0;
	IEC103data.pl1 = FALSE;

	m_bSYNC = FALSE;

	system.opened = TRUE;
	m_GEN_NA = FALSE;
	IgiCount = 0;
	SynCount = 0;
	FrameCount = 0;
}

void CIEC103Dlg::StartIEC103Thread()
{
	HANDLE hThread = CreateThread( NULL, 0, IEC103Thread, this, 0, NULL );
	if( hThread ) CloseHandle( hThread );
}

DWORD WINAPI CIEC103Dlg::IEC103Thread( PVOID lpParam )
{
	((CIEC103Dlg*)lpParam)->IEC103_Main();
	return 0;
}

void CIEC103Dlg::IEC103_Main()
{
	IEC103Tx();
	while ( system.opened )
	{
		IEC103Tx();
		Sleep( 500 );
		IEC103Rx();
	}
}

void CIEC103Dlg::IEC103Tx()
{
	BYTE buffer[512];
	int count = 0;
	if ( !system.inited )
	{
		system.inited = TRUE;
		RCU_NA( buffer, count );
		if ( count >= 5 )
		{
			SerialCom.WriteSerialData( m_hCom, buffer, count );
			DisplayMessage( buffer, count, FALSE );
		}
	}
	if ( system.cantx )
	{
		FrameCount = 0;
		system.cantx = FALSE;
		IgiCount++;
		SynCount++;

		if ( m_nPollFrame&PL1_FRAME )
		{
			m_nPollFrame&=~PL1_FRAME;
			PL1_NA( buffer, count );
		}
		else if ( m_nPollFrame&GEN1_FRAME )
		{
			m_nPollFrame&=~GEN1_FRAME;
			GEN1_NA( buffer, count );
		}
		else if ( m_nPollFrame&GEN2_FRAME )
		{
			m_nPollFrame&=~GEN2_FRAME;
			GEN2_NA( buffer, count );
		}
		else if ( m_nPollFrame&GEN3_FRAME )
		{
			m_nPollFrame&=~GEN3_FRAME;
			GEN3_NA( buffer, count );
		}

		else if ( m_nPollFrame&WGC0_FRAME )
		{
			m_nPollFrame&=~WGC0_FRAME;
			WGC0_NA( buffer, count );
		}
		else if ( m_nPollFrame&WGC1_FRAME )
		{
			m_nPollFrame&=~WGC1_FRAME;
			WGC1_NA( buffer, count );
		}
		else if ( m_nPollFrame&PL2_FRAME )
		{
			m_nPollFrame&=~PL2_FRAME;
			PL2_NA( buffer, count );
		}
		if ( count >= 5 )
		{
			DisplayMessage( buffer, count, FALSE );
			SerialCom.WriteSerialData( m_hCom, buffer, count );
		}
	}
	if ( !system.cantx )
	{
		FrameCount++;
		if ( FrameCount == 400 )
		{
			FrameCount = 0;
			system.inited = FALSE;
		}
	}
}

void CIEC103Dlg::IEC103Rx()
{
	int m_RxLength = SerialCom.ReadSerialData( m_hCom, &RxBuf.buf[RxBuf.size], MAX_SIZE-RxBuf.size );
	if( m_RxLength <= 0 ) return;
	RxBuf.size += m_RxLength;
	int m_old_rbuf_len;
	m_old_rbuf_len = RxBuf.size;
	BYTE* pBuf;
	BYTE sumcheck, type;
	while ( RxBuf.size >= 5 )
	{
		pBuf = &RxBuf.buf[m_old_rbuf_len-RxBuf.size];
		if ( !m_bSYNC )
		{
			m_bCF = FALSE;
			m_bNF = FALSE;
			type = GetSYNC( pBuf );
			if ( type == LPCI_SYN )
			{
				m_bNF = TRUE;
				m_bSYNC = TRUE;
				continue;
			}
			if ( type == LPCI_STX )
			{
				m_bCF = TRUE;
				m_bSYNC = TRUE;
				continue;
			}
			else
			{
				RxBuf.size--;
				continue;
			}
		}
		if ( !m_bNF && !m_bCF )
		{
			RxBuf.size--;
			continue;
		}
		if ( m_bNF )
		{
			sumcheck = SumCheck( pBuf+1, 2 );
			if ( sumcheck == pBuf[3] )
			{
				DisplayMessage( pBuf, 5 );
				ProcNF( pBuf, 5 );
				system.cantx = TRUE;
				RxBuf.size -= 5;
				m_bSYNC = FALSE;
			}
			else
			{
				RxBuf.size--;
				continue;
			}
		}
		if ( m_bCF )
		{
			if ( RxBuf.size < (DWORD)(pBuf[1]+6) )
			{
				return;
			}
			sumcheck = SumCheck( pBuf+4, pBuf[1] );
			if ( sumcheck == pBuf[pBuf[1]+4] )
			{
				DisplayMessage( pBuf, pBuf[1]+6 );
				ProcCF( pBuf, pBuf[1]+6 );
				system.cantx = TRUE;
				RxBuf.size -= pBuf[1]+6;
				m_bSYNC = FALSE;
			}
			else
			{
				RxBuf.size--;
				continue;
			}
		}
	}
	if( RxBuf.size <= 0 || RxBuf.size > MAX_SIZE ) {
		RxBuf.size = 0;
		return;
	}
	if( ( m_old_rbuf_len-RxBuf.size ) > 0 ) {
		memcpy( RxBuf.buf, &RxBuf.buf[m_old_rbuf_len-RxBuf.size], RxBuf.size );
	}
	return;
}

void CIEC103Dlg::ProcNF( BYTE* buffer, int count /* = 0  */)
{
	IEC103data.fcb ^= LPCI_FCB;
	if ( (buffer[1]&LPCI_FCB) == LPCI_FCB )
	{
		m_nPollFrame |= PL1_FRAME;
	}
	else
	{
		m_nPollFrame |= PL2_FRAME;
	}
}

void CIEC103Dlg::ProcCF( BYTE* buffer, int count /* = 0  */)
{
	IEC103data.fcb ^= LPCI_FCB;
	if ( (buffer[4]&LPCI_FCB) == LPCI_FCB )
	{
		m_nPollFrame |= PL1_FRAME;
	}
	else
	{
		m_nPollFrame |= PL2_FRAME;
	}
	BYTE type;
	int len;
	type = buffer[6];
	len = buffer[7]&0x7f;
	char temp[128];
	switch( type )
	{
	case 0x02:
		Beep( 1000, 100 );
		break;
	case 0x05:
		memset( &temp, '\0', sizeof( temp ) );
		memcpy( temp, buffer+13, 8 );
		DoSuccess( temp );
		break;
	case 0x24:
		ProcDD( buffer, len );
		break;
	case 0x2a:
		ProcYX( buffer, len );
		break;
	case 0x2b:
		Beep( 500, 100 );
		break;
	case 0x33:
		ProcYC( buffer, len );
		break;
	case 0x0a:
		ProcGEN( buffer, len );
		break;
	default:
		break;
	}
}

void CIEC103Dlg::ProcGEN( BYTE* buffer, int count /* = 0  */)
{
	CString str;
	switch( buffer[11] )
	{
	case 240:
		{
			str.Format( "返回信息标识RII= %d ", buffer[12] );
			AddColorString( str );
			m_Ngd = buffer[13]&0x3f;
			str.Format( "通用分类数据集数目NGD= %d ",  m_Ngd );
			AddColorString( str );
			str.Format( "计数位COUNT= %d ", (buffer[13]&0x40)>>6 );
			AddColorString( str );
			str.Format( "后续状态位CONT= %d ", (buffer[13]&0x80)>>7 );
			AddColorLine( str );
			BYTE* pData = &buffer[14];
			char temp[128];
			int j = 0;
			for ( int i = 0; i < (buffer[13]&0x3f); i++ )
			{
				memset( &temp, '\0', sizeof( temp ) );
				memcpy( temp, (pData+6+j), pData[j+4] );
				AddColorLine( temp );
				str.Format( "通用标识序号GIN条目= %d ", pData[j] );
				AddColorString( str );
				str.Format( "通用标识序号GIN组 = %d ", pData[1+j] );
				AddColorString( str );
				str.Format( "描述类别KOD= %d ", pData[2+j] );
				AddColorString( str );
				str.Format( "数据类型DATATYPE= %d ", pData[3+j] );
				AddColorString( str );
				str.Format( "数据宽度DATASIZE= %d ", pData[4+j] );
				AddColorString( str );
				str.Format( "数目NUMBER= %d", pData[5+j]&0x7f );
				AddColorString( str );
				str.Format( "后续状态位CONT= %d", (pData[5+j]>>7) );
				AddColorLine( str );
				j += 6+pData[j+4];
			}
			m_nPollFrame |= GEN2_FRAME;
		}
		break;
	case 241:
		{
			BYTE m_Rii;
			BOOL m_Cont;
			m_Rii = buffer[12];
			str.Format( "返回信息标识RII= %d ", m_Rii );
			AddColorString( str );
			str.Format( "通用分类数据集数目NGD= %d ", buffer[13]&0x3f );
			AddColorString( str );
			str.Format( "计数位COUNT= %d ", (buffer[13]&0x40)>>6 );
			AddColorString( str );
			m_Cont = (buffer[13]&0x80)>>7;
			str.Format( "后续状态位CONT= %d ", m_Cont );
			AddColorLine( str );
			BYTE* pData = &buffer[14];
			char temp[128];
			float m_floatvalue;
			short m_shortvalue;
			int j = 0;
			BYTE DataType;
			for ( int i = 0; i < (buffer[13]&0x3f); i++ )
			{
				DataType = pData[3+j];

⌨️ 快捷键说明

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