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

📄 msgproxydlg.cpp

📁 向串口发送数据
💻 CPP
字号:
// MsgProxyDlg.cpp : 幚憰僼傽僀儖
//

#include "stdafx.h"
#include "MsgProxy.h"
#include "MsgProxyDlg.h"
#include ".\msgproxydlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 傾僾儕働乕僔儑儞偺僶乕僕儑儞忣曬偵巊傢傟傞 CAboutDlg 僟僀傾儘僌

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 僟僀傾儘僌 僨乕僞
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 僒億乕僩

// 幚憰
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CMsgProxyDlg 僟僀傾儘僌



CMsgProxyDlg::CMsgProxyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMsgProxyDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMsgProxyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMsgProxyDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
END_MESSAGE_MAP()


// CMsgProxyDlg 儊僢僙乕僕 僴儞僪儔

BOOL CMsgProxyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// "僶乕僕儑儞忣曬..." 儊僯儏乕傪僔僗僥儉 儊僯儏乕偵捛壛偟傑偡丅

	// IDM_ABOUTBOX 偼丄僔僗僥儉 僐儅儞僪偺斖埻撪偵側偗傟偽側傝傑偣傫丅
	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);
		}
	}

	// 偙偺僟僀傾儘僌偺傾僀僐儞傪愝掕偟傑偡丅傾僾儕働乕僔儑儞偺儊僀儞 僂傿儞僪僂偑僟僀傾儘僌偱側偄応崌丄
	//  Framework 偼丄偙偺愝掕傪帺摦揑偵峴偄傑偡丅
	SetIcon(m_hIcon, TRUE);			// 戝偒偄傾僀僐儞偺愝掕
	SetIcon(m_hIcon, FALSE);		// 彫偝偄傾僀僐儞偺愝掕

	// TODO: 弶婜壔傪偙偙偵捛壛偟傑偡丅
	
	return TRUE;  // 僼僅乕僇僗傪僐儞僩儘乕儖偵愝掕偟偨応崌傪彍偒丄TRUE 傪曉偟傑偡丅
}

void CMsgProxyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 僟僀傾儘僌偵嵟彫壔儃僞儞傪捛壛偡傞応崌丄傾僀僐儞傪昤夋偡傞偨傔偺
//  壓偺僐乕僪偑昁梫偱偡丅僪僉儏儊儞僩/價儏乕 儌僨儖傪巊偆 MFC 傾僾儕働乕僔儑儞偺応崌丄
//  偙傟偼丄Framework 偵傛偭偰帺摦揑偵愝掕偝傟傑偡丅

void CMsgProxyDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 昤夋偺僨僶僀僗 僐儞僥僉僗僩

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 僋儔僀傾儞僩偺巐妏宍椞堟撪偺拞墰
		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;

		// 傾僀僐儞偺昤夋
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//儐乕僓乕偑嵟彫壔偟偨僂傿儞僪僂傪僪儔僢僌偟偰偄傞偲偒偵昞帵偡傞僇乕僜儖傪庢摼偡傞偨傔偵丄
//  僔僗僥儉偑偙偺娭悢傪屇傃弌偟傑偡丅
HCURSOR CMsgProxyDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CMsgProxyDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	HANDLE hComm;
	DWORD dwError;
	hComm = CreateFile(_T("COM1"), GENERIC_READ | GENERIC_WRITE, 0, NULL,
						OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); 
	if(hComm == INVALID_HANDLE_VALUE)
	{
		// deal with the error code 
		switch (dwError = GetLastError()) 
		{ 
		case ERROR_ACCESS_DENIED: 
			{ 
				// Other Access is denied 
			} 
			break;
		default:
			// deal with other error cases 
			MessageBox(_T("Error !"));
			break;
		}
	}

	SetupComm(hComm, 2*1024*1024, 1024*1024); 

	PurgeComm(hComm, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); 

    DCB dcb; 
	GetCommState(hComm, &dcb); 

	dcb.BaudRate = CBR_115200;
	dcb.fBinary = TRUE;
	dcb.fParity = FALSE; 
	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;

	SetCommState(hComm, &dcb); 

	COMMTIMEOUTS ctoCommTimeOuts;
//	GetCommTimeouts(hComm, &ctoCommTimeOuts);

	ctoCommTimeOuts.ReadIntervalTimeout = MAXDWORD;
	ctoCommTimeOuts.ReadTotalTimeoutMultiplier = 0;
	ctoCommTimeOuts.ReadTotalTimeoutConstant = 0;
	ctoCommTimeOuts.WriteTotalTimeoutMultiplier = 0;
	ctoCommTimeOuts.WriteTotalTimeoutConstant = 0;

    SetCommTimeouts(hComm, &ctoCommTimeOuts);

	OVERLAPPED olOverLapped;

	olOverLapped.Offset = 0;
	olOverLapped.OffsetHigh = 0;
	olOverLapped.hEvent = CreateEvent(NULL,			// pointer to security attributes 
									FALSE,			// flag for manual-reset event 
									TRUE,			// flag for initial state 
									"WriteFinish");	// pointer to event-object name 
	BOOL bWriteResult;
	DWORD dwBytesWritten;
	bWriteResult = WriteFile(hComm, "0101", 5, &dwBytesWritten, &olOverLapped);
	if (!bWriteResult) 
	{ 
		// deal with the error code 
		switch (dwError = GetLastError()) 
		{ 
		case ERROR_HANDLE_EOF: 
			{ 
				// we're reached the end of the file 
				// during the call to ReadFile 

				// code to handle that 
			} 
			break;
		case ERROR_IO_PENDING: 
			{ 
				// asynchronous i/o is still in progress 

				// check on the results of the asynchronous read 
				bWriteResult = GetOverlappedResult(hComm, &olOverLapped, 
					&dwBytesWritten, TRUE); 

				// if there was a problem ... 
				if (!bWriteResult) 
				{ 
					// deal with the error code 
					switch (dwError = GetLastError()) 
					{ 
					case ERROR_HANDLE_EOF: 
						{ 
							// we're reached the end of the file 
							//during asynchronous operation 
						} 
						break;
					default:
						// deal with other error cases 
						MessageBox(_T("Error !"));
						break;
					}
				}
			}
			break;
		default:
			// deal with other error cases 
			MessageBox(_T("Error !"));
			break;
		}
	} 

	olOverLapped.Offset = 0;
	olOverLapped.OffsetHigh = 0;
	olOverLapped.hEvent = CreateEvent(NULL,			// pointer to security attributes 
									FALSE,			// flag for manual-reset event 
									TRUE,			// flag for initial state 
									"ReadFinish");	// pointer to event-object name 


	char ch[10];
	BOOL bReadResult;
	DWORD dwBytesRead;
	bReadResult = ReadFile(hComm, &ch, 5, &dwBytesRead, &olOverLapped);
	if (!bReadResult) 
	{ 
		// deal with the error code 
		switch (dwError = GetLastError()) 
		{ 
		case ERROR_HANDLE_EOF: 
			{ 
				// we're reached the end of the file 
				// during the call to ReadFile 

				// code to handle that 
			} 
			break;
		case ERROR_IO_PENDING: 
			{ 
				// asynchronous i/o is still in progress 

				// check on the results of the asynchronous read 
				bReadResult = GetOverlappedResult(hComm, &olOverLapped, 
					&dwBytesRead, TRUE); 

				// if there was a problem ... 
				if (!bReadResult) 
				{ 
					// deal with the error code 
					switch (dwError = GetLastError()) 
					{ 
					case ERROR_HANDLE_EOF: 
						{ 
							// we're reached the end of the file 
							//during asynchronous operation 
						}
						break;
					default:
						// deal with other error cases 
						MessageBox(_T("Error !"));
						break;
					}
				}
			}
			break;
		default:
			// deal with other error cases 
			MessageBox(_T("Error !"));
			break;
		}
	} 

	CloseHandle(hComm);
	MessageBox(ch);
}

⌨️ 快捷键说明

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