{//----------------------------------------------------------------------------------------- }
{//File Name:UniDAQ.pas                                                                      }
{//Update date:2012/12/20 by Dan Huang                                                       }
{//Support Language: Borland Delphi                                                          }
{//UniDAQ SDK User Manual: http://ftp.icpdas.com/pub/cd/iocard/pci/napdos/pci/unidaq/maunal/ }
{//----------------------------------------------------------------------------------------- }

unit UNIDAQ;

interface
uses
  Windows;

  type PSingle=^Single;
  type PWord=^Word;
  type PDWord=^LongInt;

const
  MAX_BOARD_NUMBER = 32;
  MAX_EVENT_NUMBER = 20;
  MAX_AO_CHANNEL = 32;

{///ModelNumber }
const
  PIOD56 = $800140;
  PIOD48 = $800130;
  PIOD64 = $800120;
  PIOD96 = $800110;
  PIOD144 = $800100;
  PIOD168 = $800150;
  PIODA = $800400;
  PIO821 = $800310;
  PIO827 = $FF0000;
  PISOP16R16U = $1800F;
  PISOC64 = $800800;
  PISOP64 = $800810;
  PISOA64 = $800850;
  PISOP32C32 = $800820;
  PISOP32A32 = $800870;
  PISOP8R8 = $800830;
  PISO730 = $800840;
  PISO730A = $800880;
  PISO725 = $8008F;
  PISODA2 = $800B00;
  PISO813 = $800A00;
  PCITMC12 = $DF2962;
  PCIM512 = $DE9562;
  PCIM256 = $DE92A6;
  PCIM128 = $DE9178;
  PCIFC16 = $B13017;

  PCI822  = $DE3823;
  PCI826  = $DE3827;
  PCI827  = $DE3828;
  PCID64  = $DE3513;
  PCI100x = $341002;
  PCI1202 = $345672;
  PCI1602 = $345676;
  PCI180x = $345678;
  PCIP8R8 = $D6102B;
  PCIP16R16 = $D61E39;


const
  MAX_CFGCODE_NUMBER = 23;{//User definition the total number of the AI ConfigCodeTotal}

{///User Analog Input Range Config Code }
const
  IXUD_BI_10V = 0; {//Bipolar +/- 10V}
  IXUD_BI_5V = 1; {//Bipolar +/- 5V}
  IXUD_BI_2V5 = 2; {//Bipolar +/- 2.5V}
  IXUD_BI_1V25 = 3; {//Bipolar +/- 1.25V}
  IXUD_BI_0V625 = 4; {//Bipolar +/- 0.625V}
  IXUD_BI_0V3125 = 5; {//Bipolar +/- 0.3125V}
  IXUD_BI_0V5 = 6; {//Bipolar +/- 0.5V}
  IXUD_BI_0V05 = 7; {//Bipolar +/- 0.05V}
  IXUD_BI_0V005 = 8; {//Bipolar +/- 0.005V}
  IXUD_BI_1V = 9; {//Bipolar +/- 1V}
  IXUD_BI_0V1 = 10; {//Bipolar +/- 0.1V}
  IXUD_BI_0V01 = 11; {//Bipolar +/- 0.01V}
  IXUD_BI_0V001 = 12; {//Bipolar +/- 0.001V}
  IXUD_UNI_20V = 13; {//Unipolar 0 ~ 20V}
  IXUD_UNI_10V = 14; {//Unipolar 0 ~ 10V}
  IXUD_UNI_5V = 15; {//Unipolar 0 ~ 5V}
  IXUD_UNI_2V5 = 16; {//Unipolar 0 ~ 2.5V}
  IXUD_UNI_1V25 = 17; {//Unipolar 0 ~ 1.25V}
  IXUD_UNI_0V625 = 18; {//Unipolar 0 ~ 0.625V}
  IXUD_UNI_1V = 19; {//Unipolar 0 ~ 1V}
  IXUD_UNI_0V1 = 20; {//Unipolar 0 ~ 0.1V}
  IXUD_UNI_0V01 = 21; {//Unipolar 0 ~ 0.01V}
  IXUD_UNI_0V001 = 22; {//Unipolar 0 ~ 0.001V}

const
  MAX_AOCFGCODE_NUMBER =6;{//User definition the total number of the AO ConfigCodeTotal}

{///User Analog Output Range Config Code }
const
  {//User AO Config Code for Voltage}
  IXUD_AO_UNI_5V = 0; {//0 ~ 5V}
  IXUD_AO_BI_5V = 1; {//+/- 5V}
  IXUD_AO_UNI_10V = 2; {//0 ~ 10V}
  IXUD_AO_BI_10V = 3; {//+/- 10V}
  IXUD_AO_UNI_20V = 4; {//0 ~ 20V}
  IXUD_AO_BI_20V = 5; {//+/- 20V}
  
  {//User AO Config Code for Current}
  IXUD_AO_I_0_20_MA	= 16;	{//0 ~ 20mA}
  IXUD_AO_I_4_20_MA	= 17;	{//4 ~ 20mA}

{///}  
const
  {//Interrupt Setting}
  IXUD_HARDWARE_INT  =1; {// Hardware Interrupt}
  IXUD_APC_READY_INT =2; {// APC Ready Interrupt}
  IXUD_ACTIVE_LOW    =4; {// Active Low Trigger}
  IXUD_ACTIVE_HIGH   =8; {// Active High Trigger}
  

{/// Return code }
const
   Ixud_NoErr  =                     0 ; {//Correct}
   Ixud_OpenDriverErr =              1 ; {//Open driver error}
   Ixud_PnPDriverErr	=			 2 ; {//Plug & Play error}
   Ixud_DriverNoOpen  =              3 ; {//The driver was not open}
   Ixud_GetDriverVersionErr =        4 ; {//Recieve driver version error}
   Ixud_ExceedBoardNumber	=		 5 ; {//Board number error}
   Ixud_FindBoardErr	=			 6 ; {//No board found}
   Ixud_BoardMappingErr	=		 7 ; {//Board Mapping error}
   Ixud_DIOModesErr	=			 8 ; {//Digital input/output mode setting error}
   Ixud_InvalidAddress	=			 9 ; {//Invalid address}
   Ixud_InvalidSize	=			10 ; {//Invalid size}
  	Ixud_InvalidPortNumber	=		11 ; {//Invalid port number}
   Ixud_UnSupportedModel	=		12 ; {//This board model is not supported}
   Ixud_UnSupportedFun	=			13 ; {//This function is not supported}
   Ixud_InvalidChannelNumber	=	14 ; {//Invalid channel number}
   Ixud_InvalidValue	=			15 ; {//Invalid value}
   Ixud_InvalidMode		=		16 ; {//Invalid mode}
   Ixud_GetAIStatusTimeOut	=		17 ; {//A timeout occurred while receiving the status of the analog input}
   Ixud_TimeOutErr	=				18 ; {//Timeout error}
   Ixud_CfgCodeIndexErr	=		19 ; {//A compatible configuration code table index could not be found}
   Ixud_ADCCTLTimeoutErr	=		20 ; {//ADC controller a timeout error}
   Ixud_FindPCIIndexErr		=	21 ; {//A compatible PCI table index value could not be found}
   Ixud_InvalidSetting	=			22 ; {//Invalid setting value}
   Ixud_AllocateMemErr	 =   		23 ; {//Error while allocating the memory space}
   Ixud_InstallEventErr	=		24 ; {//Error while installing the interrupt event}
   Ixud_InstallIrqErr		=		25 ; {//Error while installing the interrupt IRQ}
   Ixud_RemoveIrqErr		=		26 ; {//Error while removing the interrupt IRQ}
   Ixud_ClearIntCountErr =		27 ; {//Error while the clear interrupt count}
   Ixud_GetSysBufferErr	=		28 ; {//Error while retrieving the system buffer}
   Ixud_CreateEventErr	=			29 ; {//Error while create the event}
   Ixud_UnSupportedResolution	=	30 ; {//Resolution not supported}
   Ixud_CreateThreadErr	=		31 ; {//Error while create the thread}
   Ixud_ThreadTimeOutErr	=		32 ; {//Thread timeout error}
   Ixud_FIFOOverFlowErr	=		33 ; {//FIFO overflow error}
   Ixud_FIFOTimeOutErr	=			34 ; {//FIFO timeout error}
   Ixud_GetIntInstStatus	=		35 ; {//Retrieves the status of the interrupt installation}
   Ixud_GetBufStatus		=		36 ; {//Retrieves the status of the system buffer}
   Ixud_SetBufCountErr	=		    37 ; {//Error while setting the buffer count}
   Ixud_SetBufInfoErr    =          38 ; {//Error while setting the buffer data}
   Ixud_FindCardIDErr		=		39 ; {//Card ID code could not be found}
   Ixud_EventThreadErr	=			40 ; {//Event Thread error}
   Ixud_AutoCreateEventErr	=		41 ; {//Error while automatically creating an event}
   Ixud_RegThreadErr	=			42 ; {//Register Thread error}
   Ixud_SearchEventErr	=			43 ; {//Search Event error}
   Ixud_FifoResetErr	=			44 ; {//Error while resetting the FIFO}
   Ixud_InvalidBlock	=			45 ; {//Invalid EEPROM block}
   Ixud_InvalidAddr		=		46 ; {//Invalid EEPROM address}
   Ixud_AcqireSpinLock	=			47 ; {//Error while acquiring spin lock}
   Ixud_ReleaseSpinLock	=		48 ; {//Error while releasing spin lock}
   Ixud_SetControlErr		=		49 ; {//Analog input setting error}
   Ixud_InvalidChannels	=		50 ; {//Invalid channel number}
   Ixud_SearchCardErr		=		51 ; {//Invalid model number}
   Ixud_SetMapAddressErr	=		52 ; {//Error while setting the mapping address}
   Ixud_ReleaseMapAddressErr	=	53 ; {//Error while releasing the mapping address}
   Ixud_InvalidOffset		=		54 ; {//Invalid memory offset}
   Ixud_ShareHandleErr	=			55 ; {//Open the share memory fail}
   Ixud_InvalidDataCount	=		56 ; {//Invalid data count}
   Ixud_WriteEEPErr		=		57 ; {//Error while writing the EEPROM}
   Ixud_CardIOErr    =              58 ; {//CardIO error}
   Ixud_IOErr        =              59 ; {//MemoryIO error}

type
  _IXUD_DEVICE_INFO_ = record
    dwSize: LongInt;
    wVendorID: Word;
    wDeviceID: Word;
    wSubVendorID: Word;
    wSubDeviceID: Word;
    dwBAR: Array[0..5] of LongInt;
    BusNo: UCHAR;
    DevNo: UCHAR;
    IRQ: UCHAR;
    Aux: UCHAR;
    dwReserved1: Array[0..5] of LongInt;
  end {_IXUD_DEVICE_INFO_};
  IXUD_DEVICE_INFO = _IXUD_DEVICE_INFO_;
  PIXUD_DEVICE_INFO = ^_IXUD_DEVICE_INFO_;

{///extern IXUD_DEVICE_INFO DevInfo[MAX_BOARD_NUMBER]; }

type
  _IXUD_CARD_INFO_ = record
    dwSize: LongInt;
    dwModelNo: LongInt;
{///CardID is update when calling the function each time. }
    CardID: UCHAR;
    wSingleEnded: UCHAR;
    wReserved: Word;
    wAIChannels: Word;
    wAOChannels: Word;
    wDIPorts: Word;
    wDOPorts: Word;
    wDIOPorts: Word;
    wDIOPortWidth: Word;
    wCounterChannels: Word;
    wMemorySize: Word;
    dwReserved1: Array[0..6-1] of LongInt;
  end {_IXUD_CARD_INFO_};
  IXUD_CARD_INFO = _IXUD_CARD_INFO_;
  PIXUD_CARD_INFO = ^_IXUD_CARD_INFO_;
{///extern IXUD_CARD_INFO CardInfo[MAX_BOARD_NUMBER]; }

{/// Driver functions }
function
  Ixud_GetDllVersion(var wDLLVer: PLongInt): WORD ;stdcall;
function
  Ixud_DriverInit(var wTotalBoards: Word): WORD ;stdcall; 
function
  Ixud_DriverClose: WORD ;stdcall; 
function
  Ixud_GetBoardNoByCardID(var wBoardNo: Word; 
                                    dwModelNumber: LongInt; 
                                    wCardID: Word): WORD ;stdcall; 
function
  Ixud_GetCardInfo(wBoardNo: Word;
                             var sDevInfo: IXUD_DEVICE_INFO;
                             var sCardInfo: IXUD_CARD_INFO; 
                             szModelName: PChar): WORD ;stdcall; 
function
  Ixud_ReadPort(dwAddress: LongInt; 
                          wSize: Word; 
                          var dwVal: LongInt): WORD ;stdcall;
function
  Ixud_WritePort(dwAddress: LongInt; 
                           wSize: Word; 
                           dwVal: LongInt): WORD ;stdcall; 
{// Digital I/O functions}
function
  Ixud_SetDIOModes32(wBoardNo: Word; 
                               dwDioMode: LongInt): WORD ;stdcall; 
function
  Ixud_SetDIOMode(wBoardNo: Word; 
                            wPortNo: Word; 
                            wDioMode: Word): WORD ;stdcall; 
function
  Ixud_ReadDI(wBoardNo: Word; 
                        wPortNo: Word; 
                        var dwDIVal: LongInt): WORD ;stdcall; 
function
  Ixud_WriteDO(wBoardNo: Word; 
                         wPortNo: Word; 
                         dwDOVal: LongInt): WORD ;stdcall;
{//Callback functions}
function
  Ixud_SetEventCallback(wBoardNo: Word;
                                  wEventType: Word; 
                                  wInterruptSource: Word; 
                                  var hEvent: THandle;
                                  CallbackFun: pointer;
                                  dwCallBackParameter: LongInt): WORD ;stdcall; 
function
  Ixud_RemoveEventCallback(wBoardNo: Word; 
                                     wInterruptSource: Word): WORD ;stdcall; 
{//Interrupt functions}
function
  Ixud_InstallIrq(wBoardNo: Word; 
                            dwInterruptMask: LongInt): WORD ;stdcall; 
function
  Ixud_RemoveIrq(wBoardNo: Word): WORD ;stdcall; 

{//Timer/Counter functions}
function
  Ixud_ReadCounter(wBoardNo: Word; 
                             wChannel: Word; 
                             var dwValue: LongInt): WORD ;stdcall; 
function
  Ixud_SetCounter(wBoardNo: Word; 
                            wChannel: Word; 
                            wMode: Word; 
                            dwValue: LongInt): WORD ;stdcall; 
function
  Ixud_DisableCounter(wBoardNo: Word;
                                wChannel: Word): WORD ;stdcall;

function
  Ixud_ReadFrequency(wBoardNo: Word;
                               wChannel: Word;
                               var fFrequency: Single;
                               dwTimeOutMs: LongInt;
                               var wStatus: Word ): WORD ;stdcall;
function
  Ixud_SetFCChannelMode(wBoardNo: Word;
                                  wChannel: Word;
                                  wMode: Word;
                                  wDelayMs: Word ): WORD ;stdcall;

{//Memory I/O functions}
function
  Ixud_ReadMemory(wBoardNo: Word;
                            dwOffset: LongInt;
                            Size: Word;
                            var dwValue: LongInt): WORD ;stdcall;
function
  Ixud_WriteMemory(wBoardNo: Word;
                             dwOffset: LongInt; 
                             Size: Word; 
                             dwValue: LongInt): WORD ;stdcall; 
{//Analog Input functions}
function
  Ixud_ReadAI(wBoardNo: Word; 
                        wChannel: Word; 
                        wConfig: Word; 
                        var fValue: Single): WORD ;stdcall; 
function
  Ixud_ReadAIH(wBoardNo: Word; 
                         wChannel: Word; 
                         wConfig: Word; 
                         var dwValue: LongInt): WORD ;stdcall; 
function
  Ixud_PollingAI(wBoardNo: Word; 
                           wChannel: Word; 
                           wConfig: Word; 
                           dwDataCount: LongInt; 
                           fValue: PSingle): WORD ;stdcall;
function
  Ixud_PollingAIH(wBoardNo: Word; 
                            wChannel: Word; 
                            wConfig: Word; 
                            dwDataCount: LongInt; 
                            dwValue: PDWord): WORD ;stdcall;
function
  Ixud_PollingAIScan(wBoardNo: Word; 
                               wChannels: Word; 
                               wChannelList: PWord;
                               wConfigList: PWord;
                               dwDataCountPerChannel: LongInt;
                               fValue:PSingle): WORD ;stdcall;
function
  Ixud_PollingAIScanH(wBoardNo: Word; 
                                wChannels: Word; 
                                wChannelList: PWord;
                                wConfigList: PWord;
                                dwDataCountPerChannel: LongInt; 
                                dwValue: PDWord): WORD ;stdcall;

function
  Ixud_ConfigAI(wBoardNo: Word; 
                          wFIFOSizeKB: Word; 
                          BufferSizeCount: LongInt; 
                          wCardType: Word; 
                          wDelaySettlingTime: Word): WORD ;stdcall; 
function
  Ixud_ClearAIBuffer(wBoardNo: Word): WORD ;stdcall; 
function
  Ixud_GetBufferStatus(wBoardNo: Word; 
                                 var wBufferStatus: Word; 
                                 var dwDataCount: LongInt): WORD ;stdcall; 

function
  Ixud_StartAI(wBoardNo: Word; 
                         wChannel: Word; 
                         wConfig: Word; 
                         fSamplingRate: Single; 
                         dwDataCount: LongInt): WORD ;stdcall; 
function
  Ixud_StartAIScan(wBoardNo: Word; 
                             wChannels: Word;
                             wChannelList: PWord;
                             wConfigList: PWord;
                             fSamplingRate: Single; 
                             dwDataCountPerChannel: LongInt): WORD ;stdcall;
function
  Ixud_GetAIBuffer(wBoardNo: Word; 
                             dwDataCount: LongInt; 
                             fValue: PSingle): WORD ;stdcall;
function
  Ixud_GetAIBufferH(wBoardNo: Word;
                              dwDataCount: LongInt;
                              hValue: PDWord): WORD ;stdcall;
function
  Ixud_StopAI(wBoardNo: Word): WORD ;stdcall; 

{//Analog Output functions}
function
  Ixud_ConfigAO(wBoardNo: Word; 
                          wChannel: Word; 
                          wCfgCode: Word): WORD ;stdcall; 
function
  Ixud_WriteAOVoltage(wBoardNo: Word; 
                                wChannel: Word; 
                                fValue: Single): WORD ;stdcall; 
function
  Ixud_WriteAOVoltageH(wBoardNo: Word; 
                                 wChannel: Word; 
                                 hValue: LongInt): WORD ;stdcall; 
function
  Ixud_WriteAOCurrent(wBoardNo: Word; 
                                wChannel: Word; 
                                fValue: Single): WORD ;stdcall; 
function
  Ixud_WriteAOCurrentH(wBoardNo: Word; 
                                 wChannel: Word; 
                                 hValue: LongInt): WORD ;stdcall; 

implementation

    function Ixud_GetDllVersion;   external 'UniDAQ.DLL'   name 'Ixud_GetDllVersion'

    function Ixud_DriverInit;   external 'UniDAQ.DLL'   name 'Ixud_DriverInit'
 
    function Ixud_DriverClose;   external 'UniDAQ.DLL'   name 'Ixud_DriverClose'
  
    function Ixud_GetBoardNoByCardID;   external 'UniDAQ.DLL'   name 'Ixud_GetBoardNoByCardID'
  
    function Ixud_GetCardInfo;   external 'UniDAQ.DLL'   name 'Ixud_GetCardInfo'
 
    function Ixud_ReadPort;   external 'UniDAQ.DLL'   name 'Ixud_ReadPort'
 
    function Ixud_WritePort;   external 'UniDAQ.DLL'   name 'Ixud_WritePort'
  
    function Ixud_SetDIOModes32;   external 'UniDAQ.DLL'   name 'Ixud_SetDIOModes32'
  
    function Ixud_SetDIOMode;   external 'UniDAQ.DLL'   name 'Ixud_SetDIOMode'
 
    function Ixud_ReadDI;   external 'UniDAQ.DLL'   name 'Ixud_ReadDI'
 
    function Ixud_WriteDO;   external 'UniDAQ.DLL'   name 'Ixud_WriteDO'
 
    function Ixud_SetEventCallback;   external 'UniDAQ.DLL'   name 'Ixud_SetEventCallback'
  
    function Ixud_RemoveEventCallback;   external 'UniDAQ.DLL'   name 'Ixud_RemoveEventCallback'
  
    function Ixud_InstallIrq;   external 'UniDAQ.DLL'   name 'Ixud_InstallIrq'

    function Ixud_RemoveIrq;   external 'UniDAQ.DLL'   name 'Ixud_RemoveIrq'
  
    function Ixud_ReadCounter;   external 'UniDAQ.DLL'   name 'Ixud_ReadCounter'
  
    function Ixud_SetCounter;   external 'UniDAQ.DLL'   name 'Ixud_SetCounter'
 
    function Ixud_DisableCounter;   external 'UniDAQ.DLL'   name 'Ixud_DisableCounter'

    function Ixud_ReadFrequency;   external 'UniDAQ.DLL'   name 'Ixud_ReadFrequency'

    function Ixud_SetFCChannelMode;   external 'UniDAQ.DLL'   name 'Ixud_SetFCChannelMode'

    function Ixud_ReadMemory;   external 'UniDAQ.DLL'   name 'Ixud_ReadMemory'
  
    function Ixud_WriteMemory;   external 'UniDAQ.DLL'   name 'Ixud_WriteMemory'
  
    function Ixud_ReadAI;   external 'UniDAQ.DLL'   name 'Ixud_ReadAI'
 
    function Ixud_ReadAIH;   external 'UniDAQ.DLL'   name 'Ixud_ReadAIH'
 
    function Ixud_PollingAI;   external 'UniDAQ.DLL'   name 'Ixud_PollingAI'
  
    function Ixud_PollingAIH;   external 'UniDAQ.DLL'   name 'Ixud_PollingAIH'
  
    function Ixud_PollingAIScan;   external 'UniDAQ.DLL'   name 'Ixud_PollingAIScan'
  
    function Ixud_PollingAIScanH;   external 'UniDAQ.DLL'   name 'Ixud_PollingAIScanH'
  
    function Ixud_ConfigAI;   external 'UniDAQ.DLL'   name 'Ixud_ConfigAI'
  
    function Ixud_ClearAIBuffer;   external 'UniDAQ.DLL'   name 'Ixud_ClearAIBuffer'
  
    function Ixud_GetBufferStatus;   external 'UniDAQ.DLL'   name 'Ixud_GetBufferStatus'
 
    function Ixud_StartAI;   external 'UniDAQ.DLL'   name 'Ixud_StartAI'
  
    function Ixud_StartAIScan;   external 'UniDAQ.DLL'   name 'Ixud_StartAIScan'
  
    function Ixud_GetAIBuffer;   external 'UniDAQ.DLL'   name 'Ixud_GetAIBuffer'
  
    function Ixud_GetAIBufferH;   external 'UniDAQ.DLL'   name 'Ixud_GetAIBufferH'
  
    function Ixud_StopAI;   external 'UniDAQ.DLL'   name 'Ixud_StopAI'
  
    function Ixud_ConfigAO;   external 'UniDAQ.DLL'   name 'Ixud_ConfigAO'
 
    function Ixud_WriteAOVoltage;   external 'UniDAQ.DLL'   name 'Ixud_WriteAOVoltage'
 
    function Ixud_WriteAOVoltageH;   external 'UniDAQ.DLL'   name 'Ixud_WriteAOVoltageH'
 
    function Ixud_WriteAOCurrent;   external 'UniDAQ.DLL'   name 'Ixud_WriteAOCurrent'
  
    function Ixud_WriteAOCurrentH;   external 'UniDAQ.DLL'   name 'Ixud_WriteAOCurrentH'
 
   
end.
