Advertisement
Guest User

Untitled

a guest
Feb 24th, 2022
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.37 KB | None | 0 0
  1. unit example_dll;
  2.  
  3. interface
  4. uses
  5.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  6.   Dialogs, StdCtrls, ExtCtrls, Grids, tedia_daq01h, Registry, Math, ComCtrls,
  7.   AppEvnts;
  8.  
  9. type
  10.   ADWord=array of DWord;
  11.   TExample = class(TForm)
  12.     Panel2: TPanel;
  13.     GroupBox6: TGroupBox;
  14.     NumberOfChannels: TLabeledEdit;
  15.     PageControl1: TPageControl;
  16.     TabSheet1: TTabSheet;
  17.     TabSheet2: TTabSheet;
  18.     Panel1: TPanel;
  19.     StringGrid1: TStringGrid;
  20.     StringGrid2: TStringGrid;
  21.     OpenAndSetParam: TButton;
  22.     GetValues: TButton;
  23.     Panel3: TPanel;
  24.     SetValues: TButton;
  25.     procedure GetValuesClick(Sender: TObject);
  26.     procedure FormCreate(Sender: TObject);
  27.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  28.     procedure OpenAndSetParamClick(Sender: TObject);
  29.     procedure SetValuesClick(Sender: TObject);
  30.   private
  31.   public
  32.     aktivni_sloupec:integer;
  33.     is_running:boolean;
  34.     call_buffer:PByte;
  35.     call_size_of_buffer:integer;
  36.     Fintput_or_output:DWord;  //0-vstupni porty, 1-vystupni porty
  37.  
  38.     procedure resolve_error(n_error:DWord); //Procedura, ktera vytvori message box s chybovou zprávou
  39.   end;
  40.  
  41. var
  42.   Example: TExample;
  43.   Ihandle:PHandle;
  44.   count_of_channels:integer;
  45.   buffer:PDWord;
  46.   frequency:double;
  47.   period:double;
  48. implementation
  49.  
  50. {$R *.dfm}
  51.  
  52. procedure TExample.GetValuesClick(Sender: TObject);
  53. type
  54.   AByte=array of byte;
  55. var
  56.   result_:integer;
  57.   i,j:integer;
  58.   myRect:TGridRect;
  59. begin
  60.   if(is_running) and (Fintput_or_output=0) then
  61.   begin
  62.     //Zjisteni hodnoty digitalnich vstupu
  63.     result_:=Td_Get_DI(IHandle,0,buffer,count_of_channels);
  64.     if result_<>0 then
  65.     begin
  66.       //Nepodařilo se odměřit požadovaný počet kanálů.
  67.       resolve_error(result_);
  68.       Exit;
  69.     end;
  70.  
  71.     StringGrid1.RowCount:=max(count_of_channels+1,2);
  72.     for j:=1 to count_of_channels do
  73.     begin
  74.       StringGrid1.Cells[aktivni_sloupec,j]:=IntToStr(ADWord(buffer)[j-1]);
  75.     end;
  76.  
  77.     myRect.Left := aktivni_sloupec;
  78.     myRect.Top := 1;
  79.     myRect.Right := aktivni_sloupec;
  80.     myRect.Bottom := count_of_channels+1;
  81.     StringGrid1.Selection := myRect;
  82.  
  83.     aktivni_sloupec:= (aktivni_sloupec+1) mod 5;
  84.   end
  85.   else
  86.     ShowMessage('Karta není nakonfigurována, nebo není nastavena ve správném režimu.');
  87. end;
  88.  
  89. procedure TExample.FormCreate(Sender: TObject);
  90. var
  91.   count_registry_card_loc:DWord;
  92.   card_exists:boolean;
  93.   result_:DWord;
  94.   i:integer;
  95. begin
  96.   //Vyplnim ListBox jmeny pripravenych karet
  97.   count_of_channels:=3;
  98.   aktivni_sloupec:=0;
  99.  
  100.   //Otevru kartu
  101.   result_:=Td_Init_Driver(@Ihandle,PChar('EXAM_DIGITAL'));
  102.   if result_<>0 then
  103.   begin
  104.     Application.MessageBox('Nenalezené žádná aktivní zařízení s názvem EXAM_DIGITAL, použijte administrační program [admin.exe].','Chyba!',MB_ICONERROR);
  105.     resolve_error(result_);
  106.     Application.Terminate;
  107.   end;
  108.  
  109.   is_running:=false;
  110.  
  111.   for i:=0 to count_of_channels-1 do
  112.   begin
  113.     StringGrid2.Cells[i,1]:='1';
  114.   end;
  115. end;
  116.  
  117. procedure TExample.FormClose(Sender: TObject; var Action: TCloseAction);
  118. var
  119.   result_:integer;
  120. begin
  121.   //Uzavreme zarizeni
  122.   result_:=Td_Close_Driver(Ihandle);
  123.   if result_<>0 then
  124.   begin
  125.     //Nepodařilo se uzavřít kartu.
  126.     resolve_error(result_);
  127.     Exit;
  128.   end;
  129. end;
  130.  
  131. procedure TExample.OpenAndSetParamClick(Sender: TObject);
  132. type
  133.   AByte=array of byte;
  134. var
  135.   result_:DWord;
  136.   i,j:integer;
  137.   myRect:TGridRect;
  138.   sample:byte;
  139.   n_param:double;
  140.   t_param:DWord;
  141.   tmp_char:array[0..200] of char;
  142.   tmp_char1:array[0..200] of char;
  143. begin
  144.   try
  145.     result_:=StrToInt(NumberOfChannels.Text);
  146.   Except
  147.     Application.MessageBox('Počet kanálů musí být celé kladné číslo.','Chyba!',MB_ICONERROR);
  148.     Exit;
  149.   end;
  150.  
  151.   if result_<=0 then
  152.   begin
  153.     Application.MessageBox('Počet kanálů musí být celé kladné číslo.','Chyba!',MB_ICONERROR);
  154.     Exit;
  155.   end;
  156.   count_of_channels:=result_;
  157.   StringGrid2.ColCount:=count_of_channels;
  158.  
  159.   for i:=0 to count_of_channels-1 do
  160.   begin
  161.     StringGrid2.Cells[i,1]:='1';
  162.   end;
  163.  
  164.   if(PageControl1.Activepage=PageControl1.Pages[0]) then
  165.     Fintput_or_output:=0
  166.   else
  167.     Fintput_or_output:=1;
  168.  
  169.   result_:=Td_Get_Info_DeviceName(PChar('EXAM_DIGITAL'),1,@n_param,@tmp_char[0],@t_param);
  170.  
  171.   result_:=Td_Get_Info_DeviceType(@tmp_char[0],1304,@n_param,@tmp_char1[0],@t_param);
  172.   if (n_param=1) then //Parametr INFO_Digital_Not_Fixed=> musim nastavit dig. porty
  173.   begin
  174.     for i:=0 to count_of_channels-1 do
  175.     begin
  176.       case i of
  177.         0: result_:=Td_Set_Parameters(Ihandle,15,Fintput_or_output,nil,0,0);
  178.         1: result_:=Td_Set_Parameters(Ihandle,16,Fintput_or_output,nil,0,0);
  179.         2: result_:=Td_Set_Parameters(Ihandle,17,Fintput_or_output,nil,0,0);
  180.         3: result_:=Td_Set_Parameters(Ihandle,18,Fintput_or_output,nil,0,0);
  181.       end;
  182.  
  183.       if result_<>0 then
  184.       begin
  185.         //Nepodařilo se nastavit porty jako výstupní.
  186.         resolve_error(result_);
  187.         Exit;
  188.       end;
  189.     end;
  190.   end;
  191.  
  192.   is_running:=true;
  193.   GetMem(buffer,count_of_channels*sizeof(DWord));
  194. end;
  195.  
  196. procedure TExample.SetValuesClick(Sender: TObject);
  197. var
  198.   i:integer;
  199.   result_:DWord;
  200.   sample:DWord;
  201. begin
  202.   if(is_running) and (Fintput_or_output=1) then
  203.   begin
  204.     for i:=0 to count_of_channels-1 do
  205.     begin
  206.       try
  207.         result_:=StrToInt(StringGrid2.Cells[i,1]);
  208.       Except
  209.         Application.MessageBox('V tabulce musíte nastavit taková čísla jaké mohou digitální výstupy nastavit [0..255].','Chyba!',MB_ICONERROR);
  210.         Exit;
  211.       end;
  212.  
  213.       if(result_<0) OR (result_>255) then
  214.       begin
  215.         Application.MessageBox('V tabulce musíte nastavit taková čísla jaké mohou digitální výstupy nastavit [0..255].','Chyba!',MB_ICONERROR);
  216.         Exit;
  217.       end;
  218.  
  219.       sample:=result_; //Mam platne cislo
  220.  
  221.       Td_Set_DO(Ihandle,i,@sample,1);  //Tak ho dodeslu
  222.     end;
  223.   end
  224.   else
  225.     ShowMessage('Karta není nakonfigurována, nebo není nastavena ve správném režimu.');
  226. end;
  227.  
  228. //Procedura, ktera vytvori message box s chybovou zprávou
  229. procedure TExample.resolve_error(n_error:DWord);
  230. var
  231.   function_:array[0..1000] of char;
  232.   error_:array[0..1000] of char;
  233. begin
  234.   Td_Get_ErrorMessage(n_error,PChar(@function_),PChar(@error_));
  235.   Application.MessageBox(PChar(@error_),PChar(@function_),MB_ICONERROR);
  236. end;
  237.  
  238. end.
  239.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement