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

📄 tempviewdlg.cpp

📁 51单片机测温程序
💻 CPP
字号:
// TempViewDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TempView.h"
#include "TempViewDlg.h"
#include "MyScroll02.h"

#include "math.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CTempViewDlg dialog

CTempViewDlg::CTempViewDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTempViewDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTempViewDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_iComSelect = 0;
	m_iFreqSelect = 1;

}

void CTempViewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTempViewDlg)
	DDX_Control(pDX, IDC_COMBO_FREQSEL, m_Ctrl_FreqSelect);
	DDX_Control(pDX, IDC_COMBO_COMSEL, m_Ctrl_ComSelect);
	DDX_Control(pDX, IDC_STATIC_MAP, m_Scroll_Map);
	DDX_Control(pDX, IDC_MSCOMM1, m_Com);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTempViewDlg, CDialog)
	//{{AFX_MSG_MAP(CTempViewDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_WM_SHOWWINDOW()
	ON_WM_CANCELMODE()
	ON_WM_DRAWITEM()
	ON_BN_CLICKED(IDC_BUTTON_CONECT, OnButtonConect)
//	ON_MESSAGE(WM_COMM_RXCHAR, OnComReciver)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTempViewDlg message handlers

BOOL CTempViewDlg::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
	B_IsFirst = true;

	//初始化串行端口的下拉选项
	m_Ctrl_FreqSelect.AddString("4800,n,8,1");
	m_Ctrl_FreqSelect.AddString("9600,n,8,1");
	m_Ctrl_FreqSelect.AddString("19200,n,8,1");
	m_Ctrl_FreqSelect.AddString("38400,n,8,1");
	m_Ctrl_FreqSelect.AddString("57600,n,8,1");
	m_Ctrl_FreqSelect.AddString("115200,n,8,1");
	m_Ctrl_ComSelect.SetCurSel(m_iComSelect);
	m_Ctrl_FreqSelect.SetCurSel(m_iFreqSelect);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTempViewDlg::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 CTempViewDlg::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
	{
//		m_Scroll_Map.InitDrawBk();
		if(B_IsFirst)
		{
			B_IsFirst = false;
	//		m_Scroll_Map.InitDrawBk();
			m_Scroll_Map.SetScrollColor(RGB(0,0,255));

		//	m_Scroll_Map.IsShowNetLine(false);
			m_Scroll_Map.SetNetLine(20,10);
			m_Scroll_Map.SetNetLineColor(RGB(0,0,0));
	
			m_Scroll_Map.SetNumberX(400);
			m_Scroll_Map.SetBkMapColor(RGB(100,255,202));
			m_Scroll_Map.SetNumberY(2000,0);
			m_Scroll_Map.InitDrawBk();
		//	m_Scroll_Map.SetUpdateFlag(true);
		}
		CDialog::OnPaint();
	}
}

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

void CTempViewDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	m_Scroll_Map.SetUpdateFlag(true);
//	SetTimer(1,5,NULL);					//SetTimer仅作为测试用
	
}
///此函数仅作为测试用
void CTempViewDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1)
	{
		m_PointY++;
		
		int y;

		y=(int)((200.0*cos((3.14*2)/200*m_PointY))*1.8);

		m_Scroll_Map.SetScrollPoint(y);

	}

	CDialog::OnTimer(nIDEvent);
}

void CTempViewDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	m_Scroll_Map.SetUpdateFlag(false);
//	KillTimer(1);					//仅作测试用
}

void CTempViewDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
	
}

void CTempViewDlg::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}

void CTempViewDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);

}

void CTempViewDlg::OnButtonConect() 
{
	// TODO: Add your control notification handler code here
	CString SComFreq;
	UpdateData(true);

	m_iComSelect = m_Ctrl_ComSelect.GetCurSel();
	m_iFreqSelect = m_Ctrl_FreqSelect.GetCurSel();
	switch(m_iFreqSelect)
	{
	case 0: SComFreq = _T("4800,n,8,1");
		break;
	case 1: SComFreq = _T("9600,n,8,1");
		break;
	case 2: SComFreq = _T("19200,n,8,1");
		break;
	case 3: SComFreq = _T("38400,n,8,1");
		break;
	case 4: SComFreq = _T("57600,n,8,1");
		break;
	case 5: SComFreq = _T("115200,n,8,1");
		break;
	default:
		break;
	}

	if(m_Com.GetPortOpen())
		m_Com.SetPortOpen(FALSE);

	m_Com.SetCommPort(m_iComSelect+1);
	if(!m_Com.GetPortOpen())
		m_Com.SetPortOpen(TRUE);
	else
	{
		AfxMessageBox("cannot open serial port");
		return;
	}

	m_Com.SetSettings(SComFreq);

	m_Com.SetRThreshold(1);    //设置当有多少个数据在缓冲区时发出一个串行口事件

	m_Com.SetInputMode(1);      //串行口传输数据格式

	///////清除缓冲区中的数据!
	m_Com.SetInputLen(0);
	m_Com.GetInput();

//	if(m_hCom.InitPort(this,m_iComSelect+1,SComFreq,'N',8,1,EV_RXCHAR,512))
//		m_hCom.StartMonitoring(); //启动串口监视线程
//	else
//		MessageBox("端口打开无效!!");
}

LONG CTempViewDlg::OnComReciver(WPARAM ch, LPARAM port)
{
	unsigned char ch_Reciver;
	
	ch_Reciver = (unsigned char)ch;
	

	return 0L;
}

BEGIN_EVENTSINK_MAP(CTempViewDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CTempViewDlg)
	ON_EVENT(CTempViewDlg, IDC_MSCOMM1, 1 /* OnComm */, OnCommMscomm1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CTempViewDlg::OnCommMscomm1() 
{
	// TODO: Add your control notification handler code here
	VARIANT vResponse;
	unsigned char *ucReciver;
	int Temperature;
	int Buffer_len;

	if(1)
	{
		if(m_Com.GetCommEvent()==2)     //事件值为2表示接收缓冲区内有字符
		{
		//	m_Eventdata.SetEvent();/////////////设置该理事件为有信号
			Buffer_len = m_Com.GetInBufferCount();
			vResponse = m_Com.GetInput();
			
			ucReciver = (unsigned char *)(vResponse.parray->pvData);
			while(Buffer_len)
			{
				for(int i=0;i<3;i++)
				{
					RS_Buff[i] = RS_Buff[i+1];
				}
				RS_Buff[3] = *ucReciver;
				ucReciver++;
				Buffer_len--;
				if(RS_Buff[0]==0xaa&&RS_Buff[3]==0x55)
				{								//数据包的帧头和帧尾都对上!
					Temperature = RS_Buff[1]*10+RS_Buff[2];
					Temperature = Temperature*2;
					m_Scroll_Map.SetScrollPoint(Temperature);
					break;
				}
			}

		}
		UpdateData(false);
	}
}

⌨️ 快捷键说明

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