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

📄 expansionvalvecontrollerview.cpp

📁 一本关于用计算机控制电子膨胀阀的程序
💻 CPP
字号:
// ExpansionValveControllerView.cpp : implementation of the CExpansionValveControllerView class
//

#include "stdafx.h"
#include "ExpansionValveController.h"

#include "ExpansionValveControllerDoc.h"
#include "ExpansionValveControllerView.h"

#include "Set0Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
static bool opened=false;  //初始时阀体为关闭状态
static baudrate=800;       //串口的波特率,通过该参数可以调节步进电机开闭的速度
static int max_steps=500;  //最大脉冲数,在此需要根据具体型号来确定
#endif



/////////////////////////////////////////////////////////////////////////////
// CExpansionValveControllerView

IMPLEMENT_DYNCREATE(CExpansionValveControllerView, CFormView)

BEGIN_MESSAGE_MAP(CExpansionValveControllerView, CFormView)
	//{{AFX_MSG_MAP(CExpansionValveControllerView)
	ON_BN_CLICKED(IDC_SET_OPEN_BUTTON, OnSetOpenButton)
	ON_COMMAND(ID_TOOL_SET0, OnToolSet0)
	ON_BN_CLICKED(IDC_btnOnOff, OnbtnOnOff)
	ON_BN_CLICKED(IDC_btnMaxSteps, OnbtnMaxSteps)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExpansionValveControllerView construction/destruction

CExpansionValveControllerView::CExpansionValveControllerView()
: max_open_steps(max_steps), CFormView(CExpansionValveControllerView::IDD) 
{
	//{{AFX_DATA_INIT(CExpansionValveControllerView)
	m_current_open = 0;
	m_set_open = 0;
	m_MaxSteps = 0;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_pulsbuf = new char[2048];
	std::fill_n(m_pulsbuf, 2048, 0x0F);
	m_bDTR = FALSE;
	m_open = 0;
}

CExpansionValveControllerView::~CExpansionValveControllerView()
{
	m_regkey.Close();
	CloseHandle(m_com);
	delete[] m_pulsbuf;
}

void CExpansionValveControllerView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExpansionValveControllerView)
	DDX_Control(pDX, IDC_btnOnOff, m_OnOff);
	DDX_Control(pDX, IDC_OPEN_SPIN, m_open_spin);
	DDX_Text(pDX, IDC_CURRENT_OPEN_EDIT, m_current_open);
	DDX_Text(pDX, IDC_SET_OPEN_EDIT, m_set_open);
	DDV_MinMaxInt(pDX, m_set_open, 0, 1000);
	DDX_Text(pDX, IDC_editMaxSteps, m_MaxSteps);
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

void CExpansionValveControllerView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	//GetDlgItem(IDD_EXPANSIONVALVECONTROLLER_FORM)->SetWindowText("两相混合步进电机驱动式电子膨胀阀控制调节程序");

	m_open_spin.SetRange(0, 1000);

	// OPEN COM
	m_com = CreateFile("COM1", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (m_com == (HANDLE)0XFFFFFFFF) {
		DWORD er = GetLastError();
		MessageBox("open COM1 error!");
		ExitProcess(er);
	}
	DCB dcb;
	if (!GetCommState(m_com, &dcb)) {
		DWORD er = GetLastError();
		MessageBox("get comm state error!");
		ExitProcess(er);
	}
//	CString tmp;
//	tmp.Format("%d, %d, %d", dcb.BaudRate, dcb.ByteSize, dcb.StopBits);
//	MessageBox(tmp);
	dcb.BaudRate = 800; // baudrate = 800
	dcb.ByteSize = 8;
	dcb.StopBits = 2;
	if (!SetCommState(m_com, &dcb)) {
		DWORD er = GetLastError();
		MessageBox("set comm state error!");
		ExitProcess(er);
	}
//	if (!EscapeCommFunction(m_com, CLRDTR)) 
//		MessageBox("reverse error!");

	// open and read opening from register
	LPCTSTR rg = "SOFTWARE\\xxh\\ExpansionValveController";
	LPCTSTR rk = "opening";
	if (m_regkey.Open(HKEY_LOCAL_MACHINE, rg) == ERROR_SUCCESS) {
		DWORD dop;
		if (m_regkey.QueryValue(dop, rk) == ERROR_SUCCESS) {
			m_open = dop;
			m_current_open = m_open * 1000 / max_open_steps;
			UpdateData(FALSE);
		} else {
			dop = m_open;
			if (m_regkey.SetValue(dop, rk) != ERROR_SUCCESS) 
				MessageBox("set key failed!");
		}
	} else {
		if (m_regkey.Create(HKEY_LOCAL_MACHINE, rg) != ERROR_SUCCESS)
			MessageBox("create regkey failed!");
		else{
			DWORD dop = m_open;
			if (m_regkey.SetValue(dop, rk) != ERROR_SUCCESS) 
				MessageBox("set key failed!");
		}
	} 
  if (opened==true)
  {
	 GetDlgItem(IDC_btnOnOff)->SetWindowText("阀体正在通电!");   
  }
  else
  {
	   GetDlgItem(IDC_btnOnOff)->SetWindowText("阀体已经关断!"); 
  } 
  m_MaxSteps=max_steps;   //设置总脉冲数文本框的初始值
  UpdateData(FALSE);
}

/////////////////////////////////////////////////////////////////////////////
// CExpansionValveControllerView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CExpansionValveControllerView message handlers

void CExpansionValveControllerView::OnSetOpenButton() 
{
	// TODO: Add your control notification handler code here
	if (!UpdateData(TRUE))
		return;
	if (opened==false)
	{
       MessageBox("阀体已经关断 ,请先打开再驱动!");
	   return;
	}
	int pn = m_set_open * max_open_steps / 1000;
	if (pn == m_open)
		return;
	int go = pn - m_open;
	if (go > 0) {
		if (!EscapeCommFunction(m_com, SETDTR)) {
			MessageBox("set direction error!");
			return;
		}
	} else {
		if (!EscapeCommFunction(m_com, CLRDTR)) {
			MessageBox("set direction error!");
			return;
		}
		go = -go;
	}
	unsigned long wl = 0;
	if (!WriteFile(m_com, (LPCVOID)m_pulsbuf, go, &wl, NULL)) {
		MessageBox("set open error!");
		return;
	}
	CString tmp;
	tmp.Format("done, go %d steps.", wl);
	MessageBox(tmp);
	if (pn > m_open)
		m_open += wl;
	else
		m_open -= wl;
	m_current_open = int (1000 * m_open / max_open_steps);
	LPCTSTR rk = "opening";
	DWORD dop = m_open;
	if (m_regkey.SetValue(dop, rk) != ERROR_SUCCESS) 
		MessageBox("set key failed!");
	UpdateData(FALSE);
}

void CExpansionValveControllerView::OnToolSet0() 
{
	// TODO: Add your command handler code here
	CSet0Dlg dlg(m_com);
	if (dlg.DoModal() == IDOK) {
		m_open = 0;
		m_current_open = 0;
		LPCTSTR rk = "opening";
		DWORD dop = m_open;
		if (m_regkey.SetValue(dop, rk) != ERROR_SUCCESS) 
			MessageBox("set key failed!");
		UpdateData(FALSE);
	}
}

void CExpansionValveControllerView::OnbtnOnOff() 
{
	// TODO: Add your control notification handler code here
	if (opened==true)
	{  
	   EscapeCommFunction(m_com,SETRTS);	   
	   GetDlgItem(IDC_btnOnOff)->SetWindowText("电子膨胀阀已经关断!"); 
	   opened=false;
	   UpdateData(FALSE);
	}
	else
	{
       EscapeCommFunction(m_com,CLRRTS);	   
	   GetDlgItem(IDC_btnOnOff)->SetWindowText("电子膨胀阀正在通电!"); 
	   opened=true;
	   UpdateData(FALSE);
	}
	
}

void CExpansionValveControllerView::OnbtnMaxSteps() 
{
	// TODO: Add your control notification handler code here
	int new_MaxSteps;
	new_MaxSteps=GetDlgItemInt(IDC_editMaxSteps);
	if (new_MaxSteps<=0) 
	{
		MessageBox("输入阀体的总脉冲数不对,请重新输入!");
		m_MaxSteps=max_steps;
		UpdateData(FALSE);
		return;
	}
	else
	{
		max_steps=new_MaxSteps;
		UpdateData(TRUE);
		MessageBox("总脉冲数修改成功!");
	}
}

⌨️ 快捷键说明

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