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

📄 mycom2dlg.cpp

📁 rs232 通信
💻 CPP
字号:
// mycom2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "mycom2.h"
#include "mycom2Dlg.h"
//#include "calculate.cpp"


#include "VcPlot.h"
#include "VcAxis.h"
#include "VcValueScale.h"
#include "VcSeriesCollection.h"
#include "VcSeries.h"
#include "VcCategoryScale.h"
#include "VcColor.h"
#include "VcDataGrid.h"
#include "VcBackdrop.h"
#include "VcFill.h"
#include "VcBrush.h"
#include "VcDataPoints.h"
#include "VcDataPoint.h"
#include "VcDataPointLabel.h"
#include "VcAxisTitle.h"
#include "VcPen.h"
//#include "max_value.cpp"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

    WORD row;
	WORD Trow;
	WORD Fulscrnum;
    CString temp_row;
    CMSChart m_Chart1;   

	float acpt;
    long count;
 
	float sample_max;
    float xdata;
	float weight;
//CString strtemp;
  // long k,len;

   // BYTE rxdata[2048];
  // BYTE temp_data[7];
//	BYTE array[7];


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()

/////////////////////////////////////////////////////////////////////////////
// CMycom2Dlg dialog

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

void CMycom2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMycom2Dlg)
	DDX_Control(pDX, IDC_MSCHART1, m_Chart1);
	DDX_Control(pDX, IDC_MSCOMM1, m_wndCOM1);
	DDX_Text(pDX, IDC_EDIT2, m_edit2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMycom2Dlg, CDialog)
	//{{AFX_MSG_MAP(CMycom2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_STARTR, OnStartr)
	ON_BN_CLICKED(IDC_STOPR, OnStopr)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMycom2Dlg message handlers

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

    /*设置串口*/



  //   if(m_wndCOM1.GetPortOpen())
   //  m_wndCOM1.SetPortOpen(FALSE);

   //  m_wndCOM1.SetCommPort(1); //选择com1
   //  if( !m_wndCOM1.GetPortOpen())
  //   m_wndCOM1.SetPortOpen(TRUE);//打开串口
  //   else
  //   AfxMessageBox("cannot open serial port");




     m_wndCOM1.SetCommPort(1);     //选择COM1
	 m_wndCOM1.SetInBufferSize(4096);  //设置输入缓冲区大小,单位为字节
     m_wndCOM1.SetOutBufferSize(512);  //设置输出缓冲区大小,单位为字节
	 m_wndCOM1.SetSettings("9600,N,8,1");   //波特率9600,无校验,8个数据位,1个停止位
	 m_wndCOM1.SetInputMode(1);     //设置接收数据模式为二进制形式
	 m_wndCOM1.SetRThreshold(20);    //字符数达到1,若有字符,产生接收响应事件
	 m_wndCOM1.SetInBufferCount(0);  //清空接收缓冲区
	 m_wndCOM1.SetInputLen(0);    //设置当前接受区数据长度为0,1次从接收缓冲区读取全部字节
	 m_wndCOM1.SetPortOpen(TRUE); //打开串口
	 

   /*设置MSCHART控件*/
	 VARIANT var;
	 // 设置标题
	m_Chart1.SetTitleText("N-t 曲线 by QDI-16峰值测试仪 ");

	// 下面两句改变背景色
	m_Chart1.GetBackdrop().GetFill().SetStyle(1);
	m_Chart1.GetBackdrop().GetFill().GetBrush().GetFillColor().Set(255, 255, 255);

    m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(FALSE);	// 不自动标注Y轴刻度
    m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(200);	// Y轴最大刻度
	m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0);		// Y轴最小刻度
	m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetMajorDivision(10);	// Y轴刻度10等分
	m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetMinorDivision(1);	// 每刻度一个刻度线
	m_Chart1.SetColumnCount(1);   //设置1条曲线
	m_Chart1.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(0, 0, 255);
	m_Chart1.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("压力");	// Y轴名称

    m_Chart1.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);	// 不自动标注X轴刻度
	m_Chart1.SetRowCount(20);  //一屏设置20个刻度
    m_Chart1.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(1);	// 每刻度一个刻度线
	m_Chart1.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(1);	// 每刻度一个标注
	m_Chart1.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("时间");	// X轴名称




     m_Chart1.SetChartType(1|2);  //线状显示



     row=1;   //row赋初值
	/*********清屏,标注一屏数值***********/
	while ((row<=20))
	{
		m_Chart1.SetRow(row);
		m_Chart1.GetDataGrid().SetData(row,1,0,1);
		temp_row.Format("%d",row);
		m_Chart1.SetRowLabel(temp_row);
        row++;


	}
        
	
	    m_Chart1.Refresh();
	    row=1; //row重新赋值1
		Fulscrnum=1;//初始化屏值
  
      /*编辑框初始化*/
  //  	m_edit1=0;
	   
     	m_edit2=0;

	
       UpdateData(FALSE);

       


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

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

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



	  
	

void CMycom2Dlg::Oncomm1() 
{
	// TODO: Add your control notification handler code here
    
	 VARIANT variant_inp;
     COleSafeArray safearray_inp;
     long len,k;
     BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
	 int xdata[2048];
     CString strtemp;
     long i,j;
     BYTE temp_data[7];
 	 BYTE array[7];
	 BYTE *str;
     int t=7;
	 //float weight;
     

      if(m_wndCOM1.GetCommEvent()==2) //事件值为2,表示接收缓冲区有数据到达
	  {
		
        int event;
	    event=m_wndCOM1.GetInBufferCount();


		if(event>20)

		{	variant_inp=m_wndCOM1.GetInput();  //read
            safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
            len=safearray_inp.GetOneDimSize(); //得到有效数据长度

            for(k=0;k<len;k++)
               safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
           // for(k=0;k<len;k++) //将数组转换为Cstring型变量
		//	{ //BYTE  bt=*(int*)(rxdata+k); //字符型
               
            //  strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
			 //  strtemp.Format("%02X ",bt);

            //  m_edit1+=strtemp; //加入接收编辑框对应字
               //  Sleep(100);
			
		  //  	m_edit2=len;

          //     UpdateData(FALSE);
		//	}
		  
            
	


            for(k=0;k<len;k++)
			{


				if(rxdata[k]==61)
					if(rxdata[k+9]==13)		       
                   
          
				  {
					     
				         for(i=0;i<7;i++)
				       
						 {
				 
				             
					         temp_data[i]=rxdata[k+1];  //取有用数据
				     			k++;
					
						 }
                            
						 
						for(j=0;j<7;j++)
						{
							array[j]=temp_data[j];
						}
                        for(int m=0;m<t;m++)
						{	if(array[m]==32)
								t=m;
						}
				
                   switch(t)
					{

                       case 0:break;
		               case 1:break;
		               case 2:break;					  
	                   case 3:weight=(array[2]-48)+(array[0]-48)*0.1;
					          break;
		               case 4:weight=(array[3]-48)*10+(array[2]-48)+0.1*(array[0]-48);
						      break;
		               case 5:weight=(array[4]-48)*100+(array[3]-48)*10+(array[2]-48)+(array[0]-48)*0.1;
						      break;
		               case 6:break;
                	//	case 7:break;
			           default: ASSERT(false);
			  

					}
              
	                
					
				

        
            	 
				
					}		
			 

         	}
           
 
                     


				


                

			  
		
        }
    
  
	}
   // m_wndCOM1.SetInBufferCount(0);  //清空接收缓冲区





	
}
	  









void CMycom2Dlg::OnStartr() 
{
	// TODO: Add your control notification handler code here


    SetTimer(ID_SAMPLE_TIMER,100,NULL); //启动定时器,定时时间是500ms
	 
	 

}



void CMycom2Dlg::OnStopr() 
{   
	// TODO: Add your control notification handler code here
	KillTimer(ID_SAMPLE_TIMER);
	
  //  m_wndCOM1.SetPortOpen(FALSE); //关闭串口
	 



}

void CMycom2Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	        
         
       
              m_edit2=weight;
			  UpdateData(FALSE);
               
            
	
	   if(row%20==0)   //判断是否满屏
	   {
		   ++Fulscrnum; //满屏次数
		   m_Chart1.SetRowCount(20*Fulscrnum);//屏幕扩大相应倍数,保持前屏曲线
		   for(Trow=row;Trow<=20*Fulscrnum;Trow++)
		   {
	             m_Chart1.SetRow(Trow);
		         m_Chart1.GetDataGrid().SetData(Trow,1,0,1);
		         temp_row.Format("%d",Trow);
		         m_Chart1.SetRowLabel(temp_row);

		   }

	     m_Chart1.Refresh();
		  
	   }

 
    m_Chart1.SetRow(row);

	m_Chart1.GetDataGrid().SetData(row,1,weight,0); //绘制当前时刻的曲线

	row++;
 

  

		
		
	    
        
		
      
		 
	CDialog::OnTimer(nIDEvent);
}

void CMycom2Dlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	/*设置MSCHART控件*/
	 VARIANT var;
	 // 设置标题
	m_Chart1.SetTitleText("N-t 曲线 by QDI-16峰值测试仪 ");

	// 下面两句改变背景色
	m_Chart1.GetBackdrop().GetFill().SetStyle(1);
	m_Chart1.GetBackdrop().GetFill().GetBrush().GetFillColor().Set(255, 255, 255);

    m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(FALSE);	// 不自动标注Y轴刻度
    m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(200);	// Y轴最大刻度
	m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0);		// Y轴最小刻度
	m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetMajorDivision(10);	// Y轴刻度10等分
	m_Chart1.GetPlot().GetAxis(1,var).GetValueScale().SetMinorDivision(1);	// 每刻度一个刻度线
	m_Chart1.SetColumnCount(1);   //设置1条曲线
	m_Chart1.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(0, 0, 255);
	m_Chart1.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("压力");	// Y轴名称

    m_Chart1.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);	// 不自动标注X轴刻度
	m_Chart1.SetRowCount(20);  //一屏设置20个刻度
    m_Chart1.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(1);	// 每刻度一个刻度线
	m_Chart1.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(1);	// 每刻度一个标注
	m_Chart1.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("时间");	// X轴名称




     m_Chart1.SetChartType(1|2);  //线状显示
	 row=1;   //row赋初值
	/*********清屏,标注一屏数值***********/
	while ((row<=20))
	{
		m_Chart1.SetRow(row);
		m_Chart1.GetDataGrid().SetData(row,1,0,1);
		temp_row.Format("%d",row);
		m_Chart1.SetRowLabel(temp_row);
        row++;


	}
        
	
	    m_Chart1.Refresh();
	    row=1; //row重新赋值1
		Fulscrnum=1;//初始化屏值
  



	
}

⌨️ 快捷键说明

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