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

📄 main.pas

📁 使用研华DAQ和Delphi实现研华数据采集卡的多种功能.包括数据输入输出。内含多个源码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, DAQDILib_TLB;

type
  TfMain = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    txtDeviceNum: TEdit;
    txtDeviceName: TEdit;
    cmdSelectDevice: TButton;
    GroupBox2: TGroupBox;
    cmbPort: TComboBox;
    Label3: TLabel;
    Label4: TLabel;
    cmbBit: TComboBox;
    GroupBox3: TGroupBox;
    cmdByteRead: TButton;
    cmdBitRead: TButton;
    GroupBox4: TGroupBox;
    cmdByteScan: TButton;
    cmdByteStop: TButton;
    cmdBitScan: TButton;
    cmdBitStop: TButton;
    cmdExit: TButton;
    Label7: TLabel;
    txtScanTime: TEdit;
    Label8: TLabel;
    DAQDI1: TDAQDI;
    cmdEventScan: TButton;
    cmdEventStop: TButton;
    GroupBox5: TGroupBox;
    Label6: TLabel;
    txtRead: TEdit;
    Label5: TLabel;
    Label9: TLabel;
    txtEventNum: TEdit;
    txtMatchNum: TEdit;
    GroupBox6: TGroupBox;
    chkMatchEnabled: TCheckBox;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    GroupBox7: TGroupBox;
    radHBit0: TRadioButton;
    chkBit0: TCheckBox;
    chkBit1: TCheckBox;
    chkBit2: TCheckBox;
    chkBit3: TCheckBox;
    chkBit4: TCheckBox;
    chkBit5: TCheckBox;
    chkBit6: TCheckBox;
    chkBit7: TCheckBox;
    radLBit0: TRadioButton;
    GroupBox8: TGroupBox;
    radHBit1: TRadioButton;
    radLBit1: TRadioButton;
    GroupBox9: TGroupBox;
    radHBit2: TRadioButton;
    radLBit2: TRadioButton;
    GroupBox10: TGroupBox;
    radHBit3: TRadioButton;
    radLBit3: TRadioButton;
    GroupBox11: TGroupBox;
    radHBit4: TRadioButton;
    radLBit4: TRadioButton;
    GroupBox12: TGroupBox;
    radHBit5: TRadioButton;
    radLBit5: TRadioButton;
    GroupBox13: TGroupBox;
    radHBit6: TRadioButton;
    radLBit6: TRadioButton;
    GroupBox14: TGroupBox;
    radHBit7: TRadioButton;
    radLBit7: TRadioButton;
    txtFilter: TEdit;
    txtData: TEdit;
    Label18: TLabel;
    Label19: TLabel;
    procedure cmdExitClick(Sender: TObject);
    procedure cmdSelectDeviceClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cmdByteReadClick(Sender: TObject);
    procedure cmdBitReadClick(Sender: TObject);
    procedure cmdByteScanClick(Sender: TObject);
    procedure cmdByteStopClick(Sender: TObject);
    procedure txtScanTimeChange(Sender: TObject);
    procedure DAQDI1ByteScan(Sender: TObject; Data: Smallint);
    procedure DAQDI1BitScan(Sender: TObject; Data: WordBool);
    procedure cmdBitScanClick(Sender: TObject);
    procedure cmdBitStopClick(Sender: TObject);
    procedure txtDeviceNumChange(Sender: TObject);
    procedure cmbPortChange(Sender: TObject);
    procedure cmbBitChange(Sender: TObject);
    procedure DisplayPattern();
    procedure cmdEventScanClick(Sender: TObject);
    procedure cmdEventStopClick(Sender: TObject);
    procedure chkMatchEnabledClick(Sender: TObject);
    procedure DAQDI1Event(Sender: TObject);
    procedure DAQDI1EventPatternMatch(Sender: TObject);
    procedure CheckFilter(nIndex: Smallint);
    procedure CheckData(nIndex: Smallint; bHighBit: Boolean);
    procedure chkBit0Click(Sender: TObject);
    procedure chkBit1Click(Sender: TObject);
    procedure chkBit2Click(Sender: TObject);
    procedure chkBit3Click(Sender: TObject);
    procedure chkBit4Click(Sender: TObject);
    procedure chkBit5Click(Sender: TObject);
    procedure chkBit6Click(Sender: TObject);
    procedure chkBit7Click(Sender: TObject);
    procedure radHBit0Click(Sender: TObject);
    procedure radHBit1Click(Sender: TObject);
    procedure radHBit2Click(Sender: TObject);
    procedure radHBit3Click(Sender: TObject);
    procedure radHBit4Click(Sender: TObject);
    procedure radHBit5Click(Sender: TObject);
    procedure radHBit6Click(Sender: TObject);
    procedure radHBit7Click(Sender: TObject);
    procedure radLBit0Click(Sender: TObject);
    procedure radLBit1Click(Sender: TObject);
    procedure radLBit2Click(Sender: TObject);
    procedure radLBit3Click(Sender: TObject);
    procedure radLBit4Click(Sender: TObject);
    procedure radLBit5Click(Sender: TObject);
    procedure radLBit6Click(Sender: TObject);
    procedure radLBit7Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fMain: TfMain;
  eventCount: integer;
  matchCount: integer;
  nTemp: array[0..7] of Smallint;
  chkBit: array[0..7] of TCheckBox;
  radHBit: array[0..7] of TRadioButton;
  radLBit: array[0..7] of TRadioButton;

implementation

{$R *.DFM}
{********************************************************************}
{ Function : Display error message to a dialog box.                  }
{            The format of Long string and short string in Delphi is }
{            different from Null-terminated that used in C.          }
{                                                                    }
{            The long/short string use 1'st char to denote the string}
{            length without termonated char in the tail of string.   }
{                                                                    }
{            The Null-terminated string use Null Char as a string    }
{            termonated and without string length information.       }
{                                                                    }
{            The Message box use Null-Terminated string.             }
{                                                                    }
{ Paramater: sErrMsg: In, Error message string.                      }
{ Return:    None                                                    }
{********************************************************************}
procedure ErrorMsgBox( sErrMsg : WideString);
var
   sErrString : String[51];
   sCaption   : String[51];
begin
   sErrString := sErrMsg + Char(0);
   sCaption := fMain.Caption + ' Error' + Char(0);
   Application.MessageBox(@sErrString[1], @sCaption[1], MB_OK or MB_ICONEXCLAMATION);
end;

procedure TfMain.cmdExitClick(Sender: TObject);
begin
   application.Terminate
end;

procedure TfMain.cmdSelectDeviceClick(Sender: TObject);
Var
   sTmp : string[50];
   i    : Smallint;
begin
   {Select Device from list}
   DAQDI1.SelectDevice;

   {Dispaly selection result}
   str(DAQDI1.DeviceNumber, sTmp);
   txtDeviceNum.Text := sTmp;
   txtDeviceName.Text := DAQDI1.DeviceName;

   {Get device's DI figure}
   If DAQDI1.OpenDevice Then
   begin
      ErrorMsgBox(DAQDI1.ErrorMessage);
      Exit;
   End;

   if DAQDI1.MaxPortNumber = 0 then
   begin
      ShowMessage('Function Not Supported');
      DAQDI1.CloseDevice;
      Exit;
   end;

   {Display DI feature for selection ... The DI channel}
   cmbPort.Clear;
   For i := 0 To DAQDI1.MaxPortNumber - 1 do
   begin
      str(i, sTmp);
      cmbPort.Items.Add(sTmp);
   end;
   If DAQDI1.MaxPortNumber > 0 Then
      cmbPort.ItemIndex := DAQDI1.Port;

   DAQDI1.CloseDevice;
end;

procedure TfMain.FormCreate(Sender: TObject);
begin
   chkBit[0] := chkBit0;
   chkBit[1] := chkBit1;
   chkBit[2] := chkBit2;
   chkBit[3] := chkBit3;
   chkBit[4] := chkBit4;
   chkBit[5] := chkBit5;
   chkBit[6] := chkBit6;
   chkBit[7] := chkBit7;

   radHBit[0] := radHBit0;
   radHBit[1] := radHBit1;
   radHBit[2] := radHBit2;
   radHBit[3] := radHBit3;
   radHBit[4] := radHBit4;
   radHBit[5] := radHBit5;
   radHBit[6] := radHBit6;
   radHBit[7] := radHBit7;

   radLBit[0] := radLBit0;
   radLBit[1] := radLBit1;
   radLBit[2] := radLBit2;
   radLBit[3] := radLBit3;
   radLBit[4] := radLBit4;
   radLBit[5] := radLBit5;
   radLBit[6] := radLBit6;
   radLBit[7] := radLBit7;

   nTemp[0] := 1;
   nTemp[1] := 2;
   nTemp[2] := 4;
   nTemp[3] := 8;
   nTemp[4] := 16;
   nTemp[5] := 32;
   nTemp[6] := 64;
   nTemp[7] := 128;

   cmdSelectDeviceClick(Sender);

   eventCount := 0;
   matchCount := 0;

   txtDeviceNum.Text := IntToStr(DAQDI1.DeviceNumber);
   txtDeviceName.Text := DAQDI1.DeviceName;
   txtScanTime.Text := FloatToStr(DAQDI1.ScanTime);
   cmbPort.ItemIndex := DAQDI1.Port;
   cmbBit.ItemIndex := DAQDI1.Bit;

   txtFilter.Text := IntToStr(DAQDI1.PatternMatchFilter);
   txtData.Text := IntToStr(DAQDI1.PatternMatchData);

   if DAQDI1.PatternMatchEnabled then
      chkMatchEnabled.Checked := True
   else
      chkMatchEnabled.Checked := False;

   txtEventNum.Text := IntToStr(eventCount);
   txtMatchNum.Text := IntToStr(matchCount);
    
   DisplayPattern;
end;

procedure TfMain.cmdByteReadClick(Sender: TObject);
begin
   {Read DI Byte from device}
   if DAQDI1.OpenDevice then
   begin
      ErrorMsgBox(DAQDI1.ErrorMessage);
      exit;
   end;

   {Display data}
   DAQDI1.Port := cmbPort.ItemIndex;
   txtRead.Text := IntToHex(DAQDI1.ByteInput, 2);

   DAQDI1.CloseDevice;
end;

procedure TfMain.cmdBitReadClick(Sender: TObject);
begin
   {Read DI one bit from device}
   if DAQDI1.OpenDevice then
   begin
      ErrorMsgBox(DAQDI1.ErrorMessage);
      exit;
   end;
   
   DAQDI1.Port := cmbPort.ItemIndex;
   DAQDI1.Bit := cmbBit.ItemIndex;
   {Display data}
   if DAQDI1.BitInput then
      txtRead.Text := '1'
   else
      txtRead.Text := '0';

   DAQDI1.CloseDevice;
end;

procedure TfMain.cmdByteScanClick(Sender: TObject);
begin
   {Start cyclic DI byte scan function}
   if DAQDI1.OpenDevice then
   begin
      ErrorMsgBox(DAQDI1.ErrorMessage);
      exit;
   end;

   DAQDI1.Port := cmbPort.ItemIndex;
   if DAQDI1.EnableByteScan(True) then
   begin
      ErrorMsgBox(DAQDI1.ErrorMessage);
      DAQDI1.CloseDevice;
      Exit;
   end;

   {Manage UI}
   cmdByteScan.Enabled := False;
   cmdBitScan.Enabled := False;
   cmdEventScan.Enabled := False;
   cmdByteStop.Enabled := True;
   cmdExit.Enabled := False;
   cmdSelectDevice.Enabled := False;
end;

procedure TfMain.cmdByteStopClick(Sender: TObject);
begin
   {Stop DI byte scan function}
   if DAQDI1.EnableByteScan(False) then
      ErrorMsgBox(DAQDI1.ErrorMessage);

   DAQDI1.CloseDevice;

   {Manage UI}
   cmdByteScan.Enabled := True;
   cmdBitScan.Enabled := True;
   cmdEventScan.Enabled := True;
   cmdByteStop.Enabled := False;
   cmdExit.Enabled := True;
   cmdSelectDevice.Enabled := True;
End;

procedure TfMain.cmdBitScanClick(Sender: TObject);
begin
   {Start cyclic bit scan function}
   if DAQDI1.OpenDevice then
   begin
      ErrorMsgBox(DAQDI1.ErrorMessage);
      Exit;
   end;

   DAQDI1.Port := cmbPort.ItemIndex;
   DAQDI1.Bit := cmbBit.ItemIndex;
   if DAQDI1.EnableBitScan(True) then
   begin
      ErrorMsgBox(DAQDI1.ErrorMessage);
      DAQDI1.CloseDevice;
      Exit;
   end;

   {Manage UI}

⌨️ 快捷键说明

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