在QT项目中添加对研华数采模块的支持

一、正确安装研华数采模块管理程序Advantech Device Manager,并安装相应模块驱动。

  

二、将C:\Program Files\Advantech\Adsapi路径(默认安装位置)下的IncludeLib文件夹复制到QT项目文件夹下。

  

三、在代码中添加研华数采模块函数库头文件:

#include "Include\Driver.h"

并添加系统相关头文件:

#include <windef.h>

#include <stdio.h>

 

四、在pro文件中添加研华数采模块函数库文件。

HEADERS += Include/Driver.h

LIBS += Lib/Adsapi32.lib

 

五、基本代码:

1、打开驱动

    DWORD  dwErrCde = DRV_DeviceOpen(0, &lDriverHandle);

    if (dwErrCde != SUCCESS) {

        qDebug() << "dwErrCde = " << dwErrCde;

        exit(1);

    }

2、读取IO输入数字量

    PT_DioReadPortWord m_ptDioReadPortWord;

    USHORT m_ValidMask = 0;

    USHORT m_DiValue = 0;

    m_ptDioReadPortWord.port = 0;

    m_ptDioReadPortWord.ValidChannelMask = ( USHORT far * )&m_ValidMask;

    m_ptDioReadPortWord.value = ( USHORT far * )&m_DiValue;

    LRESULT m_ErrCde = DRV_DioReadPortWord( lDriverHandle, (LPT_DioReadPortWord)&m_ptDioReadPortWord);

    if (m_ErrCde != SUCCESS) {

        qDebug() << "m_ErrCde = " << m_ErrCde;

        exit(1);

    }

 

3、输出IO数字量

        PT_DioWriteBit ptDioWriteBit;

        ptDioWriteBit.port  = 0;  // output port: 0

        ptDioWriteBit.bit   = 0;  // output channel: 0

        ptDioWriteBit.state = !(tmp_bit & current_dataOut); // output state

        ///*

        DWORD dwErrCde = DRV_DioWriteBit(lDriverHandle, (LPT_DioWriteBit)&ptDioWriteBit);

        if (dwErrCde != SUCCESS) {

            qDebug() << "dwErrCde = " << dwErrCde;

            exit(1);

        } //*/

4、关闭驱动

    DWORD dwErrCde = DRV_DeviceClose(&lDriverHandle);

    if (dwErrCde != SUCCESS) {

        qDebug() << "dwErrCde = " << dwErrCde;

        exit(1);

}

5、更多使用方法请参照使用手册及相关例程。

你可能感兴趣的:(qt)