應(yīng)用程序設(shè)計實驗報告2完成AD數(shù)據(jù)采集的數(shù)據(jù)庫管理_第1頁
應(yīng)用程序設(shè)計實驗報告2完成AD數(shù)據(jù)采集的數(shù)據(jù)庫管理_第2頁
應(yīng)用程序設(shè)計實驗報告2完成AD數(shù)據(jù)采集的數(shù)據(jù)庫管理_第3頁
應(yīng)用程序設(shè)計實驗報告2完成AD數(shù)據(jù)采集的數(shù)據(jù)庫管理_第4頁
應(yīng)用程序設(shè)計實驗報告2完成AD數(shù)據(jù)采集的數(shù)據(jù)庫管理_第5頁
已閱讀5頁,還剩27頁未讀, 繼續(xù)免費閱讀

付費下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

實驗報告課程名稱計算機(jī)測控技術(shù)實驗項目完成AD數(shù)據(jù)采集的數(shù)據(jù)庫管理實驗儀器VisualC++6.0系別儀器科學(xué)與光電工程專業(yè)測控技術(shù)與儀器班級/學(xué)號學(xué)生姓名實驗日期成績指導(dǎo)教師實驗2一、實驗名稱:完成AD數(shù)據(jù)采集的數(shù)據(jù)庫管理二、實驗?zāi)康模赫莆諗?shù)據(jù)庫應(yīng)用程序的設(shè)計與開發(fā)三、實驗內(nèi)容:1、創(chuàng)建數(shù)據(jù)源2、連接數(shù)據(jù)庫3、進(jìn)行數(shù)據(jù)操作【實驗】結(jié)合實驗1編寫一個數(shù)據(jù)庫應(yīng)用程序,將實驗1所采集的數(shù)據(jù)存入數(shù)據(jù)庫中。數(shù)據(jù)庫中記錄包含的字段有“實驗次數(shù)”、“采樣點”、“波形幅值”、“采樣時間”,編寫應(yīng)用程序,使它具備按每一個字段進(jìn)行查詢的功能。編程思路及參考程序:1.創(chuàng)建基于對話框的項目,項目名稱為自定,假定名稱為MyProject。2.本例題介紹另外一種數(shù)據(jù)庫訪問方式,即使用ADO來進(jìn)行數(shù)據(jù)訪問,ADOX進(jìn)行創(chuàng)建數(shù)據(jù)庫操作,使用普通Windows控件來顯示數(shù)據(jù)。首先需要在stdafx.h中添加對ADO組件的引用:#import"C:\ProgramFiles\CommonFiles\system\ado\msadox.dll"#import"C:\ProgramFiles\CommonFiles\System\ado\msado15.dll"no_namespacerename("EOF","adoEOF")由于ADO是COM組件,因此程序需要添加對COM的支持。BOOLCMyProjectApp::InitInstance(){//初始化COM環(huán)境If(FAILED(::CoInitialize(NULL))) returnFALSE;AfxEnableControlContainer();… returnFALSE;}intCMyProjectApp::ExitInstance(){ //釋放COM環(huán)境::CoUninitialize(); returnCWinApp::ExitInstance();}其中ExitInstance方法需要通過ClassWizard重載來添加。3.添加ADO成員。為正常使用ADO,為CMyProjectDlg類添加ADO成員。ADOX::CatalogPtrm_pCatalog;//ADOX對象,用于建表_ConnectionPtrm_pConnection;//數(shù)據(jù)庫連接對象,用于保持連接_RecordsetPtrm_pRs;//結(jié)果集對象,用于訪問數(shù)據(jù)庫4.初次使用ADO時,難免遇到各種錯誤,因此程序中需要判斷函數(shù)調(diào)用返回值和捕獲例外,因此先實現(xiàn)兩個用于錯誤處理的成員函數(shù)。voidDumpComError(_com_error&e);//顯示ADO錯誤消息voidTestComError(HRESULThr);//檢測HRESULT值voidCMyProjectDlg::TestComError(HRESULThr){If(FAILED(hr)) _com_issue_error(hr);//顯示HRESULT錯誤消息}voidCMyProjectDlg::DumpComError(_com_error&e){CStringErrorStr; _bstr_tbstrSource(e.Source()); _bstr_tbstrDescription(e.Description()); ErrorStr.Format("Error\n\t代碼=%08lx\n\t代碼表示=%s\n\t來源=%s\n\t描述=%s\n",e.Error(),e.ErrorMessage(),(LPCSTR)bstrSource,(LPCSTR)bstrDescription);#ifdef_DEBUG AfxMessageBox(ErrorStr,MB_OK|MB_ICONERROR);#endif}5.接下來關(guān)注初始化過程。程序運行時,先檢查磁盤上是否存在該數(shù)據(jù)庫,如果不存在則創(chuàng)建數(shù)據(jù)庫以及表,并初始化10條記錄。建立ADO連接的函數(shù)如下:voidInitConnection();//建立ADO連接voidCMyProjectDlg::InitConnection(){HRESULThr=m_pConnection.CreateInstance(__uuidof(Connection)); //創(chuàng)建連接對象實例 TestComError(hr); hr=m_pConnection->Open( _bstr_t(L"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=./MyDB.mdb;"), _bstr_t(L""), _bstr_t(L""), adModeUnknown);//建立連接 TestComError(hr);}6.打開數(shù)據(jù)庫表。建立連接之后可以使用如下方法打開表:BOOLOpenTable();//打開一張表BOOLCMyProjectDlg::OpenTable(){ HRESULThr=S_OK; If(m_pRs==NULL) hr=m_pRs.CreateInstance(__uuidof(Recordset)); TestComError(hr);hr=m_pRs->Open("MyTable",_variant_t((IDispatch*)m_pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable);//打開表 If(m_pRs->adoEOF||m_pRs->BOF)//如果表為空則不再進(jìn)行任何操作 returnTRUE; m_pRs->MoveFirst();//移動到開頭 returnTRUE;}7.添加新的記錄。打開一張表之后,需要通過添加、更新、賦值等操作來向數(shù)據(jù)庫中插入新的記錄。BOOLAddNew();//添加新記錄BOOLUpdate();//更新voidClose();//關(guān)閉//為fieldName字段賦值fieldValueBOOLPutValue(LPCTSTRfieldName,_variant_tfieldValue);BOOLPutValue(LPCTSTRfieldName,longfieldValue);BOOLPutValue(LPCTSTRfieldName,COleDateTimefieldValue);BOOLPutValue(LPCTSTRfieldName,LPCTSTRfieldValue);BOOLCMyProjectDlg::AddNew(){ If(m_pRs->AddNew()!=S_OK) returnFALSE; returnTRUE;}BOOLCMyProjectDlg::Update(){ BOOLbret; try { If(m_pRs->Update()!=S_OK) bret=FALSE; } catch(_com_error&e) { DumpComError(e); bret=FALSE; }if(!bret) m_pRs->CancelUpdate(); returnbret;}voidCMyProjectDlg::Close(){ if(IsOpen()) m_pRs->Close();}BOOLCMyProjectDlg::PutValue(LPCTSTRfieldName,_variant_tfieldValue){ try { m_pRs->Fields->GetItem(fieldName)->PutValue(fieldValue); returnTRUE; } catch(_com_error&e) { DumpComError(e); returnFALSE; }}BOOLCMyProjectDlg::PutValue(LPCTSTRfieldName,longfieldValue){ returnPutValue(fieldName,_variant_t(fieldValue));}BOOLCMyProjectDlg::PutValue(LPCTSTRfieldName,COleDateTimefieldValue){ _variant_tvt; vt.vt=VT_DATE; vt.date=fieldValue; returnPutValue(fieldName,vt);}BOOLCMyProjectDlg::PutValue(LPCTSTRfieldName,LPCTSTRfieldValue){ returnPutValue(fieldName,_variant_t(fieldValue));}有了這些基本函數(shù),可以使用ADOX來創(chuàng)建數(shù)據(jù)庫和表:voidCreateDBAndTable();//創(chuàng)建數(shù)據(jù)庫和表voidCMyProjectDlg::CreateDBAndTable(){HRESULThr;try{ hr=m_pCatalog.CreateInstance(__uuidof(ADOX::Catalog));//創(chuàng)建Catalog對象 TestComError(hr); CFiledb; //判斷數(shù)據(jù)庫是否已經(jīng)存在 if(db.Open("./MyDB.mdb",CFile::modeRead|CFile::shareDenyWrite)) db.Close();//關(guān)閉文件 else{m_pCatalog->Create("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=./MyDB.mdb;");//數(shù)據(jù)庫不存在則創(chuàng)建該數(shù)據(jù)庫 ADOX::_TablePtrm_pTable=NULL;//分別創(chuàng)建表hr=m_pTable.CreateInstance(__uuidof(ADOX::Table)); TestComError(hr); m_pTable->PutName("MyTable");//設(shè)置表名 //添加列m_pTable->Columns->Append("實驗次數(shù)",ADOX::adInteger,0);//整數(shù)類型 m_pTable->Columns->Append("采樣點",ADOX::adInteger,0);//整數(shù)類型 m_pTable->Columns->Append("波形幅值",ADOX::adInteger,0);//整數(shù)類型 m_pTable->Columns->Append("采樣時間",ADOX::adDate,0);//日期類型 m_pCatalog->Tables->Append(_variant_t((IDispatch*)m_pTable));//添加表 InitConnection();//初始化ADO連接 OpenTable();//打開一張表//插入新記錄,例子 for(inti=0;i<10;i++){ AddNew(); PutValue("實驗次數(shù)",i); PutValue("采樣時間",COleDateTime::GetCurrentTime()); Update(); } Close();}}catch(_com_error&e){ DumpComError(e); }}8.不要忘了構(gòu)造和析夠函數(shù)中資源的管理問題:CMyProjectDlg::CMyProjectDlg(CWnd*pParent/*=NULL*/):CDialog(CMyProjectDlg::IDD,pParent){… m_pCatalog=NULL; m_pConnection=NULL; m_pRs=NULL;}CMyProjectDlg::~CMyProjectDlg(){ m_pConnection->Close();}析夠函數(shù)需要手工添加。在OnInitDialog函數(shù)中需要添加創(chuàng)建數(shù)據(jù)庫、填充控件的操作。BOOLCMyProjectDlg::OnInitDialog(){… CreateDBAndTable();//創(chuàng)建數(shù)據(jù)庫和表 if(!IsConnected())//重新連接數(shù)據(jù)庫 InitConnection(); if(!IsOpen())//打開結(jié)果集 OpenTable(); InitList();//初始化各個控件 UpdateList();//更新列表 returnTRUE;}斜體的函數(shù)將會在后面實現(xiàn)。9.判斷連接和打開:BOOLIsConnected();//判斷數(shù)據(jù)庫是否連接BOOLIsOpen();//判斷結(jié)果集是否打開BOOLCMyProjectDlg::IsConnected(){ if(m_pConnection) returnm_pConnection->GetState()!=adStateClosed; returnFALSE;}BOOLCMyProjectDlg::IsOpen(){ if(m_pRs) returnm_pRs->GetState()!=adStateClosed; returnFALSE;}10.下面實現(xiàn)的函數(shù)用于從結(jié)果集中獲取字段值(為了顯示方便,無論字段是什么類型都轉(zhuǎn)化為字符串來顯示):BOOLGetFieldValue(LPCTSTRlpFieldName,CString&strValue);CStringGetFieldValue(LPCTSTRlpFieldName);BOOLCMyProjectDlg::GetFieldValue(LPCTSTRlpFieldName,CString&strValue){ CStringstr=_T(""); _variant_tvtFld; try { //獲得字段值 vtFld=m_pRs->Fields->GetItem(lpFieldName)->Value; //根據(jù)值的類型來進(jìn)行相應(yīng)的轉(zhuǎn)化 switch(vtFld.vt) { caseVT_R4: str.Format("%.3f",vtFld.fltVal); break; caseVT_R8: str.Format("%.6f",vtFld.dblVal); break; caseVT_BSTR: str=vtFld.bstrVal; break; caseVT_I2: caseVT_UI1: str.Format("%d",vtFld.iVal); break; caseVT_INT: str.Format("%d",vtFVal); break; caseVT_I4: str.Format("%d",vtFld.lVal); break; caseVT_UI4: str.Format("%d",vtFld.ulVal); break; caseVT_DECIMAL: { doubleval=vtFld.decVal.Lo32; val*=(vtFld.decVal.sign==128)?-1:1; val/=pow(10,vtFld.decVal.scale); str.Format("%.6f",val); } break; caseVT_DATE: { COleDateTimedt(vtFld); CStringstrDateFormat=_T("%Y-%m-%d%H:%M:%S"); str=dt.Format(strDateFormat); } break; caseVT_EMPTY: caseVT_NULL: str.Empty(); break; default: str.Empty(); returnFALSE; } strValue=str; returnTRUE; }catch(_com_error&e) { DumpComError(e); returnFALSE; }}CStringCMyProjectDlg::GetFieldValue(LPCTSTRlpFieldName){ CStringstr; GetFieldValue(lpFieldName,str); returnstr;}11.設(shè)計對話框模板。對話框樣式屬性如圖2-1所示,各控件屬性如表2-1所示。圖2-1對話框界面表2-1控件屬性ComboBox初始數(shù)據(jù)如表2-2所示。表2-2ComboBox控件的初始值控件查詢條件排序條件升序/降序數(shù)據(jù)實驗次數(shù)實驗次數(shù)升序采樣點采樣點降序波形幅值波形幅值采樣時間采樣時間12.實現(xiàn)控件控制函數(shù)。InitList函數(shù)負(fù)責(zé)初始化各個控件。UpdateList函數(shù)負(fù)責(zé)遍歷結(jié)果集來填充列表控件。voidInitList();//初始化各個控件voidUpdateList();//更新列表控件voidCMyProjectDlg::InitList(){//設(shè)置列表的表頭和寬度 staticconstchar*columns[]={"實驗次數(shù)","采樣點","波形幅值","采樣時間"}; staticconstintwidths[]={40,40,80,130}; for(inti=0;i<4;i++){ m_List.InsertColumn(i,columns[i]); m_List.SetColumnWidth(i,widths[i]); } //初始化ComboBox的初選值 m_Order="實驗次數(shù)"; m_Query="實驗次數(shù)"; m_Updown.SetCurSel(0); UpdateData(FALSE);}voidCMyProjectDlg::UpdateList(){try{ m_pRs->MoveFirst();//移動到第一個位置 m_List.DeleteAllItems();//清空列表框 intindex=0; while(!m_pRs->adoEOF)//遍歷{ staticconstchar*columns[]={"實驗次數(shù)","采樣點","波形幅值","采樣時間"}; CStringnum; num.Format("%d",index); m_List.InsertItem(index,num);//插入一行 for(inti=0;i<4;i++)//設(shè)置該行的各個字段 m_List.SetItemText(index,i,GetFieldValue(columns[i])); m_pRs->MoveNext();//移動到下一個位置 index++;}} catch(_com_errore){ DumpComError(e); }}13.最后實現(xiàn)查詢排序功能。這里采用最簡單的SQL拼接方法來根據(jù)控件的內(nèi)容查詢。首先實現(xiàn)查詢SQL的函數(shù):BOOLOpenCommand(CStringsql);//查詢指定的SQLBOOLCMyProjectDlg::OpenCommand(CStringsql){HRESULThr=S_OK; if(m_pRs==NULL) hr=m_pRs.CreateInstance(__uuidof(Recordset)); TestComError(hr); //根據(jù)SQL查詢并返回結(jié)果集hr=m_pRs->Open((LPCTSTR)sql,_variant_t((IDispatch*)m_pConnection,true), adOpenStatic,adLockReadOnly,adCmdText); if(m_pRs->adoEOF||m_pRs->BOF)//如果表為空則不再進(jìn)行任何操作 returnTRUE; m_pRs->MoveFirst();//移動到開頭 returnTRUE;}接下來實現(xiàn)按鈕的響應(yīng)操作:voidCMyProjectDlg::OnButton1(){ try{ CStringquery="SELECT*FROMMyTable";//拼接SQL字符串 UpdateData(); m_pRs->Close();//關(guān)閉原結(jié)果集 m_Value.TrimLeft();//過濾查詢值 m_Value.TrimRight(); if(m_Value.GetLength()!=0){ CStringcriteria;//如果有查詢值則添加WHERE子句 criteria=m_Query+"="+m_Value+"";//只支持“=”比較 query+="WHERE"+criteria; } CStringorder=m_Order+"";//添加ORDERBY子句 if(m_Updown.GetCurSel()==0)//判斷升序還是降序 order+="ASC"; else order+="DESC"; query+="ORDERBY"+order; OpenCommand(query);//執(zhí)行查詢 UpdateList();//更新列表 } catch(_com_errore){DumpComError(e); }}實驗程序:virtualOscilloscopeDlg.h//virtualOscilloscopeDlg.h:headerfile//#if!defined(AFX_VIRTUALOSCILLOSCOPEDLG_H__1BEC7015_7348_4493_BF45_37A5D24CEE4D__INCLUDED_)#defineAFX_VIRTUALOSCILLOSCOPEDLG_H__1BEC7015_7348_4493_BF45_37A5D24CEE4D__INCLUDED_#if_MSC_VER>1000#pragmaonce#endif//_MSC_VER>1000#include"scope.h"#include"SoundIn.h"http:///////////////////////////////////////////////////////////////////////////////CVirtualOscilloscopeDlgdialogclassCVirtualOscilloscopeDlg:publicCDialog{//Constructionpublic: ADOX::_CatalogPtrm_pCatalog;//ADOX對象,用于建表_ConnectionPtrm_pConnection;//數(shù)據(jù)庫連接對象,用于保持連接_RecordsetPtrm_pRs;//結(jié)果集對象,用于訪問數(shù)據(jù)庫 CVirtualOscilloscopeDlg(CWnd*pParent=NULL); //standardconstructor voidscope(intnTime,intdata); voidDumpComError(_com_error&e);//顯示ADO錯誤消息voidTestComError(HRESULThr);//檢測HRESULT值voidInitConnection();//建立ADO連接BOOLOpenTable();//打開一張表 BOOLAddNew();//添加新記錄BOOLUpdate();//更新voidClose();//關(guān)閉//為fieldName字段賦值fieldValueBOOLPutValue(LPCTSTRfieldName,_variant_tfieldValue);BOOLPutValue(LPCTSTRfieldName,longfieldValue);BOOLPutValue(LPCTSTRfieldName,COleDateTimefieldValue);BOOLPutValue(LPCTSTRfieldName,LPCTSTRfieldValue); voidCreateDBAndTable(); BOOLIsConnected();//判斷數(shù)據(jù)庫是否連接BOOLIsOpen();//判斷結(jié)果集是否打開 BOOLGetFieldValue(LPCTSTRlpFieldName,CString&strValue); CStringGetFieldValue(LPCTSTRlpFieldName); voidInitList();//初始化各個控件voidUpdateList();//更新列表控件 BOOLOpenCommand(CStringsql);~CVirtualOscilloscopeDlg(); CSoundInm_soundIn;//DialogData //{{AFX_DATA(CVirtualOscilloscopeDlg) enum{IDD=IDD_VIRTUALOSCILLOSCOPE_DIALOG}; CComboBox m_Updown; CListCtrl m_List; CButton m_clear4; CButton m_clear3; CButton m_clear2; CButton m_clear1; CScopem_scope; CString m_Query; CString m_Value; CString m_Order; int m_sd; int m_hz; CString m_fuzhi; int m_bit; //}}AFX_DATA //ClassWizardgeneratedvirtualfunctionoverrides //{{AFX_VIRTUAL(CVirtualOscilloscopeDlg) protected: virtualvoidDoDataExchange(CDataExchange*pDX); //DDX/DDVsupport //}}AFX_VIRTUAL//Implementationprotected: HICONm_hIcon; //Generatedmessagemapfunctions //{{AFX_MSG(CVirtualOscilloscopeDlg) virtualBOOLOnInitDialog(); afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam); afx_msgvoidOnPaint(); afx_msgHCURSOROnQueryDragIcon(); afx_msgvoidopen(); afx_msgvoidStartMic(); afx_msgvoidStopMic(); afx_msgvoidOpenMic(); afx_msgvoidCloseMic(); afx_msgvoidOnRadio1(); afx_msgvoidOnRadio2(); afx_msgvoidOnButton6(); afx_msgvoidOnRadio3(); afx_msgvoidOnRadio4(); afx_msgvoidOnRadio5(); afx_msgvoidOnRadio6(); afx_msgvoidOnRadio7(); afx_msgvoidOnRadio8(); //}}AFX_MSG DECLARE_MESSAGE_MAP()};//{{AFX_INSERT_LOCATION}}//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.#endif//!defined(AFX_VIRTUALOSCILLOSCOPEDLG_H__1BEC7015_7348_4493_BF45_37A5D24CEE4D__INCLUDED_)virtualOscilloscopeDlg.app//virtualOscilloscopeDlg.cpp:implementationfile//#include"stdafx.h"#include"virtualOscilloscope.h"#include"virtualOscilloscopeDlg.h"#include"math.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endifintnum=0;inttimes=0;///////////////////////////////////////////////////////////////////////////////CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public: CAboutDlg();//DialogData //{{AFX_DATA(CAboutDlg) enum{IDD=IDD_ABOUTBOX}; //}}AFX_DATA //ClassWizardgeneratedvirtualfunctionoverrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport //}}AFX_VIRTUAL//Implementationprotected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg():CDialog(CAboutDlg::IDD){ //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT}voidCAboutDlg::DoDataExchange(CDataExchange*pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg,CDialog) //{{AFX_MSG_MAP(CAboutDlg) //Nomessagehandlers //}}AFX_MSG_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////CVirtualOscilloscopeDlgdialogCVirtualOscilloscopeDlg::CVirtualOscilloscopeDlg(CWnd*pParent/*=NULL*/) :CDialog(CVirtualOscilloscopeDlg::IDD,pParent){ m_pCatalog=NULL; m_pConnection=NULL; m_pRs=NULL; //{{AFX_DATA_INIT(CVirtualOscilloscopeDlg) m_Query=_T(""); m_Value=_T(""); m_Order=_T(""); m_sd=-1; m_hz=-1; m_fuzhi=_T(""); m_bit=-1; //}}AFX_DATA_INIT //NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32 m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);}CVirtualOscilloscopeDlg::~CVirtualOscilloscopeDlg(){ m_pConnection->Close();}voidCVirtualOscilloscopeDlg::DoDataExchange(CDataExchange*pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CVirtualOscilloscopeDlg) DDX_Control(pDX,IDC_COMBO_UPDOWN,m_Updown); DDX_Control(pDX,IDC_LIST1,m_List); DDX_Control(pDX,IDC_BUTTON4,m_clear4); DDX_Control(pDX,IDC_BUTTON3,m_clear3); DDX_Control(pDX,IDC_BUTTON2,m_clear2); DDX_Control(pDX,IDC_BUTTON1,m_clear1); DDX_Control(pDX,IDC_SCOPE,m_scope); DDX_CBString(pDX,IDC_COMBO_QUERY,m_Query); DDX_Text(pDX,IDC_EDIT_VALUE,m_Value); DDX_CBString(pDX,IDC_COMBO_ORDER,m_Order); DDX_Radio(pDX,IDC_RADIO1,m_sd); DDX_Radio(pDX,IDC_RADIO3,m_hz); DDX_Text(pDX,IDC_EDIT1,m_fuzhi); DDX_Radio(pDX,IDC_RADIO7,m_bit); //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CVirtualOscilloscopeDlg,CDialog) //{{AFX_MSG_MAP(CVirtualOscilloscopeDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1,StartMic) ON_BN_CLICKED(IDC_BUTTON2,StopMic) ON_BN_CLICKED(IDC_BUTTON3,OpenMic) ON_BN_CLICKED(IDC_BUTTON4,CloseMic) ON_BN_CLICKED(IDC_RADIO1,OnRadio1) ON_BN_CLICKED(IDC_RADIO2,OnRadio2) ON_BN_CLICKED(IDC_BUTTON6,OnButton6) ON_BN_CLICKED(IDC_RADIO3,OnRadio3) ON_BN_CLICKED(IDC_RADIO4,OnRadio4) ON_BN_CLICKED(IDC_RADIO5,OnRadio5) ON_BN_CLICKED(IDC_RADIO6,OnRadio6) ON_BN_CLICKED(IDC_RADIO7,OnRadio7) ON_BN_CLICKED(IDC_RADIO8,OnRadio8) //}}AFX_MSG_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////CVirtualOscilloscopeDlgmessagehandlersBOOLCVirtualOscilloscopeDlg::OnInitDialog(){ CDialog::OnInitDialog(); //Add"About..."menuitemtosystemmenu. //IDM_ABOUTBOXmustbeinthesystemcommandrange. ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX<0xF000); CMenu*pSysMenu=GetSystemMenu(FALSE); if(pSysMenu!=NULL) { CStringstrAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if(!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu); } } //Settheiconforthisdialog.Theframeworkdoesthisautomatically //whentheapplication'smainwindowisnotadialog SetIcon(m_hIcon,TRUE); //Setbigicon SetIcon(m_hIcon,FALSE); //Setsmallicon //TODO:Addextrainitializationhere //設(shè)置示波器橫縱坐標(biāo)單位 m_soundIn.GetHwnd(this->m_hWnd); CreateDBAndTable();//創(chuàng)建數(shù)據(jù)庫和表 if(!IsConnected())//重新連接數(shù)據(jù)庫 InitConnection(); if(!IsOpen())//打開結(jié)果集 OpenTable();InitList();//初始化各個控件UpdateList();//更新列表 returnTRUE;//returnTRUEunlessyousetthefocustoacontrol}voidCVirtualOscilloscopeDlg::OnSysCommand(UINTnID,LPARAMlParam){ if((nID&0xFFF0)==IDM_ABOUTBOX) { CAboutDlgdlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID,lParam); }}//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,//thisisautomaticallydoneforyoubytheframework.voidCVirtualOscilloscopeDlg::OnPaint(){ if(IsIconic()) { CPaintDCdc(this);//devicecontextforpainting SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0); //Centericoninclientrectangle intcxIcon=GetSystemMetrics(SM_CXICON); intcyIcon=GetSystemMetrics(SM_CYICON); CRectrect; GetClientRect(&rect); intx=(rect.Width()-cxIcon+1)/2; inty=(rect.Height()-cyIcon+1)/2; //Drawtheicon dc.DrawIcon(x,y,m_hIcon); } else { CDialog::OnPaint(); }}//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags//theminimizedwindow.HCURSORCVirtualOscilloscopeDlg::OnQueryDragIcon(){ return(HCURSOR)m_hIcon;}voidCVirtualOscilloscopeDlg::scope(intnTime,intdata){ m_fuzhi.Format("%d",data);GetDlgItem(IDC_EDIT1)->SetWindowText(m_fuzhi); m_scope.AddValue(nTime,(-(double)data+128)/20);// m_scope.AddValue(nTime,(data+128)/20);if(!IsConnected())//重新連接數(shù)據(jù)庫 InitConnection(); if(!IsOpen())//打開結(jié)果集 OpenTable(); AddNew(); PutValue("實驗次數(shù)",times); PutValue("采樣點",num); PutValue("波形幅值",data); PutValue("采樣時間",COleDateTime::GetCurrentTime()); Update(); num++; if(num==2048) { m_scope.UpdateCurve(); num=0; }}voidCVirtualOscilloscopeDlg::StartMic(){ m_soundIn.StartMic(); }voidCVirtualOscilloscopeDlg::StopMic(){ m_soundIn.StopMic();}voidCVirtualOscilloscopeDlg::OpenMic(){m_soundIn.OpenMic(m_sd,m_hz,m_bit); m_soundIn.StartMic();}voidCVirtualOscilloscopeDlg::CloseMic(){m_soundIn.CloseMic();}voidCVirtualOscilloscopeDlg::OnRadio1(){ m_sd=1;}voidCVirtualOscilloscopeDlg::OnRadio2(){ m_sd=2;}voidCVirtualOscilloscopeDlg::OnRadio3(){ //TODO:Addyourcontrolnotificationhandlercodehere m_hz=8000;}voidCVirtualOscilloscopeDlg::OnRadio4(){ //TODO:Addyourcontrolnotificationhandlercodehere m_hz=11025;}voidCVirtualOscilloscopeDlg::OnRadio5(){ //TODO:Addyourcontrolnotificationhandlercodehere m_hz=22050; }voidCVirtualOscilloscopeDlg::OnRadio6(){ //TODO:Addyourcontrolnotificationhandlercodehere m_hz=44100; }voidCVirtualOscilloscopeDlg::OnRadio7(){ //TODO:Addyourcontrolnotificationhandlercodehere m_bit=8;}voidCVirtualOscilloscopeDlg::OnRadio8(){ //TODO:Addyourcontrolnotificationhandlercodehere m_bit=16;}voidCVirtualOscilloscopeDlg::TestComError(HRESULThr){ if(FAILED(hr)) _com_issue_error(hr);//顯示HRESULT錯誤消息}voidCVirtualOscilloscopeDlg::DumpComError(_com_error&e){ CStringErrorStr; _bstr_tbstrSource(e.Source()); _bstr_tbstrDescription(e.Description()); ErrorStr.Format("Error\n\t代碼=%08lx\n\t代碼表示=%s\n\t來源=%s\n\t描述=%s\n",e.Error(),e.ErrorMessage(),(LPCSTR)bstrSource,(LPCSTR)bstrDescription);#ifdef_DEBUG AfxMessageBox(ErrorStr,MB_OK|MB_ICONERROR);#endif}voidCVirtualOscilloscopeDlg::InitConnection(){HRESULThr=m_pConnection.CreateInstance(__uuidof(Connection)); //創(chuàng)建連接對象實例TestComError(hr);hr=m_pConnection->Open( _bstr_t(L"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=./MyDB.mdb;"), _bstr_t(L""), _bstr_t(L""), adModeUnknown);//建立連接 TestComError(hr);}BOOLCVirtualOscilloscopeDlg::OpenTable(){ HRESULThr=S_OK; if(m_pRs==NULL) hr=m_pRs.CreateInstance(__uuidof(Recordset)); TestComError(hr);hr=m_pRs->Open("MyTable",_variant_t((IDispatch*)m_pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable);//打開表 if(m_pRs->adoEOF||m_pRs->BOF)//如果表為空則不再進(jìn)行任何操作 returnTRUE; m_pRs->MoveFirst();//移動到開頭 returnTRUE;}BOOLCVirtualOscilloscopeDlg::AddNew(){ if(m_pRs->AddNew()!=S_OK) returnFALSE; returnTRUE;}BOOLCVirtualOscilloscopeDlg::Update(){ BOOLbret; try { if(m_pRs->Update()!=S_OK) bret=FALSE; } catch(_com_error&e) { DumpComError(e); bret=FALSE; }if(!bret) m_pRs->CancelUpdate();returnbret;}voidCVirtualOscilloscopeDlg::Close(){if(IsOpen()) m_pRs->Close();}BOOLCVirtualOscilloscopeDlg::PutValue(LPCTSTRfieldName,_variant_tfieldValue){ try { m_pRs->Fields->GetItem(fieldName)->PutValue(fieldValue); returnTRUE; } catch(_com_error&e) { DumpComError(e); returnFALSE; }}BOOLCVirtualOscilloscopeDlg::PutValue(LPCTSTRfieldName,longfieldValue){ returnPutValue(fieldName,_variant_t(fieldValue));}BOOLCVirtualOscilloscopeDlg::PutValue(LPCTSTRfieldName,COleDateTimefieldValue){ _variant_tvt; vt.vt=VT_DATE; vt.date=fieldValue; returnPutValue(fieldName,vt);}BOOLCVirtualOscilloscopeDlg::PutValue(LPCTSTRfieldName,LPCTSTRfieldValue){ returnPutValue(fieldName,_variant_t(fieldValue));}voidCVirtualOscilloscopeDlg::CreateDBAndTable(){HRESULThr;try{ hr=m_pCatalog.CreateInstance(__uuidof(ADOX::Catalog));//創(chuàng)建Catalog對象 TestComError(hr); CFiledb; //判斷數(shù)據(jù)庫是否已經(jīng)存在 if(db.Open("./MyDB.mdb",CFile::modeRead|CFile::shareDenyWrite)) db.Close();//關(guān)閉文件 else{m_pCatalog->Create("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=./MyDB.mdb;");//數(shù)據(jù)庫不存在則創(chuàng)建該數(shù)據(jù)庫 ADOX::_TablePtrm_pTable=NULL;//分別創(chuàng)建表hr=m_pTable.CreateInstance(__uuidof(ADOX::Table)); TestComError(hr); m_pTable->PutName("MyTable");//設(shè)置表名 //添加列m_pTable->Columns->Append("實驗次數(shù)",ADOX::adInteger,0);//整數(shù)類型 m_pTable->Columns->Append("采樣點",ADOX::adInteger,0);//整數(shù)類型 m_pTable->Columns->Append("波形幅值",ADOX::adInteger,0);//整數(shù)類型 m_pTable->Columns->Append("采樣時間",ADOX::adDate,0);//日期類型 m_pCatalog->Tables->Append(_variant_t((IDispatch*)m_pTable));//添加表 InitConnection();//初始化ADO連接 OpenTable();//打開一張表 Close();}}catch(_com_error&e){DumpComError(e);}}BOOLCVirtualOscilloscopeDlg::IsConnected(){ if(m_pConnection) returnm_pConnection->GetState()!=adStateClosed; returnFALSE;}BOOLCVirtualOscilloscopeDlg::IsOpen(){ if(m_pRs) returnm_pRs->GetState()!=adStateClosed; returnFALSE;}BOOLCVirtualOscilloscopeDlg::GetFieldValue(LPCTSTRlpFieldName,CString&strValue){ CStringstr=_T(""); _variant_tvtFld; try { //獲得字段值 vtFld=m_pRs->Fields->GetItem(lpFieldName)->Value; //根據(jù)值的類型來進(jìn)行相應(yīng)的轉(zhuǎn)化 switch(vtFld.vt) { caseVT_R4: str.Format("%.3f",vtFld.fltVal); break; caseVT_R8: str.Format("%.6f",vtFld.dblVal); break; caseVT_BSTR: str=vtFld.bstrVal; break; caseVT_I2: caseVT_UI1: str.Format("%d",vtFld.iVal); break; caseVT_INT: str.Format("%d",vtFVal); break; caseVT_I4: str.Format("%d",vtFld.lVal); break; caseVT_UI4: str.Format("%d",vtFld.ulVal); break; caseVT_DECIMAL: { doubleval=vtFld.decVal.Lo32; val*=(vtFld.decVal.sign==128)?-1:1; val/=pow(10,vtFld.decVal.scale); str.Format("%.6f",val); } break; caseVT_DATE: { COleDateTimedt(vtFld); CStringstrDateFormat=_T("%Y-%m-%d%H:%M:%S")

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論