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

📄 adan4050testdlg.cpp

📁 研华ADAN4050 检测程序(自做) 压缩文件中 无密码
💻 CPP
字号:
// ADAN4050TestDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CADAN4050TestDlg dialog

CADAN4050TestDlg::CADAN4050TestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CADAN4050TestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CADAN4050TestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_ucInMark=0;
	m_ucOutMark=0;
	m_bReportInputError=TRUE;
}

void CADAN4050TestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CADAN4050TestDlg)
	DDX_Control(pDX, IDC_OUTPUT8, m_chkOutput8);
	DDX_Control(pDX, IDC_OUTPUT7, m_chkOutput7);
	DDX_Control(pDX, IDC_OUTPUT6, m_chkOutput6);
	DDX_Control(pDX, IDC_OUTPUT5, m_chkOutput5);
	DDX_Control(pDX, IDC_OUTPUT4, m_chkOutput4);
	DDX_Control(pDX, IDC_OUTPUT3, m_chkOutput3);
	DDX_Control(pDX, IDC_OUTPUT2, m_chkOutput2);
	DDX_Control(pDX, IDC_OUTPUT1, m_chkOutput1);
	DDX_Control(pDX, IDC_INPUT8, m_chkInput8);
	DDX_Control(pDX, IDC_INPUT7, m_chkInput7);
	DDX_Control(pDX, IDC_INPUT6, m_chkInput6);
	DDX_Control(pDX, IDC_INPUT5, m_chkInput5);
	DDX_Control(pDX, IDC_INPUT4, m_chkInput4);
	DDX_Control(pDX, IDC_INPUT3, m_chkInput3);
	DDX_Control(pDX, IDC_INPUT2, m_chkInput2);
	DDX_Control(pDX, IDC_INPUT1, m_chkInput1);
	DDX_Control(pDX, IDC_COMSEL, m_cmbComSel);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CADAN4050TestDlg, CDialog)
	//{{AFX_MSG_MAP(CADAN4050TestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_COMSEL, OnSelchangeComsel)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_OUTPUT1, OnOutput1)
	ON_BN_CLICKED(IDC_OUTPUT2, OnOutput2)
	ON_BN_CLICKED(IDC_OUTPUT3, OnOutput3)
	ON_BN_CLICKED(IDC_OUTPUT4, OnOutput4)
	ON_BN_CLICKED(IDC_OUTPUT5, OnOutput5)
	ON_BN_CLICKED(IDC_OUTPUT6, OnOutput6)
	ON_BN_CLICKED(IDC_OUTPUT7, OnOutput7)
	ON_BN_CLICKED(IDC_OUTPUT8, OnOutput8)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CADAN4050TestDlg message handlers

BOOL CADAN4050TestDlg::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
	ActiveThread();
	::Sleep(100);

	CAppRegistry oAppReg(ADAN_COMPANY,ADAN_APP,ADAN_VER,_T(""));
	long lComSel=0;
	if(oAppReg.GetRegLong(NULL,ADAN_COMSEL,lComSel)==FALSE)
		oAppReg.SetRegLong(NULL,ADAN_COMSEL,lComSel);

	m_cmbComSel.SetCurSel(lComSel);
	PostThreadMessage(ADANM_COMCHANGED,0,0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CADAN4050TestDlg::OnSelchangeComsel() 
{
	// TODO: Add your control notification handler code here
	long lComSel=m_cmbComSel.GetCurSel();
	CAppRegistry oAppReg(ADAN_COMPANY,ADAN_APP,ADAN_VER,_T(""));
	oAppReg.SetRegLong(NULL,ADAN_COMSEL,lComSel);
	PostThreadMessage(ADANM_COMCHANGED,0,0);
}

BOOL CADAN4050TestDlg::MessageProc(MSG &rMsg)
{
	CAppRegistry oAppReg(ADAN_COMPANY,ADAN_APP,ADAN_VER,_T(""));
	long lComSel=0;
	if(oAppReg.GetRegLong(NULL,ADAN_COMSEL,lComSel)==FALSE)
		oAppReg.SetRegLong(NULL,ADAN_COMSEL,lComSel);
	CString strTemp;
	char acCmd[10];
	switch(rMsg.message)
	{
	case ADANM_COMCHANGED:
		m_oCommPort.Close();
		if(lComSel==0)
			break;
		m_cmbComSel.GetLBText(lComSel,strTemp);
		if(m_oCommPort.Open(strTemp,ADAN_DEFBAUDRATE)==FALSE)
		{
			m_cmbComSel.SetCurSel(0);
			oAppReg.SetRegLong(NULL,ADAN_COMSEL,0);
			CString strErrMsg;
			strErrMsg.Format(_T("通讯端口<%s>已经被占用或者不存在!"),strTemp);
			MessageBox(strErrMsg,_T("错误提示"),MB_OK|MB_ICONHAND);
		}
		else
			m_bReportInputError=TRUE;
		break;
	case ADANM_DIGITALOUTPUT:
		if(BOOL(rMsg.lParam))
			m_ucOutMark|=BYTE(1<<rMsg.wParam);
		else
			m_ucOutMark&=~BYTE(1<<rMsg.wParam);
		if(m_oCommPort.IsOpened())
		{
			::sprintf(acCmd,"#0000%02X\r",m_ucOutMark);
			m_oCommPort.Write(acCmd,::strlen(acCmd));
			if(m_oCommPort.Read(acCmd,4)==0)
				MessageBox(_T("输出数字量时IO模块没有响应!"),_T("错误提示"),MB_OK|MB_ICONHAND);
			else if(acCmd[0]=='?')
				MessageBox(_T("输出数字量命令执行失败!"),_T("错误提示"),MB_OK|MB_ICONHAND);
		}
		break;
	}
	return TRUE;
}

BOOL CADAN4050TestDlg::ThreadProc()
{
	if(m_oCommPort.IsOpened())
	{
		BYTE ucInMark;
		BYTE ucOutMark;
		char acResStr[10];
		m_oCommPort.Write("$006\r",5);
		//取回状态并进行分析
		acResStr[0]=0;
		if(m_oCommPort.Read(acResStr,10)==8&&acResStr[0]=='!')
		{
			DWORD dwMark=0;
			acResStr[5]=0;
			::sscanf(acResStr+1,"%x",&dwMark);
			ucInMark=BYTE(dwMark);
			ucOutMark=BYTE(dwMark>>8);
			BYTE ucTemp;
			ucTemp=ucInMark^m_ucInMark;
			CButton * apbtInput[8]={&m_chkInput1,&m_chkInput2,&m_chkInput3,&m_chkInput4,&m_chkInput5,&m_chkInput6,&m_chkInput7,&m_chkInput8};
			if(ucTemp)
			{
				for(int i=0;i<8;i++)
				{
					if(ucTemp&BYTE(1<<i))
					{
						if(ucInMark&BYTE(1<<i))
							apbtInput[i]->SetCheck(1);
						else
							apbtInput[i]->SetCheck(0);
					}
				}
			}
			ucTemp=ucOutMark^m_ucOutMark;
			CButton * apbtOutput[8]={&m_chkOutput1,&m_chkOutput2,&m_chkOutput3,&m_chkOutput4,&m_chkOutput5,&m_chkOutput6,&m_chkOutput7,&m_chkOutput8};
			if(ucTemp)
			{
				for(int i=0;i<8;i++)
				{
					if(ucTemp&BYTE(1<<i))
					{
						if(ucOutMark&BYTE(1<<i))
							apbtOutput[i]->SetCheck(1);
						else
							apbtOutput[i]->SetCheck(0);
					}
				}
			}
			m_ucInMark=ucInMark;
			m_ucOutMark=ucOutMark;
			if(m_bReportInputError==FALSE)
			{
				MessageBox(_T("输入数字量命令执行成功!"),_T("成功提示"),MB_OK);
				m_bReportInputError=TRUE;
			}
		} //if(m_oCommPort.RecvData(acResStr,10)==8&&acResStr[0]=='!')
		else if(m_bReportInputError)
		{
			m_bReportInputError=FALSE;
			if(acResStr[0]=='?')
				MessageBox(_T("输入数字量命令执行失败!"),_T("错误提示"),MB_OK|MB_ICONHAND);
			else if(acResStr[0]==0)
				MessageBox(_T("输入数字量时IO模块没有响应!"),_T("错误提示"),MB_OK|MB_ICONHAND);
		}
	} //if(m_oCommPort.IsOK())
	else
		::Sleep(1);
		
	return TRUE;
}

ULONG CADAN4050TestDlg::OnThreadDestroy(ULONG ulExitCode)
{
	m_oCommPort.Close();
	return ulExitCode;
}

void CADAN4050TestDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	ActiveThread(FALSE);
}

void CADAN4050TestDlg::OnOutput1() 
{
	// TODO: Add your control notification handler code here
	DigitalOutput(0,m_chkOutput1.GetCheck());
}

void CADAN4050TestDlg::OnOutput2() 
{
	// TODO: Add your control notification handler code here
	DigitalOutput(1,m_chkOutput2.GetCheck());
}

void CADAN4050TestDlg::OnOutput3() 
{
	// TODO: Add your control notification handler code here
	DigitalOutput(2,m_chkOutput3.GetCheck());
}

void CADAN4050TestDlg::OnOutput4() 
{
	// TODO: Add your control notification handler code here
	DigitalOutput(3,m_chkOutput4.GetCheck());
}

void CADAN4050TestDlg::OnOutput5() 
{
	// TODO: Add your control notification handler code here
	DigitalOutput(4,m_chkOutput5.GetCheck());
}

void CADAN4050TestDlg::OnOutput6() 
{
	// TODO: Add your control notification handler code here
	DigitalOutput(5,m_chkOutput6.GetCheck());
}

void CADAN4050TestDlg::OnOutput7() 
{
	// TODO: Add your control notification handler code here
	DigitalOutput(6,m_chkOutput7.GetCheck());
}

void CADAN4050TestDlg::OnOutput8() 
{
	// TODO: Add your control notification handler code here
	DigitalOutput(7,m_chkOutput8.GetCheck());
}

void CADAN4050TestDlg::DigitalOutput(WORD wChannelNo, BOOL bState)
{
	PostThreadMessage(ADANM_DIGITALOUTPUT,WPARAM(wChannelNo),LPARAM(bState));
}

⌨️ 快捷键说明

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