Quelltext /~heha/messtech/mio_dde.zip/daqadc.pas

library daqadc; {$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: array[0..15] of Boolean;
 Gains: array[0..15] of Byte;
 CatchBuf: TCatchBuf;

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

procedure Warning(s: PChar);
 begin
  mexVPrintf('DAQADC.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 SetGain(chan:Integer; gain: Byte);
 begin
  if chan=-1 then FillChar(Gains,sizeof(Gains),gain)
  else Gains[chan]:=gain;
 end;

procedure mexFunction(nlhs:Integer; var plhs:TMatArr;
  nrhs:Integer; const prhs:TMatArr); export;
 const
  gainEnd: array[0..6] of Single=(10,5,2,1,0.5,0.2,0.1);
  gainVal: array[0..6] of Byte=(1,2,5,10,20,50,100);
 var
  chan: Integer;
  ip: Bool;
  im: Integer;
  gain: Integer;
  xa,xe: Double;
  xp: HPDouble;
  l: LongInt;
 begin
  if Catch(CatchBuf)=0
  then case nrhs of
   3: begin
    asm int 3 end;
    if not R2I(prhs[0],-1,15,chan)
    then Error('Kanalnummer ungültig!');
    gain:=1;
    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;
     ip:=xa<0;			{bipolar, wenn Minimum negativ}
     if (xa<-10) or (xe>10) then Warning('Bereich zu groß (max. -10..10V)');
     {optimale Verst„rkung ermitteln}
     xa:=abs(xa);
     if xe<xa then xe:=xa;	{vzl. Maximum in XE ermitteln}
     for im:=0 to 6 do begin
      if xe>gainEnd[im] then break;
      gain:=gainVal[im];
     end;
    end else if not R2I(prhs[1],0,1,Integer(ip))
    then Error('Bereich ungültig!');
    SetGain(chan,gain);		{Verst„rkung(en) setzen}
    if not R2I(prhs[2],0,2,im)	{im: 0=DIFF, 1=RSE, 2=NRSE}
    then Error('Pegel-Modus ungültig!');
    AI_Configure(1,chan,im,0,not ip,0);
    if chan=-1 then FillChar(Configured,sizeof(Configured),true)
    else Configured[chan]:=true;
   end;
   2,1: begin
    if not R2I(prhs[0],0,15,chan)
    then Error('Kanalnummer ungueltig!');
    if not Configured[chan] then begin
     case MessageBox(0,
       'Der Befehl DAQADC wurde zur Ausgabe ohne vorherige '+
       'Konfiguration gerufen. Jetzt konfigurieren?',
       'DAQADC',
       MB_TaskModal or MB_YesNoCancel or MB_IconQuestion) of
      IDYes: WinExec(ConfigExeName,SW_Show);
      IDCancel: Error('Abbruch!');
     end;
     FillChar(Configured,sizeof(Configured),true);
    end;
    gain:=Gains[chan];
    if (nrhs=2) and not (R2I(prhs[1],1,100,gain)
    and (gain in [1,2,5,10,20,50,100]))
    then Error('Verstärkung ungültig!');
    plhs[0]:=mxCreateFull(1,1,false);
    AI_VRead(1,chan,gain,mxGetPr(plhs[0])^);
   end;
   else mexVPrintf(
    '1 Parameter: <Kanal(0..15)> (Verstärkung ist 1 oder Setup-Vorgabe)'#10+
    '2 Parameter: <Kanal(0..15)> <Verstärkung(1,2,5,10,20,50,100)>'#10+
    '3 Par.SETUP: <Kanal(-1..15)> <Bereich 0=0..10V, 1=-10..10V ODER [min,max]>'#10+
    ' <Mode 0=DIFF, 1=RSE, 2=NRSE>    [min,max] in Volt setzt Verstärkung'#10+
    'Mehr Info: help daq'#10,chan,0)
  end;
 end;

function GetGain(chan:Integer):Integer; export;
 begin
  GetGain:=Gains[chan];
 end;

exports
 set_entry_point index 2,
 mexFunction 	 index 3,
 mexAtExitFcn	 index 4,
 GetGain	 index 5;	{Seiteneinstieg fr DAQWAVI}

begin
 FillChar(Configured,sizeof(Configured),false);
 FillChar(Gains,sizeof(Gains),1);
 SetGain(-1,1);
end.
Vorgefundene Kodierung: UTF-80