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

📄 checkstate.cpp

📁 使用vc++技术实现光电机串口通信管理程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					strpre.Format("%sE2",strGetAdd);
					WritePrivateProfileString("default",strpre,"0",strFileName);
				}
				if(strRXBin.Mid(3,1)=="1")
				{
					strpre.Format("%sE1",strGetAdd);
					WritePrivateProfileString("default",strpre,"1",strFileName);
				}
				else
				{
					strpre.Format("%sE1",strGetAdd);
					WritePrivateProfileString("default",strpre,"0",strFileName);
				}
			}

		
		OneCyle();
		}
		else
		{
			OneCyle();		
		}			
		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);
		//////////////////////////////////////////////////////////////////////////
		CDlgAllStaSet dlgalstaset;
		CString strPath;
		char Get[255];
		dlgalstaset.GetModulePath(strPath,NULL);
		strPath+="config.ini";
		GetPrivateProfileString("Com",staAdd,"1",Get,255,strPath);
		UINT uiCom=(UINT)atoi(Get);
		//////////////////////////////////////////////////////////////////////////
		
		if(m_Port.m_hComm==NULL&&CDlgConfig::iState!=2&& CDlgShx::iState!=2&&CDlgE1Mod::iState!=2)
		{			
			//AfxMessageBox("串口没有打开,请打开串口");
			if(!OnButtonOpenport(uiCom))//打开串口			
			{
				
				OnButtonOpenport(uiCom);
			}		
		
		}

	
		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;	
		checkmod();
		//SetTimer(5,idelay,NULL);
	//	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;


}

bool CCheckState::OnButtonOpenport(UINT uiCom) 
{
	// 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();
			return true;
		}
		else
		{
		//	AfxMessageBox("没有发现此串口或被占用");
			return false;
		}		

}

void CCheckState::OnAutoCheck()
{
		//ShowWindow(SW_MINIMIZE);
	UpdateData(true);
	staAllID.RemoveAll();
//	SetTimer(1,CRectView::uiPeriod*1000,NULL);
	staAllStaName.RemoveAll();
	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)
	{	
	/*	if(!OnButtonOpenport())//打开串口
		{
			m_Port.ClosePort();
			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);
	KillTimer(5);
	bAuto=false;
	CDialog::OnCancel();
}
void CCheckState::XstrtoBin(CString str,CString &strBin)
{
	str.Replace("0","0000");
	str.Replace("1","0001");
	str.Replace("2","0010");
	str.Replace("3","0011");
	str.Replace("4","0100");
	str.Replace("5","0101");
	str.Replace("6","0110");
	str.Replace("7","0111");
	str.Replace("8","1000");
	str.Replace("9","1001");
	str.Replace("A","1010");
	str.Replace("B","1011");
	str.Replace("C","1100");
	str.Replace("D","1101");
	str.Replace("E","1110");
	str.Replace("F","1111");
	strBin=str;
}
void CCheckState::checkmod()
{
//	
	CCheckState::iState=2;
	CString strGA,strMod;
	strGA=staAllID.GetAt(iIDidx);
	strMod.Format("7E 81 %s %s 00 04 00",strGA,strGA);
//////////////////////////////////////////////////////////////////////////
	CDlgAllStaSet dlgalstaset;
	CString strPath;
	char Get[255];
	dlgalstaset.GetModulePath(strPath,NULL);
	strPath+="config.ini";
	GetPrivateProfileString("Com",strGA,"1",Get,255,strPath);
	UINT uiCom=(UINT)atoi(Get);
//////////////////////////////////////////////////////////////////////////

	if(m_Port.m_hComm==NULL)
	{	
		if(!OnButtonOpenport(uiCom))
		{
			m_Port.ClosePort();
			OnButtonOpenport(uiCom);
		}
		m_strRXData="";
		char data[512];
		int len=Str2Hex(strMod,data);
		m_Port.WriteToPort(data,len);
		//AfxMessageBox("串口占用或不存在,请稍后重试");
		//return;
	}
	else
	{		
		if(OnButtonOpenport(uiCom))
		{
		m_strRXData="";
		char data[512];
		int len=Str2Hex(strMod,data);
		m_Port.WriteToPort(data,len);
		}
		
	}	
	SetTimer(5,idelay,NULL);//等1000毫秒后取接收内容
	
	
}

⌨️ 快捷键说明

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