www.gusucode.com > 串口通讯,用控件做的,可以做串口调试 > 串口通讯,用控件做的,可以做串口调试/ex7/ex7Dlg.cpp

    // ex7Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ex7.h"
#include "ex7Dlg.h"
#include "setting.h"
#include "filesavepathdlg.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()

/////////////////////////////////////////////////////////////////////////////
// CEx7Dlg dialog

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

void CEx7Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEx7Dlg)
	DDX_Control(pDX, IDC_MSCOMM2, m_mscomm1);
	DDX_Text(pDX, IDC_texttosend, m_texttosend);
	DDX_Text(pDX, IDC_revdata, m_revdatastr);
	DDX_Text(pDX, IDC_textfiletosend, m_filepath);
	DDX_Text(pDX, IDC_bytefiletosend, m_bytefilepath);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEx7Dlg, CDialog)
	//{{AFX_MSG_MAP(CEx7Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_setcom, Onsetcom)
	ON_BN_CLICKED(IDC_sendtext, Onsendtext)
	ON_COMMAND(ID_opencom, Onopencom)
	ON_BN_CLICKED(IDC_sendtextfile, Onsendtextfile)
	ON_BN_CLICKED(IDC_opentextfile, Onopentextfile)
	ON_COMMAND(ID_filesavepathset, Onfilesavepathset)
	ON_COMMAND(ID_helpme, Onhelpme)
	ON_BN_CLICKED(IDC_openfile, Onopenfile)
	ON_COMMAND(ID_closecom, Onclosecom)
	ON_COMMAND(ID_exit, Onexit)
	ON_COMMAND(ID_defaultcom, Ondefaultcom)
	ON_COMMAND(ID_defaultfolder, Ondefaultfolder)
	ON_EN_CHANGE(IDC_revdata, OnChangerevdata)
	ON_BN_CLICKED(IDC_sendbytefile, Onsendbytefile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx7Dlg message handlers

BOOL CEx7Dlg::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
	//m_mscomm.ShowWindow(SW_HIDE);
	// TODO: Add extra initialization here
	m_ncom="1";
	m_ncomset="9600,n,8,1";
	filesavepath="d:\\2.txt";
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CEx7Dlg::Onsetcom() 
{
	// TODO: Add your command handler code here
	Csetting setdlg;
	setdlg.m_ncom=m_ncom;
	setdlg.m_ncomset=m_ncomset;
	if(setdlg.DoModal()==IDOK)
	{
	m_ncom=setdlg.m_ncom;	
	m_ncomset=setdlg.m_ncomset;
	if(m_mscomm1.GetPortOpen()) m_mscomm1.SetPortOpen(FALSE);
	CString str= "当前使用com"+m_ncom+"\n其参数如下:"+m_ncomset;
	m_mscomm1.SetCommPort(atoi(m_ncom));
	m_mscomm1.SetSettings(m_ncomset);
	m_mscomm1.SetPortOpen(TRUE);
    MessageBox(str+"\n并打开了此端口","设置成功");
	
	}
}

void CEx7Dlg::Onsendtext() 
{
	// TODO: Add your control notification handler code here

	if(!m_mscomm1.GetPortOpen())
	{
		if(MessageBox("你还没打开com口,是否现在立即打开com口?","请先打开串口再操作",MB_YESNO)==IDYES)
			m_mscomm1.SetPortOpen(TRUE);
	}

	UpdateData();
	VARIANT sendvt;
	sendvt.vt=VT_BSTR;
	sendvt.bstrVal=m_texttosend.AllocSysString();
	m_mscomm1.SetOutput(COleVariant("sendstart(\r\n"));
	m_mscomm1.SetOutput(sendvt);
	m_mscomm1.SetOutput(COleVariant("\r\n)sendend"));

}

void CEx7Dlg::Onopencom() 
{
	// TODO: Add your command handler code here
	m_mscomm1.SetPortOpen(TRUE);
}






BEGIN_EVENTSINK_MAP(CEx7Dlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CEx7Dlg)
	ON_EVENT(CEx7Dlg, IDC_MSCOMM2, 1 /* OnComm */, OnOnCommMscomm2, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CEx7Dlg::OnOnCommMscomm2() 
{
	// TODO: Add your control notification handler code here
	VARIANT revvt;
	FILE *fp;	
	revvt.vt=VT_BSTR;
	revvt=m_mscomm1.GetInput();
	
	CString str=revvt.bstrVal,revstr;
	revstr=str;
	if(str.Find("sendstart")!=-1)
	{revstatus=1;
	m_revdatastr=m_revdatastr+"\r\n收到发来消息:\r\n";	
	}
	else if(str.Find("txtfilestart")!=-1)
	{revstatus=1;
	
	m_revdatastr=m_revdatastr+"\r\n检测到com口有文本文件到达,正在接收文件,保存于"+filesavepath+"\r\n";		

	fp=fopen(filesavepath,"wb");
	str=str.Mid(15,str.GetLength()-15);
	fwrite(str,str.GetLength(),1,fp);
	fclose(fp);

	}
	else if(str.Find("bytefilestart")!=-1)
	{revstatus=1;
	m_revdatastr=m_revdatastr+"\r\n检测到com口有二进制文件到达,正在接收文件,保存于"+filesavepath+"\r\n";
	fp=fopen(filesavepath,"wb");
	str=str.Mid(15,str.GetLength()-15);
	fwrite(str,str.GetLength(),1,fp);
	fclose(fp);
	}

	 m_revdatastr=m_revdatastr+revstr;

	
	UpdateData(FALSE);
	CEdit *ce=((CEdit *)GetDlgItem(IDC_revdata));
    ce->LineScroll(ce->GetLineCount()-1);
	
}

void CEx7Dlg::Onsendtextfile() 
{
	if(!m_mscomm1.GetPortOpen())
	{
		if(MessageBox("你还没打开com口,是否现在立即打开com口?","请先打开串口再操作",MB_YESNO)==IDYES)
			m_mscomm1.SetPortOpen(TRUE);
	}
	UpdateData();
	CFileStatus fs;
	CFile::GetStatus(m_filepath,fs);
	int size=fs.m_size;
	CString str;str.Format("将要发送的文件大小为:%d字节",size);
	MessageBox(str);
	FILE *fp;
	char  *filechar=new char[size];
	for (int i=0;i<size;i++)
	{
		filechar[i]='\0';
	}
	
	fp=fopen(m_filepath,"rb");
	
	fread(filechar,size,1,fp);
	fclose(fp);
   m_mscomm1.SetOutput(COleVariant("txtfilestart(\r\n"));  
   m_mscomm1.SetOutput(COleVariant(filechar));
   m_mscomm1.SetOutput(COleVariant("\r\n)txtfileend"));

}

void CEx7Dlg::Onopentextfile() 
{
CFileDialog cf(TRUE,NULL,NULL,OFN_HIDEREADONLY,"文本文件(*.txt)|*.txt");
if(cf.DoModal()==IDOK)
	{
CString filepath;
filepath=cf.GetPathName();	
GetDlgItem(IDC_textfiletosend)->SetWindowText(filepath);
}
}

void CEx7Dlg::Onfilesavepathset() 
{
Cfilesavepathdlg pathset;
pathset.m_filesavepath=filesavepath;
if(pathset.DoModal()==IDOK)
{filesavepath=pathset.m_filesavepath;MessageBox("如果有文件发来将自动保存到"+filesavepath);};	


}

void CEx7Dlg::Onhelpme() 
{
CAboutDlg dlg;
dlg.DoModal();	
}

void CEx7Dlg::Onopenfile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog cf(TRUE);
	if(cf.DoModal()==IDOK)
	{	
	CString filepath;
	filepath=cf.GetPathName();	
    GetDlgItem(IDC_bytefiletosend)->SetWindowText(filepath);
	
	}
}

void CEx7Dlg::Onclosecom() 
{
	// TODO: Add your command handler code here
	m_mscomm1.SetPortOpen(FALSE);
}

void CEx7Dlg::Onexit() 
{
	// TODO: Add your command handler code here
	exit(1);
}

void CEx7Dlg::Ondefaultcom() 
{
	m_ncom="1";
	m_ncomset="9600,n,8,1";
	if(m_mscomm1.GetPortOpen()) m_mscomm1.SetPortOpen(FALSE);
	CString str= "当前使用com"+m_ncom+"\n其参数如下:"+m_ncomset;
	m_mscomm1.SetCommPort(atoi(m_ncom));
	m_mscomm1.SetSettings(m_ncomset);
	m_mscomm1.SetPortOpen(TRUE);
    MessageBox(str+"\n并打开了新的端口","恢复默认设置成功");
}

void CEx7Dlg::Ondefaultfolder() 
{
	// TODO: Add your command handler code here
filesavepath="d:\\2.txt";	
MessageBox("如果有文件发来将自动保存到"+filesavepath);

}

void CEx7Dlg::OnChangerevdata() 
{


}

void CEx7Dlg::Onsendbytefile() 
{
	if(!m_mscomm1.GetPortOpen())
	{
		if(MessageBox("你还没打开com口,是否现在立即打开com口?","请先打开串口再操作",MB_YESNO)==IDYES)
			m_mscomm1.SetPortOpen(TRUE);
	}
	UpdateData();
	CFileStatus fs;
	CFile::GetStatus(m_bytefilepath,fs);
	int size=fs.m_size;
	CString str;str.Format("将要发送的文件大小为:%d字节",size);MessageBox(str);
	FILE *fp;
	byte  *filebyte=new byte[size];
	for (int i=0;i<size;i++)
	{
		filebyte[i]='\0';
	}
	
	fp=fopen(m_bytefilepath,"rb");
	
	fread(filebyte,size,1,fp);
	fclose(fp);
	m_mscomm1.SetOutput(COleVariant("bytefilestart(\r\n"));
	COleVariant cv=(char* )filebyte;
	m_mscomm1.SetOutput(cv);
   m_mscomm1.SetOutput(COleVariant("\r\n)bytefileend"));	
}

void CEx7Dlg::savefile(VARIANT data)
{
    

}