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

📄 pulseeddyview.cpp

📁 一个用于远场涡流检测的数据采集系统的界面编程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// pulseeddyView.cpp : implementation of the CPulseeddyView class
//

#include "stdafx.h"
#include "pulseeddy.h"
#include "math.h"
#include "C:\Art\PCI2000\INCLUDE\PCI2000.H" 
#include "pulseeddySet.h"
#include "pulseeddyDoc.h"
#include "pulseeddyView.h"

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

HANDLE hEvent;
HANDLE hFileObject;
#define MAX_SEGMENT 32
#define Pi 3.14
int  m=0;
int km;
int  zerotime1;
int jj=0;
int ggd;
int waveflag;
float avemax1,avemax2,avemax3,avemax4,avemax5,avemax6,avemax7,avemax8;
int perx,pery;
int maxduan;
int maxdian;
int zijie;
int MB;
int m_Height;
int m_Width;
float maxzhi1;
float yuanzhi;
float maxzhi=0;
int  SAMPLEDIAN=2048;
WORD InUserRegion[MAX_SEGMENT][2048]; // 缓冲队列
CString strNewFileName;
float InUser[4096];
float InUser1[2048];
float   Real[2048];
float   Imag[2048];
int CurrentIndex; // AD数据处理线程当前缓冲区索引号
int CurrentIndex1=0; 
int ReadIndex;    // AD数据采集线程当前缓冲区索引号
int SegmentCounts; // 记录共有多少段缓冲区没有来得及处
PCI2000_PARA_AD Para;
float max1=0;
float m1;
float FuZhi[1024];
UINT ReadThread(PVOID hWnd);//数据线程
UINT DrawWindowProc(PVOID hWnd);
/////////////////////////////////////////////////////////////////////////////
// CPulseeddyView

IMPLEMENT_DYNCREATE(CPulseeddyView, CRecordView)

BEGIN_MESSAGE_MAP(CPulseeddyView, CRecordView)
	//{{AFX_MSG_MAP(CPulseeddyView)
	ON_BN_CLICKED(IDC_BEGSAM, OnBegsam)
	ON_BN_CLICKED(IDC_STOPSAM, OnStopsam)
	ON_BN_CLICKED(IDC_TIMEBO, OnTimebo)
	ON_BN_CLICKED(IDC_TEPIN, OnTepin)
	ON_BN_CLICKED(IDC_MAXVAL, OnMaxval)
	ON_BN_CLICKED(IDC_MAXVALTIME, OnMaxvaltime)
	ON_BN_CLICKED(IDC_ZEROTIME, OnZerotime)
	ON_BN_CLICKED(IDC_PINPUTU, OnPinputu)
	ON_BN_CLICKED(IDC_CUNPAN, OnCunpan)
	ON_BN_CLICKED(IDC_PHASETU, OnPhasetu)
	ON_WM_TIMER()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_STOPWAVE, OnStopwave)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPulseeddyView construction/destruction

CPulseeddyView::CPulseeddyView()
	: CRecordView(CPulseeddyView::IDD)
{
	//{{AFX_DATA_INIT(CPulseeddyView)
	m_pSet = NULL;
	m_shutongdao = 1;
	m_motongdao = 1;
	m_pinlv = 100000;
	m_zengyi = 1;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CPulseeddyView::~CPulseeddyView()
{
}

void CPulseeddyView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPulseeddyView)
	DDX_Control(pDX, IDC_EDIT5, m_ctrlPicture);
	//DDX_Control(pDX, IDC_LIST1, m_shuzixianshi);
	DDX_Text(pDX, IDC_EDIT1, m_shutongdao);
	DDX_Text(pDX, IDC_EDIT2, m_motongdao);
	DDX_Text(pDX, IDC_EDIT3, m_pinlv);
	DDX_Text(pDX, IDC_EDIT4, m_zengyi);
	//}}AFX_DATA_MAP
}

BOOL CPulseeddyView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs


	return CRecordView::PreCreateWindow(cs);
}

void CPulseeddyView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_pulseeddySet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	
    m_bFirstCreateDeviceFile=TRUE;  // 一开始,为第一次创建文件对象
}

/////////////////////////////////////////////////////////////////////////////
// CPulseeddyView printing

BOOL CPulseeddyView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CPulseeddyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CPulseeddyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CPulseeddyView diagnostics

#ifdef _DEBUG
void CPulseeddyView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CPulseeddyView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

CPulseeddyDoc* CPulseeddyView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPulseeddyDoc)));
	return (CPulseeddyDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPulseeddyView database support
CRecordset* CPulseeddyView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////                                                                                                                                               
// CPulseeddyView message handlers

void CPulseeddyView::OnBegsam() 
{
	// TODO: Add your control notification handler code here
	CPulseeddyApp *app=(CPulseeddyApp*)AfxGetApp();
    m_ReadThread=AfxBeginThread(ReadThread,NULL, THREAD_PRIORITY_ABOVE_NORMAL, 0, CREATE_SUSPENDED);
    m_ReadThread->m_bAutoDelete=false;  
	m_DrawWindowThread=AfxBeginThread(DrawWindowProc,NULL, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
    m_DrawWindowThread->m_bAutoDelete=false;   
    hEvent = PCI2000_CreateSystemEvent();
	UpdateData(TRUE);
	int DeviceID = 0;    
    PCI2000_PARA_AD Para; 
	bDeviceRun=1;
	app->k_run1=1;
	km=0;
	ggd=0;
	app->kb=0;
	MB=0;
	Para.FirstChannel = m_shutongdao;
	Para.LastChannel =m_motongdao;
	Para.Frequence = m_pinlv;
	Para.Gains = m_zengyi;
	Para.TriggerMode = PCI2000_IN_TRIGGER;
	hDevice =PCI2000_CreateDevice(DeviceID);
	if(hDevice==INVALID_HANDLE_VALUE)	
	{ 		
		MessageBox("创建设备对象失败...","提醒您...", MB_ICONSTOP);		
	}
	CString      RootDir;
	TCHAR  exeFullPath[MAX_PATH] ;
    GetModuleFileName(NULL,exeFullPath,MAX_PATH);
    RootDir=(const char*)exeFullPath;
	CString      sPath;
	int        nPos;
	nPos=RootDir.ReverseFind ('\\');
	sPath=RootDir.Left (nPos);
	strNewFileName=sPath+_T("\\Data.txt");
    CProgressCtrl* pMB=(CProgressCtrl*)(GetDlgItem(IDC_PROGRESS2_MB));
    CProgressCtrl* pKB=(CProgressCtrl*)(GetDlgItem(IDC_PROGRESS1_KB));  
    CEdit* pWroteMB=(CEdit*)(GetDlgItem(IDC_MB));
    CEdit* pWroteKB=(CEdit*)(GetDlgItem(IDC_KB));  
	ULONG DiskFreeBytes=PCI2000_GetDiskFreeBytes(strNewFileName.Left(3)); 
	ULONG m_RemainMB=(ULONG)(DiskFreeBytes/(1024*1024*2));  
	CString str;
    if(m_RemainMB<2) 
	{
		MessageBox("硬盘剩余空间不足2M,存盘操作不能进行...","提醒您...",MB_ICONSTOP);
		return;
	}
	
	pMB->SetRange32(0,10);
	pMB->SetPos(0);
	pKB->SetRange(0,512);             
	pKB->SetPos(0);
	pWroteMB->SetWindowText("0");
	pWroteKB->SetWindowText("0");
	if(!m_bFirstCreateDeviceFile)
	{  
		if(!PCI2000_ReleaseFile(hFileObject))
		{
			MessageBox("释放文件对象失败...","提醒您...",MB_ICONSTOP);
		}
	} 
    hFileObject = PCI2000_CreateFileObject(hDevice, strNewFileName, PCI2000_modeCreate|PCI2000_modeWrite);  // 创建文件对象
	app->k_hFileObject=hFileObject;
	if(hFileObject == INVALID_HANDLE_VALUE)
	{
		MessageBox("创建文件对象失败...","提醒您...",MB_ICONSTOP);
		
	}	
	m_bFirstCreateDeviceFile=FALSE;  
	if(!PCI2000_InitDeviceProAD(hDevice, &Para))
	{	
		MessageBox("不明确的初始化错误...","提醒您...");
	} 
	app->k_hDevice=hDevice;
	app->k_run=1;
	BOOL bDeviceRun=TRUE; 
	ULONG LinearAddr, PhysAddr;
    if(!PCI2000_GetDeviceAddr(hDevice, &LinearAddr,&PhysAddr,0))
	{
		AfxMessageBox("baibai");
	}
	PCI2000_WriteRegisterULong(hDevice, LinearAddr,0x290,0x0000);
    m_ReadThread->ResumeThread();  
	m_DrawWindowThread->ResumeThread(); 
	SetTimer(1,10,NULL);
	
}

UINT ReadThread(PVOID hWnd)  
{
	int DeviceID = 0;
	HANDLE  hDevice;
	HANDLE hFileObject;
    CPulseeddyApp *app=(CPulseeddyApp*)AfxGetApp();
	hDevice=app->k_hDevice;
	
    ReadIndex = 0;
	SegmentCounts = 0;
	int bDeviceRun;
	
	while(app->k_run)  
	{   
		long   PerLsbVolt;
        PerLsbVolt =10000.0/4096;
		bDeviceRun=app->k_run;	
		
		while(TRUE)
		{
			if(PCI2000_GetDevStatusAD_Half(hDevice))		
			{
				break;
			}
			else
			{
				
				Sleep(1);
			}
		}
		if(!PCI2000_ReadDeviceProAD_Half(hDevice, InUserRegion[ReadIndex], SAMPLEDIAN))//2048//  // 从设备上读取8192个字的数据
		{
			AfxMessageBox("读数据出错...");
			return FALSE;
		}
		
		SetEvent(hEvent);			
		ReadIndex++;
		if(ReadIndex==MAX_SEGMENT) 
		{
			
			ReadIndex=0;
			
			
		}
		SegmentCounts++;
		
		
	}
	
	if(!PCI2000_ReleaseDeviceProAD(hDevice))
	{
		AfxMessageBox("释放AD部件失败");
	}
	
	if(!PCI2000_ReleaseDevice(hDevice)) 
	{
		AfxMessageBox("关闭设备失败...");
	}    
	AfxMessageBox("数据采集完毕,可以分析处理!");	
	return 1;
}
UINT DrawWindowProc(PVOID hWnd)  
{
	CPulseeddyApp *app=(CPulseeddyApp*)AfxGetApp();
	int i, j;
	int h=0;
	CurrentIndex=0;
	HANDLE  hDevice;
	HANDLE hFileObject;
	hDevice=app->k_hDevice;
	hFileObject=app->k_hFileObject;
	while(app->k_run)  
	{
		WaitForSingleObject(hEvent, INFINITE);
		j=SegmentCounts;
		for(i=0; i<j; i++)  
		{	
			PCI2000_WriteFile(hFileObject, InUserRegion[CurrentIndex],4096); 
			CurrentIndex++;
            h++;
			app->zijie=h;
            app->kb++;
			if(CurrentIndex==MAX_SEGMENT) 
			{
				CurrentIndex=0;
			}
		}
		SegmentCounts=SegmentCounts-j;
		if(SegmentCounts<0) SegmentCounts=0;
	}
	
	return TRUE;
}
void CPulseeddyView::OnStopsam() 
{
	// TODO: Add your control notification handler code here
	CPulseeddyApp *app=(CPulseeddyApp*)AfxGetApp();
    app->k_run=0;
	app->k_stop1=2;
	KillTimer(1);
	
}


void CPulseeddyView::OnTimebo() 
{
	// TODO: Add your control notification handler code here
	SetTimer(2,50,NULL);
	km=0;
	yuanzhi=0;
	waveflag=1;
	
}
void CPulseeddyView::OnStopwave() 
{
	// TODO: Add your control notification handler code here
     KillTimer(2);
	
}
void CPulseeddyView::OnTepin() 
{
	// TODO: Add your control notification handler code here
	int     N=SAMPLEDIAN;
    float   PerLsbVolt =10000.0/4096;
	int     a1=0;
	///////////////////////
	m_pSet->MoveFirst();
	CString nn;
	for(int b=0;b<SAMPLEDIAN/2;b++)
	{
       Real[b]=m_pSet->m_IData;
	   Imag[b]=0;
       m_pSet->MoveNext();
	}
	
	///////////////////////
	//**倒序
	
	int   Lh=N/2;
	int   j=Lh,N1=N-2;	
	for(int i=1;i<=N1;i++)
	{
		if(i<j)//**交换数据,包括实部和虚部
		{
			float a1,a2;
			a1=Real[i];
			a2=Imag[i];
			Real[i]=Real[j];
			Imag[i]=Imag[j];
			Real[j]=a1;
			Imag[j]=a2;
		}
		int t;
		t=Lh;
		while(j>=t)//**求解倒序值
		{
			j=j-t;
			t/=2;	
		}

		j+=t;
	}



	//**FFT变换
	int    B;
    int   M;
	if(SAMPLEDIAN/2==1024)
		M=10;
    else if(SAMPLEDIAN/2==2048)
		M=11;
    else if(SAMPLEDIAN/2==4096)
		M=12;
    else if(SAMPLEDIAN/2==8192)
		M=13;
	int   m_H;
	for(int L=1;L<=M;L++)//**循环M次
	{
		B=(int)pow(2,L-1);
		for(int J=0;J<=B-1;J++)//**旋转因子的个数对应蝶形运算
		{
			int  P;
			P=(int)pow(2,M-L)*J;//**旋转因子指数的计算
			for(int k=J;k<N-1;k+=(int)pow(2,L))
			{
				float   WReal,WImag;
				float   c,s,arg;
				int     k2;
				k2=k+B;//**蝶形的第二个坐标
				arg=(float)(2*Pi*P)/N;
				c=(float)cos(arg);
				s=(float)sin(arg);

				WReal=Real[k2]*c-Imag[k2]*s;
				WImag=Imag[k2]*c+Real[k2]*s;
				//**实现同址运算
				Real[k2]=Real[k]-WReal;
				Imag[k2]=Imag[k]-WImag;

				Real[k]=Real[k]+WReal;
				Imag[k]=Imag[k]+WImag;

			}
		
		}
	
	}
/////////////////////////////////////
    	
	for( i=0;i<SAMPLEDIAN/2;i++)
		FuZhi[i]=0;
	for(int k=0;k<SAMPLEDIAN/2;k++)
	{
		FuZhi[k]=(float)sqrt(Real[k]*Real[k]+Imag[k]*Imag[k]);
		float nn=FuZhi[k];
		if(max1<=nn)
		{
			max1=nn;
			m_H=k;
		}
	}
	float    h;
	h=2*(m_H/SAMPLEDIAN)*m_pinlv;
    m_H=m_H*10;
	char str[500];
	CString string;
	sprintf(str, "特征频率点的时间为:%dus 特征频率为:%fhz ",m_H,h);	 
	string=string+str;   
	MessageBox(string,"提醒您...");		
}

void CPulseeddyView::OnMaxval() 
{
	CPulseeddyApp *app=(CPulseeddyApp*)AfxGetApp();	
	m_pSet->MoveFirst();
	float  maxval=0; 
	for(int b=0;b<app->zijie;b++)
	{
       if(maxval<(m_pSet->m_RData))
	   {
		   maxval=m_pSet->m_RData;
	       maxduan=b;
	   }
	    m_pSet->MoveNext();
	}
    char str[500];
	CString string;
	sprintf(str, "磁场垂直分量的最大值为:%fmv ",maxval );	 
	string=string+str;   
	MessageBox(string,"提醒您...");
}

void CPulseeddyView::OnMaxvaltime() 
{
	// TODO: Add your control notification handler code here
    m_File.Open(strNewFileName, PCI2000_modeRead);
	int dian=maxduan*4096;
    m_File.Seek(dian,CFile::begin);  
	m_File.Read(m_InUserRegion,2048*2);
    float   PerLsbVolt;
	PerLsbVolt =10000.0/4096;
	for(int b1=0;b1<SAMPLEDIAN;b1++)
	{
		float  c1= ((m_InUserRegion[b1]&0x0FFF)*PerLsbVolt-5000);//0x800
		InUser[b1]=c1;	
	}
	float  maxvalue=0;
	for(int j=0;j<2048;j++)
	{
		if(maxvalue<=InUser[j])
		{
			maxvalue=InUser[j];
		    maxdian=j;
		}
		else 
			maxvalue=maxvalue;
	}
	for(int k=(maxdian-30);k<maxdian;k++)
	{
	   if(abs(InUser[k+1]-InUser[k])>=20)
	   {
          zerotime1=k;
		  break;
	   }

	}
	int zhi=(maxdian-zerotime1)*10;
	m_File.Close();
	char str[500];
	CString string;
	sprintf(str, "最大值出现的时间为:%dus ",zhi);	 
	string=string+str;   
	MessageBox(string,"提醒您...");
	
}

void CPulseeddyView::OnZerotime() 
{
	// TODO: Add your control notification handler code here

	int  zerotime2=0;
	int  zerotime;
    for(int k=maxdian;k<SAMPLEDIAN;k++)
	{
	 if(InUser[k]<=0)
	 {
	      zerotime2=k;
		  break;
	 }
	}
	zerotime=(zerotime2-zerotime1)*10;
	char str[500];
	sprintf(str, "过零点的时间为:%dus",zerotime);	 
	MessageBox(str,"提醒您...");
	
}

void CPulseeddyView::OnPinputu() 

⌨️ 快捷键说明

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