在QT專案中新增對研華數採模組的支援
一、正確安裝研華數採模組管理程式Advantech Device Manager,並安裝相應模組驅動。
二、將C:\Program Files\Advantech\Adsapi路徑(預設安裝位置)下的Include、Lib資料夾複製到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、更多使用方法請參照使用手冊及相關例程。