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

📄 comtestdlgdlg.cpp

📁 简易的“串口调试助手” 即 可以像超级终端一样
💻 CPP
字号:
// comtestdlgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "comtestdlg.h"
#include "comtestdlgDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifndef ErrMsg
#define ErrMsg(x) MessageBox(x,"memeo",MB_OK);
#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()

/////////////////////////////////////////////////////////////////////////////
// CComtestdlgDlg dialog

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

void CComtestdlgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CComtestdlgDlg)
	DDX_Control(pDX, IDC_EDRECEIVEDATA, m_edReceiveDataCtrl);
	DDX_Control(pDX, IDC_CBPORT, m_cbPort);
	DDX_Control(pDX, IDC_MSCOMM1, m_comm);
	DDX_Text(pDX, IDC_EDSENDDATA, m_szSendData);
	DDX_Text(pDX, IDC_EDRECEIVEDATA, m_szReceiveData);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CComtestdlgDlg, CDialog)
	//{{AFX_MSG_MAP(CComtestdlgDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTPORT, OnBtport)
	ON_BN_CLICKED(IDC_BTCLOSE, OnBtclose)
	ON_BN_CLICKED(IDC_BTSEND, OnBtsend)
	ON_BN_CLICKED(IDC_BTRECEIVE, OnBtreceive)
	ON_BN_CLICKED(IDC_SENDFILE, OnSendfile)
	ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComtestdlgDlg message handlers

BOOL CComtestdlgDlg::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

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

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

BEGIN_EVENTSINK_MAP(CComtestdlgDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CComtestdlgDlg)
	ON_EVENT(CComtestdlgDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CComtestdlgDlg::OnOnCommMscomm1() 
{
    VARIANT var;
	CString szTmp;
	LPSTR   strTmp;
	CFileException e;
	CStdioFile incfile;
	

	short nEvent =	m_comm.GetCommEvent();

	switch(nEvent)
	{
	case 2:
		    
		var = m_comm.GetInput();
		szTmp = var.bstrVal;
			m_szReceiveData += szTmp;
	////	MessageBox(szTmp);
	///	strTmp = var.bstrVal;

		//if( !incfile.Open("D:\\test.dat", CFile::modeCreate|CFile::modeWrite, &e ) ) 
		//	{
		     //   MessageBox("Can't Open the file","Pmpicon",MB_OK);
		///	}
	///	incfile.SeekToEnd();
	///	incfile.WriteHuge((void *)strTmp,1024);
	///	incfile.Close();
///

		break;
	case 1:
	////	szTmp = "S";
		break;
	default:
	///	szTmp = "@";
	//	var = m_comm.GetInput();
	//	szTmp = var.bstrVal;
		break;
	}


	int nLine = m_edReceiveDataCtrl.GetLineCount();
	UpdateData(FALSE);
	m_edReceiveDataCtrl.LineScroll(nLine,0);//////自动滚动到最底行






	
	// TODO: Add your control notification handler code here
	
}

void CComtestdlgDlg::DataInit()
{
	m_cbPort.AddString("1");
	m_cbPort.AddString("2");
	m_cbPort.AddString("3");
	m_cbPort.AddString("4");
	m_cbPort.AddString("5");
	m_cbPort.SetCurSel(0);
	


}



void CComtestdlgDlg::OnBtport() 
{
	// TODO: Add your control notification handler code here
	int nIndex = m_cbPort.GetCurSel();
	CString szPortNo;
	m_cbPort.GetLBText(nIndex,szPortNo);
	////ErrMsg(szPortNo);
	int nPortNo = 0;
	
	sscanf(szPortNo,"%d",&nPortNo);

	///int retVal = nPortNo;
	m_comm.SetCommPort(nPortNo);
	m_comm.SetInBufferSize(1024 );
	m_comm.SetOutBufferSize(1024);
	if(m_comm.GetPortOpen() != TRUE)
	{
		m_comm.SetPortOpen(TRUE);
	}
	m_comm.SetInputMode(0);/////以文本的方式取回
	m_comm.SetSettings("9600,n,8,1");
	m_comm.SetSThreshold(1);
	m_comm.SetRThreshold(1);
	m_comm.SetInputLen(0);
	m_comm.SetDTREnable(TRUE);
	m_comm.SetRTSEnable(TRUE);

///	m_comm.SetDTREnable(TRUE);



	
	
}

void CComtestdlgDlg::OnBtclose() 
{
	// TODO: Add your control notification handler code here
	if(m_comm.GetPortOpen() == TRUE)
	{
		m_comm.SetPortOpen(FALSE);
	}
	else
	{
		ErrMsg("has closded");
	}
	
}

void CComtestdlgDlg::OnBtsend() 
{
	// TODO: Add your control notification handler code here
	
	UpdateData(TRUE);
   ////CByteArray 

	VARIANT var ;
	
  /*  ///var.bstrVal = m_szSendData.GetBuffer(m_szSendData.GetLength());///error C2440: '=' : cannot convert from 'char *' to 'unsigned short *'
    int nLength = m_szSendData.GetLength();
	char *tmpStr ;
	tmpStr =(char *) malloc(nLength);
	for(int i=0; i<nLength; i++)
	{
		
		tmpStr[i] = ;
	}
	var.bstrVal = tmpStr;
*/
	////16 bit str---->16 bit srt
	CByteArray str16;
	long nLength = m_szSendData.GetLength();
	str16.SetSize(nLength);
//	for(long k = 0; k<655;k++)
	{
	   for(long i = 0; i< nLength;i++)
	   {
	    	str16.SetAt(i,m_szSendData.GetAt(i));
	   }
   

    	m_comm.SetOutput((COleVariant ) str16);
	//	Sleep(10);
	}

	
}

void CComtestdlgDlg::OnBtreceive() 
{
	// TODO: Add your control notification handler code here
	m_szReceiveData = "";
	UpdateData(FALSE);
	
}



void CComtestdlgDlg::OnSendfile() 
{
	// TODO: Add your control notification handler code here
	CFileException e;
	CStdioFile incfile1;
	CString strBuf;
	
//	int i;
	long lFileLen;
	LPTSTR lpBuf;
	

     if( !incfile1.Open(m_strfilename, CFile::modeRead, &e ) ) 
			{
		        MessageBox("Can't Open the file","Pmpicon",MB_OK);
			}

     lFileLen = incfile1.GetLength();
	 lpBuf = (LPTSTR) malloc(lFileLen);
	 incfile1.ReadHuge(lpBuf,lFileLen);


	 CByteArray str16;
	 str16.SetSize(lFileLen);

     for(int i = 0; i< lFileLen;i++)
	   {
	    	str16.SetAt(i,*(lpBuf+i));
	   }
      i = str16.GetSize();

    	m_comm.SetOutput((COleVariant ) str16);
	  incfile1.Close();

	 // strBuf = lpBuf;
	 // i = strBuf.GetLength();
}

void CComtestdlgDlg::OnOpenfile() 
{
	// TODO: Add your control notification handler code here

 //	CString strfilename;

	CString filt="JPG File (*.JPG)|*.JPG|BMP (*.BMP)|*.BMP|All files (*.*)|*.*||";
	CFileDialog fileDlg(TRUE,"*.JPG","*.JPG",NULL,filt,NULL);
	fileDlg.m_ofn.Flags|=OFN_FILEMUSTEXIST;
	fileDlg.m_ofn.lpstrTitle="File to load";
	if(fileDlg.DoModal()==IDOK)m_strfilename=fileDlg.GetPathName();

	if(m_strfilename.GetLength()<=0)return;


   
	char chr[255]; 
	strcpy(chr,m_strfilename);




	
}

⌨️ 快捷键说明

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