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

📄 systemtool.cpp

📁 电信机房MDF、电源柜监控源码,主要用在通信机房配线设备监控、电源柜监控
💻 CPP
字号:
#include "stdafx.h"
#include "MDF.h"
#include "System.h"

extern CMDFApp theApp ;
// 字节数据转换为可打印字符串
// 如:{0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01} --> "C8329BFD0E01" 
// 输入: pSrc - 源数据指针
//       nSrcLength - 源数据长度
// 输出: pDst - 目标字符串指针
// 返回: 目标字符串长度
int BytesToString(const UCHAR* pSrc, char* pDst, int nSrcLength)
{
	const char tab[]="0123456789ABCDEF";	// 0x0-0xf的字符查找表
	CString strError ;
	try
	{
		for (int i = 0; i < nSrcLength; i++)
		{
			*pDst++ = tab[*pSrc >> 4];		// 输出高4位
			*pDst++ = tab[*pSrc & 0x0f];	// 输出低4位
			pSrc++;
		}

		// 输出字符串加个结束符
		*pDst = '\0';
	}
	catch(_com_error e)
	{
		strError = "异常:将为十六进制转换字符时内存溢出" ;
		theApp.OutPut(strError) ;
		return -1 ;
	}

	// 返回目标字符串长度
	return (nSrcLength * 2);
}

// 可打印字符串转换为字节数据
// 如:"C8329BFD0E01" --> {0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01}
// 输入: pSrc - 源字符串指针
//       nSrcLength - 源字符串长度
// 输出: pDst - 目标数据指针
// 返回: 目标数据长度
int StringToBytes(const char* pSrc, UCHAR* pDst, int nSrcLength)
{
	CString strError ;
	//pDst = new UCHAR[nSrcLength/2];
	memset(pDst,0,nSrcLength/2);
	try
	{
		int nChange = 0 ;
		for (int i = 0; i < nSrcLength; i += 2)
		{
			// 输出高4位
			if ((*pSrc >= '0') && (*pSrc <= '9'))
				*pDst = (*pSrc - '0') << 4;
			else if((*pSrc >= 'A') && (*pSrc <= 'F'))
				*pDst = (*pSrc - 'A' + 10) << 4;
			else if((*pSrc >= 'a') && (*pSrc <= 'f'))
				*pDst = (*pSrc - 'a' + 10) << 4;
			else
				*pDst = 0x00 << 4 ;

			pSrc++;
			
			// 输出低4位
			if ((*pSrc>='0') && (*pSrc<='9'))
				*pDst |= *pSrc - '0';
			else if((*pSrc >= 'A') && (*pSrc <= 'F'))
				*pDst |= *pSrc - 'A' + 10 ;
			else if((*pSrc >= 'a') && (*pSrc <= 'z'))
				*pDst |= *pSrc - 'a' + 10 ;
			else
				*pDst &= 0xF0 ;

			pSrc++;
			if( i < nSrcLength - 2 )
			{
				pDst++;
			}
		}

	}
	catch(_com_error e)
	{
		strError = "异常:将字符转换为十六进制时内存溢出" ;
		TRACE0(strError);
		return -1 ;
	}
	 //返回目标数据长度
	return (nSrcLength / 2);
}


//短信息拷贝GB码与ASCII混编
int MsgCopy(const char* pSrc, char* pDst,int nSrcLength)
{
	int nResult = 0 ;

	while( nResult < nSrcLength )
	{
		if( *pSrc < 0 )
		{
			*pDst++ = *pSrc++ ;
			nResult++ ;
		}
		*pDst++ = *pSrc++ ;
		nResult++ ;
	}
	return nResult ;
}

//服务器运行线程
UINT ServerThd(LPVOID lp)
{
	CString strCmdNote,strTip,strTemp,strSend ;
	sCmd f_Cmd ;
	sCommEQ* pEQ ;
	CList<sCmd,sCmd>* pCmdList ;

	//数据库嵌入对象初始化
	if(!AfxOleInit())
	{
		AfxMessageBox("OLE INIT ERROR! ");
		return -1 ;
	}

	theApp.Show("系统:服务器初始化") ;

	//连接数据库
	if(!theApp.m_pAdoDB->InitDB())
		return -1 ;

	theApp.Show("系统:成功连接数据库") ;
	
	//if( theApp.m_pComm->m_pEQPool->InitEQ() < 0 )		//初始化设备池。
	//{
	//	theApp.Show("异常:监测设备初始化失败") ;
	//	return -1 ;
	//}
	//else
	//	theApp.Show("系统:监测设备初始化成功") ;

	//if( theApp.m_pComm->m_pMan->InitMan() != 0 )
	//{
	//	theApp.Show("*异常:管理人员初始化失败") ;
	//	return -1 ;
	//}
	//else
	//	theApp.Show("系统:监测设备初始化成功") ;

	////启动通讯池,打开各端口
	//theApp.m_pComm->m_pCommPool->Open() ;

	//theApp.m_pComm->m_bComm = TRUE ;

	//theApp.m_pComm->nServerState = SERVER_SCAN ;

	//while(theApp.m_pComm->m_bComm)
	//{
	//	switch(theApp.m_pComm->nServerState)
	//	{
	//	case SERVER_SCAN:			//扫描监测设备
	//		
	//		nCount = theApp.m_pComm->m_pEQPool->m_EQArray.GetSize() ;

	//		for( i=0 ; i < nCount ; i++)
	//		{
	//			pEQ= &theApp.m_pComm->m_pEQPool->m_EQArray[i] ;

	//			if( pEQ == NULL )
	//				continue ;
	//			//超时处理
	//			if( pEQ->bResponse ) 
	//			{

	//				pCmdList = (CList<sCmd,sCmd>*)pEQ->pCmdList ;

	//				if( pCmdList != NULL )
	//				{
	//					if( !pCmdList->IsEmpty() )
	//					{
	//						//增加设备通讯超时计数
	//						pEQ->nTimeOut++ ;

	//						//设备通讯异常记录
	//						if( pEQ->nTimeOut == CONFIRM_TIME )
	//						{
	//							strTip.Format("异常:%s 发生通讯故障(%d次超时)",pEQ->strName,CONFIRM_TIME) ;
	//							theApp.OutPut(strTip) ;
	//							pEQ->unCommState = 0 ;//设置设备为
	//						}
	//						if( !pCmdList->IsEmpty())
	//						{
	//							f_Cmd = pCmdList->GetHead() ;

	//							strCmdNote = theApp.m_pComm->m_pProtocol->GetCmdNote(pEQ,f_Cmd.ucCmd) ;
	//							theApp.m_pAdoDB->WriteCommRd(f_Cmd.strTime,1,1,"监测中心",strCmdNote,f_Cmd.strPack,theApp.m_pComm->m_sHostIp.strIp,"响应包超时无返回") ;
	//							theApp.m_pComm->m_pEQPool->SubCommand(pEQ) ; //删除设备通讯命令
	//						}
	//					}
	//				}
	//			}
	//			//扫描设备
	//			if( pEQ->unCommState == 0 )//通讯未连接
	//			{
	//				theApp.m_pComm->m_pProtocol->Build(pEQ,0x60,strSend) ;
	//				theApp.m_pComm->SendData(strSend,pEQ,0x60) ;
	//			}
	//			else if( pEQ->unCommState == 1 )//状态轮询
	//				theApp.m_pComm->SendData(pEQ->strScanPack,pEQ,0x44) ;

	//			pEQ->bResponse = FALSE ;

	//		}					
	//		Sleep(ALARM_TIMEOUT) ;	//设备轮询时间
	//		break ;

	//	case SERVER_REFRESH:		//刷新数据库

	//		theApp.Show("系统:服务器数据刷新...") ;
	//		theApp.OutPut("系统:服务器数据刷新...") ;
	//		//关闭通讯池
	//		theApp.m_pComm->m_pCommPool->Close() ;
	//		//清空管理员
	//		theApp.m_pComm->m_pMan->FreeMan() ;
	//		//清空设备池
	//		theApp.m_pComm->m_pEQPool->ClearEQ() ;
	//		//连接数据库判断
	//		if(!theApp.m_pAdoDB->m_ConPtr->State)
	//		{
	//			if(!theApp.m_pAdoDB->InitDB())
	//				return -1 ;
	//		}
	//		//初始化设备池。
	//		if( theApp.m_pComm->m_pEQPool->InitEQ() < 0 )		
	//		{
	//			theApp.Show("异常:监测设备初始化失败") ;
	//			return -1 ;
	//		}
	//		else
	//			theApp.Show("异常:监测设备初始化成功") ;

	//		if( theApp.m_pComm->m_pMan->InitMan() != 0 )
	//		{
	//			theApp.Show("* 异常:管理人员初始化失败") ;
	//			return -1 ;
	//		}
	//		else
	//			theApp.Show("异常:监测设备初始化成功") ;

	//		//启动通讯池,打开各端口
	//		theApp.m_pComm->m_pCommPool->Open() ;
	//		theApp.m_pComm->nServerState = SERVER_SCAN ;
	//		break ;
	//		
	//	case SERVER_STOP:			//停止服务器
	//		
	//		//关闭通讯池
	//		theApp.m_pComm->m_pCommPool->Close() ;
	//		//清空管理员
	//		theApp.m_pComm->m_pMan->FreeMan() ;
	//		//清空设备池
	//		theApp.m_pComm->m_pEQPool->ClearEQ() ;
	//		theApp.Show("系统:服务器终止") ;
	//		theApp.OutPut("系统:服务器终止") ;
	//		//关闭数据库
	//		theApp.m_pAdoDB->CloseDB() ;
	//		theApp.m_pComm->m_bComm = FALSE ;

	//		break ;
	//	}
	//}	
	return 0 ;
}
UINT WriteDBThd(LPVOID lp)
{

	//CAdoDB cAdo ;
	//CString strNote ;
	////数据库嵌入对象初始化
	//CoInitialize(NULL) ;

	//cAdo.InitDB() ;

	//while(theApp.m_pComm->m_bComm)
	//{
	//	if( !theApp.m_strDBArray.IsEmpty() )
	//	{
	//		strNote.Empty() ;
	//		strNote = theApp.m_strDBArray.GetHead() ;
	//		cAdo.ExecuteDB(strNote) ;
	//		theApp.m_strDBArray.RemoveHead() ;
	//	}
	//	else
	//		Sleep(10) ;

	//	if( !cAdo.GetConnectState() )
	//	{
	//		cAdo.CloseDB() ;
	//		cAdo.InitDB() ;
	//	}
	//}
	//cAdo.CloseDB() ;
	//CoUninitialize() ;
	return 0 ;
}


int String2Hex(CString& str, BYTE *senddata,int iLen)
{
	CString sChar,sResult,strTemp;
	//strTemp.Format("%s",senddata);
	sChar.Empty();
	sResult.Empty();
	for(int i=0;i< iLen;i++)
	{
		sChar.Format("%.2X",*senddata++);
		sResult += sChar;
	}
	str = sResult;	
	return(0);
}


⌨️ 快捷键说明

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