Source file: /~heha/messtech/mio_dde.zip/daqdac.pas

library daqdac; {$N+,E-} {MATLAB setzt ohnehin Koprozessor voraus}
{$D Matlab-MEX: Ansteuerung NI-DAQ-Karte (h#s 12/99)}
uses pasmex,winprocs,wintypes,wdaq;

const
 ConfigExeName='c:\nidaqwin\wdaqconf.exe';
var
 Configured,Bipolar: array[0..1] of Boolean;
 UpdateModus: array[0..1] of Byte;

var
 CatchBuf: TCatchBuf;

procedure Error(s: PChar);
 begin
  mexVPrintf('DAQDAC.DLL: FEHLER: %s'#10,s,4);
  Throw(CatchBuf,-1);
 end;

procedure Warning(s: PChar);
 begin
  mexVPrintf('DAQDAC.DLL: WARNUNG: %s'#10,s,4);
 end;

function R2I(pm: PMatrix; min,max: Integer; var i: Integer):Boolean;
 var
  z: Double;
 begin
  R2I:=false;
  if not mxIsNumeric(pm) then exit;
  z:=mxGetScalar(pm);
  if (z<min) or (z>max) then exit;
  i:=Round(z);
  R2I:=true;
 end;

function mxGetMxN(pm:PMatrix):LongInt;
 begin
  mxGetMxN:=mxGetM(pm)*mxGetN(pm);
 end;

procedure mexFunction(nlhs:Integer; var plhs:TMatArr;
  nrhs:Integer; const prhs:TMatArr); export;
 var
  chan: Integer;
  op: Bool;		{TRUE wenn BIpolar}
  um: Integer;		{Update Mode}
  x,xa,xe: Double;
  xp: HPDouble;
  e: Integer;
  l: LongInt;
  s: array[0..255] of Char;
 begin
  if Catch(CatchBuf)=0
  then case nrhs of
   3,4: begin
    asm int 3 end;
    if not R2I(prhs[0],0,1,chan)
    then Error('Kanalnummer ungültig!');
    l:=mxGetMxN(prhs[1]);
    if l>1 then begin
     xp:=mxGetPr(prhs[1]);
     xa:=100; xe:=-100;
     for l:=l downto 1 do begin
      if xa>xp^ then xa:=xp^;	{Minimum suchen}
      if xe<xp^ then xe:=xp^;	{Maximum suchen}
      IncHP(PChar(xp),sizeof(Double));
     end;
     op:=xa<0;			{BIpolar wenn Minimum negativ}
     if (xa<-10) or (xe>10) then Warning('Bereich zu groß (max. -10..10V)');
    end else if not R2I(prhs[1],0,1,Integer(op))
    then Error('Bereich ungültig!');
	{Ausgabepolarit„t op: 0=unipolar, 1=bipolar}
    Bipolar[chan]:=op;
    if not R2I(prhs[2],0,2,um)
    then Error('UpdateModus ungültig!');
	{Umsetzung um: 0=sofort, 1=per DAQDAC(0), 2=per EXDACUPDATE}
    UpdateModus[chan]:=um;
    if nrhs=3
    then AO_Configure(1,chan,not op,0,10.0,um)
    else AO_Configure(1,chan,not op,1,mxGetScalar(prhs[3]),um);
    Configured[chan]:=true;
   end;
   2: begin
    if not R2I(prhs[0],0,1,chan)
    then Error('Kanalnummer ungültig!');
    if not Configured[chan] then begin
     case MessageBox(0,
       'Der Befehl DAQDAC wurde zur Ausgabe ohne vorherige '+
       'Konfiguration gerufen. Jetzt konfigurieren?',
       'DAQDAC',
       MB_TaskModal or MB_YesNoCancel or MB_IconQuestion) of
      IDYes: WinExec(ConfigExeName,SW_Show);
      IDCancel: Error('Abbruch!');
     end;
     FillChar(Configured,sizeof(Configured),true);
    end;
    x:=mxGetScalar(prhs[1]);
    e:=AO_VWrite(1,chan,x);
    if e=-71 then begin	{outOfRangeErr}
     if x>5
     then if bipolar[chan] then um:= 32767 else um:=Integer(65535)
     else if bipolar[chan] then um:=-32768 else um:=0;
     e:=AO_Write(1,chan,um);
     if e>=0 then Warning('Ausgabe in Begrenzung')
    end;
    if e<0 then begin
     wvsprintf(s,'Ausgabe versagt (Code %d)!',e);
     Error(s);
    end;
    if e>0 then begin
     wvsprintf(s,'Ausgabe-Problem (Code %d)!',e);
     Warning(s);
    end;
   end;
   1: begin
    if not R2I(prhs[0],1,1,chan)
    then Error('Pflichtparameter nicht Eins!');
    AO_Update(1);
   end;
   else mexVPrintf(
    '1 Parameter: <Parameter 1> (Sample-Ausgabe-Trigger)'#10+
    '2 Parameter: <Kanal(0,1)> und <Wert>'#10+
    '3..4 Par.SETUP: <Kanal(0,1)> <Bereich 0=[0 10]V, 1=[-10 10]V>'#10+
    ' <UpdateMode: 0=sofort, 1=per DAQDAC(0) 2=per EXDACUPDATE-Pin>'#10+
    ' [<externe Referenzspannung in Volt>]'#10+
    'Mehr Info: help daq'#10,chan,0);
  end;
 end;

function GetUpdateModus(chan:Integer):Integer; export;
 begin
  GetUpdateModus:=UpdateModus[chan];
 end;

exports
 set_entry_point index 2,
 mexFunction 	 index 3,
 mexAtExitFcn	 index 4,
 GetUpdateModus  index 5;	{Seiteneinstieg fr DAQWAVO}

begin
 FillChar(Configured,sizeof(Configured),false);
 FillChar(UpdateModus,sizeof(UpdateModus),false);
end.
Detected encoding: UTF-80