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

📄 fert2000.cpp

📁 这是一个变电站的监控程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Fert2000.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Fert2000.h"
#include "Fert2000Dlg.h"
#include "SysParaSet.h"

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

#pragma data_seg("Shared")

LONG thisUsageCount = -1;

#pragma data_seg()

bool bStopFlag=false;
CHANNEL Channels[MAX_CHANNEL_NUM];
RTU Rtus[MAX_RTU_NUM];
SYSTEMCOUNT SystemCount;
BYTE DebugCommand[0x25];//当前调试的命令数组
char DebugRtuNo;
EVENT Events;
RecSendThread RecSendThread1;
long ProgramStartTime;
NetProcess NetProcess1;
char YKEchoFlag[MAX_RTU_NUM];
BYTE YKEcho[MAX_RTU_NUM][6];//?
BYTE YKReserved[24];//?
BYTE GpsSendBuf[100];//第一字节表示是否可显示,第二字节为缓冲区长度,后面字节表示发送数据
BYTE GpsRecBuf[100];
BYTE SwitchSendBuf[100];//第一字节表示是否可显示,第二字节为缓冲区长度,后面字节表示发送数据
BYTE SwitchRecBuf[100];

unsigned char TBCH0[] =   
{
    0x00,0x07,0x0E,0x09,0x1C,0x1B,0x12,0x15,0x38,0x3F,0x36,0x31,0x24,0x23,0x2A,0x2D,
    0x70,0x77,0x7E,0x79,0x6C,0x6B,0x62,0x65,0x48,0x4F,0x46,0x41,0x54,0x53,0x5A,0x5D,
    0xE0,0xE7,0xEE,0xE9,0xFC,0xFB,0xF2,0xF5,0xD8,0xDF,0xD6,0xD1,0xC4,0xC3,0xCA,0xCD,
    0x90,0x97,0x9E,0x99,0x8C,0x8B,0x82,0x85,0xA8,0xAF,0xA6,0xA1,0xB4,0xB3,0xBA,0xBD,
    0xC7,0xC0,0xC9,0xCE,0xDB,0xDC,0xD5,0xD2,0xFF,0xF8,0xF1,0xF6,0xE3,0xE4,0xED,0xEA,
    0xB7,0xB0,0xB9,0xBE,0xAB,0xAC,0xA5,0xA2,0x8F,0x88,0x81,0x86,0x93,0x94,0x9D,0x9A,
    0x27,0x20,0x29,0x2E,0x3B,0x3C,0x35,0x32,0x1F,0x18,0x11,0x16,0x03,0x04,0x0D,0x0A,
    0x57,0x50,0x59,0x5E,0x4B,0x4C,0x45,0x42,0x6F,0x68,0x61,0x66,0x73,0x74,0x7D,0x7A,
    0x89,0x8E,0x87,0x80,0x95,0x92,0x9B,0x9C,0xB1,0xB6,0xBF,0xB8,0xAD,0xAA,0xA3,0xA4,
    0xF9,0xFE,0xF7,0xF0,0xE5,0xE2,0xEB,0xEC,0xC1,0xC6,0xCF,0xC8,0xDD,0xDA,0xD3,0xD4,
    0x69,0x6E,0x67,0x60,0x75,0x72,0x7B,0x7C,0x51,0x56,0x5F,0x58,0x4D,0x4A,0x43,0x44,
    0x19,0x1E,0x17,0x10,0x05,0x02,0x0B,0x0C,0x21,0x26,0x2F,0x28,0x3D,0x3A,0x33,0x34,
    0x4E,0x49,0x40,0x47,0x52,0x55,0x5C,0x5B,0x76,0x71,0x78,0x7F,0x6A,0x6D,0x64,0x63,
    0x3E,0x39,0x30,0x37,0x22,0x25,0x2C,0x2B,0x06,0x01,0x08,0x0F,0x1A,0x1D,0x14,0x13,
    0xAE,0xA9,0xA0,0xA7,0xB2,0xB5,0xBC,0xBB,0x96,0x91,0x98,0x9F,0x8A,0x8D,0x84,0x83,
    0xDE,0xD9,0xD0,0xD7,0xC2,0xC5,0xCC,0xCB,0xE6,0xE1,0xE8,0xEF,0xFA,0xFD,0xF4,0xF3
};//CRC 校验码表


extern int SetNtRegValue(LPCTSTR lpszSection,LPCTSTR lpszEntry,void *nValue,int WriteSize);

/////////////////////////////////////////////////////////////////////////////
// CFert2000App

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

/////////////////////////////////////////////////////////////////////////////
// CFert2000App construction

CFert2000App::CFert2000App()
{
	// TODO: add construction code here,
	InterlockedIncrement(&thisUsageCount);
	bStopFlag=false;
}

CFert2000App::~CFert2000App()
{
	// TODO: add construction code here,
	InterlockedDecrement(&thisUsageCount);
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CFert2000App object

CFert2000App theApp;

/////////////////////////////////////////////////////////////////////////////
// CFert2000App initialization

BOOL CFert2000App::InitInstance()
{
	if( thisUsageCount!=0 )
	{
		CString title;
		title = "KY2000 SCADA系统前置通讯软件";
		HWND hPre = ::FindWindow(NULL,title);//如果有前一实例,激活并退出
		if ( hPre ){
			CWnd	*wnd = CWnd::FromHandle(hPre);
			if ( wnd->IsIconic() )
				wnd->ShowWindow(SW_RESTORE);

			wnd->SetForegroundWindow();
			wnd->GetLastActivePopup()->SetForegroundWindow();
		}
		return FALSE;
	}

	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	if (MessageBox(0,"另一台前置机是否在运行?\n且参数正确!","",MB_YESNO|MB_ICONQUESTION) == IDYES)
	{
		TransferPara();
	}

	BOOL Error1;
	try{
		Error1 = Database.OpenEx("DSN=FERT",CDatabase::noOdbcDialog);
	}
	catch(CDBException * e)
	{
		MessageBox(0,"打开数据库出错!\n系统将退出!","错误",MB_OK|MB_ICONSTOP);
		e->Delete();
		return FALSE;
	}
	if (Error1 == 0 )
	{
		MessageBox(0,"打开数据库出错!\n系统将退出!","错误",MB_OK);
		return FALSE;
	}

	//调入系统参数
	LoadSysPara();
	Database.Close();

	NetProcess1.InitNet();
	char str[4];
	char buff[MAX_COMPUTERNAME_LENGTH + 1 + 1];
	DWORD length = MAX_COMPUTERNAME_LENGTH + 1 + 1;
	if (!GetComputerName(buff,&length))
		return FALSE;
	str[0] = *(_strupr(&buff[0]));
	str[1] = *(_strupr(&buff[4]));
	if (str[0] == 'F')
	{
		if (str[1] == 'A')
			NetProcess1.ThisNodeId = 'A';
		else
			NetProcess1.ThisNodeId = 'B';
	}
	else
		NetProcess1.ThisNodeId = 'B';
	InitPassword();
	CTime Time = CTime::GetCurrentTime();
	ProgramStartTime = Time.GetTime();
	//初始化MOXA通道
	for (int i = 0;i < SystemCount.ChNum ;i++)
	{
//		InitChannel(i,0);
		InitChannelData(i);
	}
	for (i = 0;i < SystemCount.RtuNum ;i++)
		InitRtuData(i);
	DebugRtuNo = -1;

	m_bPermit = false;
	CFert2000Dlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

void CFert2000App::LoadSysPara()
{
	LoadSystemCount();
	LoadChannelPara();
	LoadRtuPara();
}
void CFert2000App::LoadSystemCount()
{
	CSysParaSet SysParaSet(&Database);
	try
	{
		if (!SysParaSet.Open()) 
		{
			SystemCount.ChNum = 0;
			SystemCount.RtuNum = 0;
			return;
		}
	}
	catch(CDBException * e)
	{
		MessageBox(0,"系统参数表打开出错!","错误",MB_OK);
		e->Delete();
		SystemCount.ChNum = 0;
		SystemCount.RtuNum = 0;
		return;
	}
	if (!SysParaSet.IsEOF() && !SysParaSet.IsBOF()) 
	{
		SysParaSet.MoveFirst();
		SystemCount.ChNum = SysParaSet.m_CHANNUM;
		SystemCount.RtuNum = SysParaSet.m_RTUNUM;
	}else 
	{	
		SystemCount.ChNum = 0;
		SystemCount.RtuNum = 0;
	}
	SysParaSet.Close();
}

void CFert2000App::LoadChannelPara()
{
	CChanParaSet ChanParaSet(&Database);
	for (int i = 0;i < SystemCount.ChNum;i++)
	{
		try
		{
			CString str1;
			str1.Format ("channo = %d",i);
			ChanParaSet.m_strFilter = str1;
			if (!ChanParaSet.Open()) continue;
		}
		catch(CDBException *e)
		{
			e->Delete();
			Channels[i].Flag = 0;
			Channels[i].SorA = 1;
			Channels[i].Baud = 0;
			Channels[i].WorkType = 0;
			Channels[i].EndBit = 0;
			Channels[i].ParityBit = 0;
			Channels[i].CheckBit = 0;
			Channels[i].DataBit = 0;
			Channels[i].ChRtuNum = 0;
			Channels[i].Protocol=0;
			Channels[i].SynWord = 0;
			for (int j = 0;j < 8;j++)
				Channels[i].ChRtuNo [j] = 0;
			continue;
		}
		if (ChanParaSet.IsEOF() && ChanParaSet.IsBOF())
		{
			Channels[i].Flag = 0;
			Channels[i].SorA = 1;
			Channels[i].Baud = 0;
			Channels[i].WorkType = 0;
			Channels[i].EndBit = 0;
			Channels[i].ParityBit = 0;
			Channels[i].CheckBit = 0;
			Channels[i].DataBit = 0;
			Channels[i].ChRtuNum = 0;
			Channels[i].Protocol=0;
			Channels[i].SynWord = 0;
			for (int j = 0;j < 8;j++)
				Channels[i].ChRtuNo [j] = 0;
		}
		else
		{
			ChanParaSet.MoveFirst();
			Channels[i].Flag = ChanParaSet.m_useflag;
			Channels[i].SorA = ChanParaSet.m_syncflag;
			Channels[i].Baud = ChanParaSet.m_baudrate;
			Channels[i].WorkType = ChanParaSet.m_worktype;
			Channels[i].EndBit = ChanParaSet.m_stopbit;
			Channels[i].ParityBit = ChanParaSet.m_oddevent;//奇偶性
			Channels[i].CheckBit = ChanParaSet.m_checkbit ;
			Channels[i].DataBit = ChanParaSet.m_databit;
			Channels[i].Protocol = ChanParaSet.m_protocol;
			Channels[i].ChRtuNum = ChanParaSet.m_rtunum;
			Channels[i].ChRtuNo[0] = (ChanParaSet.m_rtuno0>=SystemCount.RtuNum)?0:ChanParaSet.m_rtuno0;
			Channels[i].ChRtuNo[1] = (ChanParaSet.m_rtuno1>=SystemCount.RtuNum)?0:ChanParaSet.m_rtuno1;
			Channels[i].ChRtuNo[2] = (ChanParaSet.m_rtuno2>=SystemCount.RtuNum)?0:ChanParaSet.m_rtuno2;
			Channels[i].ChRtuNo[3] = (ChanParaSet.m_rtuno3>=SystemCount.RtuNum)?0:ChanParaSet.m_rtuno3;
			Channels[i].ChRtuNo[4] = (ChanParaSet.m_rtuno4>=SystemCount.RtuNum)?0:ChanParaSet.m_rtuno4;
			Channels[i].ChRtuNo[5] = (ChanParaSet.m_rtuno5>=SystemCount.RtuNum)?0:ChanParaSet.m_rtuno5;
			Channels[i].ChRtuNo[6] = (ChanParaSet.m_rtuno6>=SystemCount.RtuNum)?0:ChanParaSet.m_rtuno6;
			Channels[i].ChRtuNo[7] = (ChanParaSet.m_rtuno7>=SystemCount.RtuNum)?0:ChanParaSet.m_rtuno7;
		}
		ChanParaSet.Close();
	}
}

void CFert2000App::LoadRtuPara()
{
	CRtuParaSet RtuParaSet(&Database);
	for (int i = 0;i < SystemCount.RtuNum ;i++)
	{
		try
		{
			CString str1;
			str1.Format ("rtuno = %d",i);
			RtuParaSet.m_strFilter = str1;
			if (!RtuParaSet.Open()) continue;
		}
		catch(CDBException * e)
		{
			e->Delete();
			sprintf(Rtus[i].Name,"%s"," ");
			Rtus[i].Flag = 0;
			Rtus[i].TimeForbit = 0 ;
			Rtus[i].RtuChNo = 0;
			Rtus[i].YcNum = 60;
			Rtus[i].YxNum = 48;
			Rtus[i].KwhNum = 0;
			Rtus[i].Addr = 1;
			Rtus[i].WaitTime=5;
			Rtus[i].AnswerWaitTime=5;
			Rtus[i].DeadArea=3;
			for (int j = 0;j < 16;j++)
				Rtus[i].RRC [j] = 0;
			Rtus[i].ProtocolStamp[0] = 0;
			Rtus[i].ProtocolStamp[1] = 0xf0;
			Rtus[i].ProtocolStamp[2] = 0xa0;
			for (int k=3;k<10;k++) Rtus[i].ProtocolStamp[k] = 0;
			Rtus[i].SynWord[0] = 0;
			for (k=0;k<3;k++) *(WORD*)&Rtus[i].SynWord[1+k*2] = 0X09D7;
			Rtus[i].Protocol = 0;
			continue;
		}
		if (RtuParaSet.IsBOF() && RtuParaSet.IsEOF())
		{
			sprintf(Rtus[i].Name,"%s"," ");
			Rtus[i].Flag = 0;
			Rtus[i].TimeForbit = 0 ;
			Rtus[i].RtuChNo = 0;
			Rtus[i].YcNum = 60;
			Rtus[i].YxNum = 48;
			Rtus[i].KwhNum = 0;
			Rtus[i].Addr = 1;
			Rtus[i].WaitTime=5;
			Rtus[i].AnswerWaitTime=5;
			Rtus[i].DeadArea=3;
			for (int j = 0;j < 16;j++)
				Rtus[i].RRC [j] = 0;
			Rtus[i].ProtocolStamp[0] = 0;
			Rtus[i].ProtocolStamp[1] = 0xf0;
			Rtus[i].ProtocolStamp[2] = 0xa0;
			for (int k=3;k<10;k++) Rtus[i].ProtocolStamp[k] = 0;
			Rtus[i].SynWord[0] = 0;
			for (k=0;k<3;k++) *(WORD*)&Rtus[i].SynWord[1+k*2] = 0X09D7;
			Rtus[i].Protocol = 0;
		}
		else
		{
			RtuParaSet.MoveFirst();
			Rtus[i].Addr = RtuParaSet.m_rtuaddr;
			Rtus[i].Flag = RtuParaSet.m_useflag;//使用标志
			Rtus[i].RtuChNo = (RtuParaSet.m_channo>=SystemCount.ChNum)?0:RtuParaSet.m_channo;//RTU对应的通道
			Rtus[i].WaitTime = 5;
			Rtus[i].AnswerWaitTime = RtuParaSet.m_cycletime;
			Rtus[i].DeadArea = 5;
			//Rtus[i].FreqNum = RtuData.FreqNum;
			//Rtus[i].WaterNum = RtuData.WaterNum;
			Rtus[i].YcNum = (RtuParaSet.m_ycnum>RTU_MAX_YC_NUM)?0:RtuParaSet.m_ycnum; 		   //YC
			Rtus[i].YxNum = (RtuParaSet.m_yxnum>RTU_MAX_YX_NUM)?0:RtuParaSet.m_yxnum;        //YX
			Rtus[i].KwhNum = (RtuParaSet.m_kwhnum>RTU_MAX_KWH_NUM)?0:RtuParaSet.m_kwhnum;        //KWH
			Rtus[i].SoeNum = (RtuParaSet.m_soenum>RTU_MAX_SOE_NUM)?0:RtuParaSet.m_soenum;
			Rtus[i].SoeOnYxFlag = RtuParaSet.m_soeonyx;
			const char *Name = (LPCTSTR)RtuParaSet.m_rtuname;
			for (int j=0;j<20;j++) Rtus[i].Name[j] = Name[j];//RTU名字

			const char *Temp = (LPCTSTR)RtuParaSet.m_RRCSynWordProtocol;
			for( j=0;j<16;j++) sscanf(Temp+j*2,"%02x",&Rtus[i].RRC[j]);
			Temp += 32;
			for (j=0;j<7;j++) sscanf(Temp+j*2,"%02x",&Rtus[i].SynWord[j]);
			Temp += 14;
			for (j=0;j<10;j++) sscanf(Temp+j*2,"%02x",&Rtus[i].ProtocolStamp[j]);
			Rtus[i].Protocol = Rtus[i].ProtocolStamp[9];
		}
		RtuParaSet.Close();
		if (Rtus[i].Flag && ((Rtus[i].Protocol == 4) || (Rtus[i].Protocol == 5))) 
		{
			if (NetProcess1.ZFRtuNo == -1) NetProcess1.ZFRtuNo = i;
			else 
			{
				MessageBox(0,"不能有两个转发RTU!请启动后更改!","错误",MB_ICONSTOP|MB_OK);
			}
		}
	}
}


void CFert2000App::WriteSysPara() 
{
	WriteSystemCount();
	for (int i = 0;i < SystemCount.ChNum ;i++)
		WriteAChannelPara(i);
	for (i = 0;i < SystemCount.RtuNum ;i++)
		WriteARtuPara(i);
}

int CFert2000App::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
	for (int i = 0;i < SystemCount.ChNum ;i++)
		CloseChannel(i);
//	WriteSysPara();
	if (Database.IsOpen ()) 
		Database.Close();
	return CWinApp::ExitInstance();
}

void CFert2000App::CloseChannel(int iChNo)
{
	if (ComFileFlag[iChNo] == false) return;
	ComFileFlag[iChNo] = false;
	sio_close(iChNo + 1);//异步
}

//成功返回1 不成功返回0 函数原为void类型,Modify by lqs on 2000.12.21 20:00
int CFert2000App::InitChannel(int i,char flag)
{                                            
	char buf[50];
	int ret ,ParityBit,DataBit,EndBit;
	int bps[16]={B75,B150,B300,B600,B1200,B2400,B4800,B9600,B19200,B38400,B57600,
				B115200,B230400,B460800,B921600};
	int datas[4]={BIT_5,BIT_6,BIT_7,BIT_8};

	Channels[i].MoxaStart = 0;
	Channels[i].MoxaEnd = 0;
	Channels[i].MoxaShowStart = 0;

	if (!Channels[i].Flag) 
	{
		ComFileFlag[i] = false;
		return 0;
	}
	if (!ComFileFlag[i])//Add by yzw 2000.6.2
	{
		if (Channels[i].CheckBit == 2)//Terminal server
			ret = sio_resetport(i+1,"",0);

⌨️ 快捷键说明

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