1view.cpp

来自「用于数据采集分析的对话框界面」· C++ 代码 · 共 312 行

CPP
312
字号
// 1View.cpp : implementation of the CMy1View class
//

#include "stdafx.h"
#include "1.h"

#include "1Doc.h"
#include "1View.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CCAITestApp theApp;
DWORD gPulseWidth;
DWORD gIgition;
DWORD gRPM;
DWORD gLoad;
DWORD gIgn;

/////////////////////////////////////////////////////////////////////////////
// CMy1View

IMPLEMENT_DYNCREATE(CMy1View, CFormView)

BEGIN_MESSAGE_MAP(CMy1View, CFormView)
	//{{AFX_MSG_MAP(CMy1View)
	ON_BN_CLICKED(IDC_start, Onstart)
	ON_BN_CLICKED(IDC_cancel, Oncancel)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy1View construction/destruction

CMy1View::CMy1View()
	: CFormView(CMy1View::IDD)
{
	//{{AFX_DATA_INIT(CMy1View)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CMy1View::~CMy1View()
{
}

void CMy1View::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy1View)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CMy1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CMy1View::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CMy1View printing

BOOL CMy1View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMy1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}
BOOL CCAITestView::CommSpeedRecive(CString strRecive)
{
	CString strDataFrist, strDataSecond,strSign;//sR000,000d
	CHAR strFunction;

	if (strRecive.Mid(0, 1) != "S")
		return false;

    strFunction = strRecive.GetAt(1);//Mid(1, 1);
    strSign = strRecive.Mid(2, 1);
	int iSign = atoi(strSign);

    switch (iSign)
    {
		case 0:
            strSign = "";break;
		case 1:
            strSign = "-";break;
    }
    
	
    switch(strFunction)
	{
		
        case 'V':
			//strDataFrist.Format("%5.3f",atof(strRecive.Mid(3, 4)) / 10);
			m_PulseWidth=atof(strRecive.Mid(3, 4)) / 10;
			gPulseWidth=atol(strRecive.Mid(3, 4));
			break;
        case 'U':
			//strDataFrist.Format("%5.3f",atof(strRecive.Mid(3, 4)) / 10);
			m_Igition=atof(strRecive.Mid(3, 4)) / 10;
			gIgition=atol(strRecive.Mid(3, 4));
			break; 
                
        case 'W':
            //strDataFrist.Format("%d",atoi(strRecive.Mid(3, 4)));
            //m_Load.SetWindowText(strDataFrist);
			m_Load=atoi(strRecive.Mid(3, 4));
			gLoad=m_Load;
			break;
            
        case 'Y':
             //strDataFrist.Format("%d",atoi(strRecive.Mid(3, 4)));
             //m_Rpm.SetWindowText(strSign + strDataFrist);
			m_RPM=atoi(strRecive.Mid(3, 4));
			gRPM=m_RPM;
			break;

        case 'X'://'插值周期
            //strDataFrist.Format("%d",atoi(strRecive.Mid(3, 4)));
            //m_Cycle.SetWindowText(strSign + strDataFrist);
			break;

        case 'A': //'标定喷射次数
            //strDataFrist.Format("%d",atoi(strRecive.Mid(3, 4)));
            //m_InjectTimes.SetWindowText(strSign + strDataFrist);
			break;

        case 'L':// '点火定时增量
            //strDataFrist.Format("%5.3f",atof(strRecive.Mid(3, 4)) / 10);
            //m_IgniTime.SetWindowText(strSign + strDataFrist);
			break;

        case 'K'://'喷油脉宽增量
            //strDataFrist.Format("%5.3f",atof(strRecive.Mid(3, 4)) / 10);
            //m_OilInject.SetWindowText(strSign + strDataFrist);
			break;
	}

	UpdateData(false);
	return true;
}

void CMy1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CMy1View::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CMy1View diagnostics

#ifdef _DEBUG
void CMy1View::AssertValid() const
{
	CFormView::AssertValid();
}

void CMy1View::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CMy1Doc* CMy1View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy1Doc)));
	return (CMy1Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMy1View message handlers

void CMy1View::OnStatic1() 
{
	// TODO: Add your control notification handler code here
	
}
void CCAITestView::OpenPort()
{
	m_comflag = true;
	m_mscomm.SetInBufferSize(9);

	if(m_mscomm.GetPortOpen())
		m_mscomm.SetPortOpen(FALSE);

	if( !m_mscomm.GetPortOpen())
		m_mscomm.SetPortOpen(TRUE);//打开串口
	else
		AfxMessageBox("cannot open serial port");

	m_mscomm.SetSettings("19200,e,8,1"); //m_strSettings波特率19200,无校验,8个数据位,1个停止位
	m_mscomm.SetRThreshold(9); //

	//参数1表示每当串口接收缓冲区中有多于或等于9个字符时将引发一个接收数据的OnComm事件
	m_mscomm.SetInputMode(1);
	m_mscomm.SetInputLen(0);  //设置当前接收区数据长度为0
	m_mscomm.GetInput();//先预读缓冲区以清除残留数据	
}
void CCAITestView::OnComm() 
{
	CString Instring;
	VARIANT variant_inp;
	COleSafeArray safearray_inp;
	LONG len,k;
	BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed
	CString strtemp, str = _T("");

	//如果采集
	//if (theApp.m_bNewData)
		//return;
	//如果绘图
//	if (theApp.m_bDrawing)
		//return;

	int nEvent = m_mscomm.GetCommEvent();
 
	if (!m_comflag)    
		return ;
	else
		{	//回传转速
			switch (nEvent)
			{   
			case 2: //事件值为2表示接收缓冲区内有字符
				{	
					m_mscomm.SetInputLen(0);
					variant_inp = m_mscomm.GetInput();//读缓冲区
					//VARIANT型变量转换为ColeSafeArray型变量
					safearray_inp = variant_inp;  
					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=*(char*)(rxdata+k);      //字符型					
						strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
						str = str + strtemp;    //加入接收编辑框对应字符串    
					}

					Instring = str.Left(9);
					//转速
					CommSpeedRecive(Instring);
					m_MainFrame->SetComStatus(5,"上传数据:" + Instring);
					m_mscomm.SetInBufferCount(0);
				}
			}
		}
	//UpdateData(FALSE);           //更新编辑框内容
}

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

void CMy1View::Onstart() 
{	m_comflag = true;
	m_mscomm.SetInBufferSize(9);

	if(m_mscomm.GetPortOpen())
		m_mscomm.SetPortOpen(FALSE);

	if( !m_mscomm.GetPortOpen())
		m_mscomm.SetPortOpen(TRUE);//打开串口
	else
		AfxMessageBox("cannot open serial port");

	m_mscomm.SetSettings("19200,e,8,1"); //m_strSettings波特率19200,无校验,8个数据位,1个停止位
	m_mscomm.SetRThreshold(9); //

	//参数1表示每当串口接收缓冲区中有多于或等于9个字符时将引发一个接收数据的OnComm事件
	m_mscomm.SetInputMode(1);
	m_mscomm.SetInputLen(0);  //设置当前接收区数据长度为0
	m_mscomm.GetInput();//先预读缓冲区以清除残留数据
	
}

void CMy1View::Oncancel() 
{//判断串口的状态,如果是打开状态,则关闭 
if( m_Comm.GetPortOpen() )  
m_Comm.SetPortOpen(FALSE);  
UpdateData(FALSE);
}

⌨️ 快捷键说明

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