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

📄 pci8407dlg.cpp

📁 用MFC编的PCI总线数据采集卡:主要用在工业自动化.测试电磁阀的响应频率程序
💻 CPP
字号:
// pci8407Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "pci8407.h"
#include "pci8407Dlg.h"
#include"pci8kp.h"
#include <sys/timeb.h>//4
#include <time.h>//5

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
const int WM_THREADPROCESSFUNCTIONEND = WM_USER + 1;

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPci8407Dlg dialog

CPci8407Dlg::CPci8407Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPci8407Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPci8407Dlg)
	m_s = _T("");
	m_T1 = 100.0;
	m_T2 = 50.0;
	i=0;
	m_bButton1Type=1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPci8407Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPci8407Dlg)
	DDX_Control(pDX, IDC_BUTTON1, m_cButton1);
	DDX_Text(pDX, IDC_EDIT1, m_s);
	DDX_Text(pDX, IDC_TIME1, m_T1);
	DDX_Text(pDX, IDC_T2, m_T2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPci8407Dlg, CDialog)
	//{{AFX_MSG_MAP(CPci8407Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_CLOSE()
	ON_WM_TIMER()
    ON_MESSAGE(WM_UPDATETIME,OnUpdateTime)//3.映射消息并定义相应函数
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
	ZT_PCIBOARD pci8407;

//更新时间
void CPci8407Dlg::OnUpdateTime()
{   

	LARGE_INTEGER litmp;  
    LONGLONG QPart1,QPart3;  
    double dfMinus, dfFreq, dfTim;  
    QueryPerformanceFrequency(&litmp);  
    dfFreq = (double)litmp.QuadPart;         // 获得计数器的时钟频率 
	
	double freq2=m_T2/10000;
   

	//CString str;	   
    pci8407.lIndex = 0;
	pci8407.lCode = 0;
	pci8407.nCh = 2;//选择通道1
	pci8407.lData=1; 
  
    ZT8407DO(&pci8407);

    i++;

    m_s.Format("%d",i);
	this->UpdateData(FALSE);

   	QueryPerformanceCounter(&litmp);  
    QPart1 = litmp.QuadPart;

	 do  
	{  
      QueryPerformanceCounter(&litmp);  
      QPart3 = litmp.QuadPart;             //获得中止值  
      dfMinus = (double)(QPart3-QPart1);  
      dfTim = dfMinus / dfFreq;            // 获得对应的时间值,单位为秒  
	} while(dfTim<freq2);                   //延时一定时间


  //  for(int j=0;j<380000;j++);   //延时一定时间
//	::Sleep(55);
	pci8407.lData=0;
    ZT8407DO(&pci8407);
}
/////////////////////////////////////////////////////////////////////////////
// CPci8407Dlg message handlers

//定义线程函数
UINT ShowTimeProc(LPVOID lParam);
UINT ShowTimeProc(LPVOID lParam)
{

    CPci8407Dlg*pdlg=(CPci8407Dlg*)lParam;
	double m_T1=20;
	double freq=m_T1/10000;
    LARGE_INTEGER litmp;  
    LONGLONG QPart1,QPart2;  
    double dfMinus, dfFreq, dfTim;  
    QueryPerformanceFrequency(&litmp);  
    dfFreq = (double)litmp.QuadPart;         // 获得计数器的时钟频率   

	while(TRUE)
	{
		//上次的中止值变为新的起始值
	QueryPerformanceCounter(&litmp);  
    QPart1 = litmp.QuadPart;                 // 获得初始值
		//发送消息同志对话框更新时间
		::SendMessage(pdlg->m_hWnd,WM_UPDATETIME,0,0);
	 do  
	{  
      QueryPerformanceCounter(&litmp);  
      QPart2 = litmp.QuadPart;             //获得中止值  
      dfMinus = (double)(QPart2-QPart1);  
      dfTim = dfMinus / dfFreq;            // 获得对应的时间值,单位为秒  
	} while(dfTim<freq);  
   // break;
	   }
	return 0;
}


BOOL CPci8407Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//打开控制卡
  	 pci8407.lIndex = 0;
	 if(OpenDevicePlx(&pci8407) != 0)
	AfxMessageBox("Open device failed");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPci8407Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPci8407Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPci8407Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPci8407Dlg::OnButton1() 
{	
	UpdateData();  
	if (m_bButton1Type==1)
	{
    m_bButton1Type=2;
    m_cButton1.SetWindowText("停止运行");
  
  m_bThreadProcessThread=AfxBeginThread(ThreadProcessFunction,this,0,0,0,NULL); 	//开启一个线程
	}
	else if(m_bThreadProcessThread) 
	{     
     m_bButton1Type=1;
	 TerminateThread(m_bThreadProcessThread->m_hThread,0); //中止线程
     m_cButton1.SetWindowText("开始运行");
  	}  
	

 //  this->SetTimer(1,30,NULL);
}

void CPci8407Dlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
    this->KillTimer(1);
	CloseDevicePlx(&pci8407);
	CDialog::OnClose();
}

void CPci8407Dlg::OnTimer(UINT nIDEvent) 
{
    //int i;
	CString str;
/*	pci8407.lIndex = 0;
	pci8407.lCode = 0;
	pci8407.nCh = 1;//选择通道1
	pci8407.lData=1;
    ZT8407DO(&pci8407);*/
	//str.Format("%d",i);
    //this->m_s=str;
	//this->UpdateData(FALSE);

	pci8407.lIndex = 0;
	pci8407.lCode = 1;
	//pci8407.nCh = 1;//选择通道1
	pci8407.lData=5;
    ZT8407DO(&pci8407);

	::Sleep(10);
	pci8407.lData=0;
    ZT8407DO(&pci8407);
	CDialog::OnTimer(nIDEvent);
}
UINT CPci8407Dlg::ThreadProcessFunction(LPVOID pParam)//必须是静态函数
{
	CPci8407Dlg *pthis=(CPci8407Dlg*)pParam;
	try
	{
		pthis->ProcessWorkFunction();
	}
	catch(COleDispatchException *e)
	{
		e->ReportError();
		e->Delete();
		ExitProcess(1);
	}

    pthis->PostMessage(WM_THREADPROCESSFUNCTIONEND);
	return  0;
}

void CPci8407Dlg::ProcessWorkFunction()
{	
	double freq1=m_T1/10000;
    double freq2=m_T2/10000;
    LARGE_INTEGER litmp;  
    LONGLONG QPart1,QPart2;  
    double dfMinus, dfFreq, dfTim;  
    QueryPerformanceFrequency(&litmp);  
    dfFreq = (double)litmp.QuadPart;         // 获得计数器的时钟频率   

	while(TRUE)
	{
		//上次的中止值变为新的起始值
	QueryPerformanceCounter(&litmp);  
    QPart1 = litmp.QuadPart;                 // 获得初始值
	::SendMessage(m_hWnd,WM_UPDATETIME,0,0);  	//发送消息同志对话框更新时间

/*	long  i=0;
	    i++;
	//CString str;	   
    pci8407.lIndex = 0;
	pci8407.lCode = 0;
	pci8407.nCh = 2;//选择通道1
	pci8407.lData=1; 
  
    ZT8407DO(&pci8407);

   
   if (i>500)
   {
    m_s.Format("%d",i);
	this->UpdateData(FALSE);
   }
	
     do  
	{  
      QueryPerformanceCounter(&litmp);  
      QPart3 = litmp.QuadPart;             //获得中止值  
      dfMinus = (double)(QPart3-QPart1);  
      dfTim = dfMinus / dfFreq;            // 获得对应的时间值,单位为秒  
	} while(dfTim<freq2);                   //延时一定时间
//	::Sleep(55);
	pci8407.lData=0;
    ZT8407DO(&pci8407);
*/
	 do  
	{  
      QueryPerformanceCounter(&litmp);  
      QPart2 = litmp.QuadPart;             //获得中止值  
      dfMinus = (double)(QPart2-QPart1);  
      dfTim = dfMinus / dfFreq;            // 获得对应的时间值,单位为秒  
	} while(dfTim<freq1);  
   // break;
	}
    
	
}

⌨️ 快捷键说明

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