扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
//定义一个结构体变量保存64通道的DI信息 struct tagSYSTEMSTATE { int nState;//DI状态 BOOL bState;//是否为数字量接点 }; //初始化里面启动线程进行板卡的采集工作 void CMy11View::OnInitialUpdate() { CView::OnInitialUpdate(); m_Info = new tagSYSTEMSTATE[64]; for(int i=0;i<64;i++) { m_Info[i].nState = 0; m_Info[i].bState = FALSE; } unsigned int nDummy; m_pThread=(HANDLE) _beginthreadex(NULL,0,Main_Thread_DI,this,CREATE_SUSPENDED,&nDummy);//开辟DI线程 if (!m_pThread) TRACE(_T(" Couldn't start a thread\n")); else ResumeThread(m_pThread); } //转线程进行采集的循环工作 UINT WINAPI CMy11View::Main_Thread_DI(LPVOID pParam)//控制DI线程 { CMy11View * pView=(CMy11View *)pParam; while(1) { pView->ptDeviceReadDIByte(); pView->nVal++; pView->ShowMsg(); Sleep(10); } return 0; } //下面是调用DLL进行信号采集工作 void CMy11View::ptDeviceReadDIByte() { SHORT gnNumOfDevices; LONG DriverHandle = (LONG)NULL; USHORT gwChannel; static PT_DioReadPortByte ptDioReadPortByte; USHORT gwValue; gnNumOfDevices = 1; DRV_DeviceOpen(gnNumOfDevices,//板号0; (LONG far *)&DriverHandle); for(gwChannel = 0; gwChannel < 8;gwChannel ++) { ptDioReadPortByte.port = gwChannel;//////////0-7通道号 ptDioReadPortByte.value = (USHORT far *)&gwValue; DRV_DioReadPortByte(DriverHandle, (LPT_DioReadPortByte)&ptDioReadPortByte); gwValue=gwValue&0x00ff;///////////////// for(int j=0;j<8;j++) { if( (gwValue & (1<<j)) >0) m_Info[gwChannel*8+j].nState = 1; else m_Info[gwChannel*8+j].nState = 0; } } DRV_DeviceClose((LONG far *)&DriverHandle); } //下面将个通道点状态在界面输出,这里各位可以根据实际情况进行修改 void CMy11View::ShowMsg() { CDC *pDC; pDC = GetDC(); CRect rect; GetClientRect(&rect); CString strText; CBrush pNewBrush,*pOldBrush; pNewBrush.CreateSolidBrush(RGB(128,128,255)); pOldBrush=pDC->SelectObject(&pNewBrush); pDC->FillRect(&rect,&pNewBrush); COLORREF *oldCol; oldCol=(COLORREF *)pDC->SetTextColor(RGB(255,0,0)); pDC->SetBkColor(RGB(128,128,255)); pDC->TextOut(10,10,"采集模拟点显示信息:"); strText.Format("线程采集程序运行了%d次",nVal); pDC->TextOut(10,35,strText); for(int i=0;i<64;i++) { strText.Format("第%.2d个结点的信息是:%d",i+1,m_Info[i].nState); int nHeight= (i/4)*25; int nWidth = (i%4)*180; pDC->TextOut(10+nWidth,70+nHeight,strText); } pDC->SelectObject(&oldCol); pDC->SelectObject(&pOldBrush); pNewBrush.DeleteObject(); pDC->DeleteDC(); } //到此,DI输入编程基本结束,输出方法类似,这里不在进行叙述了. |
//AD大致情况入DI,相应的主要不同在于板卡的采集程序,大致见下: void CADDemoView::ptDeviceReadADByte() { USHORT gwChannel = 0; // input channel float gwValue; SHORT gnNumOfDevices; static PT_AIConfig ptAIConfig; // structure for AIConfig table static PT_AIVoltageIn ptAIVoltageIn; // structure for AIVoltageIn table static PT_DeviceGetFeatures ptDevFeatures; static DEVFEATURES DevFeatures; // structure for device features gnNumOfDevices = 0; DRV_DeviceOpen(gnNumOfDevices,//板号0; (LONG far *)&DriverHandle); ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures; ptDevFeatures.size = sizeof(DEVFEATURES); DRV_DeviceGetFeatures(DriverHandle,//句标市柄 (LPT_DeviceGetFeatures)&ptDevFeatures); for(gwChannel = 0; gwChannel < 32;gwChannel ++) { Sleep(1); ptAIVoltageIn.chan = gwChannel; ptAIVoltageIn.gain = 0; ptAIVoltageIn.TrigMode = 0; // internal trigger ptAIVoltageIn.voltage = (FLOAT far *)&gwValue; DRV_AIVoltageIn(DriverHandle,(LPT_AIVoltageIn)&ptAIVoltageIn); m_Info[gwChannel].fState = gwValue; } DRV_DeviceClose((LONG far *)&DriverHandle); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者