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

📄 mainform.pas

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

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Driver, Global;

type
  TfrmMain = class(TForm)
    Label2: TLabel;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    Label6: TLabel;
    cmbChannel: TComboBox;
    butStart: TButton;
    lstDevice: TListBox;
    butExit: TButton;
    GroupBox2: TGroupBox;
    cmbGain: TComboBox;
    radOverall: TRadioButton;
    radGainList: TRadioButton;
    butGainList: TButton;
    lstModule: TListBox;
    Label1: TLabel;
    txtScanNum: TEdit;
    procedure lstDeviceClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure radGainListClick(Sender: TObject);
    procedure radOverallClick(Sender: TObject);
    procedure butExitClick(Sender: TObject);
    procedure butGainListClick(Sender: TObject);
    procedure cmbGainChange(Sender: TObject);
    procedure butStartClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
   gnNumOfSubdevices  : Smallint;
   Response           : Integer;
   CurrentChannel     : Smallint;
  end;

var
  frmMain: TfrmMain;
  Function PhyChanToLogChan(DevCfg:Driver.DEVCONFIG_AI;PhyChan:smallint): smallint;
implementation

uses GainListForm, DisplayForm , math;

{$R *.dfm}
Function PhyChanToLogChan(DevCfg:Driver.DEVCONFIG_AI;PhyChan:smallint): smallint;
var
        i :smallint;
        FunResult:smallint;
begin
        if(DevCfg.ulChanConfig = 1) then
        begin
                PhyChanToLogChan := PhyChan;
                Exit;
        end;
        FunResult := 0;
        i := 0;
        while (i <=Min(PhyChan,31)) do
        begin
		if ((DevCfg.ulChanConfig and (1 shl i))<>0) then i:= i+1;
		FunResult := FunResult + 1;
                i := i+1;
        end;

	if(phychan >= 32) then
        begin
                i := 32;
                while i<=phychan  do
                begin
		    if((DevCfg.ulChanConfigEx[0] and (1 shl (i-32)))<>0) then i := i+1;
                    FunResult := FunResult + 1;
                    i := i+1;                    
                end;
        end;
    PhyChanToLogChan := FunResult -1;
end;

procedure TfrmMain.lstDeviceClick(Sender: TObject);
var
  tempNum, i, ii         : Integer;
  nOutEntries            : Smallint;
  TestRes                : Boolean;
  TempStr                : String;
  lpAIGetConfig          : PT_AIGetConfig;
  iMaxSingleChannel      : Integer;
  iMaxDiffChannel        : Integer;
begin
  lstModule.Items.Clear;
  cmbChannel.Items.Clear;
  cmbGain.Items.Clear;

  tempNum := Pos('DEMO', lstDevice.Items[lstDevice.ItemIndex]);
  if (tempNum <> 0) then
     TestRes := True
  else
     TestRes := False;

  { Avoid to open Advantech Demo Card }
  If (TestRes) Then
  begin
    lstModule.Enabled := False;
    cmbChannel.Enabled := False;
    cmbGain.Enabled := False;
    cmbGain.Enabled := False;
    cmbChannel.Items.Add('No Use');
    butStart.Enabled := False;
  end;

  If (Not TestRes) Then
  begin
    { Check if there is any device attached on this COM or CAN }
    gnNumOfSubdevices := DeviceList[lstDevice.ItemIndex].nNumOfSubdevices;
    if (gnNumOfSubdevices > MaxDev) then
       gnNumOfSubdevices := MaxDev;

    dwDeviceNum := DeviceList[lstDevice.ItemIndex].dwDeviceNum;

    { COM and CAN bus }
    if (gnNumOfSubdevices <> 0) then
    begin
       ErrCde := DRV_DeviceGetSubList(dwDeviceNum, SubDeviceList[0], gnNumOfSubdevices, nOutEntries);
       If (ErrCde <> 0) Then
       begin
            DRV_GetErrorMessage(ErrCde, pszErrMsg);
            Response := Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
            Exit;
       end;
       For i := 0 To (gnNumOfSubdevices - 1) do
       begin
           tempStr := '';
           For ii := 0 To MaxDevNameLen do
               tempStr := tempStr + SubDevicelist[i].szDeviceName[ii];
           lstModule.Items.Add(tempStr);
       end;
       lstModule.Enabled := True;
       cmbChannel.Enabled := True;
       cmbGain.Enabled := True;
       butStart.Enabled := True;
    end;

    { PCL DAS & DIO card }
    if (gnNumOfSubdevices = 0) then
    begin
         ErrCde := DRV_DeviceOpen(dwDeviceNum, DeviceHandle);
         If (ErrCde <> 0) Then
         begin
              DRV_GetErrorMessage(ErrCde, pszErrMsg);
              Response := Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
              Exit;
         end;

         ptDevGetFeatures.buffer := @lpDevFeatures;
         ptDevGetFeatures.size := sizeof(DEVFEATURES);
         ErrCde := DRV_DeviceGetFeatures(DeviceHandle, ptDevGetFeatures);

         { Add analog input channel item }
         lpAIGetConfig.size := sizeof(DEVCONFIG_AI);
         lpAIGetConfig.buffer := @lpDevConfig_AI;
         ErrCde := DRV_AIGetConfig(DeviceHandle, lpAIGetConfig);
         If (ErrCde <> 0) Then
         begin
              DRV_GetErrorMessage(ErrCde, pszErrMsg);
              Response := Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
              DRV_DeviceClose(DeviceHandle);
              butStart.Enabled := False;
              Exit;
         end;

         {get max channel number}
         iMaxSingleChannel := lpDevFeatures.usMaxAISiglChl;
         iMaxDiffChannel := lpDevFeatures.usMaxAIDiffChl;
         if(lpDevConfig_AI.ulChanConfig = 1) then
               tempNum := iMaxDiffChannel
         else if ( iMaxSingleChannel > iMaxDiffChannel )   then
         begin
           tempNum := iMaxSingleChannel;
         end
         Else
         begin
           tempNum := iMaxDiffChannel;
         end;

         { Here add analog input channel. }
         if (tempNum > 0) then
         begin
              For i := 0 To (tempNum - 1) do
              begin
                   cmbChannel.Items.Add(IntToStr(i));
              end;
              cmbChannel.Enabled := True;
              cmbChannel.ItemIndex := 0;
              { Since the channel number is more than one, then Go }
              butStart.Enabled := True;
         end;

         { Here add gain code for input voltage range. }
         if (lpDevFeatures.usNumGain > 0) then
         begin
              For i := 0 To (lpDevFeatures.usNumGain - 1) do
              begin
                  cmbGain.Items.Add(lpDevFeatures.glGainList[i].szGainStr);
              end;
              cmbGain.Enabled := True;
              cmbGain.ItemIndex := 0;
         end;
         DRV_DeviceClose(DeviceHandle);
    end;
  end;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
var
  MaxEntries, OutEntries : Smallint;
  i                      : Integer;
begin
  bRun := False;

  isStartChan := True;    { This variable is for Start/Stop channel setup only }
  CurrentChannel := 0;

  { Initialize the Gain Code }
  for i := 0 to 63 do
      usGainIndx[i] := 0;


  {                 Add type of PC Laboratory Card                  }
  { You should use "Device Installation" which was contains in the  }
  { SETUP disk to install the hardware which you want to use in this}
  { Application.                                                    }

  { Here MaxEntries = OutEntries }
  ErrCde := DRV_DeviceGetNumOfList(MaxEntries);
  If (ErrCde <> 0) Then
  begin
    DRV_GetErrorMessage(ErrCde, pszErrMsg);
    Response := Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
    Exit;
  end;

  ErrCde := DRV_DeviceGetList(DeviceList[0], MaxEntries, OutEntries);
  If (ErrCde <> 0) Then
  begin
       DRV_GetErrorMessage(ErrCde, pszErrMsg);
       Response := Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
       Exit;
  end;

  { This block add the name of hardware in the device list. }
  For i := 0 To (MaxEntries - 1) do
  begin
    lstDevice.Items.Add(DeviceList[i].szDeviceName);
  end;
  lstDevice.ItemIndex := 0;
  if( MaxEntries <=0 )  then
   begin
      Application.MessageBox('No Installed Device!', 'Driver Message', MB_OK);
      butStart.Enabled := False;

       exit;
  end;

  lstDeviceClick(Sender);

end;


procedure TfrmMain.radGainListClick(Sender: TObject);
begin
   cmbGain.Enabled := false;
   butGainList.Enabled := true;

end;

procedure TfrmMain.radOverallClick(Sender: TObject);
begin
   cmbGain.Enabled := true;
   butGainList.Enabled := false;

end;

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

procedure TfrmMain.butGainListClick(Sender: TObject);
begin
    usStartChan := cmbChannel.ItemIndex;
    usChanNum := StrToInt(txtScanNum.Text);

    frmGainList.ShowModal();

end;

procedure TfrmMain.cmbGainChange(Sender: TObject);
var
    i   : integer;
begin
    for i := 0 to 63 do
          usGainIndx[i] := cmbGain.ItemIndex;

end;

procedure TfrmMain.butStartClick(Sender: TObject);
var
  i     : integer;
  phiChan       : integer;
  iMaxSingleChannel : integer;
  iMaxDiffChannel   : integer;
  iMaxChannelNum    : integer;
begin
  { The Original default is the value of lpDevConfig_AI.usGainCtrMode. }
  { Because this program uses external Trigger Signal, so AiCtrMode is }
  { set to 1. (1 External trigger) Since we find something wrong, so we}
  { set it to 0 for temporary.                                         }
  AiCtrMode := 0;
  if (gnNumOfSubdevices = 0) then
  begin
     ErrCde := DRV_DeviceOpen(dwDeviceNum, DeviceHandle);
     If (ErrCde <> 0) Then
     begin
        DRV_GetErrorMessage(ErrCde, pszErrMsg);
        Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
        Exit;
      end;

   if( radOverall.Checked ) then
   begin
      for i := 0 to 63 do
          usGainIndx[i] := cmbGain.ItemIndex;
   end;       

    {get max number of channel }
    iMaxSingleChannel := lpDevFeatures.usMaxAISiglChl;
    iMaxDiffChannel := lpDevFeatures.usMaxAIDiffChl;
    if(lpDevConfig_AI.ulChanConfig = 1) then
           iMaxChannelNum := iMaxDiffChannel
    else if ( iMaxSingleChannel > iMaxDiffChannel ) then
    begin
       iMaxChannelNum := iMaxSingleChannel;
    end
    Else
    begin
       iMaxChannelNum := iMaxDiffChannel;
    end;

    usStartChan := cmbChannel.ItemIndex;
    usChanNum := StrToInt(txtScanNum.Text);
    phiChan := usStartChan;
    {check the channel number}
    if (lpDevConfig_AI.ulChanConfig = 1 ) then
    begin
        if (usChanNum > (lpDevFeatures.usMaxAIDiffChl - usStartChan)) then
        begin
            Application.MessageBox('Invalid logical channel number.',
            'Error!!', MB_OK);
            DRV_DeviceClose(DeviceHandle);
            exit;
        end
    end;
    if (usChanNum > (iMaxChannelNum - usStartChan)) then
    begin
       Application.MessageBox('Invalid logical channel number.',
                              'Error!!', MB_OK);
       DRV_DeviceClose(DeviceHandle);
       exit;
    end;

    for i:=0 to usChanNum-1 do
    begin
      if(phiChan >= iMaxChannelNum) then
      begin
         Application.MessageBox('Invalid channel number.',
                           'Error!!', MB_OK);
         DRV_DeviceClose(DeviceHandle);
         exit;
      end;

      usGainCode[i] :=
         lpDevFeatures.glGainList[usGainIndx[PhyChanToLogChan(lpDevConfig_AI,phiChan)]].usGainCde;

      if ( lpDevConfig_AI.ulChanConfig <> 1 ) then
      begin
        if (phiChan < 32) then
           if( (lpDevConfig_AI.ulChanConfig and (1 shl phiChan)) <> 0 ) and ((phiChan mod 2) = 0) then
               phiChan:=phiChan+1
        else
           if( (lpDevConfig_AI.ulChanConfigEx[0] and (1 shl (phiChan-32))) <> 0 ) and ((phiChan mod 2) = 0) then
               phiChan:=phiChan+1;
      end;

      phiChan := phiChan + 1;

    end;


  ptMAIConfig.NumChan := usChanNum;
  ptMAIConfig.StartChan := usStartChan;

  { Here you must point the GainArray to the Start Channel, not the AI Channel }
  if gnNumOfSubdevices = 0 then
     ptMAIConfig.GainArray := @usGainCode[0]
  else
     ptMAIConfig.GainArray := Nil;

  ErrCde := DRV_MAIConfig(DeviceHandle, ptMAIConfig);
  If (ErrCde <> 0) Then
  begin
       DRV_GetErrorMessage(ErrCde, pszErrMsg);
       Response := Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
       Exit;
  end;

  frmDisplay.Show;
  frmDisplay.ScanTimer.Enabled := true;
 end;
end;

end.

⌨️ 快捷键说明

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