Chinaunix首页 | 论坛 | 博客
  • 博客访问: 16500805
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 21:27:48

下载本文示例代码
  在论坛中经常有人提出关于研华工业用控制板卡的VC编程方法。此类板卡编程方法比较复杂,根据说明书上面说明我们可以采用两种编程方法,一种是自己编写控制各种寄存器代码进行数据的采集和输出,另外一种就是利用他封装的DLL的方法访问相应的函数进行完成。  为了节省时间提高开发效率,当然使用第二种方法了.我们可以根据研华光盘里面提供的例子进行修改就可以完成了.   1:A/D板编程实例(型号:PCL1754,通道:64) //定义一个结构体变量保存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<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);}  其实VC编程研华板卡不是一件很困难的事情,在他提供的光盘里面有VC的实例,我们可以根据实例进行修改就可以完成了.   在论坛中经常有人提出关于研华工业用控制板卡的VC编程方法。此类板卡编程方法比较复杂,根据说明书上面说明我们可以采用两种编程方法,一种是自己编写控制各种寄存器代码进行数据的采集和输出,另外一种就是利用他封装的DLL的方法访问相应的函数进行完成。  为了节省时间提高开发效率,当然使用第二种方法了.我们可以根据研华光盘里面提供的例子进行修改就可以完成了.   1:A/D板编程实例(型号:PCL1754,通道:64) //定义一个结构体变量保存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<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);}  其实VC编程研华板卡不是一件很困难的事情,在他提供的光盘里面有VC的实例,我们可以根据实例进行修改就可以完成了. 下载本文示例代码


工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现工业控制研华板卡的VC编程实现
阅读(550) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~