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

📄 bastestdlg.cpp

📁 < VC++串口通信工程发型实例导航>>源代码
💻 CPP
字号:
// BASTestDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CBASTestDlg dialog

CBASTestDlg::CBASTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBASTestDlg::IDD, pParent),
	m_nDataCount(0),
	m_nTimerID(1)
{
	//{{AFX_DATA_INIT(CBASTestDlg)
		// 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);
	memset(m_szData, 0, sizeof(m_szData));
}

void CBASTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBASTestDlg)
	DDX_Control(pDX, IDC_LIST_STATUS, m_listStatus);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBASTestDlg, CDialog)
	//{{AFX_MSG_MAP(CBASTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBASTestDlg message handlers

BOOL CBASTestDlg::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_Port.InitPort(this, PORTNUM, 9600, 'N', 8, 1))
	{
		m_Port.StartMonitoring();
		SetTimer(m_nTimerID, TIMERINTERVAL, NULL);
		
		// 设定随机数种子
		srand((unsigned)time(NULL));
		GenerateRandomStatus();
	}
	else
	{
		AfxMessageBox(_T("初始化串口失败。"));
	}

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

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

LONG CBASTestDlg::OnCommunication(WPARAM wParam, LPARAM lParam)
{
	// 将接收到的数据存放在m_szData变量中
	m_szData[m_nDataCount] = wParam;
	++m_nDataCount;

	// 一条命令以回车'\r'结束,接收到该字符说明一条命令已经结束
	if (wParam == '\r')
	{
		// 给接收到的数据加上结束符
		m_szData[m_nDataCount] = '\0';
		
		// 处理接收到的数据
		ProcessCmd();

		// 清空缓存区,等待下一次接收
		m_nDataCount = 0;
		memset(m_szData, 0, sizeof(m_szData));
	}
	
	return 0;
}

void CBASTestDlg::GenerateRandomStatus()
{
	m_nFan = rand()*2/RAND_MAX;
	m_nMode = rand()*4/RAND_MAX;
	m_nSetpoint = rand()*128/RAND_MAX;
	m_nTemp = rand()*128/RAND_MAX;
}

void CBASTestDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (nIDEvent == m_nTimerID)
	{
		GenerateRandomStatus();
	}

	CDialog::OnTimer(nIDEvent);
}

void CBASTestDlg::ProcessCmd()
{
	CString strCmd(m_szData);
	
	CString strStatus;
	strStatus.Format(_T("收到 %s"), strCmd);
	m_listStatus.AddString(strStatus);
	
	int nPos1 = strCmd.ReverseFind('=');
	int nPos2 = strCmd.Find(_T("\r"), 0);

	char chCmdType = strCmd.GetAt(4);
	char chMode;
	
	switch (chCmdType)
	{
	case 'F':	// 更改风机状态
		m_nFan = atoi(strCmd.Mid(nPos1 + 1, nPos2 - nPos1 - 1));
		break;
	case 'M':	// 更改运行模式
		chMode = strCmd.GetAt(nPos1 + 1);
		switch (chMode)
		{
		case 'O':
			m_nMode = 0;
			break;
		case 'H':
			m_nMode = 1;
			break;
		case 'C':
			m_nMode = 2;
			break;
		case 'A':
			m_nMode = 3;
			break;
		default:
			m_nMode = '-1';
			break;
		}		
		break;	
	case 'R':	// 状态请求
		SendStatus();
		break;
	case 'S':	// 更改设定温度
		if (strCmd.Find(_T("SP+"), 0) > 0)		// 设定温度+1
		{
			++m_nSetpoint;
		}
		else if (strCmd.Find(_T("SP-"), 0) > 0)	// 设定温度-1
		{
			--m_nSetpoint;
		}
		else
		{
			m_nSetpoint = atoi(strCmd.Mid(nPos1 + 1, nPos2 - nPos1 - 1));
		}
		break;
	}
}

void CBASTestDlg::SendStatus()
{
	char chMode;
	switch (m_nMode)
	{
	case 0:
		chMode = 'O';
		break;
	case 1:
		chMode = 'H';
		break;
	case 2:
		chMode = 'C';
		break;
	case 3:
		chMode = 'A';
		break;
	}

	char szStatus[50];

	sprintf(szStatus, _T("A=1,O=1,Z=1,T=%d,SP=%d,M=%c,FM=%d\r"),
		m_nTemp, m_nSetpoint, chMode, m_nFan);

	CString strStatus;
	strStatus.Format(_T("发送 %s"), szStatus);
	m_listStatus.AddString(strStatus);

	m_Port.WriteToPort(szStatus);
}

⌨️ 快捷键说明

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