Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

DAQmxRegisterEveryNSamplesEvent callback not called in delphi

Hello there!

First off: I know that using DAQmx with Delphi is not officially supported by NI. But I need to get it working anyway!

I'm stuck with the callback calling conventions:

I tried:

function EveryNCallback(TaskHandle : LongWord;everyNsamplesEventType : i32; nSamples : u32;callbackData : Pointer) : i32;cdecl;
begin
  ..
  DAQmxReadBinaryI16(TaskHandle,Device.NumPoints,1,DAQmx_Val_GroupByChannel,PSmallint(Chunk.Data),Chunk.DataSize,@Chunk.DataValid,nil);
  ..
  ShowMessage('DeviceNCallBack'+IntToStr(Device.Number),10,MD_DAQ);
  result := 0;
end;

function DoneCallback(TaskHandle : LongWord;status : i32;callbackData : Pointer) : i32;cdecl;
..

function SignalEvent(TaskHandle : LongWord;signalID : i32;callbackData : Pointer) : i32;cdecl;
begin
  ShowMessage('SignalEvent!!',10,MD_DAQ);
  result := 0;
end;

procedure TDAQ_Device_E_series.Initialize(const OnStartCapture,OnStopCapture : TDAQ_Device_Event);
begin
  DAQmxCreateTask('',@TaskHandle);
  ..
  DAQmxCreateAIVoltageChan(TaskHandle,PAnsiChar(ChannelName),'',DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_VoltageUnits2_Volts,nil);

  DAQmxSetBufInputBufSize(TaskHandle,2*NumPoints);
  DAQmxCfgSampClkTiming(TaskHandle,nil,10,DAQmx_Val_Rising,DAQmx_Val_AcquisitionType_ContSamps,NumPoints);

  // For callback testing..
  DAQmxRegisterSignalEvent(TaskHandle,DAQmx_Val_Signal2_SampleClock,0,Integer(@SignalEvent),nil);

  DAQmxRegisterEveryNSamplesEvent(TaskHandle,DAQmx_Val_Acquired_Into_Buffer,NumPoints,0,Integer(@EveryNCallback),self);
  DAQmxRegisterDoneEvent(TaskHandle,0,Integer(@DoneCallback),self);
  ..
end;

I removed all error handling and internal stuff for clarity..

DAQmxStartTask is called later on.. But never one of my callbacks is executed!

I tried declaring them as stdcall instead of cdecl. No use.

Thx a lot in advance for any clues!!

Johannes Munk

Additional Info:
I'm using the mx8.5 header files provided here: http://digital.ni.com/public.nsf/websearch/A6715AA42405ACD786256F0A00633B8F?OpenDocument
I replaced "P<not supported>" by "PLongBool" and declared : type PLongBool = ^TLongBool;
0 Kudos
Message 1 of 5
(5,758 Views)
Hello there!

I solved the above problem by fixing the nidaqmx header as follows:

type
  TTaskHandle = Longint;PLongBool = ^LongBool;i32 = Longint;pi32 = ^i32;f64 = double;pf64 = ^f64;u32 = LongWord;pu32 = ^u32;
 
  //int32 (CVICALLBACK *DAQmxEveryNSamplesEventCallbackPtr)(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
  TDAQmxEveryNSamplesEventCallback = function(task : TTaskHandle;everyNsamplesEventType : i32;nSamples : u32;callbackData : Pointer) : i32;
  PDAQmxEveryNSamplesEventCallback = ^TDAQmxEveryNSamplesEventCallback;
  //int32 (CVICALLBACK *DAQmxDoneEventCallbackPtr)(TaskHandle taskHandle, int32 status, void *callbackData);
  TDAQmxDoneEventCallback = function(task : TTaskHandle;status : i32;callbackData : Pointer) : i32;
  PDAQmxDoneEventCallback = ^TDAQmxDoneEventCallback;
  //int32 (CVICALLBACK *DAQmxSignalEventCallbackPtr)(TaskHandle taskHandle, int32 signalID, void *callbackData);
  TDAQmxSignalEventCallback = function(task : TTaskHandle;signalID : i32;callbackData : Pointer) : i32;
  PDAQmxSignalEventCallback = ^TDAQmxSignalEventCallback;

function DAQmxRegisterEveryNSamplesEvent(taskHandle:TTaskHandle; everyNsamplesEventType:DAQmxEveryNSamplesEventType; nSamples:i32; options:i32; callbackFunction:PDAQmxEveryNSamplesEventCallback; callbackData:Pointer):i32; stdcall;
function DAQmxRegisterDoneEvent(taskHandle:TTaskHandle; options:i32; callbackFunction : PDAQmxDoneEventCallback; callbackData:Pointer):i32; stdcall;
function DAQmxRegisterSignalEvent(taskHandle:TTaskHandle; signalID:DAQmxSignal2; options:i32; callbackFunction : PDAQmxSignalEventCallback;callbackData:Pointer):i32; stdcall;

implementation

function DAQmxRegisterEveryNSamplesEvent; external 'nicaiu.dll';
function DAQmxRegisterDoneEvent; external 'nicaiu.dll';
function DAQmxRegisterSignalEvent; external 'nicaiu.dll';

That way, the c++ functions and not the VB6 versions are used..

function EveryNCallback(TaskHandle : TTaskHandle;everyNsamplesEventType : i32; nSamples : u32;callbackData : Pointer) : i32;cdecl;
begin
end;

DAQmxRegisterEveryNSamplesEvent(TaskHandle,DAQmx_Val_Acquired_Into_Buffer,NumPoints,1,@EveryNCallback,self);

Hope it helps someone!

JM

Message Edited by JohannesMunk on 10-25-2007 07:13 AM

0 Kudos
Message 2 of 5
(5,724 Views)

hi JM,

         Would you please give a complete code about "CallBack not called"in attachment or send to my EMail:fumaozhao@sina.com?

0 Kudos
Message 3 of 5
(5,581 Views)
nidaqmx.pas is porvided by JM.
 
unit DAQ_test;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,nidaqmx,NIDAQmxCAPI_TLB;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    TaskHandle:TTaskHandle;
  end;
var
  Form1: TForm1;
  callbackResult:integer;
  Numpoints:integer;
  data:array[0..999] of f64;
implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
function EveryNCallback(TaskHandle : LongWord;everyNsamplesEventType : i32; nSamples : u32;callbackData : Pointer) : i32;cdecl;
var
 DataSize,DataValid:integer;
begin
  DAQmxReadAnalogF64(TaskHandle,Numpoints,5,DAQmx_Val_GroupByScanNumber,@Data,DataSize,@DataValid,self);
arraySizeInSamps:i32; sampsPerChanRead:pi32; reserved:Pointer):i32; stdcall;
  callbackResult := callbackResult+1;
  if callbackResult mod 5 =0 then
   label1.Caption:=inttostr(CallBackresult);
end;
function DoneCallback(TaskHandle : LongWord;status : i32;callbackData : Pointer) : i32;cdecl;
begin
end;
function SignalEvent(TaskHandle : LongWord;signalID : i32;callbackData : Pointer) : i32;cdecl;
begin
  ShowMessage('SignalEvent!!');
  result := 0;
end;
var
 NumPoints:integer;
begin
  DAQmxCreateTask('',@TaskHandle);
//  ..
  DAQmxCreateAIVoltageChan(TaskHandle,('Dev1/ai0'),'',DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_VoltageUnits2_Volts,nil);
  DAQmxSetBufInputBufSize(TaskHandle,2*NumPoints);
  DAQmxCfgSampClkTiming(TaskHandle,'',10000,DAQmx_Val_Rising,DAQmx_Val_AcquisitionType_ContSamps,NumPoints);
  DAQmxRegisterEveryNSamplesEvent(TaskHandle,DAQmx_Val_Acquired_Into_Buffer,NumPoints,0,@EveryNCallback,nil);
  DAQmxRegisterDoneEvent(TaskHandle,0,(@DoneCallback),nil);
  DAQmxStartTask(taskHandle);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
 i:integer;
begin
  callbackresult:=0;  NumPoints:=1000;
  for i:=0 to Numpoints-1 do data[i]:=0;
end;
end.


帖子被haidian在01-01-2008 08:57 PM时编辑过了

帖子被haidian在01-01-2008 08:57 PM时编辑过了
0 Kudos
Message 4 of 5
(5,572 Views)
hi the code you provide still does not work.have you run the code?
0 Kudos
Message 5 of 5
(4,662 Views)