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

📄 west4170commdlg.cpp

📁 可同时显示8块West4170的当前温度值 并可设值目标值
💻 CPP
字号:
// West4170CommDlg.cpp : implementation file
//

#include "stdafx.h"
#include "West4170Comm.h"
#include "West4170CommDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
static int meter_num=0;	//表号

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

/////////////////////////////////////////////////////////////////////////////
// CWest4170CommDlg dialog

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

void CWest4170CommDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWest4170CommDlg)
	DDX_Text(pDX, IDC_EDIT3_RECEIVE, m_strEditReceive);
	DDX_Text(pDX, IDC_EDIT_TX, m_strSendData);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWest4170CommDlg, CDialog)
	//{{AFX_MSG_MAP(CWest4170CommDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_MESSAGE(WM_COMM_RXCHAR,OnComm)
	ON_BN_CLICKED(IDC_BUTTON_OPEN_COM, OnButtonOpenCom)
	ON_BN_CLICKED(IDC_BUTTON_START_WORK, OnButtonStartWork)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_READ_PARMETET, OnButtonReadParm)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWest4170CommDlg message handlers

BOOL CWest4170CommDlg::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_bSerialPortOpened=FALSE;
	m_complete=TRUE;
	GetDlgItem(IDC_BUTTON_START_WORK)->EnableWindow(m_bSerialPortOpened);
	//设定串口Com1
	CComboBox* pcombo=(CComboBox*)GetDlgItem(IDC_COMBO1_COM_PORT);
	pcombo->SetCurSel(3);
	//设定表号
	pcombo=(CComboBox*)GetDlgItem(IDC_COMBO_METER_INDEX);
	pcombo->SetCurSel(0);
	//设置参数表:
	pcombo=(CComboBox*)GetDlgItem(IDC_COMBO_PARAMETER);
	pcombo->AddString("M:过程值(只读)");
	pcombo->AddString("v:过程值偏置");
	pcombo->AddString("G:量程上限");
	pcombo->AddString("H:量程下限");
	pcombo->AddString("Q:输入小数点位置");
	pcombo->AddString("m:数字滤波时间");
	pcombo->AddString("N:阀门满行程时间");
	pcombo->AddString("O:阀门最小开启时间");
	pcombo->AddString("[:变送输出上限");
	pcombo->AddString("\\变送输出下限");
	pcombo->AddString("S:设定值");
	pcombo->AddString("^:设定值斜率");
	pcombo->AddString("A:设定值上限");
	pcombo->AddString("T:设定值下限");
	pcombo->AddString("C:报警1的值");
	pcombo->AddString("E:报警2的值");
	pcombo->AddString("D:微分时间常数");
	pcombo->AddString("I:积分时间常数");
	pcombo->AddString("P:比例带");
	pcombo->AddString("L:控制器状态");
	pcombo->AddString("V:偏差值");
	pcombo->AddString("]:扫描表");


	pcombo->SetCurSel(10);				//显示设定值

	//设置串口通讯方式
	CEdit* pcomSet=(CEdit*)GetDlgItem(IDC_EDIT_COM_SET);
	pcomSet->SetWindowText("9600,e,7,1");

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

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

LONG CWest4170CommDlg::OnComm(WPARAM wParam,LPARAM lParam)
{
	CString strTemp;
	strTemp.Format("%c",wParam);
	m_strEditReceive+=strTemp;
	int len=m_strEditReceive.GetLength();
	if(m_strEditReceive.GetAt(0)=='L' && m_strEditReceive.GetAt(len-1)=='*')
	{
		ProcessRxData();
		
		m_strEditReceive="";
	}
	//UpdateData(FALSE);

	return 0;
}

void CWest4170CommDlg::OnButtonOpenCom() 
{
	// TODO: Add your control notification handler code here
	


		CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTON_OPEN_COM);

	
	
	
	if(!m_bSerialPortOpened)
	{
		//读串口Com设定
		CComboBox* pcomport=(CComboBox*)GetDlgItem(IDC_COMBO1_COM_PORT);
		int nPort=pcomport->GetCurSel()+1;

			//读串口通讯方式设置
// 		CEdit* pcomSet=(CEdit*)GetDlgItem(IDC_EDIT_COM_SET);
// 		CString str;
// 		pcomSet->GetWindowText(str);
		if(m_ctrlComm.InitPort(this,nPort,9600,'E',7,1,EV_RXFLAG|EV_RXCHAR,512))
		{
			m_ctrlComm.StartMonitoring();
			m_bSerialPortOpened=TRUE;
					
			pButton->SetWindowText("关闭串口");
		
		
		}
	}
	else
	{
		m_ctrlComm.ClosePort();
		m_bSerialPortOpened=FALSE;
		
		pButton->SetWindowText("打开串口");	
		
	
	}

	GetDlgItem(IDC_BUTTON_START_WORK)->EnableWindow(m_bSerialPortOpened);
	
}

void CWest4170CommDlg::OnButtonStartWork() 
{
	CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTON_OPEN_COM);

	CButton* pButton1=(CButton*)GetDlgItem(IDC_BUTTON_START_WORK);
	CString str;
	pButton1->GetWindowText(str);
	if(str=="开始发送")
	{
	
	pButton->EnableWindow(FALSE);


	//启动定时器,发送数据
	SetTimer(1,200,NULL);
	pButton1->SetWindowText("停止发送");
	}
	else
	{
		pButton1->SetWindowText("开始发送");
		pButton->EnableWindow(TRUE);
		KillTimer(1);

	}
	
}

void CWest4170CommDlg::SendData(int meter_index, CString parameter_index)
{
	

	CString strTemp="L";
	CString temp;
	
	temp.Format("%02d",meter_index);			//表号
	strTemp+=temp;					//L{N}
	
	
	if(parameter_index=="1")		//send L{N}??*,usually return:L{N}?A*
	{
		
		strTemp+="??*";
	}
	else if(parameter_index=="]")		//read actual value and set value:
	{
		strTemp+="]?*";
		
	}
	else if(parameter_index=="S")
	{
		CString str;
		GetDlgItem(IDC_EDIT_ENTER_VALUE1+meter_num-1)->GetWindowText(str);
		//int setvalue=atoi((const char*)str);
		strToData(str);
		strTemp+="S#"+str+"*";
	}
		
	else if(parameter_index=="4")
	{
		strTemp+="SI*";
		m_complete=TRUE;

	}
	else
	{

	}
	


	
// 	for(int i=0;i<strTemp.GetLength();i++)
// 		hexdata.Add(strTemp.GetAt(i));
	
	if(m_ctrlComm.m_hComm==NULL)
	{
		
		//AfxMessageBox("串口没有打开,请打开串口");
		return;
	}
	else
	{
		
		m_ctrlComm.WriteToPort((LPCTSTR)strTemp);	//发送数据
		SetTimer(2,80,NULL);		//如果表没有回应,对表进行状态处理
	}

}


void CWest4170CommDlg::OnTimer(UINT nIDEvent) 
{
	
	
	switch(nIDEvent)
	{
	case 1:		//定时发送读表信号
		{
			if(m_complete)
			{
				CButton* pCheck=(CButton*)GetDlgItem(IDC_CHECK_USE1+meter_num);
				while(!pCheck->GetCheck())
				{
					meter_num++;	//去掉不工作的表号
					if(meter_num>7)		//最多8块表,可任意选择
						break;		//跳出
					
					pCheck=(CButton*)GetDlgItem(IDC_CHECK_USE1+meter_num);
				}
				
				
				meter_num++;		//表号加一
				
				if(meter_num>8)		//最多8块表,可任意选择
					meter_num=0;	//又从第一块开始
				else if(meter_num!=0)
				{
					SendData(meter_num,"1");			//发送数据
					
					m_complete=FALSE;
				}
				
			}
			
			break;
			
		}
		
	case 2:
		KillTimer(2);
		m_complete=TRUE;
		Beep(3000,30);				//有error时发出提示音
		
		break;
		
	}
	
	CDialog::OnTimer(nIDEvent);
}

void CWest4170CommDlg::OnButtonReadParm() 
{
	if(m_ctrlComm.m_hComm==NULL)
	{
		//m_ctrlAutoSend.SetCheck(0);
		AfxMessageBox("串口没有打开,请打开串口");
		return;
	}
	else
	{
		UpdateData(TRUE);
		

			m_ctrlComm.WriteToPort((LPCTSTR)m_strSendData);	//发送数据

// 		}
	
		
	}
	
}

void CWest4170CommDlg::ProcessRxData()
{

	int len=m_strEditReceive.GetLength();				//收到数据的长度
	CString str;
	CString strtemp1,strtemp2,strtemp3;

	strtemp1="L";
	strtemp2.Format("%02d",meter_num);
	strtemp1+=strtemp2+"?A*";
	if(m_strEditReceive.Find(strtemp1)!=-1)		//一类指令成功返回
	{
		KillTimer(2);
		SendData(meter_num,"]");
		m_complete=FALSE;
		return;
	}

	if(m_strEditReceive.Find("]20")!=-1)				//扫描表指令
	{
		KillTimer(2);
		strtemp1=m_strEditReceive.Mid(6,5);		//设定值
		strTostr(strtemp1);
		GetDlgItem(IDC_EDIT_SET_VALUE1+meter_num-1)->SetWindowText(strtemp1);		//在主显示位置显示数据
		strtemp2=m_strEditReceive.Mid(11,5);		//实际值
		strTostr(strtemp2);
		GetDlgItem(IDC_EDIT_ACTUAL_VALUE1+meter_num-1)->SetWindowText(strtemp2);

		
		GetDlgItem(IDC_EDIT_ENTER_VALUE1+meter_num-1)->GetWindowText(strtemp3);
		if(strtemp1!=strtemp3 && strtemp3!="")
		{
			//int setvalue=atoi((const char*)strtemp3);
			SendData(meter_num,"S");

		}
		else
			m_complete=TRUE;
		return;
		
	}

	if(m_strEditReceive.Find("I*")!=-1)
	{
		KillTimer(2);
		SendData(meter_num,"4");	//4类指令
		
	}
	


}

void CWest4170CommDlg::strToData(CString &str)
{
	int len=str.GetLength();
	int x=0;
	int y=0;


	if(len>4)
	{
		MessageBox("字数太多",NULL,MB_OK);
		return;
	}
	x=str.Find('.');
	if(x==-1)
	{
		y=0;				//最后面应补的字
	}
	else					//有小数点
	{
		y=len-x-1;			//最后面应补的字
		
		CString left=str.Left(x);
		CString righ=str.Right(len-x-1);
		str=left+righ;			//去小数点

	}
	
	
	if(str.GetAt(0)=='-')
	{
		y+=5;				//是负数加上5
		str.Delete(0);			//去掉负号
		
	}
	x=atoi((const char*)str);
	x=x*10+y;
	str.Format("%05d",x);

//	str+=(char)(y+0x30);
//	str.Format("%05c",str);

}

void CWest4170CommDlg::strTostr(CString &ton)
{

	CString temp,str;
	char ch;
	int answer=0;
	int k,x,y;
	temp=ton.Left(4);
	ch=ton.GetAt(4);

	answer=atoi((const char*)temp);
	k=atoi(&ch);
	switch(k)
	{
	case 0:
	case 5:			//无小数
		x=answer;
		break;
	case 1:
	case 6:			//一位小数
		x=answer/10;
		y=answer%10;
		break;
	case 2:
	case 7:			//2位小数
		x=answer/100;
		y=answer%100;
		break;
	case 3:
	case 8:			//3位小数
		x=answer/1000;
		y=answer%1000;

	}
	if(k>4)			//大于5是负数
		x=-x;
	if(k==0 ||k==5)
	{
		str.Format("%i",x);
	}
	else
	{
		str.Format("%i.%i",x,y);
	}
	

	ton=str;
}

⌨️ 快捷键说明

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