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

📄 main.pas

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit Main;

interface

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

   //The program can only support 255 devices in Device List.
   const MAX_ENTRIES = 255;
   //The program can only support 50 ports on every device.
   const MAX_PORTS   = 50;
   //Note: If any more devices or ports you want to support, please modify the
   //      number given above.
type
  TMainForm = class(TForm)
    Group_Device: TGroupBox;
    Label1: TLabel;
    CmbDevList: TComboBox;
    Label2: TLabel;
    Label3: TLabel;
    DOPorts: TEdit;
    DIPorts: TEdit;
    BtnOpenDev: TButton;
    Group_Read: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    BtnRead: TButton;
    DIPortStart: TEdit;
    DIData: TEdit;
    DIPortCnt: TEdit;
    Label7: TLabel;
    Group_Write: TGroupBox;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    DOPortStart: TEdit;
    DOData: TEdit;
    DOPortCnt: TEdit;
    BtnWrite: TButton;
    Group_State: TGroupBox;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    DOSPortStart: TEdit;
    DOSPortCnt: TEdit;
    DOSData: TEdit;
    BtnDOState: TButton;
    Label16: TLabel;
    Label18: TLabel;
    Label17: TLabel;

    procedure BtnOpenDevClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnReadClick(Sender: TObject);
    procedure BtnWriteClick(Sender: TObject);
    procedure BtnDOStateClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);

  private
    { Private declarations }
    DevNumList       : array[0..MAX_ENTRIES] of Integer;
    DevHandle        : Integer;
    DIPortCount      : Longword;
    DOPortCount      : Longword;
    szErrMsg         : ShortString;
    DataBuffer       : array[0..MAX_PORTS] of BYTE;

  public
    { Public declarations }
    function InitialDeviceList() : Boolean;
    function CharToHex ( c : char ) : BYTE;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

{---------------------------------------------------}
function TMainForm.InitialDeviceList() : Boolean;
var
      lErrCde : LRESULT;
      DevCount : Smallint;
      outEntries : Smallint;
      DeviceList : array [0..MAX_ENTRIES] of PT_DEVLIST;
      i : Integer;
begin

   {Firstly, get the count of the device list}
   lErrCde := DRV_DeviceGetNumOfList( DevCount );
   if ( lErrCde <> SUCCESS ) then
   begin
      DRV_GetErrorMessage( lErrCde, @szErrMsg );
      begin
         Application.MessageBox( @szErrMsg, 'Driver Error', MB_OK or MB_ICONERROR);
      end;
      InitialDeviceList := false;
      Exit;
   end;
    if ( DevCount > MAX_ENTRIES ) then
    begin
      Application.MessageBox( 'Too many device to run the program!', 'Sorry', MB_OK or MB_ICONINFORMATION );
      InitialDeviceList := false;
      Exit;
    end;
    if ( DevCount = 0 ) then
    begin
      Application.MessageBox( 'There no device in the Device list!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      InitialDeviceList := false;
      Exit;
    end;

   {Secondly, get device list}
   lErrCde := DRV_DeviceGetList( DeviceList[0], MAX_ENTRIES, outEntries );
   if ( lErrCde <> SUCCESS ) then
   begin
      DRV_GetErrorMessage( lErrCde, @szErrMsg );
      begin
         Application.MessageBox( @szErrMsg, 'Driver Error', MB_OK or MB_ICONERROR );
      end;
      InitialDeviceList := false;
      Exit;
   end;

   {Thirdly, fill the combobox and save the device num orderly}
   for i := 0 To (outEntries - 1) do
   begin
      CmbDevList.Items.Add( DeviceList[i].szDeviceName );
      DevNumList[i] := DeviceList[i].dwDeviceNum;
   end;
   CmbDevList.ItemIndex := 0;
   InitialDeviceList := true;
   
end;
{----------------------------------------------------}


{---------------------------------------------------}
procedure TMainForm.BtnOpenDevClick(Sender: TObject);
var
   nSel     : Integer;
   nDevNum  : Integer;
   lErrCde  : Longint;
   nDataLen : Integer;

begin
   {Firstly, Get the device number}
   nSel := CmbDevList.ItemIndex;
   if ( nSel < 0 ) then
   begin
      Application.MessageBox( 'Please select a device firstly', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;
   nDevNum := DevNumList[ nSel ];

   {Secondly, open the device}
   {--Firstly, close the device if the device has been opened}
   if ( DevHandle <> 0 ) then
   begin
      DRV_DeviceClose( DevHandle );
      DevHandle := 0;
   end;
   {--Secondly, open the device}
   lErrCde := DRV_DeviceOpen( nDevNum, DevHandle );
   if ( lErrCde <> SUCCESS ) then
   begin
      DRV_GetErrorMessage( lErrCde, @szErrMsg );
      Application.MessageBox( @szErrMsg, 'Driver Error', MB_OK or MB_ICONERROR );
      BtnDOState.Enabled := false;
      BtnRead.Enabled := false;
      BtnWrite.Enabled := false;
      DOPorts.Text := '';
      DIPorts.Text := '';
      DIPortCount := 0;
      DOPortCount := 0;
      Exit;
   end;

   {Thirdly, find DIO port count and fill the edit}
   {--DI port count}
   nDataLen := Sizeof( Integer );
   lErrCde := DRV_DeviceGetProperty( DevHandle, CFG_DiPortCount, @DIPortCount, nDataLen );
   if ( lErrCde = SUCCESS ) then
   begin
      DIPorts.Text := IntToStr( DIPortCount );
      if ( DIPortCount > 0 ) then
      begin
         BtnRead.Enabled := true
      end
      else
      begin
         BtnRead.Enabled := false;
         DIPorts.Text := '';
         DIPortCount := 0;
      end;
   end
   else
   begin
      BtnRead.Enabled := false;
      DIPorts.Text := '';
      DIPortCount := 0;
   end;
   {--DO port count}
   nDataLen := Sizeof( Integer );
   lErrCde := DRV_DeviceGetProperty( DevHandle, CFG_DoPortCount, @DOPortCount, nDataLen );
   if ( lErrCde = SUCCESS ) then
   begin
      DOPorts.Text := IntToStr( DOPortCount );
      if ( DOPortCount > 0 ) then
      begin
         BtnWrite.Enabled := true;
         BtnDOState.Enabled := true
      end
      else
      begin
         BtnWrite.Enabled := false;
         BtnDOState.Enabled := false;
         DOPorts.Text := '';
         DOPortCount := 0;
      end;
   end
   else
   begin
      BtnWrite.Enabled := false;
      BtnDOState.Enabled := false;
      DOPorts.Text := '';
      DOPortCount := 0;
   end;


end;
{---------------------------------------------------}
procedure TMainForm.FormCreate(Sender: TObject);
begin
   {Initial variables necessariely}
   DevHandle := 0;
   DIPortCount := 0;
   DOPortCount := 0;
   {Initial Device list}
   if ( InitialDeviceList() = false ) then
   begin
      Application.MessageBox( 'Can not find devices','Error',MB_OK or MB_ICONERROR );
      Exit;
   end;
end;
{----------------------------------------------------}

procedure TMainForm.BtnReadClick(Sender: TObject);
var
   nPortStart : Longword;
   nPortCount : Longword;
   lErrCde    : Longint;
   i,j        : Integer;
   str        : string;
   strTemp    : ShortString;

begin

   if ( DevHandle = 0 ) then
   begin
      Application.MessageBox('Please open a device firstly!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;

   {Firstly, get start port number}
   strTemp := DIPortStart.Text;
   if ( Length(strTemp) = 0 ) then
   begin
      Application.MessageBox('Please input an appropriate number firstly!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;
   try
      nPortStart := StrToInt( strTemp );
   except
      on EConvertError do
      begin
      Application.MessageBox('Please input an appropriate number !', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
      end;
   end;

   if ( nPortStart >= DIPortCount ) then
   begin
      Application.MessageBox('Start port is not avaliable, please input again!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;

⌨️ 快捷键说明

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