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

📄 temperaturedlg.cpp

📁 c++串口通信 基于ad7862四路电压采集系统232川口通信
💻 CPP
字号:
// TemperatureDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Temperature.h"
#include "TemperatureDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CTemperatureDlg dialog

CTemperatureDlg::CTemperatureDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTemperatureDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTemperatureDlg)
	m_str_data = _T("");
	m_str_data2 = _T("");
	m_str_data3 = _T("");
	m_str_data4 = _T("");
	m_str_data5 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTemperatureDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTemperatureDlg)
	DDX_Text(pDX, IDC_EDIT_DATA, m_str_data);
	DDX_Control(pDX, IDC_MSCOMM1, m_mscomm);
	DDX_Text(pDX, IDC_EDIT_DATA2, m_str_data2);
	DDX_Text(pDX, IDC_EDIT_DATA3, m_str_data3);
	DDX_Text(pDX, IDC_EDIT_DATA4, m_str_data4);
	DDX_Text(pDX, IDC_EDIT_DATA5, m_str_data5);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTemperatureDlg, CDialog)
	//{{AFX_MSG_MAP(CTemperatureDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_RADIO_HUASHI, OnRadioHuashi)
	ON_BN_CLICKED(IDC_RADIO_SHESHI, OnRadioSheshi)
	ON_EN_CHANGE(IDC_EDIT_DATA4, OnChangeEditData4)
	ON_EN_CHANGE(IDC_EDIT_DATA3, OnChangeEditData3)
	ON_EN_CHANGE(IDC_EDIT_DATA2, OnChangeEditData2)
	ON_EN_CHANGE(IDC_EDIT_DATA5, OnChangeEditData5)
	ON_EN_CHANGE(IDC_EDIT_DATA, OnChangeEditData)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTemperatureDlg message handlers

BOOL CTemperatureDlg::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
   if(m_mscomm.GetPortOpen())//如果串口已打开,则关闭串口
		m_mscomm.SetPortOpen(FALSE);
        m_mscomm.SetCommPort(1);//选择COM1
	if(m_mscomm.GetPortOpen())
	{
	  m_mscomm.SetPortOpen(FALSE);
	}
    m_mscomm.SetSettings("9600,n,8,1");//设置波特率为9600,无校验,8位数据,1位停止位
	m_mscomm.SetInputMode(1); //1:表示以二进制方式检取数据
	CheckRadioButton(IDC_RADIO_SHESHI,IDC_RADIO_HUASHI,IDC_RADIO_SHESHI);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CTemperatureDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here
   if(!m_mscomm.GetPortOpen())
	{
		m_mscomm.SetPortOpen(TRUE);
	}
	m_mscomm.SetRThreshold(1);//串口接收缓冲区中有多于或等于1个字符时产生OnComm事件
    m_mscomm.SetInputLen(0);//设置当前接收区数据长度为0
	m_mscomm.GetInput();//预读缓冲区以清除残留数据	
}

void CTemperatureDlg::OnRadioHuashi() 
{
	// TODO: Add your control notification handler code here
	
}

void CTemperatureDlg::OnRadioSheshi() 
{
	// TODO: Add your control notification handler code here
	
}

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


void CTemperatureDlg::OnComm() 
{
	// TODO: Add your control notification handler code here
    VARIANT m_input1; 
    COleSafeArray m_input2; 
    LONG length,i; 
    BYTE r_data[1024]; 
    CString str1,str2,str3,str4,str5; 
	double temp1,temp2,temp3,temp4,temp5;
     //接收缓冲区内字符 
    if(m_mscomm.GetCommEvent()== 2) 
    {
		//m_tempdata.Empty(); 
               //读取缓冲区内的数据 
        m_input1=m_mscomm.GetInput(); 
               //将VARIANT型变量转换为ColeSafeArray型变量 
        m_input2=m_input1; 
               //确定数据长度 
        length=m_input2.GetOneDimSize(); 
               //将数据转换为BYTE型数组 
       // for(i=0;i<length;i++) 
        for(i=0;i<8;i++)
		     m_input2.GetElement(&i,r_data+i); 
              
		for(i=0;i<8;i++)
		{
	
         temp1=(r_data[0]*256.0+r_data[1])/2048.0;
		 temp2=(r_data[2]*256.0+r_data[3])/2048.0;
		 temp3=(r_data[4]*256.0+r_data[5])/2048.0;
		 temp4=(r_data[6]*256.0+r_data[7])/2048.0;
		 temp5=(r_data[8]*256.0+r_data[9])/2048.0;
		 
    
         temp1=temp1*10;
		 temp2=temp2*10;
		 temp3=temp3*10;
		 temp4=temp4*10;
		 temp5=temp5*10;
       

		str1.Format("%f",temp1);
		str2.Format("%.6f",temp2);
		str3.Format("%.6f",temp3);
		str4.Format("%.6f",temp4);
		str5.Format("%.6f",temp5);
        

		m_str_data=str1;
		m_str_data2=str2;
		m_str_data3=str3;
		m_str_data4=str4;
		m_str_data5=str5;
		}
	}
	UpdateData(false);
     //   str2=str1;
	//	m_mscomm.SetOutput(COleVariant(str3+str2));



}

void CTemperatureDlg::OnChangeEditData4() 
{
	// 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
	
}

void CTemperatureDlg::OnChangeEditData3() 
{
	// 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
	
}

void CTemperatureDlg::OnChangeEditData2() 
{
	// 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
	
}

void CTemperatureDlg::OnChangeEditData5() 
{
	// 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
	
}

void CTemperatureDlg::OnChangeEditData() 
{
	// 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
	
}

void CTemperatureDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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