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

📄 testoscopedlg.cpp

📁 基于串口通讯(通过USB转串口)的示波器界面
💻 CPP
字号:
// TestOScopeDlg.cpp : implementation file
//
#include "stdafx.h"
#include <stdlib.h>
#include <fstream.h>
#include "TestOScope.h"
#include "TestOScopeDlg.h"
#include <iostream.h>

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

#define BUFFER_LENGTH  3072
#define WM_USER_PLAYER  WM_USER+1000


/////////////////////////////////////////////////////////////////////////////
// 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)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestOScopeDlg dialog

CTestOScopeDlg::CTestOScopeDlg(CWnd* pParent /*=NULL*/)
  : CDialog(CTestOScopeDlg::IDD, pParent)
{
  //{{AFX_DATA_INIT(CTestOScopeDlg)
	m_nShowEdit = 0;
	m_strChannel = _T("");
	//}}AFX_DATA_INIT
  // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  m_bStartStop = FALSE ;
  srand( (unsigned)time( NULL ) );
  
}

void CTestOScopeDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CTestOScopeDlg)
	DDX_Control(pDX, IDC_LIST3, m_SampleResult);
	DDX_Control(pDX, IDC_LIST1, m_ctrlListBox);
	DDX_Control(pDX, IDC_SLIDER1, m_nSlider);
	DDX_Control(pDX, IDC_COMBO, m_ctrlChannel);
	DDX_Text(pDX, IDC_EDIT1, m_nShowEdit);
	DDV_MinMaxInt(pDX, m_nShowEdit, 0, 110000);
	DDX_CBString(pDX, IDC_COMBO, m_strChannel);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestOScopeDlg, CDialog)
  //{{AFX_MSG_MAP(CTestOScopeDlg)
  ON_WM_SYSCOMMAND()
  ON_WM_PAINT()
  ON_WM_QUERYDRAGICON()
  ON_WM_TIMER()
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnCustomdrawSlider1)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestOScopeDlg message handlers

BOOL CTestOScopeDlg::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(-10.0, 10.0, 0) ;
  m_OScopeCtrl.SetYUnits("Volts") ;
  m_OScopeCtrl.SetXUnits("Samples (Windows Timer: 100 msec)") ;
  m_OScopeCtrl.SetBackgroundColor(RGB(0, 0, 64)) ;
  m_OScopeCtrl.SetGridColor(RGB(192, 192, 255)) ;
  m_OScopeCtrl.SetPlotColor(RGB(255, 255, 255)) ;
  
  /*自己添加的控件的初始化*/
  m_pPlayThread = NULL; 
  Init_Flag=1;
  m_ctrlChannel.SetCurSel(0);
  m_nSlider.SetRange(0,110000);     //设置滑块的范围
  m_nSlider.SetTicFreq(10);
  CheckRadioButton(IDC_RADIO1, IDC_RADIO4,IDC_RADIO1);//设置默认的选项
m_nPlayerStatus =SAMPLING_STATUS_STOP;					
  /****************/
  return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestOScopeDlg::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 CTestOScopeDlg::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 CTestOScopeDlg::OnQueryDragIcon()
{
  return (HCURSOR) m_hIcon;
}

void CTestOScopeDlg::OnRunstop() 
{
  // TODO: Add your control notification handler code here
  m_bStartStop ^= TRUE;

  if (m_bStartStop)
    SetTimer(1,12,NULL);
  else
    KillTimer(1);
  
}
void CTestOScopeDlg::OnTimer(UINT nIDEvent) 
{
  double nRandom; 
  USHORT vol_temp=0x0000;
  USHORT vol_temp1=0;
  
  vol_temp|=(unsigned)szRequest[1];
  vol_temp1|=(unsigned)szRequest[0];
  vol_temp|=vol_temp<<8;

  nRandom=vol_temp*5.0/4096.0;

  
  
  // generate a random number between -5 and 5

// nRandom = -5.0 + 10.0*rand()/(double)RAND_MAX;

  // append the new value to the plot
  m_OScopeCtrl.AppendPoint(nRandom); 
  CDialog::OnTimer(nIDEvent);
 }


UINT ThreadPoc1(LPVOID pParam)
{	
	CTestOScopeDlg * pDlg = (CTestOScopeDlg*) pParam;
	pDlg->DataSample();
	return 1;
}

/*void CTestOScopeDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
    if(NULL== m_pPlayThread)
	{
		m_pPlayThread=AfxBeginThread(ThreadPoc1,this,0,0,0);
	}
	m_pPlayThread->PostThreadMessage(WM_USER_PLAYER,0,0);
	m_nPlayerStatus =PLAYER_STATUS_PLAYING;
	m_ctrlPlay.EnableWindow(FALSE);
	m_ctrlStop.EnableWindow(TRUE);
}*/






void CTestOScopeDlg::OnOK() 
{ 
  if(NULL== m_pPlayThread)
  {
	m_pPlayThread=AfxBeginThread(ThreadPoc1,this,0,0,0);
  }
  m_pPlayThread->PostThreadMessage(WM_USER_PLAYER,0,0);
  m_nPlayerStatus =1-m_nPlayerStatus;
}

void CTestOScopeDlg::DataSample()
{
  CString str_Radio; //电压范围
  CString str_temp,str_format,str_temp1,str_LiangCheng,str_CaiYang,str_TongDao;//
  ULONG nRead =0;
  ULONG	nWritten;
  
  int nIndex,nSlider;
  int i;//用于for循环
  MSG msg;
  
	  
  m_ctrlListBox.ResetContent();
  /*获得采样参数*/
  
  UINT nID=GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO4);
  GetDlgItemText(nID,str_Radio);
  
  nIndex=m_ctrlChannel.GetCurSel();
  
  nSlider=m_nSlider.GetPos();

  str_LiangCheng="量程:";
  str_LiangCheng+=str_Radio;
  str_LiangCheng+="\n\n";
  //m_ctrlListBox.AddString(str_temp1);

  str_temp1="通道:";
  switch(nIndex)
  {
  case 0:
	  str_temp1+="CH1";
	  break;
  case 1:
	  str_temp1+="CH2";
	  break;
  case 2:
	  str_temp1+="CH3";
	  break;
  case 3:
	  str_temp1+="CH4";
	  break;
  case 4:
	  str_temp1+="CH5";
	  break;
  case 5:
	  str_temp1+="CH6";
	  break;
  case 6:
	  str_temp1+="CH7";
	  break;
  case 7:
	  str_temp1+="CH8";
	  break;
  }
  str_temp1+="\n\n";
 // m_ctrlListBox.AddString(str_temp1);
  
  str_CaiYang="采样率:";
  str_format.Format("%d", nSlider);
  str_CaiYang+=str_format;
  m_ctrlListBox.AddString(str_temp1);
  /**************/
  m_ctrlListBox.AddString(str_LiangCheng);
  m_ctrlListBox.AddString(str_CaiYang);

  m_bStartStop ^= TRUE;
  if (m_bStartStop)
  {
	if(Init_Flag==1)
	{
     // FindPlayerCOMPath();	
      CString strCom;	
	  m_strCOMPath="COM4";
	  strCom.Format("\\\\.\\%s",m_strCOMPath);
	
	  // 打开指定的串口
	  m_hDevice = ::CreateFile(strCom,GENERIC_READ | GENERIC_WRITE,0,NULL,
     	OPEN_EXISTING,0,NULL);
      if(m_hDevice== INVALID_HANDLE_VALUE )
	  {
		AfxMessageBox("找不到硬件!");
		//m_ctrlPlay.EnableWindow(FALSE);
	  } 
	  else
	  { 
		Init_Flag++;
	  } 
	  
	}
    
  /********利用USB进行数据传输*********/
   str_temp="0x";
   WriteFile(m_hDevice,"sample",6,&nWritten,NULL);
   while(1)
   { 
    //if(0!=GetMessage(&msg,0,0,0))
    if(m_nPlayerStatus ==SAMPLING_STATUS_START)
	{
     WriteFile(m_hDevice,"sample",6,&nWritten,NULL);
     ReadFile(m_hDevice,szRequest, 2, &nRead, NULL);
     /************************************/
	 str_temp="";
     for(i=1;i>=0;i--)
	 { 
      switch(szRequest[i]>>4&0x0F)
	  { 
	   case 0:
		str_temp+="0";
		break;
	   case 1:
		str_temp+="1";
		break;
	   case 2:
		str_temp+="2";
		break;
	   case 3:
		str_temp+="3";
		break;
	   case 4:
		str_temp+="4";
		break;
	   case 5:
		str_temp+="5";
		break;
	   case 6:
		str_temp+="6";
		break;
   	   case 7:
		str_temp+="7";
		break;
	   case 8:
		str_temp+="8";
		break;
	   case 9:
		str_temp+="9";
		break;
	   case 10:
		str_temp+="A";
		break;
	   case 11:
	    str_temp+="B";
		break;
	   case 12:
		str_temp+="C";
		break;
	   case 13:
		str_temp+="D";
		break;
	   case 14:
		str_temp+="E";
		break;
	   case 15:
		str_temp+="F";
		break;
	  }
      switch(szRequest[i]&0x0F)
	  { 
	  case 0:
		str_temp+="0";
		break;
	  case 1:
		str_temp+="1";
		break;
	  case 2:
		str_temp+="2";
		break;
	  case 3:
		str_temp+="3";
		break;
	  case 4:
		str_temp+="4";
		break;
	  case 5:
		str_temp+="5";
		break;
	  case 6:
		str_temp+="6";
		break;
	  case 7:
		str_temp+="7";
		break;
	  case 8:
		str_temp+="8";
		break;
	  case 9:
		str_temp+="9";
		break;
 	  case 10:
		str_temp+="A";
		break;
	  case 11:
	    str_temp+="B";
		break;
	  case 12:
		str_temp+="C";
		break;
	  case 13:
		str_temp+="D";
		break;
	  case 14:
		str_temp+="E";
		break;
	  case 15:
		str_temp+="F";
		break;
	  }
  }//SetTimer(1,1,NULL);
  m_SampleResult.ResetContent();
  m_SampleResult.AddString(str_temp);

  double nRandom; 
  USHORT vol_temp=0x0000;
  USHORT vol_temp1=0;
  
  vol_temp|=(unsigned)szRequest[1];
  vol_temp1|=(unsigned)szRequest[0];
  vol_temp|=vol_temp<<8;

  nRandom=vol_temp*5.0/4096.0;

  
  
  // generate a random number between -5 and 5

// nRandom = -5.0 + 10.0*rand()/(double)RAND_MAX;

  // append the new value to the plot
  m_OScopeCtrl.AppendPoint(nRandom); 
  
  
  }
  } 
  } 
  else

    KillTimer(1);
	
 
  

}

void CTestOScopeDlg::OnCancel() 
{
  if (!m_bStartStop)
    KillTimer(1) ;

  CDialog::OnCancel();
}

/*readdata()
{
	for(int j=0;j<2000;++j)
		a[j]=-5.0 + 10.0*rand()/(double)RAND_MAX;
	return 0;
}
void openfile()
{
	CString Name1("data1.dat");
    CString Name2("data2.dat");
    CFile MyFile1(Name1,CFile::modeRead);
    CFile MyFile2(Name2,CFile::modeCreate|CFile::modeWrite);
    DWORD Length=MyFile1.GetLength();
    void far*p=GlobalAlloc(0,Length);
    if(p==NULL)
	{
		AfxMessageBox("Alloc memory error!");
	}
    MyFile1.ReadHuge(p,Length);
    MyFile2.ReadHuge(p,Length);
    MyFile1.Close();
    MyFile2.Close();
    AfxMessageBox("File Copy Succeed!");
}*/


void CTestOScopeDlg::OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	
  UpdateData(TRUE);
  m_nShowEdit=m_nSlider.GetPos();
  UpdateData(FALSE); 
  *pResult = 0;
}

void CTestOScopeDlg::OnChangeEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
    m_nSlider.SetPos(m_nShowEdit);  //根据文本框中输入的内容来改变滑块的位置
	UpdateData(FALSE); 	
	
}

⌨️ 快捷键说明

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