unit HVmon;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  THVMonForm = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Vset2: TEdit;
    Ch2: TEdit;
    LabelCh: TLabel;
    ButtonCh: TButton;
    Label5: TLabel;
    Vmeas: TEdit;
    Timer1: TTimer;
    Label4: TLabel;
    Ramp2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ButtonChClick(Sender: TObject);
    procedure Vset2KeyPress(Sender: TObject; var Key: Char);
    procedure Ramp2KeyPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  HVMonForm: THVMonForm;

const
    sddgdll='c:\ddg_dll\ddg.dll';
    sdll='c:\msadll32\msadll32.dll';
    icandll='c:\winnt\system32\isegcanv.dll';
type
   fn=pchar;
   pfn=^fn;
   ID=word;
   pID=^ID;
   Stat=byte;
   pStat=^Stat;
   Typ=byte;
   pTyp=^Typ;
   TStamp= pchar;
   pTStamp=^TStamp;
   Buff=array[0..9] of byte;
   pBuff=^Buff;
var
   a : array[0..9] of byte;
   i, j : integer;
   fname : pfn;
   fna : pchar;
   hhhstr : string;
   idv : pID;
   statv : pStat;
   typv : pTyp;

   w1 : word;
   buffv : pBuff;

   CANClinent : pchar;

   Time : pchar;

const ClientString: array[0..128] of Char = 'TestCan'#0;
const TimeString: array[0..20] of Char = '00:00:00.000'#0;

var P: PChar;


procedure ReInitCanSystem(CANline : word);cdecl;external icandll;
function InitCanPCI(CANline : word; BitRate : word; Handle : word; Cname : pchar) : BOOLEAN; cdecl; external icandll;
function CanLogOnOff(idv : pID; statv : pStat; typv : pTyp; time : pchar) : BOOLEAN; cdecl; external icandll;
function Write2Can(idd : word; buffv : pBuff;  time : pchar) : BOOLEAN; cdecl; external icandll;
function WriteReadfCan(idd : word; buffv : pBuff;  time : pchar) : BOOLEAN; cdecl; external icandll;
function ReadfCan(idv : pID; buffv : pBuff;  time : pchar) : BOOLEAN; cdecl; external icandll;
procedure SetCanTimeOut(tout : word);  cdecl; external icandll;

implementation

{$R *.DFM}

procedure THVMonForm.Button1Click(Sender: TObject);
begin
  close;
end;

procedure THVMonForm.FormActivate(Sender: TObject);
VAR  S: String;
begin
  ReInitCanSystem(1);

  P:=@ClientString;
  if (InitCanPCI(1, 250, Application.Handle, P)) THEN
  BEGIN
     SetCanTimeOut(500);
     messagedlg(P,mtinformation,[mbOK],0);
  END
  ELSE
     messagedlg(P,mtinformation,[mbOK],0);

  for i:=0 to 32000 do
     for j:=0 to 32000 do;

  idv^:=0;
  P:=@TimeString;       //Time:='00:00:00.000';
  IF (CanLogOnOff(idv, statv, typv, P)) THEN
  BEGIN
    w1:=idv^ AND $7Fe;
    a[0]:=2;
    a[1]:=$d8;
    a[2]:=1;
    buffv:=@a;
    TimeString:='00:00:00.000'#0;
    P:=@TimeString;
    Write2Can(w1, buffv, P);

    STR((idv^ AND $1F8) SHR 3, S);
    Label2.Caption:=S;

    repeat idv^:=0; until (NOT(ReadfCan(idv, buffv, P)));

    Timer1.Enabled:=True;
  END;

end;

procedure THVMonForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    w1:=idv^ AND $7Fe;
    a[0]:=2;
    a[1]:=$d8;
    a[2]:=0;
    buffv:=@a;
    TimeString:='00:00:00.000'#0;
    P:=@TimeString;
    Write2Can(w1, buffv, P);

    Sleep(10);

    ReInitCanSystem(1);
end;

procedure THVMonForm.FormCreate(Sender: TObject);
begin
  new(idv);
  new(statv);
  new(typv);
  new(buffv);
end;

procedure THVMonForm.Timer1Timer(Sender: TObject);
VAR S : String;
    t : Buff;
    ch: byte;
    Code: Integer;
begin
   idv^:=w1;
   w1:=idv^ OR 1;
   a[0]:=1;
   Val(Ch2.Text, ch, Code);
   a[1]:=$80 OR ch;
   buffv:=@a;
   TimeString:='00:00:00.000'#0;
   P:=@TimeString;
   if (WriteReadfCan(w1, buffv, P)) THEN
   BEGIN
      t:=buffv^;
      Str( (t[2] SHL 8) + t[3], S);
      Vmeas.Text:=S;
   END
   else
      Vmeas.Text:='fault';
end;

procedure THVMonForm.ButtonChClick(Sender: TObject);
VAR S : String;
    t : Buff;
    ch: byte;
    Code: Integer;
begin
   a[0]:=3;
   a[1]:=$CC;
   Val(Ch2.Text, ch, Code);
   if  ButtonCh.Caption='ON' then
   begin
      if ch>7 then
      begin
         a[2]:=1 SHL ch;
         a[3]:=0;
      end
      else
      begin
         a[2]:=0;
         a[3]:=1 SHL ch;
      end;
   end
   else
   begin
      begin
         a[2]:=0;
         a[3]:=0;
      end
   end;

   buffv:=@a;
   TimeString:='00:00:00.000'#0;
   P:=@TimeString;
   w1:=w1 AND $3F8;
   Write2Can(w1, buffv, P);

   Sleep(10);

   idv^:=w1;
   w1:=idv^ OR 1;
   a[0]:=1;
   a[1]:=$CC;
   buffv:=@a;
   TimeString:='00:00:00.000'#0;
   P:=@TimeString;
   if (WriteReadfCan(w1, buffv, P)) THEN
   BEGIN
      t:=buffv^;
      if (((t[2] SHL 8) + t[3]) AND (1 SHL ch) ) >0 then
         ButtonCh.Caption:='OFF'
      else
         ButtonCh.Caption:='ON';
   END
   else
      Vmeas.Text:='fault';
end;

procedure THVMonForm.Vset2KeyPress(Sender: TObject; var Key: Char);
VAR S : String;
    t : Buff;
    ch: byte;
    Vset: word;
    Code: Integer;
begin
     if ORD(Key)=13 then
     begin
       a[0]:=3;
       Val(Ch2.Text, ch, Code);
       Val(Vset2.Text, Vset, Code);
       a[1]:=$A0 OR ch;
       a[2]:=Vset SHR 8;
       a[3]:=Vset AND $ff;
       buffv:=@a;
       TimeString:='00:00:00.000'#0;
       P:=@TimeString;
       w1:=w1 AND $3F8;
       Write2Can(w1, buffv, P);

       Sleep(10);

       idv^:=w1;
       w1:=idv^ OR 1;
       a[0]:=1;
       a[1]:=$a0 OR ch;
       buffv:=@a;
       TimeString:='00:00:00.000'#0;
       P:=@TimeString;
       if (WriteReadfCan(w1, buffv, P)) THEN
       BEGIN
            t:=buffv^;
            Str((t[2] SHL 8) + t[3], S);
            Vset2.Text:=S;
       END
       else
            Vset2.Text:='fault';
       end;
end;

procedure THVMonForm.Ramp2KeyPress(Sender: TObject; var Key: Char);
VAR S : String;
    t : Buff;
    ch: byte;
    Ramp: word;
    Code: Integer;
begin
     if ORD(Key)=13 then
     begin
       a[0]:=3;
       Val(Ramp2.Text, Ramp, Code);
       a[1]:=$d0;
       a[2]:=Ramp SHR 8;
       a[3]:=Ramp AND $ff;
       buffv:=@a;
       TimeString:='00:00:00.000'#0;
       P:=@TimeString;
       w1:=w1 AND $3F8;
       Write2Can(w1, buffv, P);

       Sleep(10);

       idv^:=w1;
       w1:=idv^ OR 1;
       a[0]:=1;
       a[1]:=$d0;
       buffv:=@a;
       TimeString:='00:00:00.000'#0;
       P:=@TimeString;
       if (WriteReadfCan(w1, buffv, P)) THEN
       BEGIN
            t:=buffv^;
            Str((t[2] SHL 8) + t[3], S);
            Ramp2.Text:=S;
       END
       else
            Ramp2.Text:='fault';
       end;
end;

procedure THVMonForm.FormDestroy(Sender: TObject);
begin
     Dispose(idv);
     Dispose(statv);
     Dispose(typv);
     {Dispose(buffv);}
end;

end.
