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

📄 commserialdlg.cpp

📁 串口通信实验代码,使用VC++开发
💻 CPP
字号:
// CommSerialDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CommSerial.h"
#include "CommSerialDlg.h"
#include "afxmt.h"
#include ".\commserialdlg.h"

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

#define BUFFER_SIZE	2048

HANDLE	m_hCommPort;		//保存打开的串行口设备句柄

UINT ReadChar(PVOID hWnd);	//读取字符线程

char RecvBuf[BUFFER_SIZE], SendBuf[BUFFER_SIZE];
UINT RecvPTR;

CEvent	SendEvent(0,true,0,0),RecvEvent(0,true,0,0);
OVERLAPPED	SendOV,RecvOV;


/////////////////////////////////////////////////////////////////////////////
// CCommSerialDlg dialog

CCommSerialDlg::CCommSerialDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCommSerialDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCommSerialDlg)
		// 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 CCommSerialDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCommSerialDlg)
	DDX_Control(pDX, IDOK, m_OK);
	DDX_Control(pDX, IDC_SEND, m_Send);
	DDX_Control(pDX, IDC_RECV, m_Receive);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCommSerialDlg, CDialog)
	//{{AFX_MSG_MAP(CCommSerialDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCommSerialDlg message handlers

BOOL CCommSerialDlg::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
	RecvPTR=0;

	DCB	CommDCB;	//串行口的设备控制块

	m_hCommPort=CreateFile ("COM1",		//打开串行口
		GENERIC_READ|GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
		NULL);
	if(m_hCommPort==INVALID_HANDLE_VALUE)
		{
			AfxMessageBox("系统无法打开此串口设备(CreateFile)!");
			PostMessage(WM_QUIT,0,0);
			return false;
		}
	
	if(!GetCommState(m_hCommPort,&CommDCB)) //得到原来的串口参数
	{
		AfxMessageBox("无法得到设置串口状态(GetCommState)!");
		PostMessage(WM_QUIT,0,0);
		return false;
	}
  
	CommDCB.BaudRate =CBR_9600;	//设置新的串口参数
	CommDCB.ByteSize =8; 
	CommDCB.Parity = NOPARITY; 
    CommDCB.StopBits = ONESTOPBIT ;
	CommDCB.fBinary = TRUE ;
	CommDCB.fParity = FALSE; 

   	if(!SetCommState(m_hCommPort, &CommDCB))
	{
		AfxMessageBox("无法设置串口状态(SetCommState)!");
		PostMessage(WM_QUIT,0,0);
		return false;
	} 

	if (!SetupComm(m_hCommPort,BUFFER_SIZE,BUFFER_SIZE))		//设置缓冲区
	{
		AfxMessageBox("串口缓冲区设置错误(SetupComm)!");
		PostMessage(WM_QUIT,0,0);
		return false;
	}
	if (!PurgeComm(m_hCommPort, PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT)) //清除所有接收和发送缓冲区中的数据
	{
		AfxMessageBox("清空串口错误(PurgeComm)!");
		PostMessage(WM_QUIT,0,0);
		return false;
	}

	memset(&SendOV,0,sizeof(SendOV));		//发送
	SendOV.hEvent=SendEvent;
	memset(&RecvOV,0,sizeof(RecvOV));		//发送
	RecvOV.hEvent=RecvEvent;

	
	AfxBeginThread(ReadChar,NULL,THREAD_PRIORITY_NORMAL);
	
	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 CCommSerialDlg::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 CCommSerialDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

UINT ReadChar(PVOID hWnd)
{
	CString		ShowStr;
	ULONG		ReadLen;

	while (true)		//只要线程运行,就监视端口是否收到数据
	{
		ReadFile(m_hCommPort,&RecvBuf[RecvPTR],1,&ReadLen,&RecvOV); //读字符
		GetOverlappedResult(m_hCommPort,&RecvOV,&ReadLen,TRUE);		//等待读状态完成
		if (ReadLen>0)
		{
			if (RecvBuf[RecvPTR]!=NULL)	//不是一行的最后一个字符
				RecvPTR++;
			else
			{
				ShowStr="RECV:";
				ShowStr=ShowStr+RecvBuf;
				CCommSerialApp* pApp=(CCommSerialApp*)AfxGetApp();
				CCommSerialDlg* pDlg=(CCommSerialDlg*)pApp->m_pMainWnd;
				pDlg->m_Receive.InsertString (0,ShowStr);

				RecvPTR=0;
			}
		}
		RecvEvent.ResetEvent();		//复位事件变量
	}

	return 0;
}

void CCommSerialDlg::OnOK() 
{
	// TODO: Add extra validation here
	ULONG SendLen,SentLen;
    CString ShowStr="SEND:";
	
	//获取要发送的字符串
	SendLen=m_Send.GetWindowText(SendBuf,BUFFER_SIZE-1);
    if (SendLen==0)
	{
		AfxMessageBox("请输入要传输的数据");
		return;
	}
	SendBuf[SendLen++]=NULL;
	
	m_Send.EnableWindow(FALSE);		
	m_OK.EnableWindow (FALSE);

	WriteFile(m_hCommPort,SendBuf,SendLen,&SentLen,&SendOV);
	GetOverlappedResult(m_hCommPort,&SendOV,&SentLen,TRUE);		//等待发送完成
	SendEvent.ResetEvent();

	m_Send.EnableWindow(TRUE);
	m_OK.EnableWindow (TRUE);
	
	ShowStr=ShowStr+SendBuf;		//显示发送的消息
	m_Receive.InsertString (0,(LPCTSTR)ShowStr);
    m_Send.SetWindowText(NULL);
	m_Send.SetFocus();

	//CDialog::OnOK();
}

void CCommSerialDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	CDialog::OnCancel();
}

BOOL CCommSerialDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	CloseHandle(m_hCommPort); //关闭串行口

	return CDialog::DestroyWindow();
}

















void CCommSerialDlg::OnBnClickedSend()
{
	// TODO: 在此添加控件通知处理程序代码
}

void CCommSerialDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	OnOK();
}

⌨️ 快捷键说明

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