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

📄 fert2000.cpp

📁 这是一个变电站的监控程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		ret = sio_open(i+1);
		if (ret != 0 )
		{
			ComFileFlag[i]=false;
			// flag=4 表示在线程中调用本函数(下同) 
			if (flag !=4)//Modify by lqs on 2000.12.21 10:20
			{
				wsprintf(buf,"打开COM%d通道出错!",i + 1);
				MessageBox(0,buf,"notice",MB_OK);
			}
			return 0;
		}
	}
	if ( Channels[i].ParityBit == 1 )
		ParityBit = P_ODD;
	else if ( Channels[i].ParityBit == 2 )
		ParityBit = P_EVEN;
	else
		ParityBit = P_NONE;
	if ( Channels[i].EndBit == 2 )
		EndBit = STOP_2;
	else
		EndBit = STOP_1;
	DataBit = datas[Channels[i].DataBit];
	ret = sio_ioctl(i+1,bps[Channels[i].Baud],ParityBit|DataBit|EndBit);
	if (ret != 0 ) 
	{
		sio_close(i+1);
		ComFileFlag[i]=false;
		if (flag !=4) //Modify by lqs on 2000.12.21 10:20
		{
			wsprintf(buf,"COM%d设置错误!",i - 1);
			MessageBox(0,buf,"notice",MB_OK);
		}
		return 0;
	}
	Beep(1000,50);
	ComFileFlag[i]=true;
	return 1;
}

//成功返回1 不成功返回0 函数原为void类型,Modify by lqs on 2000.12.21 20:00
int CFert2000App::InitCom(int iChNo, char flag)
{
	int bps[6]={CBR_300,CBR_600,CBR_1200,CBR_2400,CBR_4800,CBR_9600};
	int datas[4]={5,6,7,8};
	int ParityBit[3] = {NOPARITY,ODDPARITY,EVENPARITY};
	int EndBit[3] = {ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS};

	DCB dcb;
	COMMTIMEOUTS MyTimeouts;
	int iComNo = iChNo + 1 ;
	char sFile[10];
	CString str1;
	str1.Format ("COM%d",iComNo);
	lstrcpy(sFile,str1);
	if (!ComFileFlag[iChNo])
	{
		Channels[iChNo].hFile = CreateFile(sFile,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_WRITE_THROUGH,NULL);
		if ((Channels[iChNo].hFile==INVALID_HANDLE_VALUE )) 
		{
			ComFileFlag[iChNo]=FALSE;
			if (flag!=4) //Modify by lqs on 2000.12.21 10:20
				MessageBox(0,str1 + " 打开错误!","FIND",MB_OK);
			return 0;
		}
	}
	GetCommState(Channels[iChNo].hFile,&dcb);
	dcb.BaudRate = bps[Channels[iChNo].Baud] ;
	dcb.Parity = ParityBit[Channels[iChNo].ParityBit];
	dcb.ByteSize = datas[Channels[iChNo].DataBit];
	dcb.StopBits = EndBit[Channels[iChNo].EndBit];
	SetCommState(Channels[iChNo].hFile,&dcb);
	if(!GetCommTimeouts(Channels[iChNo].hFile,&MyTimeouts))
	{
		CloseHandle(Channels[iChNo].hFile);
		ComFileFlag[iChNo]=FALSE;
	}
	MyTimeouts.ReadIntervalTimeout=MAXDWORD;
	MyTimeouts.ReadTotalTimeoutConstant=0;
	MyTimeouts.ReadTotalTimeoutMultiplier = 0;
	if(!SetCommTimeouts(Channels[iChNo].hFile,&MyTimeouts))
	{
		CloseHandle(Channels[iChNo].hFile);
		ComFileFlag[iChNo]=FALSE;
		return 0;
	}
	Beep(1000,50);
	ComFileFlag[iChNo]=TRUE;
	return 1;
}

void CFert2000App::InitChannelData(int i)
{
	Channels[i].ChStatus = 0;
	Channels[i].bWorking = false;
	Channels[i].SynstrCount = 0;
	Channels[i].CurSendRtuNo =-1;
	Channels[i].MoxaEnd = 0;
	Channels[i].MoxaStart = 0;
	Channels[i].MoxaShowStart = 0;
	Channels[i].OnTest = 0;
	Channels[i].bReInit = false;
}

void CFert2000App::InitRtuData(int i)
{
	Rtus[i].FreqNum = 2;
	Rtus[i].NoDataTime =0;
	Rtus[i].FaultTime = 0;
	Rtus[i].ErrFrameSum = 0;
	Rtus[i].RecFrameSum = 0;
	Rtus[i].ErrFrameSumOld = 0;
	Rtus[i].RecFrameSumOld = 0;
	Rtus[i].bFault =false;
	Rtus[i].bRet = true;
	Rtus[i].bWorking = true;
	for (int k = 0;k < 25;k++)
		Rtus[i].ErrRate [k] = 0;
	Rtus[i].RecFullYx = 0;
	if (Rtus[i].Protocol == 4 || Rtus[i].Protocol == 5)
	{
		for (int j = 0;j < RTU_MAX_KWH_NUM;j++)
		{
			Rtus[i].KwhValue [j] = 0;
		}
		for (j = 0;j < RTU_MAX_YC_NUM;j++)
		{
			if (Rtus[i].Protocol == 4)
			{
				Rtus[i].YcValue [j] = 0;
				Rtus[i].YcValueOld [j] = 0;
				Rtus[i].YcBase[j] = 0;
			}
			else
			{
				Rtus[i].YcValue [j] = 2048;
				Rtus[i].YcValueOld [j] = 2048;
				Rtus[i].YcBase [j] = 2048;
			}
			Rtus[i].YcChangeFlag [j] = 0;
		}
		for (j = 0;j < RTU_MAX_YX_NUM;j++)
		{
			Rtus[i].YxValue [j] = 0;
			Rtus[i].YxValueOld [j] = 0;
			Rtus[i].YxChangeFlag [j] = 0;
		}
		Rtus[i].SoePtr = 0;
		Rtus[i].SoeSendPtr = 0;
	}
	else
	{
		for (int j = 0;j < RTU_MAX_KWH_NUM;j++)
		{
			Rtus[i].KwhValue [j] = 0;
		}
		for (j = 0;j < RTU_MAX_YC_NUM;j++)
		{
			Rtus[i].YcValue [j] = 0;
		}
		for (j = 0;j < RTU_MAX_YX_NUM;j++)
		{
			Rtus[i].YxValue [j] = 0;
		}
	}
	Rtus[i].RTUStatus = 0;
	Rtus[i].ModifyFlag = 0;
	Rtus[i].CurSendFrameType = 0;
}

void CFert2000App::InitPassword()
{
	HKEY Key;
	if (RegOpenKey(HKEY_CURRENT_USER,"FenlSys\\",&Key))
	{
		BYTE UserNum = 1;
		char UserPassword[41] = "password";
		for (int j=0;j<9;j++) UserPassword[j+20] = UserPassword[j];
		UserPassword[40] = 1;
		if (RegCreateKey(HKEY_CURRENT_USER,"FenlSys\\",&Key))
		{
			MessageBox(0,"密码读写错误!","错误",MB_OK|MB_ICONSTOP);
			return;
		}
		RegCloseKey(Key);

		if (!SetNtRegValue("FenlSys\\","UserNum",(void*)&UserNum,1) || !SetNtRegValue("FenlSys\\","User01",(void*)UserPassword,41))
		{
			MessageBox(0,"密码读写错误!","错误",MB_OK|MB_ICONSTOP);
			return;
		}
		DWORD dw;
		HKEY Key2;
		RegCreateKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\", 0, REG_NONE,
			REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
			&Key2, &dw);

		char chr[] = "c:\\run\\fert.exe";
		LONG lResult = RegSetValueEx(Key2,"fert", NULL,REG_EXPAND_SZ,(BYTE*)chr,15);
		RegCloseKey(Key2);
			
		MessageBox(0,"第一次读口令系统!\n用户名:password 密码:password\n请添加新用户后删除!","",MB_ICONWARNING);
		return;	
	}
	DWORD dw;
	HKEY Key2;
	RegCreateKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\", 0, REG_NONE,
		REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
		&Key2, &dw);

	char chr[] = "c:\\run\\fert.exe";
	LONG lResult = RegSetValueEx(Key2,"fert", NULL,REG_EXPAND_SZ,(BYTE*)chr,15);
	RegCloseKey(Key2);
	RegCloseKey(Key);
}


void CFert2000App::WriteSystemCount()
{
	BOOL Error1;
	try{
		Error1 = Database.OpenEx("DSN=FERT",CDatabase::noOdbcDialog);
	}
	catch(CDBException * e)
	{
		MessageBox(0,"打开数据库出错!","错误",MB_OK|MB_ICONSTOP);
		e->Delete();
		return ;
	}
	if (Error1 == 0 )
	{
		MessageBox(0,"打开数据库出错!","错误",MB_OK);
		return ;
	}
	CSysParaSet SysParaSet (&Database);
	try{
		if (!SysParaSet.Open())
		{
			Database.Close();
			return;
		}
	}
	catch(CDBException * e)
	{
		MessageBox(0,"打开系统参数表出错!","错误",MB_OK);
		e->Delete();
		Database.Close();
		return;
	}
	try
	{
		if (SysParaSet.IsEOF())
		{
			if (!SysParaSet.CanAppend())
			{
				MessageBox(0,"系统表不能添加数据!未保存!","",MB_OK);
				SysParaSet.Close();
				Database.Close();
				return;
			}
			SysParaSet.AddNew();
		}
		else 
			SysParaSet.Edit();
		SysParaSet.m_RTUNUM = SystemCount.RtuNum;
		SysParaSet.m_CHANNUM = SystemCount.ChNum;
		SysParaSet.Update();
		SysParaSet.Close();
	}
	catch(CDBException *e)
	{
		e->Delete ();
		SysParaSet.Close();
	}
	Database.Close();
}


void CFert2000App::WriteAChannelPara(int i)
{
	BOOL Error1;
	try{
		Error1 = Database.OpenEx("DSN=FERT",CDatabase::noOdbcDialog);
	}
	catch(CDBException * e)
	{
		MessageBox(0,"打开数据库出错!","错误",MB_OK|MB_ICONSTOP);
		e->Delete();
		return ;
	}
	if (Error1 == 0 )
	{
		MessageBox(0,"打开数据库出错!","错误",MB_OK);
		return ;
	}
	CChanParaSet ChanParaSet(&Database);
	CString strsql;
	strsql.Format ("CHANNO = %d",i);
	try
	{
		ChanParaSet.m_strFilter = strsql;
		if (!ChanParaSet.Open())
		{
			Database.Close();
			return;
		}
	}
	catch(CDBException * e)
	{
		e->Delete();
		Database.Close();
		return;
	}
	try
	{
		if (ChanParaSet.IsEOF())
			ChanParaSet.AddNew();
		else 
			ChanParaSet.Edit();

		ChanParaSet.m_channo = i;
		ChanParaSet.m_useflag  = Channels[i].Flag;
		ChanParaSet.m_syncflag = Channels[i].SorA;
		ChanParaSet.m_baudrate =  Channels[i].Baud;
		ChanParaSet.m_worktype = Channels[i].WorkType;
		ChanParaSet.m_stopbit = Channels[i].EndBit;
		ChanParaSet.m_oddevent = Channels[i].ParityBit;//奇偶性
		ChanParaSet.m_checkbit  = Channels[i].CheckBit;
		ChanParaSet.m_databit  = Channels[i].DataBit;
		ChanParaSet.m_protocol = Channels[i].Protocol;
		ChanParaSet.m_rtunum = Channels[i].ChRtuNum;
		ChanParaSet.m_rtuno0 = Channels[i].ChRtuNo[0];
		ChanParaSet.m_rtuno1 = Channels[i].ChRtuNo[1];
		ChanParaSet.m_rtuno2 = Channels[i].ChRtuNo[2];
		ChanParaSet.m_rtuno3 = Channels[i].ChRtuNo[3];
		ChanParaSet.m_rtuno4 = Channels[i].ChRtuNo[4];
		ChanParaSet.m_rtuno5 = Channels[i].ChRtuNo[5];
		ChanParaSet.m_rtuno6 = Channels[i].ChRtuNo[6];
		ChanParaSet.m_rtuno7 = Channels[i].ChRtuNo[7];
		ChanParaSet.Update();
		ChanParaSet.Close();
	}
	catch(CDBException *e)
	{
		e->Delete ();
		ChanParaSet.Close();
	}
	Database.Close();
}

void CFert2000App::WriteARtuPara(int i)
{
	BOOL Error1;
	try{
		Error1 = Database.OpenEx("DSN=FERT",CDatabase::noOdbcDialog);
	}
	catch(CDBException * e)
	{
		MessageBox(0,"打开数据库出错!","错误",MB_OK|MB_ICONSTOP);
		e->Delete();
		return ;
	}
	if (Error1 == 0 )
	{
		MessageBox(0,"打开数据库出错!","错误",MB_OK);
		return ;
	}
	CRtuParaSet RtuParaSet(&Database);
	try
	{
		CString strsql;
		strsql.Format ("rtuno = %d",i);
		RtuParaSet.m_strFilter = strsql;
		if (!RtuParaSet.Open())
		{
			Database.Close();
			return;
		}
	}
	catch(CDBException * e)
	{
		e->Delete();
		Database.Close();
		return;
	}
	try
	{
		if (RtuParaSet.IsEOF())
			RtuParaSet.AddNew();
		else 
			RtuParaSet.Edit();

		RtuParaSet.m_rtuno = i;
		RtuParaSet.m_rtuaddr = Rtus[i].Addr;
		RtuParaSet.m_useflag = Rtus[i].Flag;//使用标志
		RtuParaSet.m_channo = Rtus[i].RtuChNo;//RTU对应的通道
		//Rtus[i].WaitTime = RtuData.WaitTime;
		RtuParaSet.m_cycletime = Rtus[i].AnswerWaitTime;
		//Rtus[i].DeadArea = RtuData.DeadArea;
		//Rtus[i].FreqNum = RtuData.FreqNum;
		//Rtus[i].WaterNum = RtuData.WaterNum;
		RtuParaSet.m_ycnum = Rtus[i].YcNum; 		   //YC
		RtuParaSet.m_yxnum = Rtus[i].YxNum;        //YX
		RtuParaSet.m_kwhnum = Rtus[i].KwhNum;        //KWH
		RtuParaSet.m_soenum = Rtus[i].SoeNum;
		RtuParaSet.m_soeonyx = Rtus[i].SoeOnYxFlag;
		char *Name = RtuParaSet.m_rtuname.GetBufferSetLength(20);
		for (int j=0;j<20;j++) Name[j] = Rtus[i].Name[j];		//RTU名字

		CString str1,str2 = "";
		for( j=0;j<16;j++)
		{
			str1.Format("%02x",Rtus[i].RRC[j]);
			str2 += str1;
		}
		for (j=0;j<7;j++)
		{
			str1.Format("%02x",Rtus[i].SynWord[j]);
			str2 += str1;
		}
		Rtus[i].ProtocolStamp[9] = Rtus[i].Protocol;
		for (j=0;j<10;j++)
		{
			str1.Format("%02x",Rtus[i].ProtocolStamp[j]);
			str2 += str1;
		}
		RtuParaSet.m_RRCSynWordProtocol = str2;
		RtuParaSet.Update();
		RtuParaSet.Close();
	}
	catch(CDBException *e)
	{
		e->Delete ();
		RtuParaSet.Close();
	}
	Database.Close();
}

void CFert2000App::TransferPara()
{
	NETRESOURCE  ns;
	DWORD        dw;
	char         Name1[2][30]; 
	char         Name[] = "\\\\Ferta\\run";
	sprintf(Name1[0],"%s","c:\\run\\fert.mdb");
	sprintf(Name1[1],"%s","K:fert.mdb");
	
	if (NetProcess1.ThisNodeId == 'A') Name[6] = 'b';
	else Name[6] = 'a';

	ns.dwType=RESOURCETYPE_DISK;
	ns.lpLocalName="K:";
	ns.lpRemoteName=Name;
	ns.lpProvider=NULL;
	dw = ::WNetAddConnection2( &ns,
		NULL,
		NULL,
		CONNECT_UPDATE_PROFILE );

	if (dw == NO_ERROR)
	{
		BOOL iRet = CopyFile(Name1[1],Name1[0],FALSE);
		::WNetCancelConnection2("K:",CONNECT_UPDATE_PROFILE,TRUE );
	}
}

⌨️ 快捷键说明

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