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

📄 testdlg.cpp

📁 VC++串口上位机简单例程
💻 CPP
字号:
// testDlg.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "testDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
		// 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 CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Control(pDX, IDC_BUTTON2, m_serial);
	DDX_Control(pDX, IDC_MSCOMM1, m_comm1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

BOOL CTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

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

//************************************************************************************
	gllen = 0;											//记录转换次数全局变量清零
	if(! m_comm1.GetPortOpen())							//判断串口是否已经打开
	{
		m_comm1.SetCommPort(1);							//选择串口号1
		m_comm1.SetPortOpen(TRUE);						//打开串口
		m_comm1.SetRThreshold(2);						//收到两个字节引发OnComm事件
		m_comm1.SetInputMode(1);						//输入模式选为二进制
		m_comm1.SetSettings("57600,n,8,1");				//设置串口参数,波特率57600,无奇偶校验,1位停止位,8位数据位
		MessageBox("串口初始化完毕","提示");			//提示串口成功初始化
	}
	else MessageBox("串口被占用","提示");				//如果已经打开串口,消息框提醒
	pbar = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);	//获得指向IDC_PROGRESS1的指针
	pbar -> SetRange(0,1023);							//设置进度条的范围0~1023
	pbar -> SetPos(0);									//当前位置为0
	m_serial.SetWindowText("关闭串口");					//按钮显示状态改变
//************************************************************************************

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

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

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

void CTestDlg::OnOnCommMscomm1() 
{
	// TODO: Add your control notification handler code here

//*********************************************************************************************
	VARIANT variant1;				//定义VARIANT型变量,用于存放接收到的数据
	COleSafeArray safearray;		//定义safearray型变量
	LONG len,k;						//定义长整型变量len,k
	BYTE rxdata[2048];				//定义BYTE型数组
	CString stremp1,stremp2;		//定义两个字符串	
	if(m_comm1.GetCommEvent()==2)			//判断引起OnComm时间的原因
	{										//如果是接收到特定个字节数,则读取接收到的数据
		variant1 = m_comm1.GetInput();		//把接收到的数据存放到VARIANT型变量里
		safearray = variant1;				//VARIANT型变量转换为ColeSafeArray型变量
		len = safearray.GetOneDimSize();	
		for(k=0;k<len;k++)
		{
			safearray.GetElement(&k,rxdata+k);			//得到接接收到的数据放到BYTE型数组rxdata里
		}
		for(k=0;k<len;k++)
		{
			BYTE bt = (*(unsigned char*)(rxdata+k));	//读取AD转换的高字节
			if((k%2)==0)
				if((k+1)<len)
				{
					gllen++;							//全局的变量,对接收到的转换结果的个数进行计算
					stremp2.Format("第%d次转换结果:",gllen);						//显示第几次转换
					int temp = bt*4+((*(unsigned char *)(rxdata+k+1))>>6);			//高低字节合并成实际的转换结果,注意转换结果是左对齐
					stremp1.Format("%2.2f",(2.56*temp/1024));						//计算成实际电压值
					SetDlgItemText(IDC_STATIC,("当前电压值为: "+stremp1+" V"));	//更新静态文本控件
					pbar -> SetPos(temp);				//更新进度条的当前位置
					strRXDdata += stremp2;				//把新的数据放到全局的字符串里		
					strRXDdata += stremp1;
					strRXDdata += " V\r\n";				//字符串加单位V后换行			
				}
		}
	}
	SetDlgItemText(IDC_EDIT1,strRXDdata);				//更新文本控件的显示
//**************************************************************************************************
}

void CTestDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
//*********************************************************
	CByteArray m_Array;							//定义字节数组
	m_Array.RemoveAll();						//字节数组清空
	m_Array.SetSize(1);							//设定维数为1
	m_Array.SetAt(0,0xaa);						//给m_array[0]赋值0
	m_comm1.SetOutput(COleVariant(m_Array));	//由于SetOutput函数的参数为VARIANT型,必须强制转换后才能发送
//*********************************************************
}

void CTestDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
//*************************************************************************************	
	if(! m_comm1.GetPortOpen())							//判断串口是否已经打开
	{													
		m_comm1.SetPortOpen(TRUE);						//如果串口是关闭的,则打开串口
		m_serial.SetWindowText("关闭串口");				//按钮显示状态改变
	}
	else 
	{
		m_comm1.SetPortOpen(FALSE);						//如果已经打开串口,则关闭串口
		m_serial.SetWindowText("打开串口");				//按钮显示状态改变
	}
//****************************************************************************************
}

⌨️ 快捷键说明

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