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

📄 injecthosttdlg.cpp

📁 一个6缸发动机ECU的上位机监控程序源码
💻 CPP
字号:
// InjectHosttDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CInjectHosttDlg dialog

CInjectHosttDlg::CInjectHosttDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CInjectHosttDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CInjectHosttDlg)
	m_spray1Num= 1;
	m_tPump1Cyc = 4;     //4ms
	m_tSpray1On = 3;     //3ms   
	m_tSpray1Cyc = 6;    //60ms
	m_tPump1On = 2;    //2ms
	m_spray2Num=1;
	m_tPump2Cyc = 3;     //3ms
	m_tPump2On = 1;    //1ms
	m_tSpray2On = 0;
	m_tSpray2Cyc = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CInjectHosttDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInjectHosttDlg)
	DDX_Control(pDX, IDC_EDIT9, m_upback);
	DDX_Control(pDX, IDC_OPEN, m_open);
	DDX_Text(pDX, IDC_EDIT1, m_spray1Num);
	DDV_MinMaxInt(pDX, m_spray1Num, 0, 10000);
	DDX_Text(pDX, IDC_EDIT2, m_tPump1Cyc);
	DDX_Text(pDX, IDC_EDIT3, m_tSpray1On);
	DDX_Text(pDX, IDC_EDIT4, m_tSpray1Cyc);
	DDX_Text(pDX, IDC_EDIT5, m_tPump1On);
	DDX_Control(pDX, IDC_MSCOMM1, m_MSComm);
	DDX_Text(pDX, IDC_EDIT11, m_spray2Num);
	DDV_MinMaxInt(pDX, m_spray2Num, 0, 10000);
	DDX_Text(pDX, IDC_EDIT12, m_tPump2Cyc);
	DDX_Text(pDX, IDC_EDIT13, m_tSpray2On);
	DDX_Text(pDX, IDC_EDIT14, m_tSpray2Cyc);
	DDX_Text(pDX, IDC_EDIT15, m_tPump2On);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CInjectHosttDlg, CDialog)
	//{{AFX_MSG_MAP(CInjectHosttDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(ID_SEND, OnSend)
	ON_BN_CLICKED(IDC_ADBUTTON, OnAdbutton)
	
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInjectHosttDlg message handlers

BOOL CInjectHosttDlg::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
	
//	m_wave.SetLineColor(RGB(255,255,0),1);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CInjectHosttDlg::OnOpen() 
{

	
	if(!m_MSComm.GetPortOpen()) 
	{
		//在串口打开之前初始化
	    m_MSComm.SetCommPort(1);             //选择COM1 
        m_MSComm.SetInBufferSize(1024);      //接收缓冲区 
        m_MSComm.SetOutBufferSize(1024);     //发送缓冲区 
        m_MSComm.SetInputLen(0);             //设置当前接收区数据长度为0,表示全部读取 
        m_MSComm.SetInputMode(1);            //以二进制方式读写数据 
        m_MSComm.SetRThreshold(2);           //接收缓冲区有2个及2个以上字符时,将引发接收数据的OnComm事件 
        m_MSComm.SetSettings("9600,e,8,1");  //波特率9600 偶检验位,8个数据位,1个停止位 

		m_MSComm.SetPortOpen(true); //打开串口
		m_open.SetWindowText("关闭串口");

	}
    else
	{
	    m_MSComm.SetPortOpen(false); //关闭串口
		m_open.SetWindowText("打开串口");
	}




}

void CInjectHosttDlg::OnSend() 
{
	BYTE *ptr;
	UpdateData(true);    //从对话框中获取数据


	CByteArray array;
	array.RemoveAll();
	//array.SetSize(100);
	array.Add(255);    //0xff 请求发送
   
//				 array.RemoveAll();
				 ptr = (BYTE*)((void *) &m_spray1Num);
				 array.Add(*ptr++);
				 array.Add(*ptr);
			
				 //ptr = (BYTE*)((void *) &m_tSpray1On);
				 //array.Add(*ptr++);
                 //array.Add(*ptr);
                 array.Add(m_tSpray1On);

				 array.Add(m_tSpray1Cyc);
				 array.Add(m_tPump1Cyc);
				 array.Add(m_tPump1On);




                 ptr = (BYTE*)((void *) &m_spray2Num);
				 array.Add(*ptr++);
				 array.Add(*ptr);
			
			     array.Add(m_tSpray2On);
				
				 array.Add(m_tSpray2Cyc);
				 array.Add(m_tPump2Cyc);
				 array.Add(m_tPump2On);   
				 
				 

//				 int a = array.GetSize();
//				 CString made;
//				 made.Format("%d",a);    //debug
//				 MessageBox(made);       //debug

                 m_MSComm.SetOutput(COleVariant(array));

//	m_MSComm.SetOutput(COleVariant(array));
}

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

void CInjectHosttDlg::OnOnCommMscomm1() 
{
	int nEvent;
	VARIANT vRes;
	COleSafeArray rxData;
	LONG length,i;
	BYTE buf[16];
	CByteArray array;
	BYTE *ptr;
	int npress = 0;      //采集几次压力数据才刷新一次

	nEvent = m_MSComm.GetCommEvent();

	switch(nEvent)
	{
	case 2:
          vRes = m_MSComm.GetInput();  //读数据
          rxData = vRes;             //转换成COleSafeArray
          length = rxData.GetOneDimSize();
		  for(i=0;i<length;i++)
			  rxData.GetElement(&i,buf+i);
		  {

		/*	 else */if(buf[0] == 252 && buf[1] == 252)      //下位机接收成功
			 {
				 MessageBox("接收成功!");

			 }

			 else if(buf[0] == 254 && buf[1] == 254)      //重新发送
				 MessageBox("接收不成功,重新发送!");
			 else if(buf[0] == 250 && buf[1] == 250)      //下收到一个字符
				 MessageBox("超时!");
			 else                                         //压力数据
			 {
//				 CString made;       //debug
//				 made.Format("%d %d %d %d %d %d",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);//debug
//				 MessageBox(made);    //debug

				 unsigned short press;
				 ptr = (BYTE *) &press;
				 *ptr++ = buf[0];
				 *ptr = buf[1];

//				 CString hh;
//				 hh.Format("%d bar",press);
//				 m_press.SetWindowText(hh);        //debug  

           

				 if(press > 0 && press < 1020)
				 {
//				 cal = press * 20/ 1023;   //Ima,R = 250ohm
//				 float press1;
//                 press1  = (press * 20/ 1023.0 - 4.0) * 350.0 / 16.0  ;   //pressure/bar
//                 pressure = (int)cal;
//                ptr = (char*)&pressure;
				 npress ++;
                 CString hh;
				 
				 m_upback.SetWindowText(hh);

//				 m_wave.Push((int)(press1*100),1);


//					 m_wave.Update();
//					 npress = 0;

				 }

			 }
				
		  }
		  break;
	case 3:
		break;
	case 4:
		break;
	case 5:
		break;
	case 6:
		break;
	}

}

void CInjectHosttDlg::OnAdbutton() 
{
	// TODO: Add your control notification handler code here
    CByteArray array;
	array.RemoveAll();
	array.Add(254);    //0xff 请求发送
/*
					 int press = 680;
					 float press1;
					 press1 = press * 20/ 1023;
                 press  = (int)(press1 - 4) * 350 / 16  ;   //pressure/bar
//                 pressure = (int)cal;
//                ptr = (char*)&pressure;

                 CString hh;
				 hh.Format("%d bar",press1);
				 m_press.SetWindowText(hh);  */

/*	BYTE *ptr;
	                 UpdateData(true);    //从对话框中获取数据
				 array.RemoveAll();
				 ptr = (BYTE*)((void *) &m_spray1Num);
				 array.Add(*ptr++);
				 array.Add(*ptr);
				 array.Add(m_tPump1Cyc);
				 array.Add(m_tSpray1On);
				 array.Add(m_tSpray1Cyc);
				 array.Add(m_tPump1On);
				 array.Add(m_tPreSprayOn);
				 array.Add(m_tPreMainInterval);
				 int a = array.GetSize();
				 CString made;
				 made.Format("%d",a);    //debug
				 MessageBox(made);     */



	m_MSComm.SetOutput(COleVariant(array));
	
}




⌨️ 快捷键说明

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