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

📄 myserialdlg.cpp

📁 EVC串口通信 x86下.希望大家使用愉快。
💻 CPP
字号:
// MyserialDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Myserial.h"
#include "MyserialDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyserialDlg dialog

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

void CMyserialDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyserialDlg)
	DDX_Text(pDX, IDC_EDIT_WRITE, m_strSendEdit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyserialDlg, CDialog)
	//{{AFX_MSG_MAP(CMyserialDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_WRITE, OnWrite)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyserialDlg message handlers

BOOL CMyserialDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here

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



void CMyserialDlg::OnButton1() //关闭串口
{
	// TODO: Add your control notification handler code here

		if (m_ExitThreadEvent != NULL)
	{
		SetEvent(m_ExitThreadEvent);					/* 通知线程退出 */
		Sleep(1000);
		CloseHandle(m_ExitThreadEvent);
		m_ExitThreadEvent = NULL;
	}
ClosePort();
}

void CMyserialDlg::OnWrite() 
{
  
 int rc;
UpdateData(TRUE);
int len = m_strSendEdit.GetLength();				 /* 取得输入字符串长度 */
	char *psendbuf = new char[len];
DWORD dwactlen;
	for(int i = 0; i < len;i++)
		psendbuf[i] = (char)m_strSendEdit.GetAt(i);		 /* 转换为单字节字符 */

	rc=WriteFile(m_hSerial, psendbuf, len, &dwactlen, NULL);	 /* 从串口发送数据 */
	
	delete[] psendbuf;



}

void CMyserialDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
		
m_hSerial = CreateFile(L"COM1:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(m_hSerial == NULL)
{
	MessageBox(_T("无法打开端口或端口已打开!请检查是否已被占用."));
}

///配置串口

DCB PortDCB; 
PortDCB.DCBlength = sizeof(DCB); 
// 默认串口参数
GetCommState(m_hSerial, &PortDCB);
PortDCB.BaudRate = 9600; // baud
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8 
PortDCB.Parity = NOPARITY; 
PortDCB.StopBits = ONE5STOPBITS; 
if (! SetCommState(m_hSerial, &PortDCB))
{
MessageBox(_T("配置串口失败"));
}

////配置超时值

COMMTIMEOUTS CommTimeouts;

GetCommTimeouts(m_hSerial, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD; 
CommTimeouts.ReadTotalTimeoutMultiplier = 10; 
CommTimeouts.ReadTotalTimeoutConstant = 10; 
CommTimeouts.WriteTotalTimeoutMultiplier = 50; 
CommTimeouts.WriteTotalTimeoutConstant = 100; 
if (!SetCommTimeouts(m_hSerial, &CommTimeouts))
{
MessageBox(_T("不能设置超时参数"));
}

	DWORD IDThread;
	HANDLE hRecvThread;	
	m_ExitThreadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);	
	/* 创建串口接收线程退出事件*/

	// 创建串口接收线程
	hRecvThread = CreateThread(0, 0, CommRecvTread, this, 0, &IDThread);
	if (hRecvThread == NULL) 
	{
		MessageBox(_T("创建接收线程失败!"));
		return;
	}	
	CloseHandle(hRecvThread);

}
 DWORD CMyserialDlg::CommRecvTread(LPVOID lparam)
{
	DWORD dwLength;
	char *recvBuf = new char[1024];
	CMyserialDlg *pDlg = (CMyserialDlg*)lparam;
	while(TRUE)
	{																/* 等待线程退出事件 */
		if (WaitForSingleObject(pDlg->m_ExitThreadEvent, 0) == WAIT_OBJECT_0)
			break;	

		if (pDlg->m_hSerial != INVALID_HANDLE_VALUE)
		{															/* 从串口读取数据 */
			BOOL fReadState = ReadFile(pDlg->m_hSerial, recvBuf, 1024, &dwLength, NULL);
			if(!fReadState)
			{
	//	AfxMessageBox(_T("无法从串口读取数据!"));
			}
			else
			{
				if(dwLength != 0)
				OnCommRecv(pDlg,recvBuf,dwLength);
			}
		}
	}		

	delete[] recvBuf;
	return 0;
}
 void CALLBACK CMyserialDlg::OnCommRecv(CWnd* pWnd, char *buf, int buflen)
{
	CString tmp;

	CMyserialDlg * pDlg = (CMyserialDlg*)pWnd;
	CEdit *pRecvStrEdit = (CEdit*)pDlg->GetDlgItem(IDC_REC_DISP);
														/* 取得控件指针 */
	for (int i = 0; i < buflen; i++, buf++)
	{
		tmp.Format(_T("%c"), *buf);						/* 将字符转换为字符串 */
		pDlg->m_strRecDisp += tmp;
	}
	pRecvStrEdit->SetWindowText(pDlg->m_strRecDisp);	/* 显示在窗口上 */
}


BOOL CMyserialDlg::ClosePort()
{
	if(m_hSerial != NULL) 
{
SetCommMask(m_hSerial, 0);		
PurgeComm(m_hSerial, PURGE_TXCLEAR | PURGE_RXCLEAR);	/* 清除收/发缓冲 */
CloseHandle(m_hSerial);								/* 关闭串口操作句柄 */
m_hSerial = NULL;
return TRUE;
}
return FALSE;
}

void CMyserialDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
		if (m_ExitThreadEvent != NULL)
	{
		SetEvent(m_ExitThreadEvent);					/* 通知线程退出 */
		Sleep(1000);
		CloseHandle(m_ExitThreadEvent);
		m_ExitThreadEvent = NULL;
	}
ClosePort();
}


⌨️ 快捷键说明

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