欧冠

工业控制研华板卡的VC编程实现

2019-09-12 09:30:30来源:励志吧0次阅读

在论坛中经常有人提出关于研华工业用控制板卡的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<<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);

}

其实VC编程研华板卡不是一件很困难的事情,在他提供的光盘里面有VC的实例,我们可以根据实例进行修改就可以完成了.

查看本文来源

宝宝咳嗽怎么治
血栓是什么病严重吗
哪些原因会引起腹胀
两个月宝宝咳嗽怎么办
分享到: