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

📄 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,
  StdCtrls, Mask,
  Driver;

type
  TfrmInit = class(TForm)
    grpSelDev: TGroupBox;
    labDevName: TLabel;
    butChangDev: TButton;
    butExit: TButton;
    butConvert: TButton;
    grpGain: TGroupBox;
    chkOverallGain: TCheckBox;
    lstOverallGain: TListBox;
    butGainList: TButton;
    grpScanChl: TGroupBox;
    labStartChl: TLabel;
    labChlCnt: TLabel;
    cmbStartChl: TComboBox;
    cmbChlCount: TComboBox;
    grpTrigSrc: TGroupBox;
    labSpeed: TLabel;
    labHz: TLabel;
    radExtTrig: TRadioButton;
    radInterTrig: TRadioButton;
    editSpeed: TMaskEdit;
    grpChecks: TGroupBox;
    chkCyclic: TCheckBox;
    chkEventEnable: TCheckBox;
    chkFloatData: TCheckBox;
    labDataCount: TLabel;
    labFifo: TLabel;
    editDataCounts: TMaskEdit;
    editCountEvent: TMaskEdit;
    grpCondition: TGroupBox;
    lstOverallCond: TListBox;
    butConditions: TButton;
    chkOverallCond: TCheckBox;
    grpLevel: TGroupBox;
    chkOverallLevel: TCheckBox;
    butLevels: TButton;
    labHighLevel: TLabel;
    Label1: TLabel;
    grpAcqType: TGroupBox;
    cmbAcqType: TComboBox;
    editOverallLevelH: TMaskEdit;
    editOverallLevelL: TMaskEdit;

    Procedure DeleteBlank(var S : string);

    procedure butExitClick(Sender: TObject);
    procedure butChangDevClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure chkOverallGainClick(Sender: TObject);
    procedure chkOverallCondClick(Sender: TObject);
    procedure chkOverallLevelClick(Sender: TObject);
    procedure butGainListClick(Sender: TObject);
    procedure lstOverallGainClick(Sender: TObject);
    procedure butConditionsClick(Sender: TObject);
    procedure lstOverallCondClick(Sender: TObject);
    procedure editOverallLevelLChange(Sender: TObject);
    procedure editOverallLevelHChange(Sender: TObject);
    procedure butLevelsClick(Sender: TObject);
    procedure butConvertClick(Sender: TObject);

  private
    { Private declarations }
    lDeviceNumber : Longint;
    ptDevFeatures : PT_DeviceGetFeatures;
    dfCurDevice   : DEVFEATURES;  {Current selected device's features}
    iMaxChl       : Smallint;

    iGainIndex : array [0..15] of Smallint;
    wGainCode  : array [0..15] of Word;
    wCondLst   : array [0..15] of Word;
    fTrigLst   : array [0..15] of TRIGLEVEL;
    fLevelH    : array [0..15] of extended;    {All floating function using}
    fLevelL    : array [0..15] of extended;    {extended typ}
  public
    { Public declarations }
    ghDev       : Longint;      {Device handle}
    giConvCount : integer;
    glDmaBufPtr : Longint;      {pointer to DMA buffer}
    gptFWDC     : PT_FAIWatchdogConfig;
  end;

var
  frmInit: TfrmInit;

implementation

uses GainList, CondList, LevelLst, GetEvent, Convert, DataShow;

{$R *.DFM}
{*************************************************************
 * 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;

{*****************************************************************************
 * Fucntion : Delete the Blank space.
 * Input    : S, In/Out, String for modifing.
 * return   : none
 *****************************************************************************}
Procedure TfrmInit.DeleteBlank(var S : string);
var
  i : Integer;
begin
  i := Pos(' ', s);
  while (i > 0) do
  begin
    Delete(S, i, 1);
    i := Pos(' ', s);
  end;

end;

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

procedure TfrmInit.butChangDevClick(Sender: TObject);
var
  szDeviceName : array[0..100] of char;
  lErrCde      : Longint;

  ptAIConfig     : PT_AIGetConfig;
  aicfgCurDevice : DEVCONFIG_AI;

  i     : integer;
  szTmp : String;
begin
  {1. Select devcie from device list}
  lErrCde := DRV_SelectDevice( frmInit.Handle, False, lDeviceNumber, @szDeviceName[0]);
  if( DoesErr(lErrCde) = 1 ) then
    Exit;

  labDevName.Caption := szDeviceName;
  butConvert.Enabled := True;

  {2. Open sellected device for reading its informations}
  lErrCde := DRV_DeviceOpen( lDeviceNumber, ghDev);
  if( DoesErr(lErrCde) = 1 ) then
    Exit;

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

  {4. Get device's AI configuration}
  ptAIConfig.buffer := @aicfgCurDevice;
  ptAIConfig.size := SizeOf(DEVCONFIG_AI);
  lErrCde := DRV_AIGetConfig(ghDev, ptAIConfig);
  if ( DoesErr(lErrCde) = 1 ) then
  begin
    DRV_DeviceClose(ghDev);
    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}
  lstOverallGain.Items.Clear;
  if (dfCurDevice.usNumGain > 0) then
    For i := 0 To (dfCurDevice.usNumGain - 1) do
      lstOverallGain.Items.Add(dfCurDevice.glGainList[i].szGainStr);

  {7. Give the initialize Gain code, condition, trigger level,  list value}
  lstOverallGain.ItemIndex := 0;
  lstOverallCond.ItemIndex := 3;     {Use Great as default}

  {Give every channel's conditions a initial value}
  lstOverallGainClick(Sender);
  editOverallLevelHChange(Sender);
  editOverallLevelLChange(Sender);
  lstOverallCondClick(Sender);

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

procedure TfrmInit.FormCreate(Sender: TObject);
begin

  {Select a device for action}
  butChangDevClick(Sender);

  cmbAcqType.ItemIndex := 0;

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.chkOverallCondClick(Sender: TObject);
begin

  {If using overall active condition}
  if chkOverallCond.Checked = True then
  begin
    {Then
     Enable overall active condition selection}
    lstOverallCond.Enabled := True;
    lstOverallCondClick(Sender);

    {Stop to using individual condition selection}
    butConditions.Enabled := False
  end
  else
  begin
    {Else using individual conditions list}
    lstOverallCond.Enabled := False;
    butConditions.Enabled := True
  end;

end;

procedure TfrmInit.chkOverallLevelClick(Sender: TObject);
begin

  {If using overall level condition}
  if chkOverallLevel.Checked = True then
  begin
    editOverallLevelH.Enabled := True;
    editOverallLevelL.Enabled := True;
    editOverallLevelHChange(Sender);
    editOverallLevelLChange(Sender);

    butLevels.Enabled := False
  end
  else
  begin
    editOverallLevelH.Enabled := False;
    editOverallLevelL.Enabled := False;
    butLevels.Enabled := True
  end;

end;

procedure TfrmInit.butGainListClick(Sender: TObject);
var
    i,j  : Smallint;
    iStart, iStop : integer;
begin
  {1. Clear every Gain code combo box}
  frmGainList.ClearGainComb(0, 99);
  frmGainList.EnableGainComb(0, 99, False);

  {2. Give the available Gain code selection for individual channel}
  iStart := cmbStartChl.ItemIndex;
  iStop := cmbStartChl.ItemIndex + cmbChlCount.ItemIndex;

  for i := iStart to iStop do
  begin
    if (dfCurDevice.usNumGain > 0) then
      For j := 0 To (dfCurDevice.usNumGain - 1) do
        frmGainList.gcmbGain[i]^.Items.Add(dfCurDevice.glGainList[j].szGainStr);
    frmGainList.gcmbGain[i]^.ItemIndex := iGainIndex[i];
  end;

  {Enable the start & stop channel's gain code selection}
  frmGainList.EnableGainComb(iStart, iStop, True);

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

  {Read the changed gain code}
  if frmGainList.gbOkPushed = True then
    for i:= iStart to iStop do
      iGainIndex[i] := frmGainList.gcmbGain[i]^.itemIndex;
end;

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

procedure TfrmInit.butConditionsClick(Sender: TObject);
var
  i : Smallint;
  iStart, iStop : integer;
begin
  {1. Clear every Gain code combo box}
  frmConds.EnableSelection(0, 99, False);

  {2. Give the available Gain code selection for individual channel}
  iStart := cmbStartChl.ItemIndex;
  iStop := cmbStartChl.ItemIndex + cmbChlCount.ItemIndex;
  for i := iStart to iStop do
    frmConds.glstCond[i]^.ItemIndex := wCondLst[i];

  {Enable the start & stop channel's condition list}
  frmConds.Enableselection(iStart, iStop, True);

  {Show the condition selection form}
  frmConds.ShowModal;

  {Read the conditions code}
  if frmConds.gbOkPushed = True then
    for i:= iStart to iStop do
      wCondLst[i] := frmConds.glstCond[i]^.itemIndex;

end;

procedure TfrmInit.lstOverallCondClick(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to iMaxChl -1 do
    wCondLst[i] := lstOverallCond.ItemIndex;
end;

procedure TfrmInit.editOverallLevelHChange(Sender: TObject);
var
  i     : integer;
  szTmp : string;
begin
  szTmp := editOverAllLevelH.Text;
  DeleteBlank(szTmp);
  fLevelH[0] := StrToFloat(szTmp);
  for i := 1 to iMaxChl-1 do
    fLevelH[i] := fLevelH[0];
end;

procedure TfrmInit.editOverallLevelLChange(Sender: TObject);
var
  i     : integer;
  szTmp : string;
begin
  szTmp := editOverAllLevelL.Text;
  DeleteBlank(szTmp);
  fLevelL[0] := StrToFloat(szTmp);
  for i := 1 to iMaxChl-1 do
    fLevelL[i] := fLevelL[0];
end;

procedure TfrmInit.butLevelsClick(Sender: TObject);
var
  i : Smallint;
  iStart, iStop : integer;
begin
  {1. Clear every Gain code combo box}
  frmLevels.EnableSelection(0, 99, False);

  {2. Give the available Gain code selection for individual channel}
  iStart := cmbStartChl.ItemIndex;
  iStop := cmbStartChl.ItemIndex + cmbChlCount.ItemIndex;
  for i := iStart to iStop do
  begin
    frmLevels.gfHigh[i] := fLevelH[i];
    frmLevels.gfLow[i] := fLevelL[i];
  end;

  {Enable the start & stop channel's condition list}
  frmLevels.Enableselection(iStart, iStop, True);

  {Show the condition selection form}
  frmLevels.ShowModal;

  {Read the conditions code}
  if frmLevels.gbOkPushed = True then
    for i:= iStart to iStop do
    begin
      fLevelH[i] := frmLevels.gfHigh[i];
      fLevelL[i] := frmLevels.gfLow[i];
    end;
end;

procedure TfrmInit.butConvertClick(Sender: TObject);
var
  lErrCde    : Longint;
  i, j       : integer;

  ptDmaBuffer    : PT_AllocateDMABuffer;
  lActualBufSize : Longint;

  ptEnableEvent  : PT_EnableEvent;
  ptDmaStart     : PT_FAIDmaWatchdogStart;
begin
  {Device Open}
  lErrCde := DRV_DeviceOpen( lDeviceNumber, ghDev);
  if( DoesErr(lErrCde) = 1 ) then
    Exit;

  {Allocate DMA bnuffer for DMA transfering}
  giConvCount := StrToInt(editDataCounts.Text);
  with ptDmaBuffer do
  begin
    CyclicMode := Smallint(chkCyclic.Checked);
    RequestBufSize := giConvCount * 2;
    ActualBufSize := @lActualBufSize;       {return acture allocated buffer size}
    buffer := @glDmaBufPtr;
  end;
  lErrCde := DRV_AllocateDMABuffer(ghDev, ptDmaBuffer);
  if( DoesErr(lErrCde) = 1 ) then
  begin
    DRV_DeviceClose(ghDev);
    exit;
  end;

  {Configure envent feature}
  ptEnableEvent.EventType := ADS_EVT_INTERRUPT  or
                             ADS_EVT_BUFCHANGE  or
                             ADS_EVT_TERMINATED or
                             ADS_EVT_OVERRUN    or
                             ADS_EVT_WATCHDOG;
  ptEnableEvent.Enabled := Word(chkEventEnable.Checked);
  ptEnableEvent.Count   := StrToInt(editCountEvent.Text);
  lErrCde := DRV_EnableEvent(ghDev,ptEnableEvent);
  if DoesErr(lErrCde) = 1  then
  begin
    DRV_FreeDMABuffer(ghDev, @glDmaBufPtr);
    DRV_DeviceClose(ghDev);
    Exit;
  end;

  {Get Gain, Condition & Level setting}
  j := cmbStartChl.itemIndex;
  for i:=0 to cmbChlCount.itemIndex do
  begin
    {Make Gain code list}
    wGainCode[i] := dfCurDevice.glGainList[iGainIndex[j]].usGainCde;

    {Make Condition list}
    wCondLst[i] := wCondLst[j];

    {Make Level list}
    fTrigLst[i].fLow  := fLevell[j];
    fTrigLst[i].fHigh := fLevelH[j];

    j := j+1;
  end;

  {Configure for watchdog DMA transfering}
  with gptFWDC do
  begin
    TrigMode  := cmbAcqType.ItemIndex + 1;
    NumChans  := cmbChlCount.itemIndex + 1;
    StartChan := cmbStartChl.itemIndex;
    GainList  := @wGainCode[0];
    CondList  := @wCondLst[0];
    LevelList := @fTrigLst[0];
  End;
  lErrCde := DRV_FAIWatchdogConfig( ghDev, gptFWDC );
  if DoesErr(lErrCde) = 1 Then
  begin
    DRV_FreeDMABuffer(ghDev, @glDmaBufPtr);
    lErrCde := DRV_DeviceClose(ghDev);
    Exit;
  end;

  {Start Watch Dog DMA transfer}
  with ptDmaStart do
  begin
    TrigSrc    := Word(radExtTrig.Checked);
    SampleRate := StrToInt(editSpeed.Text);
    BufferA    := Pointer(glDmaBufPtr);
    BufferB    := Pointer(glDmaBufPtr);
    count      := giConvCount;
  end;
  lErrCde := DRV_FAIDmaWatchdogStart(ghDev, ptDmaStart);
  if DoesErr(lErrCde) = 1 Then
  begin
    DRV_FreeDMABuffer(ghDev, @glDmaBufPtr);
    lErrCde := DRV_DeviceClose(ghDev);
    Exit;
  end;

  {Enable event transfer}
  if chkEventEnable.Checked = True then
  begin
    with TGetEvent.Create( lDeviceNumber,
                           600000,  {We can wait 600 sec without any action}
                           chkCyclic.Checked) do
      frmConverting.ghThreadGetEvent := Handle;
  end;
  Sleep(0);

  {Enter converting form.}
  frmConverting.gbCyclic := chkCyclic.Checked;
  frmConverting.memEvent.Lines.Clear;

  frmConverting.prgsDataCounts.Max := giConvCount;
  frmConverting.prgsDataCounts.Position := 0;
  frmConverting.prgsDataCounts.Min := 0;    

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

end.

⌨️ 快捷键说明

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