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

📄 checkstatebak.cpp

📁 使用vc++技术实现光电机串口通信管理程序
💻 CPP
字号:
// CheckState.cpp : implementation file
//

#include "stdafx.h"
#include "PCM120.h"
#include "CheckState.h"
#include "DlgConfig.h"
#include "DlgE1Mod.h"
#include "DlgAllStaSet.h"
#include "DlgShx.h"
#include "DlgE1Sch.h"
#include "DlgStateDisplay.h"
#include "RectView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCheckState dialog


CCheckState::CCheckState(CWnd* pParent /*=NULL*/)
	: CDialog(CCheckState::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCheckState)
//	m_iCom = 1;
	//}}AFX_DATA_INIT
}


void CCheckState::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCheckState)
	
//	DDX_Text(pDX, IDC_EDIT_COM, m_iCom);
//	DDV_MinMaxInt(pDX, m_iCom, 1, 20);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCheckState, CDialog)
	//{{AFX_MSG_MAP(CCheckState)
	ON_WM_TIMER()
	ON_MESSAGE(WM_COMM_RXCHAR,OnCommunication)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCheckState message handlers

BEGIN_EVENTSINK_MAP(CCheckState, CDialog)
    //{{AFX_EVENTSINK_MAP(CCheckState)
//	ON_EVENT(CCheckState, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
int nError;
CString CCheckState::strGetSection="default";
CString CCheckState::strColorbefore="";
CString CCheckState::strRecive="";
CString CCheckState::strPath="";	
int CCheckState::iState=0;
int iSelfState=0;
int CCheckState::iTime=0;
bool CCheckState::bAuto=false;
CStringArray staAllStaName;
CStringArray staAllID;//所有站点的所有的设备ID
BOOL CCheckState::OnInitDialog() 
{
	CDialog::OnInitDialog();
//////////////////////////////////////////////////////////////////////////
	SYSTEMTIME   mySystemTime;
	GetLocalTime(&mySystemTime);
	CString   filetime;
	//	filetime.Format ("%d-%d-%d     %s%d     %d:%d:%d",mySystemTime.wYear,mySystemTime.wMonth,mySystemTime.wDay,"星期",mySystemTime.wDayOfWeek,mySystemTime.wHour,mySystemTime.wMinute,mySystemTime.wSecond);
	filetime.Format("%d-%d-%d.ini",mySystemTime.wYear,mySystemTime.wMonth,mySystemTime.wDay);
	CDlgAllStaSet dlgalstaset;
	dlgalstaset.GetModulePath(strPath,NULL);
	CString strConfigaPath;
	strConfigaPath=strPath+"config.ini";
	char charGedelay[255];
	GetPrivateProfileString("General","延时(毫秒)","1000",charGedelay,255,strConfigaPath);
	idelay=atoi((LPTSTR)charGedelay);
	strFileName=strPath+"log\\"+filetime;

//////////////////////////////////////////////////////////////////////////

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCheckState::OnOK() 
{

	CDialog::OnOK();
}
BOOL over;

void CCheckState::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	switch(nIDEvent) 
	{
	case 1:	
		KillTimer(1);
		
		if(m_strRXData.Find("7E 7E")>=0)
		{
			int ipos7E =m_strRXData.Find("7E 7E");
			CString strWarn ;
			m_strRXData=m_strRXData.Right(m_strRXData.GetLength()-ipos7E-3);
			strWarn =m_strRXData.Right(12);
			char charGetbe[255];
			GetPrivateProfileString("default",m_strRXData.Mid(6,2)," ",charGetbe,255,strFileName);
			WritePrivateProfileString("default",m_strRXData.Mid(6,2),strWarn,strFileName);
			CString strStand;
			strStand=charGetbe;
			//strStand="04 04 04 04";
			strStand+=" ";
			if(strWarn.Compare(strStand)!=0)
			{
				WritePrivateProfileString(strLogSection,m_strRXData.Mid(6,2),strWarn,strFileName);				
			}			
			if(iIDidx<iCntID-1)
			{
				iIDidx++;
				OneCyle();
			}
			else
			{
				m_Port.ClosePort();
				CCheckState::strGetSection=	strLogSection;
				strColor="";
//////////////////////////取各站的状态即颜色显示////////////////////////////////////////////////
				for(int i =0;i<staAllStaName.GetSize();i++)
				{		
					CStringArray staTemp;
					int iDevCntTemp=0;
					ReadConfigDevID(i+1,staTemp,iDevCntTemp);
					int nErr=0;
					for(int j=0;j<staTemp.GetSize();j++)
					{				
						CString staAddTemp=staTemp.GetAt(j);						
						char Get[255];
						//GetPrivateProfileString("default",staAddTemp,"04 04 04 04",Get,255,strPath);
						GetPrivateProfileString("default",staAddTemp,"FF FF FF FF",Get,255,strFileName);
						CString strGetWarning;
						strGetWarning=Get;
						if(strGetWarning=="FF FF FF FF")
						{
							nErr++;
						}
						
					}
					int iSizeDev=staTemp.GetSize();
					if(nErr==iSizeDev)
					{
						strColor+="0";
					}
					if(nErr==0)
					{
						strColor+="2";
					}
					if(0 < nErr && nErr< iSizeDev)
					{
						strColor+="1";
					}

					
				}
				strColorbefore=strColor;
//////////////////////////////////////////////////////////////////////////				
			}
		}
		else//收到的串的长度不是57时,不是正常的
		{
//////////////////////////////////////////////////////////////////////////
			char charGetbe[255];
			GetPrivateProfileString("default",staAdd," ",charGetbe,255,strFileName);
			WritePrivateProfileString("default",staAdd,"FF FF FF FF",strFileName);
			CString strStand;
			strStand=charGetbe;
		//	strStand="04 04 04 04";
			if(strStand.Compare("FF FF FF FF")!=0)
			{
				WritePrivateProfileString(strLogSection,staAdd,"FF FF FF FF",strFileName);				
			}
//////////////////////////////////////////////////////////////////////////
			//WritePrivateProfileString(strLogSection,staAdd,"FF FF FF FF ",strPath);
		//	WritePrivateProfileString("default",staAdd,"FF FF FF FF",strPath);
			if(iIDidx<iCntID-1)
			{
				iIDidx++;
				OneCyle();
				
			}	
			else
			{
				m_Port.ClosePort();//当所有站的设备检查一遍后关闭串
				CCheckState::strGetSection=	strLogSection;
				strColor="";
//////////////////////////取各站的状态即颜色显示////////////////////////////////////////////////
				for(int i =0;i<staAllStaName.GetSize();i++)
				{		
					CStringArray staTemp;
					int iDevCntTemp=0;
					ReadConfigDevID(i+1,staTemp,iDevCntTemp);
					int nErr=0;
					for(int j=0;j<staTemp.GetSize();j++)
					{				
						CString staAddTemp=staTemp.GetAt(j);						
						char Get[255];
					//	GetPrivateProfileString("default",staAddTemp,"04 04 04 04",Get,255,strPath);
						GetPrivateProfileString("default",staAddTemp,"FF FF FF FF",Get,255,strFileName);
						CString strGetWarning;
						strGetWarning=Get;
						if(strGetWarning=="FF FF FF FF")
						{
							nErr++;
						}
						
					}
					int iSizeDev=staTemp.GetSize();
					if(nErr==iSizeDev)
					{
						strColor+="0";
					}
					if(nErr==0)
					{
						strColor+="2";
					}
					if(0 < nErr && nErr< iSizeDev)
					{
						strColor+="1";
					}

					
				}
				strColorbefore=strColor;
//////////////////////////////////////////////////////////////////////////
			}
		}
		
		break;
	case 2:
		if(bAuto==true)
		{
			doCheck();
		}
		
		break;
	case 3:
		if(bAuto==true)
		{
			OneCyle();
		}
		
		break;
	case 4:
		KillTimer(4);
		if(bAuto==true)
		{
			doCheck();
		}		
		break;
		
	}
	
	CDialog::OnTimer(nIDEvent);
}
void CCheckState::OneCyle()

{	
	if (iIDidx<iCntID)
	{
		
		staAdd=staAllID.GetAt(iIDidx);
		CString strSearchcheck;
		strSearchcheck.Format("7E 81 %s %s 00 06 00",staAdd,staAdd);
		if(m_Port.m_hComm==NULL&&CDlgConfig::iState!=2&& CDlgShx::iState!=2&&CDlgE1Mod::iState!=2)
		{			
			//AfxMessageBox("串口没有打开,请打开串口");
			OnButtonOpenport();//打开串口			
		}
		if(m_Port.m_hComm==NULL)
		{
			return;
		}
		else
		{
			m_strRXData="";
			char data[512];
			int len=Str2Hex(strSearchcheck,data);
			m_Port.WriteToPort(data,len);								
			SetTimer(1,idelay,NULL);
		}
	}
	else
	{
		return;
	}

}

void CCheckState::doCheck()
{	
	SYSTEMTIME   mySystemTime;
	GetLocalTime(&mySystemTime);
	CString   filetime;
	//	filetime.Format ("%d-%d-%d     %s%d     %d:%d:%d",mySystemTime.wYear,mySystemTime.wMonth,mySystemTime.wDay,"星期",mySystemTime.wDayOfWeek,mySystemTime.wHour,mySystemTime.wMinute,mySystemTime.wSecond);
	filetime.Format("%d-%d-%d.ini",mySystemTime.wYear,mySystemTime.wMonth,mySystemTime.wDay);
	CDlgAllStaSet dlgalstaset;
	dlgalstaset.GetModulePath(strPath,NULL);
	strFileName="";
	strFileName=strPath+"log\\"+filetime;
	//strPath+=filetime;
	CStdioFile file;
//	if(file.Open(strFileName,CFile::modeRead))//如果文件已经存在,也就是此次不是今天第一次检查
//	{
	//	file.Close();
		if(file.Open(strFileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite))
		{
			file.Close();
		}
		else
		{
			AfxMessageBox("日志文件创建失败");
		}
		strLogSection.Format("%d:%d:%d",mySystemTime.wHour,mySystemTime.wMinute,mySystemTime.wSecond);
		iIDidx=0;
		OneCyle();
//	}

//////////////////////////////////////////////////////////////////////////
/*	
	else//此次是今天第一次检查
	{
		CString strYesterday;
		if(mySystemTime.wDay>1)
		{
			strYesterday.Format("%d-%d-%d.ini",mySystemTime.wYear,mySystemTime.wMonth,mySystemTime.wDay-1);
		}
		else
		{
			if(mySystemTime.wMonth==4||mySystemTime.wMonth==6||mySystemTime.wMonth==9||mySystemTime.wMonth==11)
			{
				strYesterday.Format("%d-%d-%d.ini",mySystemTime.wYear,mySystemTime.wMonth-1,31);
			}
			else if(mySystemTime.wMonth==3 )
			{
				if(mySystemTime.wYear%4==0)
				{
					strYesterday.Format("%d-%d-%d.ini",mySystemTime.wYear,2,29);
				}
				else
				{
					strYesterday.Format("%d-%d-%d.ini",mySystemTime.wYear,2,28);
				}
				
			}
			else if(mySystemTime.wMonth==1)
			{
				strYesterday.Format("%d-%d-%d.ini",mySystemTime.wYear-1,12,31);
			}
			else if(mySystemTime.wMonth==5||mySystemTime.wMonth==7||mySystemTime.wMonth==8||mySystemTime.wMonth==10||mySystemTime.wMonth==12)
			{
				strYesterday.Format("%d-%d-%d.ini",mySystemTime.wYear,mySystemTime.wMonth-1,30);
			}
		}
		CStdioFile fileyes;		
		if(fileyes.Open(strPath+"log\\"+strYesterday,CFile::modeRead))
		{
			int ifileLen =fileyes.GetLength();
			char *pTempbuf=new char[ifileLen+1];
			fileyes.Read(pTempbuf,ifileLen);
			CString strLastLog;
			strLastLog=(LPCTSTR)pTempbuf;
			int iPostEnd =strLastLog.Find("[",10);//从第十个字符开始找出现的第一个“[”就是
			
			CString strGetLastDefault;
			strGetLastDefault=strLastLog.Left(iPostEnd);
			fileyes.Close();
				if(file.Open(strFileName,CFile::modeCreate|CFile::modeWrite))
				{
					file.WriteString(strGetLastDefault);
					file.Close();			
				}
				
		}
		else
		{
			file.Open(strFileName,CFile::modeCreate|CFile::modeWrite);
			file.Close();
		}
		
	}
//////////////////////////////////////////////////////////////////////////
*/

}
void CCheckState::ReadConfigStaName(CStringArray &staName)
{
	CDlgAllStaSet dlgalstaset;
	CString strPath;
	char Get[255];
	dlgalstaset.GetModulePath(strPath,NULL);
	strPath+="PCM120networksetup.ini";
	GetPrivateProfileString("general","StationCnt","0",Get,255,strPath);
	int iCntSta =atoi(LPCTSTR(Get));
	for(int i=0;i<iCntSta;i++)
	{
		CString strSection;
		strSection.Format("station%d",i+1);
		GetPrivateProfileString(strSection,"stationname","NULL",Get,255,strPath);
		staName.Add(Get);
	}


}
void CCheckState::ReadConfigDevID(int iIndxSta,CStringArray &staDevID,int &iCntDev)
{
	CDlgAllStaSet dlgalstaset;
	CString strPath;
	char Get[255];
	dlgalstaset.GetModulePath(strPath,NULL);
	strPath+="PCM120networksetup.ini";
	CString strSection;
	strSection.Format("station%d",iIndxSta);
	GetPrivateProfileString(strSection,"DevCnt","0",Get,255,strPath);
	iCntDev =atoi(Get);
	for(int i=0;i<iCntDev;i++)
	{
		CString strSeach;
		strSeach.Format("第%d个设备ID",i+1);
		GetPrivateProfileString(strSection,strSeach,"01",Get,255,strPath);
		staDevID.Add(Get);
	}
	

}

void CCheckState::OnCancel() 
{
	// TODO: Add extra cleanup here
/*	if(m_ctrlComm.GetPortOpen())
	{
		m_ctrlComm.SetPortOpen(false);
	}
	*/
	m_ctrlComm.ClosePort();
	CDialog::OnCancel();
}

int CCheckState::Str2Hex(CString str, char* data)
{
	int t,t1;
	int rlen=0,len=str.GetLength();
	//data.SetSize(len/2);
	for(int i=0;i<len;)
	{
		char l,h=str[i];
		if(h==' ')
		{
			i++;
			continue;
		}
		i++;
		if(i>=len)
			break;
		l=str[i];
		t=HexChar(h);
		t1=HexChar(l);
		if((t==16)||(t1==16))
			break;
		else 
			t=t*16+t1;
		i++;
		data[rlen]=(char)t;
		rlen++;
	}
	return rlen;

}
char CCheckState::HexChar(char c)
{
	if((c>='0')&&(c<='9'))
		return c-0x30;
	else if((c>='A')&&(c<='F'))
		return c-'A'+10;
	else if((c>='a')&&(c<='f'))
		return c-'a'+10;
	else 
		return 0x10;
}
LONG CCheckState::OnCommunication(WPARAM ch,LPARAM port)
{
	if (port <= 0 || port > 4)
		return -1;
	CString str;

	str.Format("%02X ",ch);
	m_strRXData+=str;
	//UpdateData(false);
	return 0;


}

void CCheckState::OnButtonOpenport() 
{
	// TODO: Add your control notification handler code here
	////////////////////////读串口信息//////////////////////////////////////////////////
	CDlgAllStaSet dlgalstaset;
	CString strPath;
	char Get[255];
	dlgalstaset.GetModulePath(strPath,NULL);
	strPath+="config.ini";
	GetPrivateProfileString("General","COM","1",Get,255,strPath);
	UINT uiCom=(UINT)atoi(Get);
	//////////////////////////////////////////////////////////////////////////
		CString strStatus;
		if (m_Port.InitPort(this, uiCom, 2400,'o',8,1,EV_RXFLAG | EV_RXCHAR,2048))
		{
			m_Port.StartMonitoring();		
		}
		else
		{
		//	AfxMessageBox("没有发现此串口或被占用");
		}		

}
void CCheckState::OnAutoCheck()
{
		//ShowWindow(SW_MINIMIZE);
	UpdateData(true);

//	SetTimer(1,CRectView::uiPeriod*1000,NULL);
	ReadConfigStaName(staAllStaName);//读站名信息
//////////////////////////////////////////////////////////////////////////
	for(int i =0;i<staAllStaName.GetSize();i++)
	{		
		CStringArray staTemp;
		int iDevCntTemp=0;
		ReadConfigDevID(i+1,staTemp,iDevCntTemp);
		for(int j=0;j<staTemp.GetSize();j++)
		{				
			CString staAddTemp=staTemp.GetAt(j);
			staAllID.Add(staAddTemp);
			
		}
	}
	iCntID=staAllID.GetSize();

//////////////////////////////////////////////////////////////////////////
	
	bAuto=true;
	if(bAuto ==true &&m_Port.m_hComm==NULL&&CDlgConfig::iState!=2&& CDlgShx::iState!=2&&CDlgE1Mod::iState!=2)
	{	
		OnButtonOpenport();//打开串口
	
	}
	else if(CDlgConfig::iState!=2|| CDlgShx::iState!=2||CDlgE1Mod::iState!=2)
	{//当三个设置 模块处于工作状态时,就退出,过一秒后检查;
		
		SetTimer(4,idelay,NULL);
		return;
	}
	if(m_Port.m_hComm==NULL)
	{
		return;
	}
	else 
	{
		if (bAuto==true)
		{
			doCheck();
		}
		
	}
	SetTimer(2,CRectView::uiPeriod*1000,NULL);
	CDialog::OnOK();
}
void CCheckState::OnAutoStop()
{
	m_Port.ClosePort();
	KillTimer(1);
	KillTimer(2);
	KillTimer(3);
	KillTimer(4);
	bAuto=false;
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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