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

📄 mscommv1dlg.cpp

📁 一个虚拟串口读取程序
💻 CPP
字号:
// MSCommV1Dlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CMSCommV1Dlg dialog

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

void CMSCommV1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMSCommV1Dlg)
	DDX_Control(pDX, IDC_BUTTON1_Draw, m_Draw);
	DDX_Control(pDX, IDC_EDIT2, m_Clear);
	DDX_Control(pDX, IDC_COMBO1, m_Combo);
	DDX_Control(pDX, IDC_BUTTON_SEND, m_Send);
	DDX_Control(pDX, IDC_BUTTON_OPEN, m_Open);
	DDX_Control(pDX, IDC_BUTTON_EXIT, m_EXIT);
	DDX_Text(pDX, IDC_EDIT2, m_Rev);
	DDX_Control(pDX, IDC_MSCOMM1, m_MSComm);
	
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMSCommV1Dlg, CDialog)
	//{{AFX_MSG_MAP(CMSCommV1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
	ON_CBN_SELENDOK(IDC_COMBO1, OnSelendokCombo1)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonClear)
	ON_NOTIFY(NM_OUTOFMEMORY, IDC_PROGRESS, OnOutofmemoryProgress1)
	ON_WM_TIMER()
	ON_NOTIFY(NM_OUTOFMEMORY, IDC_PROGRESS, OnOutofmemoryProgress)
	ON_BN_CLICKED(IDC_BUTTON1_Draw, OnBUTTON1Draw)
	ON_BN_CLICKED(IDC_BUTTON1_CLOSE, OnButton1Close)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMSCommV1Dlg message handlers

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

     nCounter=0; //横坐标参数初始化为0
	 order=1; //初始化order=1
	 data[0]=0; //初始化data[0]=0
	 m_Combo.SetCurSel(3);//initial combine box is port 4
     SetTimer(0,1000,NULL);//初始化时间
     //m_WhiteBrush.CreateSolidBrush(RGB(255,255,255)); //设置背景色白色
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMSCommV1Dlg::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.

//DEL void CMSCommV1Dlg::OnPaint() 
//DEL {
//DEL 	if (IsIconic())
//DEL 	{
//DEL 		CPaintDC dc(this); // device context for painting
//DEL 
//DEL 		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
//DEL 
//DEL 		// Center icon in client rectangle
//DEL 		int cxIcon = GetSystemMetrics(SM_CXICON);
//DEL 		int cyIcon = GetSystemMetrics(SM_CYICON);
//DEL 		CRect rect;
//DEL 		GetClientRect(&rect);
//DEL 		int x = (rect.Width() - cxIcon + 1) / 2;
//DEL 		int y = (rect.Height() - cyIcon + 1) / 2;
//DEL 
//DEL 		// Draw the icon
//DEL 		dc.DrawIcon(x, y, m_hIcon);
//DEL 	}
//DEL 	else
//DEL 	{
//DEL 		CDialog::OnPaint();
//DEL 	}
//DEL }

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

void CMSCommV1Dlg::OnButtonExit() 
{
	OnOK();
	
}


BEGIN_EVENTSINK_MAP(CMSCommV1Dlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CMSCommV1Dlg)
	ON_EVENT(CMSCommV1Dlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//	ON_EVENT(CMSCommV1Dlg, IDC_MSCHART1, 1550 /* OLEStartDrag */, OnOLEStartDragMschart1, VTS_PDISPATCH VTS_PI4)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CMSCommV1Dlg::OnOnCommMscomm1() 
{
	// TODO: Add your control notification handler code here
    UpdateData(true);
	VARIANT variant_inp;
	COleSafeArray safearray_inp;
	CString strtemp;
    
	long i=0;
	int len;
	int flag;
	char rxdata[1000];

	if(m_MSComm.GetCommEvent()==2)
	{
	
		 //以下接收串口数据
	
		variant_inp=m_MSComm.GetInput();
		safearray_inp=variant_inp;
		len=safearray_inp.GetOneDimSize();
		 for(i=0;i<len;i++)
		 {
		 safearray_inp.GetElement(&i,&rxdata[i]);
		 BYTE bt=*(char*)(rxdata+i); //字符型
      
		 strtemp.Format("%x",bt); //格式转换为ascii码形式
         m_Rev+=strtemp; //显示接收的数据
		//以下画线
      
	   CPen *PenOld,PenNew1;
	   CDC  *pDC = GetDC(); //获取串口坐标值
	   PenNew1.CreatePen(PS_SOLID,1,RGB(255,0,0)); //创建画笔红色控件
		 //
		 int temp=atoi(strtemp);  //用atoi函数把CString转换成int型数据 
		 if(order==48)
		    flag=1;
		 else 
		    flag=0;

        //显示双缓冲
       /*  CRect rect;
		 GetClientRect(&rect); 
		 CDC dcMem;
		 CBitmap bmp;
		 dcMem.CreateCompatibleDC(pDC);
		 bmp.CreateCompatibleBitmap(&dcMem,rect.Width(),rect.Height());
		 dcMem.SelectObject(&bmp);
		 dcMem.FillSolidRect(rect,pDC->GetBkColor());*/

        //在内存绘图
		 switch(flag)
		 {
		 case 0: 
                 //PenNew2.CreatePen(PS_SOLID,1,RGB(255,255,255)); //创建画笔白色控件
       	         PenOld=pDC->SelectObject(&PenNew1);  //选择画笔
			     data[order]=temp; 			     
                 pDC->MoveTo(125+nCounter*10,240-4*data[order-1]);//画线坐标,10为横坐标单位跨度,4为纵坐标单位跨度		
                 pDC->LineTo(125+(nCounter+1)*10,240-4*data[order]);
				 
				 PenNew1.DeleteObject();  //ADDITION
                 
				// pDC->SelectObject(PenOld);

	             ReleaseDC(pDC);  //ADDITION
				
				 // PenNew1.DeleteObject(); 
				 order++; //数据参数自增
				 nCounter++;
				 break;
         case 1: 
			     for(i=0,nCounter=0;i<47,nCounter<48;i++,nCounter++)//擦除部分
				 {
				 CPen PenNew1;               
	             PenNew1.CreatePen(PS_SOLID,1,RGB(255,255,255)); //创建擦除白色画笔控件
				 //CDC  *pDC = GetDC(); //获取串口坐标值
       	         PenOld=pDC->SelectObject(&PenNew1);  //选择画笔
				 pDC->MoveTo(125+nCounter*10,240-4*data[i]);
			     pDC->LineTo(125+(nCounter+1)*10,240-4*data[i+1]);

		         PenNew1.DeleteObject(); //ADDITION
                 
				 pDC->SelectObject(PenOld);
                 //PenNew1.DeleteObject();   //ADDITION 
				 
			    // pDC->SelectObject(PenOld);

	             ReleaseDC(pDC);   //ADDITIION
				 
				 }
               
				 for(order=0;order<47;order++)						
			     data[order]=data[order+1]; //上一次采集数据两两交换,向前推进一位
				 data[order]=temp; //最新的一个数据即data[47]
                 //ReleaseDC(pDC);   //ADDITIION
                 
				 for(order=0,nCounter=0;order<47,nCounter<48;order++,nCounter++)
				 {
				// CDC *pDC=GetDC();
				 PenNew1.CreatePen(PS_SOLID,1,RGB(255,0,0)); //创建画笔红色控件
                 PenOld=pDC->SelectObject(&PenNew1);  //选择画笔
			     pDC->MoveTo(125+nCounter*10,240-4*data[order]); 				 
                 pDC->LineTo(125+(nCounter+1)*10,240-4*data[order+1]);
		     	 PenNew1.DeleteObject();   //ADDITION 
			 	 pDC->SelectObject(PenOld); //AADITION
	             ReleaseDC(pDC); //ADDITION
				 }	
				
				 break;
		 default:;		
		 }	
        
      //ReleaseDC(pDC);
		}
	UpdateData(false);
	}	
}






void CMSCommV1Dlg::OnSelendokCombo1() 
{
	// TODO: Add your control notification handler code here
	
	 int i;
	 i=m_Combo.GetCurSel();
	switch(i)
	{
	case 0:PORT=1;break;
    case 1:PORT=2;break;
	case 2:PORT=3;break;
    case 3:PORT=4;break;
    case 4:PORT=5;break;
	case 5:PORT=6;break;
	default:break;
	
	}
	 //UpdateData(false);
}

void CMSCommV1Dlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	 m_MSComm.SetCommPort(PORT);   //初始化端口 
	m_MSComm.SetInBufferSize(1024);  //接收缓冲区
	m_MSComm.SetOutBufferSize(512);  //发送缓冲区
	m_MSComm.SetInputMode(1);   //读取二进制格式的数据
		if(!m_MSComm.GetPortOpen())
		m_MSComm.SetPortOpen(true);
     m_MSComm.SetSettings("9600,n,8,1"); //设置波特率等
     m_MSComm.SetRThreshold(1);  //接收到一个字符就触发OnComm事件
     m_MSComm.SetInputLen(0);  //读取缓冲区内所有数据
//	m_MSComm.GetInput();

     
}


void CMSCommV1Dlg::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	m_Clear.SetSel(0,-1);
	m_Clear.ReplaceSel("");
}



void CMSCommV1Dlg::OnOutofmemoryProgress1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CMSCommV1Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	static int i;
	((CProgressCtrl*)GetDlgItem(IDC_PROGRESS))->SetPos(i);
	if(i<100)
		i+=10;
	else i=0;

    
	CDialog::OnTimer(nIDEvent);
    CMSCommV1Dlg::OnDrawInitial();

  // m_MSComm.SetInBufferCount(0);
	
}

void CMSCommV1Dlg::OnOutofmemoryProgress(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}
void CMSCommV1Dlg::OnDrawInitial()
{
    
        COLORREF DotColorRed=RGB(255,0,0); // 设定坐标标度点颜色 
        COLORREF DotColorBlue=RGB(0,0,255); // 设定坐标标度颜色

        CDC *pDC = GetDC();  
	    CPen *PenOld,PenNew1;
		PenNew1.CreatePen(PS_SOLID,2,RGB(0,0,0));
		PenOld=pDC->SelectObject(&PenNew1);
        //设定X轴位置
	    pDC->MoveTo(125,240);
    	pDC->LineTo(620,240);
        pDC->SelectObject(PenOld);
        PenNew1.DeleteObject();  //返回旧画笔
   //设定Y轴位置
        pDC->MoveTo(125,18);
    	pDC->LineTo(125,240);
        pDC->SelectObject(PenOld);
        PenNew1.DeleteObject();  //返回旧画笔
   int i=0;

   //画Y轴坐标刻度
   for(i=0;i<10;i++)
   {
    CDC *pDC = GetDC(); //获取串口坐标值
	pDC->SetPixel(125,40+20*i,DotColorRed); //画坐标点
	//画坐标刻度,40为起始位置,20为刻度间隔
    pDC->MoveTo(125,40+20*i);
    pDC->LineTo(130,40+20*i);
    pDC->SelectObject(PenOld);
    PenNew1.DeleteObject();  //返回旧画笔
	switch(i)
	{
	case 0:
	pDC->TextOut(105,40+20*i,"5.0");break;//画坐标刻度值
	case 1:
	pDC->TextOut(105,40+20*i,"4.5");break; 
	case 2:
	pDC->TextOut(105,40+20*i,"4.0");break; 
	case 3:
	pDC->TextOut(105,40+20*i,"3.5");break; 
	case 4:
	pDC->TextOut(105,40+20*i,"3.0");break; 
	case 5:
	pDC->TextOut(105,40+20*i,"2.5");break; 
	case 6:
	pDC->TextOut(105,40+20*i,"2.0");break; 
	case 7:
	pDC->TextOut(105,40+20*i,"1.5");break; 
	case 8:
	pDC->TextOut(105,40+20*i,"1.0");break; 
	case 9:
	pDC->TextOut(105,40+20*i,"0.5");break; 
	}
   }

}






void CMSCommV1Dlg::OnBUTTON1Draw() 
{
	// TODO: Add your control notification handler code here
	
	//KillTimer(0);
    ((CProgressCtrl*)GetDlgItem(IDC_PROGRESS))->SetPos(0);
	

}

void CMSCommV1Dlg::OnButton1Close() 
{
	// TODO: Add your control notification handler code here
	m_MSComm.SetPortOpen(false);
}


void CMSCommV1Dlg::DrawLine(CString Value)
{
//初始化画笔
    CPen *PenOld,PenNew1;
	CDC *pDC = GetDC(); //获取串口坐标值
	PenNew1.CreatePen(PS_SOLID,1,RGB(255,0,0)); //创建画笔红色控件
	PenOld=pDC->SelectObject(&PenNew1);  //选择画笔
	
 

PenNew1.DeleteObject(); //返回旧画笔
}


//DEL HBRUSH CMSCommV1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
//DEL {
//DEL 	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
//DEL 	
//DEL 	// TODO: Change any attributes of the DC here
//DEL 	if(nCtlColor==CTLCOLOR_DLG)
//DEL 	{
//DEL 	pDC->SetBkColor(RGB(0,0,0));
//DEL 	
//DEL 	
//DEL 	}
//DEL 	// TODO: Return a different brush if the default is not desired
//DEL 	return m_WhiteBrush;
//DEL }

//DEL void CMSCommV1Dlg::OnPaint() 
//DEL {
//DEL 	CPaintDC dc(this); // device context for painting
//DEL 	
//DEL 	// TODO: Add your message handler code here
//DEL 	
//DEL 	// Do not call CDialog::OnPaint() for painting messages
//DEL }

//DEL HBRUSH CMSCommV1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
//DEL {
//DEL 	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
//DEL 	
//DEL 	// TODO: Change any attributes of the DC here
//DEL 	
//DEL 	// TODO: Return a different brush if the default is not desired
//DEL 	return hbr;
//DEL }

⌨️ 快捷键说明

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