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

📄 pcserialtestdlg.cpp

📁 用vc实现嵌入式系统的串行通讯
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// PCSerialTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PCSerialTest.h"
#include "PCSerialTestDlg.h"
#include "PortSetDlg.h"
#include "HexEdit.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()

/////////////////////////////////////////////////////////////////////////////
// CPCSerialTestDlg dialog

CPCSerialTestDlg::CPCSerialTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPCSerialTestDlg::IDD, pParent)//提供串口和控件初始化
{
	//可以自动发送
	m_bSend = FALSE;
	//允许接受字符
	m_bReceive = FALSE;

	//端口号设置为1
	m_nPort = 1;
	m_strSettings = _T("9600, n, 8, 1");   //串行口设置
	m_strSendString = _T("Hello");         //初始化发送字符串
	m_nTime = 1000;                        //自动发送间隔时间1S=1000ms

	m_nOutPutMode = 1;                     //数据发送方式初始为ASCII码


	//{{AFX_DATA_INIT(CPCSerialTestDlg)//系统自动初始化
	m_4Or8 = FALSE;
	m_Address = FALSE;
	m_Ascii = FALSE;
	m_Hex = TRUE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPCSerialTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPCSerialTestDlg)
	DDX_Control(pDX, IDC_SLIDER_SENDRATIO, m_SendSlider);
	DDX_Control(pDX, IDC_EDIT_RECEIVE, m_ReceiveEdit);
	DDX_Control(pDX, IDC_COMBO_SENDTYPE, m_SendType);
	DDX_Control(pDX, IDC_MSCOMM1, m_CommCtrl);
	DDX_Check(pDX, IDC_CHECK_48, m_4Or8);
	DDX_Check(pDX, IDC_CHECK_ADDRESS, m_Address);
	DDX_Check(pDX, IDC_CHECK_ASCII, m_Ascii);
	DDX_Check(pDX, IDC_CHECK_HEX, m_Hex);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPCSerialTestDlg, CDialog)
	//{{AFX_MSG_MAP(CPCSerialTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SET_PRO, OnSetPro)
	ON_BN_CLICKED(IDC_OPEN_PORT, OnOpenPort)
	ON_BN_CLICKED(IDC_HAND_OUTPUT, OnHandOutput)
	ON_BN_CLICKED(IDC_AUTO_OUTPUT, OnAutoOutput)
	ON_BN_CLICKED(IDC_STOP_REC, OnStopRec)
	ON_BN_CLICKED(IDC_START_REC, OnStartRec)
	ON_BN_CLICKED(IDC_SHUT_PORT, OnShutPort)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_DELETE_RECEIVE, OnDeleteReceive)
	ON_CBN_SELCHANGE(IDC_COMBO_SENDTYPE, OnSelchangeComboSendtype)
	ON_BN_CLICKED(IDC_CHECK_HEX, OnCheckHex)
	ON_BN_CLICKED(IDC_CHECK_48, OnCheck48)
	ON_BN_CLICKED(IDC_CHECK_ADDRESS, OnCheckAddress)
	ON_BN_CLICKED(IDC_CHECK_ASCII, OnCheckAscii)
	ON_EN_CHANGE(IDC_EDIT_SEND, OnChangeEditSend)
	ON_EN_HSCROLL(IDC_EDIT_RECEIVE, OnHscrollEditReceive)
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_RECORD, OnRecord)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPCSerialTestDlg message handlers

BOOL CPCSerialTestDlg::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
	m_SendType.InsertString (0, "按ASCII码");//对于下拉列表框和滑动条的编程初始化
	m_SendType.InsertString (1, "按16进制");
	m_SendType.SetCurSel (m_nOutPutMode);

	m_SendSlider.SetRange (1, 32);//滑动条的最大和最小值
	m_SendSlider.SetPos (4);//初始化滑动条的位置

	OpenPort ();
	init();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

//属性设置按钮单击响应函数
void CPCSerialTestDlg::OnSetPro() //声明为属性对话框类对象
{
	// TODO: Add your control notification handler code here
	if (m_ProDlg.DoModal() == IDOK)//DoModal()函数的意义:启动对话框并返回对话框的值/此对话框为模态对话框
	{                                                     //如果开启的对话框是确定,则执行
		m_nPort = m_ProDlg.m_nPort;//子对话框类中的成员变量
		m_nTime = m_ProDlg.m_nTime;
		m_strSettings = m_ProDlg.m_strSettings;
	}//从属性对话框类中提取成员变量的数据,通过传递给主对话框只能感的成员变量。一种值传递的方式

	OpenPort ();//属性设置结束后,开串口
}


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

void CPCSerialTestDlg::OnOnCommMscomm1() 
{
	// TODO: Add your control notification handler code here
	VARIANT variant_inp;//函数内部变量 变量类型
	COleSafeArray safearray_inp;//串口数据结构,支持SAFEARRAY结构
	LONG len,k;
	BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.//接收数据缓冲区的长度设置
	CString strtemp;
	
	if(m_bReceive)//如果接收允许
	{
		if(m_CommCtrl.GetCommEvent () == 2)               //事件值为2表示接收缓冲区内有字符
		{
			variant_inp = m_CommCtrl.GetInput ();         //读缓冲区
			safearray_inp = variant_inp;                  //VARIANT型变量转换为ColeSafeArray型变量
			len = safearray_inp.GetOneDimSize ();         //得到有效数据长度 SAFEARRQY数据结构
			for(k=0; k<len; k++)
			{
				safearray_inp.GetElement (&k,rxdata+k);   //转换为BYTE型数组,负责接收数据入缓冲区
			}
			m_ReceiveEdit.AppendData (rxdata,len);//数据连接
		}
	}

	UpdateData(FALSE);                                    //更新编辑框内容 实现对话框与文档框的数据传递
}


void CPCSerialTestDlg::OpenPort ()
{
	//若串口已经打开,则先关闭
	if(m_CommCtrl.GetPortOpen ())
	{
		m_CommCtrl.SetPortOpen (FALSE);
	}

	//根据当前选择的端口号打开相应的串口
	m_CommCtrl.SetCommPort (m_nPort);

	//打开串口
	if (!m_CommCtrl.GetPortOpen ())
	{
		m_CommCtrl.SetPortOpen (TRUE);//开串口过程;
	}

	else
	{
		AfxMessageBox ("无法打开串行口");

⌨️ 快捷键说明

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