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

📄 103.cpp

📁 规约转换 103.104 规约转换 适用于电力系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 103 protocol DLL implementation module
// 103.cpp : Defines the initialization routines for the DLL.
//
// This is a part of the Nice Information's source code.
// Copyright (C) 1994 Nice Information Corporation
// All rights reserved.
//
// Complier: Microsoft Visual C++ 5.00, MFC 4.2
//
// create : Doo, 1999.1.6
// modify : Doo, 2000.4.20 for NZ-103
//

#include "stdafx.h"
#include "103.h"
#include "nsdialog.h"

#include "premapi.h"
#include "mandb.h"
#include "manpmes.h"
#include "nsnetapi.h"
#include "monitor.h"
#include "serial.h"
#include "IPremSvr.h"

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

DECLARE_PREMSERVER_INTERFACE

void FAR PASCAL EXPORT NSSockCallBack( UINT uMsg, const char FAR* addr, PORT port, SOCKET socket );
void FAR PASCAL EXPORT TimerProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

#define MAXWAIT_SYNCTIME		6

// enable log file
// #define _103_LOG					1

#ifdef _103_LOG
void Trace103Log( CString szInfo )
{
	CString szPath = ::GetDataFilePath( DFPATH_DATA );
	szPath += "103.log";

	CStdioFile LogFile;
	BOOL bRet = LogFile.Open( szPath, CFile::modeWrite | CFile::typeText );

	if ( !bRet )
		bRet = LogFile.Open( szPath, CFile::modeCreate | CFile::modeWrite | CFile::typeText );

	if ( bRet )
	{
		LogFile.SeekToEnd();
		LogFile.WriteString( szInfo );
		LogFile.Close();
	}
}
#endif

/////////////////////////////////////////////////////////////////////////////
// C103App

BEGIN_MESSAGE_MAP(C103App, CWinApp)
	//{{AFX_MSG_MAP(C103App)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// C103App construction

C103App::C103App()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	
	m_uBroadcastCounter = 0;
	m_uSyncCounter = 0;
	m_uGICounter = 0;

	m_BroadcastSocket = INVALID_SOCKET;
	
	m_bProtectValue = TRUE;
	
	// MAXWAIT_SYNCTIME times broadcast waiting
	m_btType = MAXWAIT_SYNCTIME;
}

C103App::~C103App()
{
	// don't delete here
	m_ListEquipment.RemoveAll();

	while( !m_ListChannel.IsEmpty() )
		delete m_ListChannel.RemoveHead();
}

CChannel* C103App::AddChannel( CString szIP )
{
	CChannel* pChannel = new CChannel();
	if ( pChannel )
	{
		pChannel->SetIP( szIP );

		// add to channel list
		m_ListChannel.AddTail( pChannel );
	}

	return pChannel;
}

void C103App::LoadData()
{
	// get all the equipments' settings
	DBHANDLE dbHandle = ::FindDbHandle( (WORD)-1, TYPE_NULL, 0 );
	for ( ; ::IsValidDbHandle( dbHandle ); dbHandle = ::GetNextDbHandle( dbHandle ) )
	{
		char szProtocol[PROTOCOLNAMELEN] = "";
		if ( !::GetAttr( dbHandle, ATTR_ST_PROTOCOLNAME, szProtocol ) )
			continue;

		if ( _stricmp( szProtocol, PROTOCOL103NAME ) )
			continue;

		// create a new device
		CEquipment* pEquipment = new CEquipment();
		if ( !pEquipment )
			continue;

		// add to device list
		m_ListEquipment.AddTail( pEquipment );

		// get device IP addr.
		char szIP[COMPORTLEN] = "";
		::GetAttr( dbHandle, ATTR_ST_COM, szIP );

		// get device logic addr.
		char* pLogicAddr = strchr( szIP, ':' );
		if ( pLogicAddr )
			*pLogicAddr = 0;

		// get channel
		CChannel* pChannel = GetChannel( szIP );
		if ( !pChannel )
			pChannel = AddChannel( szIP );

		if ( !pChannel )
			continue ;
		
		// link equipment with channel
		pChannel->AddEquipment( pEquipment );

		// get device order
		::GetAttr( dbHandle, ATTR_ST_RTUNO, &pEquipment->m_LogicAddr.m_wRtu );
		// get device name
		::GetAttr( dbHandle, ATTR_ST_NAME, pEquipment->m_LogicAddr.m_szName );
		// get device type
		::GetAttr( dbHandle, ATTR_ST_RTUTYPE, &pEquipment->m_LogicAddr.m_btType );
		// get device No.
		::GetAttr( dbHandle, ATTR_ST_NO, &pEquipment->m_LogicAddr.m_wNo );
		// get pulse timeout
		::GetAttr( dbHandle, ATTR_ST_PULTIME, &pEquipment->m_uPulseTimeout );
		
		// get device logic addr.
		BYTE btLogicAddr = 1;
		if ( pLogicAddr )
		{
			pLogicAddr++;
			btLogicAddr = atoi( pLogicAddr );
		}

		// logic addr. out of range
		pEquipment->m_LogicAddr.m_btAddrLgc = btLogicAddr;

// 2000.12.19 doo, for comm-state digital 
		sprintf( szProtocol, "%u", pEquipment->m_LogicAddr.m_wRtu );
		::GetPrivateProfileString( "CommState", szProtocol, "-1,-1", szIP, sizeof(szIP), PROTOCOL_INIFILE );
		int nST = -1, nPT = -1;
		sscanf( szIP, "%d,%d", &nST, &nPT );

		pEquipment->m_wCommRtu = (WORD)nST;
		pEquipment->m_wCommIndex = (WORD)nPT;
	}
}

void C103App::BroadcastName()
{
	if ( INVALID_SOCKET == m_BroadcastSocket )
		return ;

	BYTE szData[BROADCASTDATALEN];
	memset( szData, 0, BROADCASTDATALEN * sizeof(BYTE) );
	
	// server is running
	szData[0] = 0xFF;

	// frame type
	if ( (BYTE)-1 != m_btType )
	{
		m_btType--;
		if ( 0 == m_btType )
		{
			m_btType = MAXWAIT_SYNCTIME;
			szData[1] = 0x01;
		}
		else
			szData[1] = 0x00;
	}
	else
		szData[1] = 0x00;

	if ( szData[1] )
		CEquipment::GetSyncTime( szData + 2 );

	// server's name
	strncpy( (char*)szData + 9, ::GetLocalName(), BROADCASTDATALEN - 9 );

	::NSSock_BroadcastAll( m_BroadcastSocket, (char*)szData, BROADCASTDATALEN );
}

CChannel* C103App::GetChannel( CString szIP )
{
	// find equipment of the addr.
	POSITION pos = m_ListChannel.GetHeadPosition();
	while( pos )
	{
		CChannel* pChannel = m_ListChannel.GetNext( pos );

		if ( inet_addr( pChannel->GetIP() ) == inet_addr( szIP ) )
			return pChannel;
	}

	return NULL;
}

CChannel* C103App::GetChannel( SOCKET socket )
{
	// find equipment of the addr.
	POSITION pos = m_ListChannel.GetHeadPosition();
	while( pos )
	{
		CChannel* pChannel = m_ListChannel.GetNext( pos );

		if ( socket == pChannel->GetSocket() )
			return pChannel;
	}

	return NULL;
}

CEquipment* C103App::GetEquipmentByOrder( WORD wOrder )
{
	// find equipment of the ST_NO
	POSITION pos = m_ListEquipment.GetHeadPosition();
	while( pos )
	{
		CEquipment* pEquipment = m_ListEquipment.GetNext( pos );

		if ( pEquipment->IsLogicDevice( wOrder ) )
			return pEquipment;
	}

	return NULL;
}

int C103App::GetPointOrder( int nType, BYTE btType, int nGroup, int nCode )
{
	DEVTEM_POINT PointDefination;

	// get defination from template
	PointDefination.m_CommPoint.m_bPointType = btType;
	PointDefination.m_CommPoint.m_bGroup = (BYTE)nGroup;
	PointDefination.m_CommPoint.m_bCode = (BYTE)nCode;

	if ( !m_dbTemplate.GetPointOrder( nType, &PointDefination ) )
		return -1;

	return PointDefination.m_CommPoint.m_nIndex;
}

int C103App::GetPointCode( int nType, BYTE btType, int nOrder, BYTE& btGroup, BYTE& btCode )
{
	DEVTEM_POINT PointDefination;

	// get defination from template
	PointDefination.m_CommPoint.m_bPointType = btType;
	PointDefination.m_CommPoint.m_nIndex = nOrder;

	if ( !m_dbTemplate.GetPointCode( nType, &PointDefination ) )
		return -1;

	btGroup = PointDefination.m_CommPoint.m_bGroup;
	btCode = PointDefination.m_CommPoint.m_bCode;
	
	return 0;
}

int C103App::GetRelayCode( int nType, int nOrder, BYTE btRelayValue, BYTE& btValue )
{
	DEVTEM_POINT PointDefination;

	// get defination from template
	PointDefination.m_CommPoint.m_bPointType = TYPE_RELAY;
	PointDefination.m_CommPoint.m_nIndex = nOrder;

	if ( !m_dbTemplate.GetPointCode( nType, &PointDefination ) )
		return -1;

	if ( 0 == btRelayValue )
		btValue = PointDefination.m_RelayPoint.m_b0;

	if ( 1 == btRelayValue )
		btValue = PointDefination.m_RelayPoint.m_b1;
	
	return 0;
}

int C103App::InitPrem()
{
	// load template information
	CString szPath = ::GetDataFilePath( DFPATH_SYSTEM );
	szPath += "Template";
	szPath += "\\";

	WIN32_FIND_DATA FileData;
	HANDLE hSearch = ::FindFirstFile( szPath + "*.dll", &FileData );
	if ( INVALID_HANDLE_VALUE != hSearch )
	{
		m_dbTemplate.LoadDBTemplate( szPath + FileData.cFileName );
		::FindClose( hSearch );
	}

	// get initialize data
	m_uTimeBroadcast = ::GetPrivateProfileInt( "Settings", "TimeBroadcast", 15, PROTOCOL_INIFILE );
	m_uTimeBroadcast *= 1000 / TIMER_ELAPSE;
	m_uBroadcastCounter = m_uTimeBroadcast - 1000 / TIMER_ELAPSE;

	m_uTimeOut = ::GetPrivateProfileInt( "Settings", "TimeOut", 3, PROTOCOL_INIFILE );
	m_uTimeOut *= 1000 / TIMER_ELAPSE;

	m_uTimeSync = ::GetPrivateProfileInt( "Settings", "TimeSync", 0, PROTOCOL_INIFILE );
	m_uTimeSync *= 1000 / TIMER_ELAPSE;

	m_uTimeGI = ::GetPrivateProfileInt( "Settings", "TimeGI", 900, PROTOCOL_INIFILE );
	m_uTimeGI *= 1000 / TIMER_ELAPSE;

	if ( ::GetPrivateProfileInt( "Settings", "ProtectValue", 1, PROTOCOL_INIFILE ) )
		m_bProtectValue = TRUE;
	else
		m_bProtectValue = FALSE;

	// for fault signal
	char szValue[32];
	::GetPrivateProfileString( "Settings", "Signal", "-1,-1", szValue, sizeof(szValue), PROTOCOL_INIFILE );
	int nST = -1, nPT = -1;
	sscanf( szValue, "%d,%d", &nST, &nPT );

	m_wRtuSignal = (WORD)nST;
	m_wIndexSignal = (WORD)nPT;

	// load equipment defination
	//
	LoadData();

	// create broadcast link on the softbus
	m_BroadcastSocket = ::NSSock_CreateBroadcast( NS_BROADCASTING_PORT, ::NSSockCallBack );

	// listen on 
	::NSSock_WaitForConnect( NS_PROTOCOL_PORT, ::NSSockCallBack );

	// setup timer
	m_uTimer = ::SetTimer( NULL, 0x0001, TIMER_ELAPSE, (TIMERPROC)::TimerProc );

	return PREM_OK;
}

void C103App::ExitPrem()
{
	// don't delete here
	m_ListEquipment.RemoveAll();

	while( !m_ListChannel.IsEmpty() )
		delete m_ListChannel.RemoveHead();

	// close broadcast socket
	::NSSock_EndBroadcast( m_BroadcastSocket );

	// end wait
	::NSSock_CancelWait( NS_PROTOCOL_PORT );

	// stop the timer
	::KillTimer( NULL, m_uTimer );
}

int C103App::SendPremMessage( UINT msg, WPARAM wParam, LPARAM lParam )
{
	int nRet = PREM_OK;

	switch ( msg )
	{
		case PREM_INIT:
			nRet = InitPrem();
			break;

		case PREM_EXIT:
			ExitPrem();
			break;

		case PREM_GETPROTOCOLNAME:
			strcpy( (char*)lParam, PROTOCOL103NAME );
			break;

		case PREM_SETINTERFACE:
			if ( INTERFACE_PREMSERVER == wParam )
				SET_PREMSERVER_INTERFACE( lParam )
			break;

		case PREM_MENU:
			if ( 0 == ::GetPrivateProfileInt( "Setting", "DeviceList", 1, PROTOCOL_INIFILE ) )
				break;

			if ( !m_Menu.GetSafeHmenu() )
				m_Menu.LoadMenu( IDR_MENU );

			*(HMENU*)lParam = m_Menu.GetSafeHmenu();

			break;

		case PREM_COMMAND:
			switch( wParam )
			{
				case IDM_LIST:
				{
					CNeuronListDlg NeuronListDlg;
					NeuronListDlg.DoModal();

					break;
				}
			}

			break;

		case PREM_RELAYSELECT:
			{
				RELAYPARAM* pRelayParam = (RELAYPARAM*)wParam;
				if ( !pRelayParam )
				{
					nRet = PREM_ERR_PARAM;
					break;
				}

				CEquipment* pEquipment = GetEquipmentByOrder( pRelayParam->rtu );
				if ( !pEquipment )
				{
					nRet = PREM_ERR_RELAY;
					break;
				}

				nRet = pEquipment->RelaySelect( pRelayParam->rtu, pRelayParam->type, pRelayParam->word, pRelayParam->value );
			}
			break;

		case PREM_RELAYRUN:
			{
				RELAYPARAM* pRelayParam = (RELAYPARAM*)wParam;
				if ( !pRelayParam )
				{
					nRet = PREM_ERR_PARAM;
					break;
				}

				CEquipment* pEquipment = GetEquipmentByOrder( pRelayParam->rtu );
				if ( !pEquipment )
				{
					nRet = PREM_ERR_RELAY;
					break;
				}

				nRet = pEquipment->RelayRun( pRelayParam->rtu, pRelayParam->type, pRelayParam->word, pRelayParam->value );
			}
			break;

		case PREM_RELAYCANCEL:
			{
				RELAYPARAM* pRelayParam = (RELAYPARAM*)wParam;
				if ( !pRelayParam )
				{
					nRet = PREM_ERR_PARAM;
					break;
				}

				CEquipment* pEquipment = GetEquipmentByOrder( pRelayParam->rtu );
				if ( !pEquipment )
				{
					nRet = PREM_ERR_RELAY;
					break;
				}

				nRet = pEquipment->RelayCancel( pRelayParam->rtu, pRelayParam->type, pRelayParam->word, pRelayParam->value );
			}
			break;

		case PREM_SETPOINTPREACK:
			{
				SETPOINTPARAM* pSetpointParam = (SETPOINTPARAM*)wParam;
				if ( !pSetpointParam )
				{
					nRet = PREM_ERR_PARAM;
					break;
				}

				CEquipment* pEquipment = GetEquipmentByOrder( pSetpointParam->wRtu );
				if ( !pEquipment )
				{
					nRet = PREM_ERR_RELAY;
					break;
				}
			}
			break;

		case PREM_SETPOINTEXCUTE:
			{
				SETPOINTPARAM* pSetpointParam = (SETPOINTPARAM*)wParam;
				if ( !pSetpointParam )
				{
					nRet = PREM_ERR_PARAM;
					break;
				}

				CEquipment* pEquipment = GetEquipmentByOrder( pSetpointParam->wRtu );
				if ( !pEquipment )
				{
					nRet = PREM_ERR_RELAY;
					break;
				}

				nRet = pEquipment->WritePulse( pSetpointParam->wIndex, pSetpointParam->pValue + 1, *pSetpointParam->pValue );
			}
			break;

		case PREM_SETPOINTCANCEL:
			{
				SETPOINTPARAM* pSetpointParam = (SETPOINTPARAM*)wParam;
				if ( !pSetpointParam )
				{
					nRet = PREM_ERR_PARAM;
					break;
				}

				CEquipment* pEquipment = GetEquipmentByOrder( pSetpointParam->wRtu );
				if ( !pEquipment )
				{
					nRet = PREM_ERR_RELAY;
					break;
				}
			}
			break;

		//  online 为NARI保护界面
		case PREM_ASKPROTECTVALUE:
			{
				nRet = PREM_ERR_PARAM;
				
				PROTECTPARAM *pProtParam = (PROTECTPARAM*)wParam;
				if ( !pProtParam )
#ifndef _103_LOG
					break;
#else
				{
					CTime tmNow = CTime::GetCurrentTime();

					CString szInfo = tmNow.Format( "%Y-%m-%d %H:%M:%S" );
					szInfo += "召唤保护定值(压板),参数为NULL\n";

					::Trace103Log( szInfo );

					break;

⌨️ 快捷键说明

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