?? watertankdlg.cpp
字號:
// WatertankDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Watertank.h"
#include "WatertankDlg.h"
#include "Windows.h"
#include "stdio.h"
#include "math.h"
#include "Mserial15.h"
#include "PidControl.h"
#include "Reference.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define a 0.0625
#define b -20.9866
////////////////////////////////////////////////////////////////////////////
IOPort m_PCI2006_0;//定義0號數據采集卡
IOPort m_PCI2006_1;//定義1號數據采集卡
Produce_WhiteNoise WhiteNoise;
CMserial Mserial;
DataSave Save_Data;
CMserial15 Mserial15;
CReference Reference;
double ADValue0[3];//存儲0#采集卡采集出來的AD值
double DAValue0[2];
double DAValue1[2];
double ADValue00=0;
double buffer[2];
double reference;
BOOL m_bbuffer=TRUE;
UINT Time_Proc(LPVOID lParam);//采集定時器線程
VOID Samp_Proc();//采樣子程序
VOID View_Proc();//文本框顯示子程序
UINT Time_Proc1(LPVOID lParam);
UINT Time_Proc2(LPVOID lParam);
int in=0;//存儲數據時的數據序列
CPidControl pid;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CWatertankDlg dialog
CWatertankDlg::CWatertankDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWatertankDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWatertankDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWatertankDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWatertankDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWatertankDlg, CDialog)
//{{AFX_MSG_MAP(CWatertankDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_STARTD, OnStartd)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_SAVADATA, OnSavadata)
ON_BN_CLICKED(IDC_AddWhiteNoise, OnAddWhiteNoise)
ON_BN_CLICKED(IDC_STOPSAVEDATA, OnStopsavedata)
ON_MESSAGE(WM_MYMESSAGE,OnMyMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWatertankDlg message handlers
BOOL CWatertankDlg::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
// determine the rectangle for the control
CRect rect;
GetDlgItem(IDC_OSCOPE)->GetWindowRect(rect) ;
ScreenToClient(rect) ;
// create the control
m_OScopeCtrl.Create(WS_VISIBLE | WS_CHILD, rect, this) ;
// customize the control
m_OScopeCtrl.SetRange(-1, 1, 2) ;
m_OScopeCtrl.SetYUnits("液位值") ;
m_OScopeCtrl.SetXUnits("Samples (Windows Timer: 50 msec)") ;
m_OScopeCtrl.SetBackgroundColor(RGB(0, 0, 64)) ;
m_OScopeCtrl.SetGridColor(RGB(192, 192, 255)) ;
m_OScopeCtrl.SetPlotColor(RGB(255, 255, 255)) ;
//------
StartWhiteNoise=FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CWatertankDlg::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 CWatertankDlg::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 CWatertankDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LRESULT CWatertankDlg:: OnMyMessage(WPARAM wParam, LPARAM lParam)
{
CString str,str1;
GetDlgItemText(IDC_EDIT4,str);
GetDlgItemText(IDC_EDIT5,str1);
//atof(str1);//Signal.Produce();//atof(str1);
DAValue1[1]=0;
if(StartWhiteNoise==FALSE)
DAValue1[0]=atof(str);
else
{
reference=atof(str1)+Reference.produce();//atof(str1);
DAValue1[0]=Mserial.Produce_Mserial();//WhiteNoise.ProduceWhiteNoise();//pid.CalcU(ADValue00*a+b,reference);//DAValue1[0]=Signal.Produce();//Reference.produce();atof(str)+Mserial.Produce_Mserial()*0.16;//+WhiteNoise.ProduceWhiteNoise();
}
m_PCI2006_1.WriteData(DAValue1);//;
DAValue0[0]=0;
DAValue0[1]=atof(str1);//Mserial15.Produce_Mserial()*0.2+3.5;//atof(str);
m_PCI2006_0.WriteData(DAValue0);
return 0;
}
void CWatertankDlg::OnStartd()
{
m_PCI2006_0.Open(0);
m_PCI2006_1.Open(1);
m_PCI2006_1.reset();
m_PCI2006_0.reset();
m_hwind=AfxBeginThread(Time_Proc,this);//開始數據采集線程
m_hwind1=AfxBeginThread(Time_Proc1,this);//開始輸入線程
SetTimer(1,1000,NULL);//開始曲線顯示,打開出水閥門,顯示輸出值
// TODO: Add your control notification handler code here
}
UINT Time_Proc(LPVOID lParam)
{
LARGE_INTEGER litmp;
LONGLONG QPart1,QPart2;
double dfMinus, dfFreq, dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq = (double)litmp.QuadPart; // 獲得計數器的時鐘頻率
QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart; // 獲得初始值
QPart2=QPart1;
while(TRUE)
{
QPart1 = QPart2;// 上一次的終止值變成新的起始值
//-----------------------------------------------------------------------
Samp_Proc();
//-----------------------------------------------------------------------
do{
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;// 獲得中止值
dfMinus = (double)(QPart2-QPart1);
dfTim = dfMinus / dfFreq; // 獲得對應的時間值,單位為秒
}while(dfTim<0.001);
}
return 0;
}
UINT Time_Proc1(LPVOID lParam)
{
CWatertankDlg *pDlg=(CWatertankDlg *)lParam;
LARGE_INTEGER litmp;
LONGLONG QPart1,QPart2;
double dfMinus, dfFreq, dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq = (double)litmp.QuadPart; // 獲得計數器的時鐘頻率
QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart; // 獲得初始值
QPart2=QPart1;
while(TRUE)
{
QPart1 = QPart2;// 上一次的終止值變成新的起始值
//-----------------------------------------------------------------------
SendMessage(pDlg->m_hWnd,WM_MYMESSAGE,0,0);
//-----------------------------------------------------------------------
do{
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;// 獲得中止值
dfMinus = (double)(QPart2-QPart1);
dfTim = dfMinus / dfFreq; // 獲得對應的時間值,單位為秒
}while(dfTim<1);
}
return 0;
}
VOID Samp_Proc()
{
double sum=0;
if(m_PCI2006_1.IsPortOpen())//step1:檢測設備是否啟動
{
m_PCI2006_1.ReadData(ADValue0);//step2:開始采集數據
}
if(m_bbuffer==TRUE)
{
for(int i=0;i<2;i++)
buffer[i]=ADValue0[0];
m_bbuffer=FALSE;
}
for(int j=1;j<2;j++)
{
sum=sum+buffer[j];
buffer[j-1]=buffer[j];
}
sum=sum+ADValue0[0];
buffer[1]=ADValue0[0];
ADValue00=sum/2.0;
}
VOID View_Proc()
{
int i;
CString str[3];
if(m_PCI2006_1.IsPortOpen())
{
for(i=0;i<3;i++)
{
str[i].Format("%0.1f",ADValue00*a+b);
}
SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT3,str[0]);
//SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT2,str[1]);
// SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT1,str[2]);
}
}
void CWatertankDlg::OnStop()
{
// TODO: Add your control notification handler code here
m_hwind->ExitInstance();//停止數據采集
m_hwind1->ExitInstance();//停止輸入
KillTimer(1);//停止顯示曲線,出水閥門關閉,輸出值顯示停止;
m_PCI2006_0.Close();//關閉設備0
m_PCI2006_1.Close();//關閉設備1
}
void CWatertankDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
m_OScopeCtrl.AppendPoint(DAValue1[0]);//曲線顯示1#液位值ADValue00*a+b
//---------------
View_Proc();
//---------------
CDialog::OnTimer(nIDEvent);
}
void CWatertankDlg::OnSavadata()
{
// TODO: Add your control notification handler code here
Save_Data.Save_Initial();
m_hwind2=AfxBeginThread(Time_Proc2,this);
}
UINT Time_Proc2(LPVOID lParam)
{
LARGE_INTEGER litmp;
LONGLONG QPart1,QPart2;
double dfMinus, dfFreq, dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq = (double)litmp.QuadPart; // 獲得計數器的時鐘頻率
QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart; // 獲得初始值
QPart2=QPart1;
while(TRUE)
{
QPart1 = QPart2;// 上一次的終止值變成新的起始值
//-----------------------------------------------------------------------
in++ ;
Save_Data.Save(in,DAValue1[0],ADValue00*a+b,DAValue0[1],reference);
//-----------------------------------------------------------------------
do{
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;// 獲得中止值
dfMinus = (double)(QPart2-QPart1);
dfTim = dfMinus / dfFreq; // 獲得對應的時間值,單位為秒
}while(dfTim<1);
}
return 0;
}
void CWatertankDlg::OnAddWhiteNoise()
{
// TODO: Add your control notification handler code here
Signal.Initial();
StartWhiteNoise=TRUE;
WhiteNoise.Initial();
}
void CWatertankDlg::OnStopsavedata()
{
// TODO: Add your control notification handler code here
m_hwind2->ExitInstance();//停止存儲
// Save_Data.Save_end();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -