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

📄 10dlg.cpp

📁 常用的串行通信方式。在 Visual C++的编程中
💻 CPP
字号:
// 10Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "10.h"
#include "10Dlg.h"
#include "DlgProxy.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()

/////////////////////////////////////////////////////////////////////////////
// CMy10Dlg dialog

IMPLEMENT_DYNAMIC(CMy10Dlg, CDialog);

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

CMy10Dlg::~CMy10Dlg()
{
	// If there is an automation proxy for this dialog, set
	//  its back pointer to this dialog to NULL, so it knows
	//  the dialog has been deleted.
	if (m_pAutoProxy != NULL)
		m_pAutoProxy->m_pDialog = NULL;
}

void CMy10Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy10Dlg)
	DDX_Control(pDX, IDC_MSCOMM1, m_Comm);
	DDX_Text(pDX, IDC_EDIT_COMDATA, m_comdata);
	DDX_Text(pDX, IDC_EDIT2, m_gpsdata);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy10Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy10Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_RECEIVEGPSDATA, OnbuttonStartreceivegps)
	ON_BN_CLICKED(IDC_BUTTON_ENDRECEIVEGPSDATA, OnbuttonEndreceivegps)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy10Dlg message handlers

BOOL CMy10Dlg::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_Comm.GetPortOpen()) 
m_Comm.SetPortOpen(FALSE); 

m_Comm.SetCommPort(1); //选择com1 
if( !m_Comm.GetPortOpen()) 
m_Comm.SetPortOpen(TRUE);//打开串口 
else 
AfxMessageBox("cannot open serial port"); 

m_Comm.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位 
m_Comm.SetInputMode(1); //1:表示以二进制方式检取数据 
m_Comm.SetRThreshold(1); 
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件 
m_Comm.SetInputLen(0); //设置当前接收区数据长度为0 
m_Comm.GetInput();//先预读缓冲区以清除残留数据 

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

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

// Automation servers should not exit when a user closes the UI
//  if a controller still holds on to one of its objects.  These
//  message handlers make sure that if the proxy is still in use,
//  then the UI is hidden but the dialog remains around if it
//  is dismissed.

void CMy10Dlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CMy10Dlg::OnOK() 
{
	if (CanExit())
		CDialog::OnOK();
}

void CMy10Dlg::OnCancel() 
{
	if (CanExit())
		CDialog::OnCancel();
}

BOOL CMy10Dlg::CanExit()
{
	// If the proxy object is still around, then the automation
	//  controller is still holding on to this application.  Leave
	//  the dialog around, but hide its UI.
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	return TRUE;
}

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

void CMy10Dlg::OnComm() 
{
	m_comdata.Empty(); 
	VARIANT m_input1; 
	COleSafeArray m_input2; 
	LONG length,i; 
	BYTE data[1024]; 
	CString str; 
	//接收缓冲区内字符 
	if(m_Comm.GetCommEvent()== 2) 
	{ 
		m_comdata.Empty(); 
		//读取缓冲区内的数据 
		m_input1=m_Comm.GetInput(); 
		//将 VARIANT型变量转换为 ColeSafeArray型变量 
		m_input2=m_input1; 
		//确定数据长度 
		length=m_input2.GetOneDimSize(); 
		//将数据转换为 BYTE型数组 
		for(i=0;i< length;i++ ) 
			m_input2.GetElement(&i,data+i); 
		//将数组转换为 CString型变量 
		for(i=0;i< length;i++ ) 
		{ 
			char a=* (char* )(data+i); 
			str.Format("%c",a); 
			m_comdata+= str;
			/*在上面的程序中,先将 VARIANT型变量转换为 ColeSafeArray型变量,再将其转换为 BYTE型数组,然后将数组转换为 CString型变量,以满足不同的变量类型显示数据的需要。*/
			
		//提取 GPS信号 
			CString m_zjz; 
			m_zjz=m_comdata; 
			//寻找 GPS信号的头标志 
			int s; 
			s=m_zjz.Find("$ GPRMC,"); 
			CString m_gps; 
			//NUM为所提取 GPS信号的长度 
			int NUM=250;
			m_gps=m_zjz.Mid(s,NUM); 
			int x; 
			x=m_gps.GetLength(); 
			//信号长度不符合要求,则拒绝接收 
			if (x != NUM)
			{ 
				m_gps.Empty(); 
			} 
			//寻找所需要的 GPS信号的头标志 
			int y; 
			y=m_gps.Find("$ GPRMC,"); 
			if (y != 0) 
			{ 
				m_gps.Empty(); 
			} 
			//m-gpsdata是已声明的类成员变量 
			//CString m_gpsdata;
			m_gpsdata=m_gps; 
		} 
	} 
}

void CMy10Dlg::OnbuttonStartreceivegps() 
{
	//判断串口的状态,如果是关闭状态,则打开 
	if(!m_Comm.GetPortOpen() ) 
		//打开串口 
		m_Comm.SetPortOpen(TRUE); 
	UpdateData(TRUE); 
	
}

void CMy10Dlg::OnbuttonEndreceivegps() 
{
	//判断串口的状态,如果是打开状态,则关闭 
	if( m_Comm.GetPortOpen() ) 
		m_Comm.SetPortOpen(FALSE); 
	UpdateData(FALSE); 

}

⌨️ 快捷键说明

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