承接专栏《MFC工控项目实例之十四模拟量信号名称从文件读写》
1、在BoardTest.h文件中添加代码
class CBoardTest : public CDialog
{
public:short m_saveData[32];unsigned short m_cardAddr;CBoardTest(CWnd* pParent = NULL); // standard constructorCButtonST m_btnStart[16],m_btnStart_O[16];CWinThread* pThread; bool isThreadBegin ; //线程退出/执行控制void My_Func();static UINT My_TheradFunc(LPVOID lparam);CString NO_Combox[16];
CString strTemp[16];//数据项名称
CString strRead[16];
int strReadId[16];
char strBuff[256];
CString m_Path;
CString strFilePath;
...
protected:
unsigned short nAddr;
2、在BoardTest.cpp文件中添加代码
UINT CBoardTest::My_TheradFunc(LPVOID lparam)//将窗口指针赋给无类型指针{CBoardTest *dlg = (CBoardTest*)lparam; //可以调用CBoardTest定义的类成员函数while (dlg->isThreadBegin){dlg->My_Func(); //线程要执行的函数(调用CTheradtDlg定义的类成员函数)}return 0;}void CBoardTest::My_Func() //处理函数在这里写
{int i;int b[16];CString n[16];CString str1;int m1= rand() % 65537; //int m1= 9;
// str1.Format("%d",m1);
// SetDlgItemText(IDC_EDIT1,str1);for (i=0;i<16;i++){// b[i]=((m1>>i)& 1);b[i]=(( DI6408All(nAddr)>>i)& 1); }for (i=0;i<16;i++){n[i].Format("%d",b[i]);}for (i=0;i<16;i++){if ( b[i] == 1){m_btnStart[i].SetIcon(IDI_ICON_LedOn); }else {m_btnStart[i].SetIcon(IDI_ICON_LedOff);}}Sleep(500);
}void CBoardTest::OnButton1()
{
isThreadBegin = false; //停止线程执行
KillTimer(1); Sleep(500);
EndDialog(IDD_DIALOG_BOA_TEST);
}void CBoardTest::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call defaultif( nIDEvent == 1 ){//16通道,2 = -5000 -- 5000 mV采集AI6325AAllSingle(0X110, 2, m_saveData);CString tempStr;tempStr.Format("错误号: %d", GetDll_LastErrNO());SetWindowText(tempStr); for(int i=0; i<16; i++){tempStr.Format("%d mV", m_saveData[i]);SetDlgItemText(IDC_STATIC0+i, tempStr);}UpdateData(TRUE);} CDialog::OnTimer(nIDEvent);
}
运行程序