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

📄 txrxdlg.cpp

📁 一个非常好的串口程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TxRxDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TxRx.h"
#include "TxRxDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CTxRxDlg dialog

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

void CTxRxDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTxRxDlg)
	DDX_Control(pDX, IDC_EditTimeout, m_EditTimeout);
	DDX_Control(pDX, IDC_EditFile, m_EditFile);
	DDX_Control(pDX, IDC_ProgressTx, m_ProgressTx);
	DDX_Control(pDX, IDC_ProgressRx, m_ProgressRx);
	DDX_Control(pDX, IDC_StaticSendLED, m_LEDSend);
	DDX_Control(pDX, IDC_StaticNumber, m_StaticBaud);
	DDX_Control(pDX, IDC_StaticMemory, m_StaticMemory);
	DDX_Control(pDX, IDC_StaticLEDReceive, m_LEDReceive);
	DDX_Control(pDX, IDC_SliderMemory, m_SliderMemory);
	DDX_Control(pDX, IDC_SliderBaud, m_SliderBaud);
	DDX_Control(pDX, IDC_RadioData82, m_RadioData82);
	DDX_Control(pDX, IDC_RadioData81, m_RadioData81);
	DDX_Control(pDX, IDC_ComboCom, m_ComboCom);
	DDX_Control(pDX, IDC_ButtonFile, m_ButtonFile);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTxRxDlg, CDialog)
	//{{AFX_MSG_MAP(CTxRxDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RadioData81, OnRadioData81)
	ON_BN_CLICKED(IDC_RadioData82, OnRadioData82)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SliderBaud, OnReleasedcaptureSliderBaud)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SliderMemory, OnReleasedcaptureSliderMemory)
	ON_BN_CLICKED(IDC_ButtonFile, OnButtonFile)
	ON_BN_CLICKED(IDC_ButtonReceive, OnButtonReceive)
	ON_BN_CLICKED(IDC_ButtonExit, OnButtonExit)
	ON_BN_CLICKED(IDC_ButtonSend, OnButtonSend)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTxRxDlg message handlers

BOOL CTxRxDlg::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
	m_ButtonFile.SetBitmap(::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BitmapSaveLoad)));
	m_SliderBaud.SetRange(1,7);
	m_SliderBaud.SetTic(1);
	m_SliderMemory.SetRange(1,7);
	m_SliderMemory.SetTic(1);
	m_ComboCom.SetCurSel(0);

	m_RadioData81.SetCheck(1);
	m_Baud=_T("19200");
	m_BytesOfMemory=0x400;
	m_EditTimeout.SetWindowText(_T("1000"));

	CTxRxDlg::SendDlgItemMessage(IDC_RadioParityN,BST_CHECKED);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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


void CTxRxDlg::OnRadioData81() 
{
	// TODO: Add your control notification handler code here
	if (m_RadioData81.GetCheck())
	{
		m_RadioData82.SetCheck(0);
	}

	
}

void CTxRxDlg::OnRadioData82() 
{
	// TODO: Add your control notification handler code here
	if (m_RadioData82.GetCheck())
	{
		m_RadioData81.SetCheck(0);
	}
	
}

void CTxRxDlg::OnReleasedcaptureSliderBaud(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	switch (m_SliderBaud.GetPos())
	{
	case 7:
		m_Baud=_T("300");
		break;
	case 6:
		m_Baud=_T("600");
		break;
	case 5:
		m_Baud=_T("1200");
		break;
	case 4:
		m_Baud=_T("2400");
		break;
	case 3:
		m_Baud=_T("4800");
		break;
	case 2:
		m_Baud=_T("9600");
		break;
	case 1:
		m_Baud=_T("19200");
		break;
	}
	m_StaticBaud.SetWindowText(m_Baud);
	*pResult = 0;
}

void CTxRxDlg::OnReleasedcaptureSliderMemory(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	switch (m_SliderMemory.GetPos())
	{
	case 7:
		m_StaticMemory.SetWindowText(_T("Buffer = 64 KB"));
		m_BytesOfMemory=0x10000;
		break;
	case 6:
		m_StaticMemory.SetWindowText(_T("Buffer = 32 KB"));
		m_BytesOfMemory=0x8000;
		break;
	case 5:
		m_StaticMemory.SetWindowText(_T("Buffer = 16 KB"));
		m_BytesOfMemory=0x4000;
		break;
	case 4:
		m_StaticMemory.SetWindowText(_T("Buffer = 8 KB"));
		m_BytesOfMemory=0x2000;
		break;
	case 3:
		m_StaticMemory.SetWindowText(_T("Buffer = 4 KB"));
		m_BytesOfMemory=0x1000;
		break;
	case 2:
		m_StaticMemory.SetWindowText(_T("Buffer = 2 KB"));
		m_BytesOfMemory=0x800;
		break;
	case 1:
		m_StaticMemory.SetWindowText(_T("Buffer = 1 KB"));
		m_BytesOfMemory=0x400;
		break;
	}
	*pResult = 0;
}

void CTxRxDlg::OnButtonFile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog* fn;
	fn=new CFileDialog(TRUE,NULL,_T("Serial.bin"));
	if (!fn->DoModal())
		m_FileName=_T("Serial.bin");
	else
	{
		m_FileName=fn->GetPathName();
	}
	if (fn->GetPathName().IsEmpty())
		m_FileName=_T("Serial.bin");
	
	m_EditFile.SetWindowText(m_FileName);
	return;		
}


void CTxRxDlg::OnButtonReceive() 
{
	// TODO: Add your control notification handler code here
	
	if (m_FileName.IsEmpty())
	{
		MessageBox(_T("File has been not specified.Cancel Operation!"));
		return;
	}
	HANDLE hCom;
	LPDCB lpDcb;
	int i=m_ComboCom.GetCurSel();
	switch (i)
	{
	case 0:
		hCom=CreateFile("\\.\\COM1:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;
	case 1:
		hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;
	case 2:
		hCom=CreateFile("\\.\\COM3:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;

⌨️ 快捷键说明

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