⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init.pas

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 PAS
字号:
unit Init;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, StdCtrls, ExtCtrls, Mask,
  Driver;

type
  TfrmInit = class(TForm)
    butExit: TButton;
    lstDevice: TListBox;
    labDevice: TLabel;
    labDataCount: TLabel;
    butConvert: TButton;
    editDataCounts: TMaskEdit;
    grpGain: TGroupBox;
    chkOverallGain: TCheckBox;
    lstOverallGain: TListBox;
    butGainList: TButton;
    grpScanChl: TGroupBox;
    labStartChl: TLabel;
    cmbStartChl: TComboBox;
    labChlCnt: TLabel;
    cmbChlCount: TComboBox;
    grpTrigSrc: TGroupBox;
    radExtTrig: TRadioButton;
    radInterTrig: TRadioButton;
    labSpeed: TLabel;
    editSpeed: TMaskEdit;
    labHz: TLabel;
    grpChecks: TGroupBox;
    chkCyclic: TCheckBox;
    chkEventEnable: TCheckBox;
    chkFloatData: TCheckBox;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure butExitClick(Sender: TObject);
    procedure cmbStartChlChange(Sender: TObject);
    procedure butConvertClick(Sender: TObject);
    procedure lstDeviceClick(Sender: TObject);
    procedure chkOverallGainClick(Sender: TObject);
    procedure butGainListClick(Sender: TObject);
    procedure lstOverallGainClick(Sender: TObject);
    procedure radExtTrigClick(Sender: TObject);
    procedure radInterTrigClick(Sender: TObject);

  private
    { Private declarations }
    ptDevFeatures : PT_DeviceGetFeatures;
    iMaxChl       : Smallint;

    procedure ThreadDone(Sender: TObject);

  public
    { Public declarations }
    iGainIndex : array [0..15] of Smallint;
    wGainCode  : array [0..15] of Word;
    hDevHandle : Longint;

    ptDmaBuffer    : PT_AllocateDMABuffer;
    lActualBufSize : Longint;
    lDmaBufPtr     : Longint;

    DeviceList     : array [0..MaxDev] of PT_DEVLIST;
    dfCurDevice    : DEVFEATURES;
    aicfgCurDevice : DEVCONFIG_AI;

    ptAiStart      : PT_FAIDmaScanStart;
    bThreadLoop    : Boolean;
  end;

var
  frmInit: TfrmInit;


implementation

uses Convert, DataShow, GainList, GetEvent;

{$R *.DFM}

{Internal used procedure}
procedure IsNumbericKey(var Key: Char);
begin
     {Check the pressed key}
     if ((Key < '0') or (Key > '9')) and (Key <> Chr(8))then
        Key := Chr(0);
end;

{*************************************************************
 * Function: Handle the error code. If the input error code > 0,
 *           it means some error apperent.  This function can
 *           show the error message to a message box and stop
 *           this application.
 * Input:    The error code.
 * return:   none
 ************************************************************* }
Function DoesErr(var lErrCode: LongInt): integer;
var
    szErrMsg   : string[100];
    pszErrMsg  : PChar;
    iRet       : integer;
begin
    {Check the pressed error code}
    If (lErrCode <> 0) Then
    begin
        pszErrMsg := @szErrMsg;
        DRV_GetErrorMessage(lErrCode, pszErrMsg);
        iRet := Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
        DoesErr := 1;
    end
    else
        DoesErr := 0;
end;


procedure TfrmInit.FormCreate(Sender: TObject);
var
    iNumOfDevices : Smallint;
    lErrCde : Longint;
    i : Smallint;

begin
    { Initialize controls}

    { Make Device list }
    lErrCde := DRV_DeviceGetNumOfList(iNumOfDevices);
    if( DoesErr(lErrCde) = 1 ) then
        Exit;;

    lErrCde := DRV_DeviceGetList(DeviceList[0], iNumOfDevices, iNumOfDevices);
    if DoesErr(lErrCde) = 1 then
        Exit;

    For i := 0 To (iNumOfDevices - 1) do
    begin
        lstDevice.Items.Add(DeviceList[i].szDeviceName);
    end;
    lstDevice.ItemIndex := 0;
    lstDeviceClick(Sender);

    {Give the initialize value}

end;

procedure TfrmInit.butExitClick(Sender: TObject);
begin
     Close;
end;

procedure TfrmInit.cmbStartChlChange(Sender: TObject);
var
    i, iIndex : Smallint;
    szTmp     : string;
begin
    {We suggest the stop channel# should large then stop channel}

    {1. Make channel count selection}
    iIndex := cmbChlCount.itemIndex;
    cmbChlCount.Clear;
    for i := 1 to iMaxChl - cmbStartChl.itemIndex do
    begin
        szTmp := IntToStr(i);
        cmbChlCount.items.Add(szTmp)
    end;

    {2. Selection current count}
    if iIndex >= cmbChlCount.items.Count then
        iIndex := cmbChlCount.items.Count - 1;

    cmbChlCount.itemIndex := iIndex;
end;

procedure TfrmInit.butConvertClick(Sender: TObject);
var
    i, j          : Smallint;
    lErrCde       : Longint;
    ptEnableEvent : PT_EnableEvent;
begin
    {Start converting action}

    {1. Open device }
    lErrCde := DRV_DeviceOpen( DeviceList[lstDevice.ItemIndex].dwDeviceNum,
                               hDevHandle);
    if( DoesErr(lErrCde) = 1 ) then
        Exit;

    {2. Allocate DMA buffer}
    with ptDmaBuffer do
    begin
        CyclicMode := Smallint(chkCyclic.Checked);
        RequestBufSize := StrToInt(editDataCounts.Text) * 2;
        ActualBufSize := @lActualBufSize;       {return acture allocated buffer size}
        buffer := @lDmaBufPtr;
    end;


    lErrCde := DRV_AllocateDMABuffer(hDevHandle, ptDmaBuffer);
    if( DoesErr(lErrCde) = 1 ) then
    begin
        DRV_DeviceClose(hDevHandle);
        exit;
    end;

    {3. Enable event}
    ptEnableEvent.EventType := ADS_EVT_INTERRUPT or
                               ADS_EVT_BUFCHANGE or
                               ADS_EVT_TERMINATED or
                               ADS_EVT_OVERRUN;
    ptEnableEvent.Enabled := Word(chkEventEnable.Checked);
    ptEnableEvent.Count   := 512;
    lErrCde := DRV_EnableEvent(hDevHandle,ptEnableEvent);
    if DoesErr(lErrCde) = 1  then
    begin
        DRV_FreeDMABuffer(hDevHandle, @lDmaBufPtr);
        DRV_DeviceClose(hDevHandle);
        Exit;
    end;

    {4. Set gain list}
    j := cmbStartChl.itemIndex;
    for i:=0 to cmbChlCount.itemIndex do
    begin
        wGainCode[i] := dfCurDevice.glGainList[iGainIndex[j]].usGainCde;
        j := j+1;
    end;

    {5. Start AI interrupt funciton}
    {5.1 Fill element of PT_FAIIntScanStart}
    with ptAiStart do
    begin
        TrigSrc     := Word(radExtTrig.Checked);
        SampleRate  := StrToInt(editSpeed.Text);
        NumChans    := cmbChlCount.itemIndex + 1;
        StartChan   := cmbStartChl.itemIndex;
        GainList    := @wGainCode[0];
        buffer      := Pointer(lDmaBufPtr);
        count       := StrToInt(editDataCounts.Text);
    end;

    {5.2 Call advantech API function DRV_FAIDmaScanStart}
    lErrCde := DRV_FAIDmaScanStart( hDevHandle, ptAiStart);
    if DoesErr(lErrCde) = 1 Then
    begin
        DRV_FreeDMABuffer(hDevHandle, @lDmaBufPtr);
        lErrCde := DRV_DeviceClose(hDevHandle);
        Exit;
    end;
    frmConverting.bConverting := True;

    {6. Enable Thread for getting message.}
    if chkEventEnable.Checked = True then
    begin
        bThreadLoop :=True;
        with TGetEvent.Create( DeviceList[lstDevice.ItemIndex].dwDeviceNum)
        do
            frmConverting.hThreadGetEvent := Handle;
    end;
    Sleep(0);

    {7. Enter converting form}
    frmConverting.bCyclic := chkCyclic.Checked;

    frmConverting.prgsDataCounts.Max := ptAiStart.count;
    frmConverting.prgsDataCounts.Position := 0;
    frmConverting.prgsDataCounts.Min := 0;

    frmInit.Enabled := False;
    frmConverting.Show;
end;

procedure TfrmInit.lstDeviceClick(Sender: TObject);
var
    lErrCde          : Longint;
    i                : integer;
    szTmp            : string;
    ptAIConfig       : PT_AIGetConfig;
begin
    butConvert.Enabled := FALSE;

    {1. Reset all controls because of changing different device}
    {Clear the previous selection list & combo boxes}
    cmbStartChl.Clear;            {clear start channel}
    cmbChlCount.Clear;            {Clear stop channel}
    lstOverallGain.Clear;                {Clear the voltage range list}

    {2. Open sellected device for reading its informations}
    lErrCde := DRV_DeviceOpen( DeviceList[lstDevice.ItemIndex].dwDeviceNum,
                               hDevHandle);
    if( DoesErr(lErrCde) = 1 ) then
        Exit;

    {3. Get device's features}
    ptDevFeatures.buffer := @dfCurDevice;
    ptDevFeatures.size := SizeOf(DEVFEATURES);
    lErrCde := DRV_DeviceGetFeatures(hDevHandle, ptDevFeatures);
    if( DoesErr(lErrCde) = 1 ) then
    begin
        DRV_DeviceClose(hDevHandle);
        Exit;
    end;

    {4. Get device's AI configuration}
    ptAIConfig.buffer := @aicfgCurDevice;
    ptAIConfig.size := SizeOf(DEVCONFIG_AI);
    lErrCde := DRV_AIGetConfig(hDevHandle, ptAIConfig);
    if ( DoesErr(lErrCde) = 1 ) then
    begin
        DRV_DeviceClose(hDevHandle);
        Exit;
    end;

    {5. Setting channel number combo box}
    If (aicfgCurDevice.ulChanConfig = 0) Then
        iMaxChl := dfCurDevice.usMaxAISiglChl
    Else
        iMaxChl := dfCurDevice.usMaxAIDiffChl;

    for i := 0 to (iMaxChl - 1) do
    begin
        szTmp := IntToStr(i);
        cmbStartChl.items.Add(szTmp);
        szTmp := IntToStr(i+1);
        cmbChlCount.items.Add(szTmp)
    end;
    cmbStartChl.ItemIndex := 0;
    cmbChlCount.ItemIndex := 0;

    {6. Setting gain code selection}
    if (dfCurDevice.usNumGain > 0) then
        For i := 0 To (dfCurDevice.usNumGain - 1) do
            lstOverallGain.Items.Add(dfCurDevice.glGainList[i].szGainStr);
    lstOverallGain.ItemIndex := 0;

    {7. Give the initialize Gain code list value}
    for i := 0 to iMaxChl - 1 do
        wGainCode[i] := dfCurDevice.glGainList[0].usGainCde;
    for i := 0 to 15 do
        iGainIndex[i] := 0;

    {8. Close the device}
    lErrCde := DRV_DeviceClose(hDevHandle);
    if DoesErr(lErrCde) = 1 then
        Exit;

    butConvert.Enabled := TRUE;
end;

procedure TfrmInit.chkOverallGainClick(Sender: TObject);
begin
    if chkOverallGain.Checked = True then
    begin
        lstOverallGain.Enabled := True;
        lstOverallGainClick(Sender);

        butGainList.Enabled := False
    end
    else
    begin
        lstOverallGain.Enabled := False;
        butGainList.Enabled := True
    end;
end;

procedure TfrmInit.butGainListClick(Sender: TObject);
var
    i,j  : Smallint;
begin
    {1. Clear every Gain code combo box}
    for i := 0 to 15 do
    begin
        frmGainList.cmbGain[i]^.Clear;
        frmGainList.cmbGain[i]^.Enabled := False
    end;

    {2. Give the available Gain code selection for individual channel}
    for i := 0 to (iMaxChl - 1) do
    begin
        if (dfCurDevice.usNumGain > 0) then
            For j := 0 To (dfCurDevice.usNumGain - 1) do
                frmGainList.cmbGain[i]^.Items.Add(dfCurDevice.glGainList[j].szGainStr);
        frmGainList.cmbGain[i]^.ItemIndex := iGainIndex[i];
    end;

    {Enable the start & stop channel's gain code selection}
    for i := cmbStartChl.ItemIndex to cmbStartChl.ItemIndex + cmbChlCount.ItemIndex do
        frmGainList.cmbGain[i]^.Enabled := TRUE;

    {Show the Gain code selection form}
    frmGainList.ShowModal;
end;

procedure TfrmInit.lstOverallGainClick(Sender: TObject);
var
    i : Smallint;
begin
    for i := 0 to 15 do
        iGainIndex[i] := lstOverallGain.ItemIndex;
end;

procedure TfrmInit.radExtTrigClick(Sender: TObject);
begin
    labSpeed.Enabled  := False;
    labHz.Enabled     := False;
    editSpeed.Enabled := False;
end;

procedure TfrmInit.radInterTrigClick(Sender: TObject);
begin
    labSpeed.Enabled  := True;
    labHz.Enabled     := True;
    editSpeed.Enabled := True;
end;

procedure TfrmInit.ThreadDone(Sender: TObject);
begin
    frmConverting.close;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -