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

📄 rs232dlg.cpp

📁 RS232串口通信源代码
💻 CPP
字号:
// RS232Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "RS232.h"
#include "RS232Dlg.h"

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

HANDLE hCom;//全局变量,串口句柄

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

/////////////////////////////////////////////////////////////////////////////
// CRS232Dlg dialog

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

void CRS232Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRS232Dlg)
	DDX_Text(pDX, IDC_EDIT, m_edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRS232Dlg, CDialog)
	//{{AFX_MSG_MAP(CRS232Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RECEIVE, OnReceive)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	//ON_MESSAGE(WM_EDIT,OnEdit)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRS232Dlg message handlers

BOOL CRS232Dlg::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
	SetTimer(1,1000,NULL);
	hCom=CreateFile("COM1",//COM1口
		GENERIC_READ|GENERIC_WRITE,//允许读和写
		0,//独占方式
		NULL,
		OPEN_EXISTING,//打开而不是创建
		0,//同步方式
		NULL);
	if(hCom==(HANDLE)-1)
	{
	AfxMessageBox("打开COM失败!");
	return FALSE;
	}
	SetupComm(hCom,1024,1024); //输入缓冲区和输出缓冲区的大小都是1024
	COMMTIMEOUTS TimeOuts;
	//设定读超时
	TimeOuts.ReadIntervalTimeout=10;
	TimeOuts.ReadTotalTimeoutMultiplier=0;
	TimeOuts.ReadTotalTimeoutConstant=0;
	//在读一次输入缓冲区的内容后读操作就立即返回,
	//而不管是否读入了要求的字符。
	//设定写超时
	TimeOuts.WriteTotalTimeoutMultiplier=100;
	TimeOuts.WriteTotalTimeoutConstant=500;
	SetCommTimeouts(hCom,&TimeOuts); //设置超时
	DCB dcb;
	GetCommState(hCom,&dcb);
	dcb.BaudRate=4800; //波特率为4800
	dcb.ByteSize=8; //每个字节有8位
	dcb.Parity=NOPARITY; //无奇偶校验位
	dcb.StopBits=TWOSTOPBITS; //两个停止位
	SetCommState(hCom,&dcb);
	PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
	

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

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

void CRS232Dlg::OnReceive()
{
	// TODO: Add your control notification handler code here
	
	char str[1000];
	memset(str,'*',1000);
	DWORD wCount=1000;//读取的字节数
	BOOL bReadStat;
	bReadStat=ReadFile(hCom,str,wCount,&wCount,NULL);

	//if(!bReadStat)
		//AfxMessageBox("读串口失败!");
	
	PurgeComm(hCom, PURGE_TXABORT|
		PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
	
	m_edit=str;

	
	
	UpdateData(FALSE);
	


}

void CRS232Dlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	OnCancel();
}


void CRS232Dlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	CloseHandle(hCom);//程序退出时关闭串口
	CDialog::OnClose();
}
/*CString str=(char*)lParam;
	CString strTemp;
	GetDlgItemText(IDC_RECEIVE,strTemp);
	str+="\r\n";
	str+=strTemp;
	SetDlgItemText(IDC_RECEIVE,str);WPARAM wParam,LPARAM lParam*/





/*void CRS232Dlg::OnEdit()
{
    CString strTemp1;
	CString strTemp2;
	GetDlgItemText(IDC_EDIT,strTemp2);
	strTemp1+="\r\n";
	strTemp1+=strTemp2;
	SetDlgItemText(IDC_EDIT,strTemp1);

}*/

void CRS232Dlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	//创建一个文件:fileL0,文件名:gps.txt
	CFile fileL0;
	fileL0.Open("gps.txt",CFile::modeCreate|CFile::modeWrite);
	//将编辑框IDC_EDIT中的内容复制到创建的文件中
	CString str1;
	GetDlgItem(IDC_EDIT)->GetWindowText(str1);
	MessageBox(str1); //能正确显示出编辑框中的内容
	fileL0.WriteHuge(str1,sizeof(str1)*100);
	fileL0.Close();

}

void CRS232Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	OnReceive();
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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