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

📄 newcomdlg.cpp

📁 串口通信,R232.使用VC++ 编写.非常难得。欢迎大家传播下载
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// NewComDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NewCom.h"
#include "NewComDlg.h"

#include "InSetDlg.h"
#include "OutSetDlg.h"
#include "ViewNum.h"
#include "DlgCurve.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 };
	CString	m_ZIZHUAN;
	//}}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)
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	m_ZIZHUAN = _T("");
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Text(pDX, IDC_ZIZHUAN, m_ZIZHUAN);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNewComDlg dialog

CNewComDlg::CNewComDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNewComDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNewComDlg)
	m_RXD = _T("");
	m_CheckChar = FALSE;
	m_status = _T("");
	m_TXD = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_FileName="Decimal";
	m_Brush.CreateSolidBrush (RGB(255,255,255));
	m_TimeNum=0;
	m_Time=1000;
	m_OutputChar=FALSE;
	m_SendFile="";
}
void CNewComDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNewComDlg)
	DDX_Text(pDX, IDC_RXD, m_RXD);
	DDX_Check(pDX, IDC_CHECK_CHAR, m_CheckChar);
	DDX_Text(pDX, IDC_STATUS, m_status);
	DDX_Text(pDX, IDC_TXD, m_TXD);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNewComDlg, CDialog)
	//{{AFX_MSG_MAP(CNewComDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_COMMAND(IDM_INPUT_SETTING, OnInputSetting)
	ON_COMMAND(IDM_SAVE, OnSave)
	ON_BN_CLICKED(IDB_OPEN_CLOSE, OnOpenClose)
	ON_BN_CLICKED(IDM_EXIT, OnExit)
	ON_BN_CLICKED(IDB_INCLEAR, OnInclear)
	ON_COMMAND(IDM_OUTPUT_SETTING, OnOutputSetting)
	ON_COMMAND(IDM_ABOUT, OnAbout)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_CLEAROUTPUT, OnClearoutput)
	ON_BN_CLICKED(IDC_SENDMUL, OnSendmul)
	ON_COMMAND(IDM_NUM, OnNum)
	ON_UPDATE_COMMAND_UI(IDM_NUM, OnUpdateNum)
	ON_COMMAND(IDM_CURVE, OnCurve)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNewComDlg message handlers

BOOL CNewComDlg::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
	DWORD style=WS_VISIBLE|WS_CHILD;
	if(!m_Comm.Create (NULL,style,CRect(0,0,0,0),this,IDC_MSCOMM1))
	{
		AfxMessageBox("创建串口失败");
		return FALSE;
	}
	else
	{
		m_Comm.SetInBufferSize (2048);
		m_Comm.SetInBufferCount (0);
		m_Comm.SetInputMode (1);
		m_Comm.SetInputLen (19);
		m_Comm.SetRThreshold (19);
		//m_Comm.SetDTREnable (1);
		//m_Comm.SetRTSEnable (1);
		
		m_Comm.SetOutBufferSize (1024);
		m_Comm.SetSThreshold (1);
		
		//CEdit* pEdit=(CEdit*)GetDlgItem(IDC_RXD);
		//pEdit->LineScroll (pEdit->GetLineCount ());
        m_status="串口初始化完成...";
	}
	SetTimer(2,1000,NULL);
    UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNewComDlg::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 CNewComDlg::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();
	}
//////////////////////////////////////////////////
	/* CClientDC dc(this);
	 CBrush brush(RGB(0,0,0));
	 CPen pen(PS_SOLID,5,RGB(255,0,0));
     CFont ft;
	 ft.CreatePointFont (120,"宋体",NULL);
     CRect rect;
	 GetDlgItem(IDC_TIME)->GetClientRect(&rect);
	 ScreenToClient(&rect);
	 dc.SelectObject (&brush);
	 dc.SelectObject (&pen);
	 dc.SelectObject (&ft);
	 dc.SetTextColor (RGB(255,0,0));
	 dc.SetBkColor (RGB(0,0,0));
	 
	 //dc.Rectangle (&rect);
	 dc.TextOut(rect.left ,rect.Height ()/2,str);*///用于时时显示
	 
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNewComDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CNewComDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1)
	{
		CString str;
		if(m_OutputChar)
		{	
		    str.Format ("%d",m_TimeNum);
		    m_status="发送字符"+str;	    		    		
		}
		else
		{
			str.Format ("02X",m_TimeNum);//有问题
			m_status="发送十六进制"+str;
		}
		m_Comm.SetOutput (COleVariant(str));
		m_TimeNum++;
		UpdateData(FALSE);
	}
	if(nIDEvent==2)
	{
		time=CTime::GetCurrentTime ();
		str=time.Format ("%H:%M:%S");
		pdlg.m_static1 =str;
		//MessageBox(str);
		//GetDlgItem (IDC_TIME)
		//Invalidate();
	}
	CDialog::OnTimer(nIDEvent);
}

void CNewComDlg::OnInputSetting() 
{
	// TODO: Add your command handler code here
	InSetDlg dlg;
	if(IDOK==dlg.DoModal ())
	{
		CString str,strcom;
		str=dlg.m_combo2+","+Parity(dlg.m_combo3)+","+dlg.m_combo4+","+dlg.m_combo5;
		m_Comm.SetCommPort (dlg.m_combo1+1);
		m_Comm.SetSettings (str);
		
		strcom.Format ("%d",dlg.m_combo1+1);
		strcom="com"+strcom;
		m_status="接收状态:"+strcom+","+str;
		m_Comm.SetDTREnable (1);
	}
	UpdateData(FALSE);
}

void CNewComDlg::OnSave() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(FALSE);
	dlg.m_ofn .lpstrTitle ="接收串口文件存为";
	dlg.m_ofn .lpstrDefExt ="txt";
	dlg.m_ofn .lpstrFilter ="Text File(*.txt)\0*.txt\0All File(*.*)\0*.*\0\0";
	if(IDOK==dlg.DoModal ())
	{
		m_FileName=dlg.GetFileName ();
  
	}
}

void CNewComDlg::OnOpenClose() 
{
	// TODO: Add your control notification handler code here
	if(!m_Comm.GetPortOpen ())
	{
	    m_Comm.SetPortOpen (1);
	    this->SetDlgItemText(IDB_OPEN_CLOSE,"关闭串口");
	}
	else
	{
		m_Comm.SetPortOpen (0);
		this->SetDlgItemText(IDB_OPEN_CLOSE,"打开串口");
	}
}

void CNewComDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	DestroyWindow();  
}

CString CNewComDlg::Parity(CString str)
{
	if(str=="NULL无")
		return "n";
	else if(str=="ODD 奇")

⌨️ 快捷键说明

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