承接专栏《MFC工控项目实例之十七添加手动测试界面》
用到PC6408板卡,根据板卡设置界面组合框选项设定的输入信号,通过读取文件中保存的键值,用线程的方式实时检测输入信号通断,这里只用启动、停止两个信号示例。
1、在Data_1.h文件中添加代码

CString COMB_Data_1[]={"启动","停止"};	
int COMB_Data_Number_1;
unsigned char b_1[16];

2、在JogTest.h中添加代码

class CJogTest : public CDialog
{
// Construction
public:
	CJogTest(CWnd* pParent = NULL);   // standard constructor
    CButtonST   m_btnStart_1[16];
    CFont m_font;
	CWinThread* pThread_1; 
	bool isThreadBegin_1 ;  //线程退出/执行控制
	void My_Func_1();
  static UINT My_TheradFunc_1(LPVOID lparam);
 CString	m_Path;
CString strFilePath;
int In_lamp[16];
...
protected:
unsigned short nAddr_1;
	// Generated message map functions
	//{{AFX_MSG(CJogTest)
	virtual BOOL OnInitDialog();
	afx_msg void OnRadio1();
	afx_msg void OnRadio2();
	virtual void OnOK();
	virtual void OnCancel();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};
#endif // !defined(AFX_JOGTEST_H__7E13E123_5220_40F7_9ED7_862C1685F610__INCLUDED_)

3、在JogTest.cpp中添加代码

	CJogTest::CJogTest(CWnd* pParent /*=NULL*/)
	: CDialog(CJogTest::IDD, pParent)
{
	m_font.CreateFont(-28,0,0,0,700,FALSE,FALSE,
					0,134,OUT_DEFAULT_PRECIS,
					CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
					FF_SWISS,"楷体_GB2312");
	TCHAR exeFullPath[MAX_PATH];
	GetModuleFileName(NULL,exeFullPath,MAX_PATH);
	m_Path = exeFullPath;
	for(int i = m_Path.GetLength() - 1; i > 0 ; i --)
	{
		if(m_Path.GetAt(i) == '\\')
			break;
	}
	SetCurrentDirectory(m_Path);
	m_Path = m_Path.Left(i);
    strFilePath= m_Path + "\\Test.ini";
	nAddr_1 = 0x100; //设置板卡基地址
	COMB_Data_Number_1=(sizeof(COMB_Data_1) / sizeof(COMB_Data_1[1]));

}


BOOL CJogTest::OnInitDialog() 
{
	CDialog::OnInitDialog();
	for(int i=0;i<=COMB_Data_Number_1;i++)
   {
	   
	GetPrivateProfileString("输入信号",COMB_Data_1[i],NULL,b_str_1[i].GetBuffer(20),20,strFilePath);	

	}
	

				for ( i=0;i<2;i++)
	{
			m_btnStart_1[i].SetIcon(IDI_ICON_LedOff);
    }

    for ( i=0;i<14;i++)

	{
		GetDlgItem(IDC_STATIC0+i)->SetFont(&m_font);
		SetDlgItemText(IDC_STATIC0+i,"0");

	}

	if( OpenDevice6K() != TRUE )
		AfxMessageBox("Open device failed!");
	DO6408All(nAddr_1,0X0);
	SetTimer(1,500,NULL);
	isThreadBegin_1= true;            //运行线程执行
     pThread_1= AfxBeginThread(My_TheradFunc_1,(LPVOID)this);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


UINT CJogTest::My_TheradFunc_1(LPVOID lparam)//将窗口指针赋给无类型指针
 
{
 
     CJogTest *dlg = (CJogTest*)lparam;   //可以调用CBoardTest定义的类成员函数
     while (dlg->isThreadBegin_1)
	{
		dlg->My_Func_1(); //线程要执行的函数(调用CTheradtDlg定义的类成员函数)
	
	}
 return 0;
     
}


void CJogTest::My_Func_1() //处理函数在这里写
{
	 for(int i=0;i<2;i++)
	 {
In_lamp[i]=_ttoi(b_str_1[i]);
b_1[i]=DI6408Bit(nAddr_1,In_lamp[i]);
    if (b_1[i])
	{
      m_btnStart_1[i].SetIcon(IDI_ICON_LedOn);
	}
	else
	{
		 m_btnStart_1[i].SetIcon(IDI_ICON_LedOff);
	}
	 }
    Sleep(100);
}

void CJogTest::OnRadio1() 
{
	CString tempStr;
	//int n=b_1[1];
//str.Format("s%",n);
tempStr.Format("%s",b_1[1]);
//sprintf(tempStr,"%s",b_1[1]);
   // SetDlgItemText(IDC_STATIC99,tempStr1);
	SetDlgItemText(IDC_STATIC101,tempStr);
	
}

void CJogTest::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	
}

void CJogTest::OnOK() 
{
	isThreadBegin_1  = false;            //停止线程执行
KillTimer(1); 
 Sleep(500);
EndDialog(IDD_DIALOG_SYS_TEST);
	CDialog::OnOK();
}

void CJogTest::OnCancel() 
{
isThreadBegin_1  = false;            //停止线程执行
KillTimer(1); 
 Sleep(500);
EndDialog(IDD_DIALOG_SYS_TEST);
	CDialog::OnCancel();
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部