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

📄 cgedoc.cpp

📁 《面向对象程序设计实用教程》一书的源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		for(int i=0;i<10;i++ )
		{
			pView->m_path[i] = m_pa[i]; 
		}
	}

	UpdateAllViews(NULL);// 有时不调用OninitialUpdate()方法
	  //但是OnDraw()函数被自动调用因为OpenFile对话框打开时视窗界面已经发生了变化
}


void CCgeDoc::OnCloseupDlgbarcombo1() 
{
	UpdateAllViews(NULL);//在CView 中使用无效,迟后一次???
	
}


void CCgeDoc::OnWrcom() 
{
	//初始化串口并建立一个读线程
	MyTTY.ComPort = m_com;
	MyTTY.CreateTTYInfo();
	if(MyTTY.OpenConnection() == FALSE)
		return;
	
	// 向串口写 hello,test
	//WriteCommBlock( 串口句柄, 预备写的串 , 串的长度,实际写入的长度);
	char teststring[256];  
	teststring[0]=0x0f; //启动自校验
	BOOL bret;
	DWORD dwwritenlen;
	if (MyTTY.npTTYInfo!=NULL)
	{
		bret = WriteCommBlock( MyTTY.npTTYInfo, (BYTE *)teststring , 1,&dwwritenlen); //(DWORD )strlen(teststring)=1
		if (bret && (dwwritenlen>0))
		{
			PurgeComm(COMDEV(MyTTY.npTTYInfo),PURGE_TXCLEAR);
			char temp[256];
			sprintf(temp,"write %d success!",dwwritenlen);
			AfxMessageBox(temp);
		}
		else
			AfxMessageBox("Write failed!");
	}
	else
		AfxMessageBox("COM not opened!");
	
	// 从串口读到abIn 
	//ReadCommBlock( 串口句柄, 读到的串指针 , 可以读到的最大长度=80);	
	/*  //查询读
	int nLength;
	BYTE        abIn[ MAXBLOCK + 1] ;
	do
	{
	nLength = ReadCommBlock( MyTTY.npTTYInfo, (LPSTR) abIn, MAXBLOCK );
	if (nLength>0)
		{	AfxMessageBox((char*)abIn);}
	}while (nLength>0);  //(nLength<=0);
	*/
	//事件驱动(中断)读

}


BOOL CCgeDoc::CaiYang_Fun()
{
	MyTTY.CloseConnection(); //关闭串口
	//初始化串口并建立一个读线程
	MyTTY.ComPort = m_com;
	MyTTY.CreateTTYInfo();
	if(MyTTY.OpenConnection() == FALSE)
		return false;	
	// 向串口写 teststring[1]
	//WriteCommBlock( 串口句柄, 预备写的串, 串的长度, 实际写入的长度);

	// 从串口读到abIn 
	//ReadCommBlock( 串口句柄, 读到的串指针 , 可以读到的最大长度=80);	

	BYTE teststring=0x0a; //开始采样
	
	BOOL bret;
	int nLength;
	BYTE abIn[1];
//	char s_abIn[10];
	DWORD dwwritenlen;


	if (MyTTY.npTTYInfo!=NULL)
	{		
		bret = WriteCommBlock( MyTTY.npTTYInfo, &teststring , 1,&dwwritenlen); //(DWORD )strlen(c_temp) = 6
		if (bret && (dwwritenlen>0))
		{
			PurgeComm(COMDEV(MyTTY.npTTYInfo),PURGE_TXCLEAR);
//			char temp[256];
//			sprintf(temp,"write %d success!",dwwritenlen);
//			AfxMessageBox(temp);
		}
		else
		{	AfxMessageBox("Write failed!");
			return false;
		}
	}
	else
	{	AfxMessageBox("COM not opened!");
		return false;
	}

	//回读0x0e
	do
	{
	nLength = ReadCommBlock( MyTTY.npTTYInfo, (LPSTR) abIn, 2); //MAXBLOCK );
	/*
	if (nLength>0)
		{	
			itoa(abIn[0],s_abIn,16);
			AfxMessageBox(s_abIn);
		}
	*/
	}while (nLength<=0);  //(nLength<=0);
	
	/*
	if(abIn[0]!=0x0e)	
	{
	  AfxMessageBox("通讯失败...");
	  return false;
	}
   */
	
	//单片机正在采样......
	POSITION Position;
	Position = GetFirstViewPosition();
	CView* pVView = GetNextView(Position);
	CCgeView *pView;
	while(pVView!=NULL)
	{
		if(pVView->IsKindOf(RUNTIME_CLASS(CCgeView)))
		{ break;};
		pVView = GetNextView(Position);
	}
	pView = (CCgeView *)pVView;	
	CProgressDlg m_dlg;	
	m_dlg.Create (pView);  
	m_dlg.SetWindowText(_T("              采样进度"));
	m_dlg.SetRange (0,Num_Line*Num_Pointes);
	//m_dlg.SetRange (0,Num_Line);
    m_dlg.SetPos (0);
    //m_dlg.SetStep (1);   
	m_dlg.SetStep (5); //(Num_Line/4);   

	//再回读0x0d
	do
	{
		nLength = ReadCommBlock( MyTTY.npTTYInfo, (LPSTR) abIn, 2);  //MAXBLOCK );
		
		//if (nLength>0)
		//	{	
		//		itoa(abIn[0],s_abIn,16);
		//		AfxMessageBox(s_abIn);
		//	}		
		m_dlg.StepIt(); //修改进度条的值		 

	}while(nLength<=0);

	//m_dlg.SetPos (Num_Line*Num_Pointes-20);

	if(abIn[0]==0x0d)
	{				  
	  //AfxMessageBox("采样结束...");
	  MyTTY.CloseConnection(); //关闭串口		 
	}

	//调读并口函数
	BOOL Read_key;
	int Len_d;
	m_dlg.SetPos (Num_Line*Num_Pointes);
	m_dlg.DestroyWindow ();
	Len_d = Num_Line * Num_Pointes;
	Read_key = Read_Data(Len_d);
	
	if(!Read_key)
	{
		AfxMessageBox("读并口失败...");
		return false;	  	
	}
	
	return true;
}

BOOL CCgeDoc::Read_Data(int Data_len)
{
	BYTE Data_epp[3];
//	int Data_GHL_1[4];
	int i,Iepp0;

	POSITION Position;
	Position = GetFirstViewPosition();
CView* pVView = GetNextView(Position);
	CCgeView *pView;
	while(pVView!=NULL)
	{
		if(pVView->IsKindOf(RUNTIME_CLASS(CCgeView)))
		{ break;};
		pVView = GetNextView(Position);
	}
	pView = (CCgeView *)pVView;
	CProgressDlg m_dlg;	
	m_dlg.Create (pView);  //(this);
	m_dlg.SetWindowText(_T("             读单片机数据"));
	m_dlg.SetRange (0,120);
	//m_dlg.SetRange (0,Num_Line);
    m_dlg.SetPos (0);
    //m_dlg.SetStep (1);   
	m_dlg.SetStep (20);    //(5);   

	CFile Data_1("Data_1.txt",CFile::modeCreate | CFile::modeWrite);	
	//读奇数通道数据	
	//读零点
	
	for( i=0;i<4;i++)  //对应增益为 1,8,64,512
	{
		Read_Epp(Data_epp);//空读 采样个数偏差
		//空读7个主放零漂
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读 
		//读25个主放零漂
		for(Iepp0=0;Iepp0<Main_amp_nullshift;Iepp0++)
		{			
			Read_Epp(Data_epp);

		//写入文件		
		Data_1.WriteHuge(&Data_epp[0],1);
		Data_1.WriteHuge(&Data_epp[1],1);
		Data_1.WriteHuge(&Data_epp[2],1);	
		}
		Read_Epp(Data_epp);//空读		
		
	}
	m_dlg.StepIt ();	
	//读并口奇数通道数据	
	int LData_len;
	LData_len = Data_len/2;	

	Read_Epp(Data_epp); //空读
	for(i=0;i<LData_len;i++)
	{
		Read_Epp(Data_epp);		
		
		//写入文件			
		Data_1.WriteHuge(&Data_epp[0],1);
		Data_1.WriteHuge(&Data_epp[1],1);
		Data_1.WriteHuge(&Data_epp[2],1);

		if(!((Data_epp[0]==0) || (Data_epp[0]==3) || (Data_epp[0]==7) || (Data_epp[0]==6)) )
		{
			AfxMessageBox("读EPP数据有错");
//			Data_epp[0] = 0xaa;		
		}

	}
	
	Data_1.Close();
	m_dlg.StepIt ();	
	
	MyTTY.CloseConnection(); //关闭串口
	//初始化串口并建立一个读线程
	MyTTY.ComPort = m_com;
	MyTTY.CreateTTYInfo();
	if(MyTTY.OpenConnection() == FALSE)
		return false;	
	
	BYTE teststring=0x0e; //准备读偶数通道数据	
	BOOL bret;
	int nLength;
	BYTE abIn[2];
//	char s_abIn[10];
	DWORD dwwritenlen;
	if (MyTTY.npTTYInfo!=NULL)
	{		
		bret = WriteCommBlock( MyTTY.npTTYInfo, &teststring , 1,&dwwritenlen); //(DWORD )strlen(c_temp) = 6
		if (bret && (dwwritenlen>0))
		{
			PurgeComm(COMDEV(MyTTY.npTTYInfo),PURGE_TXCLEAR);
//			char temp[256];
//			sprintf(temp,"write %d success!",dwwritenlen);
//			AfxMessageBox(temp);
		}
		else
		{	AfxMessageBox("Write failed!");
			return false;
		}
	}
	else
	{	AfxMessageBox("COM not opened!");
		return false;
	}

	//回读0x0d
	do
	{
	nLength = ReadCommBlock( MyTTY.npTTYInfo, (LPSTR) abIn, 2); //MAXBLOCK );
	
	if (nLength>0)
		{	
//			itoa(abIn[0],s_abIn,16);
//			AfxMessageBox(s_abIn);
		}
	
	}while (nLength<=0);  //(nLength<=0);
	
	
	if(abIn[0]!=0x0d)	
	{
	  AfxMessageBox("通讯失败...");
	  return false;
	}

	//读偶数通道数据	
	//读零点
	CFile Data_2("Data_2.txt",CFile::modeCreate | CFile::modeWrite);	
//	int Data_GHL_2[4];
	
	for(i=0;i<4;i++)
	{
		Read_Epp(Data_epp);//空读 采样个数偏差
		//空读7个主放零漂
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读
		Read_Epp(Data_epp);//空读 
		//读25个主放零漂
		for(Iepp0=0;Iepp0<Main_amp_nullshift;Iepp0++)
		{
			Read_Epp(Data_epp);
			//写入偶数道零点文件		
			Data_2.WriteHuge(&Data_epp[0],1);
			Data_2.WriteHuge(&Data_epp[1],1);
			Data_2.WriteHuge(&Data_epp[2],1);
		}
		Read_Epp(Data_epp);//空读
		
	}
	m_dlg.StepIt ();	
	//读并口偶数道数据

	Read_Epp(Data_epp); //空读
	for(i=0;i<LData_len;i++)
	{
		Read_Epp(Data_epp);		
		
		//写入文件
		Data_2.WriteHuge(&Data_epp[0],1);
		Data_2.WriteHuge(&Data_epp[1],1);
		Data_2.WriteHuge(&Data_epp[2],1);
	    
		if(!((Data_epp[0]==0) || (Data_epp[0]==3) || (Data_epp[0]==7) || (Data_epp[0]==6)) )
		{
			AfxMessageBox("读EPP数据有错");
//			Data_epp[0] = 0xaa;		
		}
	}
	
	Data_2.Close();
	m_dlg.StepIt ();	

	teststring=0x0e; //读完了所有的数据,发送0x0e		
	bret = WriteCommBlock( MyTTY.npTTYInfo, &teststring , 1,&dwwritenlen); //(DWORD )strlen(c_temp) = 6
	if (bret && (dwwritenlen>0))
		{
			PurgeComm(COMDEV(MyTTY.npTTYInfo),PURGE_TXCLEAR);
//			char temp[256];
//			sprintf(temp,"write %d success!",dwwritenlen);
//			AfxMessageBox(temp);
		}
		else
		{	AfxMessageBox("Write failed!");
			return false;
		}	

	MyTTY.CloseConnection(); //关闭串口
	m_dlg.SetPos(Num_Line*Num_Pointes);
	m_dlg.DestroyWindow ();
	return true;
}

BOOL CCgeDoc::Read_Epp(BYTE Data_epp[])
{
	int counter;
	int k;	//根据计算机的速度调整
	int port = 0x0378;
	BYTE status;
	//byte h=_inp(port+2);
//	_outp(port+2,h&0xf7);	//nASTRB位初始化:nASTRB 信号与实际电平反相


	//Timeout位初始化
//	buffer = _inp(port + 1);
//	_outp(port+1, buffer || 0x01);
//	_outp(port+1, buffer & 0xfe);	
	
	// reading from EPP port
		for( counter=0; counter<3; counter++)
		{

			Data_epp[counter] = _inp( port+4 );	//read port+4  0x0378+4=37c	
	//		_inp( port + 3 );   //发送nASTRB
	//			counter = counter+1;
			k=10;	//根据计算机的速度调整
	
/*OLE方式用*/
			do
			{
				status = _inp(port+1);		// read status
				status = status & 0x80;
				k --;
			} while (status && k);//等到忙信号到来//判wait为高跳出, 原装机IBM
			do
			{
				status = _inp(port+1);		// read status
			} while ((status & 0x80) == 0);//等到忙信号到来//判wait 原装机IBM
	    
//			do
//			{
//				status = _inp(port+1);		// read status
//			}while ((status & 0x01) == 0);//判timeout 兼容机		

		}
		//循环 3 次

		Data_epp[0] = Data_epp[0] & 0x07;
		if(Data_epp[0]<2) //当增益为1时使增益编码为0
		{
			Data_epp[0] = 0;
		}
		if(Data_epp[0]==0 || Data_epp[0]==3 || Data_epp[0]==7 || Data_epp[0]==6 )
		{
			Data_epp[0] = Data_epp[0];
		}
		else
		{
//  		AfxMessageBox("Data Error");
			Data_epp[0] = 0xaa;		
		}
	return true;					// return reading bytes number

}



int CCgeDoc::BtoInt(WORD H,WORD L)
{
    int hl;		
	H = H<<8;
	H = H | L;
	if ((H & 0x8000) != 0)
	{
		H = ~H;
		H = H +1 ;
		hl = -1*(int)H;
	}
	else
	{
		hl = (int)H;
	}
		
	return hl;
}

void CCgeDoc::AddLine(int Ny)
{
	 CMyLine *pMyLine = new CMyLine(Ny);
     m_CMyLineList.AddTail(pMyLine);
}

void CCgeDoc::Onmcaiyang()   //用正弦曲线模拟采样
{    
		//m_stepY = 1;
	//if (IsModified() != 0)//提示数据已经修改,是否保存
	//{
	//	OnFileSave();
	//}

	moniKey = true;
	if(mdKey)
	{
		//mdcanshu5();
	}else
	{
		Oncanshu5();
	}
	
	if(	ret_par) return;
	int i,j;
	float y;
    POSITION Position;
	Position = GetFirstViewPosition();
	CView* pVView = GetNextView(Position);
	CCgeView *pView;
	while(pVView!=NULL)
	{
		if(pVView->IsKindOf(RUNTIME_CLASS(CCgeView)))
		{ break;};
		pVView = GetNextView(Position);
	}
	pView = (CCgeView *)pVView;
	blank=false;
	unbas =false;	
	m_Ndb = 0;
	m_H = 0;
	
	//m_stepY = 1;
//	if (IsModified() != 0)//提示数据已经修改,是否保存
//	{
//		OnFileSave();
//	}

	CProgressDlg m_dlg;	
	m_dlg.Create (pView);  //(this);
	m_dlg.SetWindowText(_T("              采样进度"));
	m_dlg.SetRange (0,Num_Line);
    m_dlg.SetPos (0);
    m_dlg.SetStep (1);   
    

⌨️ 快捷键说明

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