Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in acquiring voltage readings continuously using DLL

Dear friends since I had problems using NI API’s in Borland C++ Bulder I had to write a DLL in VC++ and then call it in Borland C++ builder.

 

Although it is working I’m only able to read 1 value at a time I want to read the values continuously.I don’t know weather I’m doing something wrong .


Name of the DLL is ni.dll and the function used is getdata() my device is USB-6009 I’m trying to read the analog inputs continuously.

Below is the attachment containing the source code of both the DLL and the Borland Program.Can somebody Please help me.

 

Message Edited by perk_bud on 01-12-2007 04:14 PM

Message Edited by perk_bud on 01-12-2007 04:16 PM

0 Kudos
Message 1 of 11
(4,328 Views)

DLL source -------->

#include <stdAfx.h>

#include <NIDAQmx.h>

#include <stdio.h>

 
BOOL APIENTRY DllMain( HANDLE hModule,

                       DWORD  ul_reason_for_call,

                       LPVOID lpReserved

                                                             )

{

    return TRUE;

}

 

 float64 getdata(void)

{

            int32       error=0,taskisrunning=1;

            TaskHandle  taskHandle=0;

            char        errBuff[2048]={'\0'};

            static int  totalRead=0;

            int32       read=0;

            float64     data[1000];

             /*********************************************/

            // DAQmx Configure Code

            /*********************************************/

           DAQmxCreateTask("",&taskHandle);

           DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));

            DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000);

 
            /*********************************************/

            // DAQmx Start Code

            /*********************************************/

           DAQmxStartTask(taskHandle);

             printf("Acquiring samples continuously. Press Enter to interrupt\n");

                         DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,1000,&read,NULL);

                        return data[0];

                      fflush(stdout);

             

}

Message Edited by perk_bud on 01-13-2007 09:13 AM

0 Kudos
Message 2 of 11
(4,304 Views)

Borland Source----------->

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

#include "stdio.h"

#pragma package(smart_init)

#pragma resource "*.dfm"

#define add_dll "ni.dll"

typedef float(CALLBACK* GETDATA)();

GETDATA getdata;

Tcancel *cancel;

HINSTANCE dll=NULL;

__fastcall Tcancel::Tcancel(TComponent* Owner)

        : TForm(Owner)

{

}

void __fastcall Tcancel::Button2Click(TObject *Sender)

{

  Close();

}


void __fastcall Tcancel::addClick(TObject *Sender)

{

dll = LoadLibrary(add_dll);

        if(dll==NULL)

        {

          char msg[255]={0};

          sprintf(msg,"The library : %s could not be loaded. Please make sure this file is in the C:\\windows\\system32\\ folder or in the forder this program is being run from.",add_dll);

          MessageBox(NULL,msg,"ERROR",MB_OK | MB_ICONERROR);

          Application->Terminate();

        }

            getdata=(GETDATA)GetProcAddress(dll,"getdata");

            float c;

while(1)

           {

              c=getdata();

               data->Text=FloatToStr(c);

            }

 }

Message Edited by perk_bud on 01-13-2007 09:14 AM

0 Kudos
Message 3 of 11
(4,303 Views)
The probelm is that the Borland application crashes.
0 Kudos
Message 4 of 11
(4,299 Views)
Now when i'm printing the error handle message using a message box in the DLL I get the following error

DAQmx Error : Attempted to read samples that are no longer available.The requested sample was previously available,but has since has been overwritten.

Increasing the buffer size,reading the data more frequently,or specifying a fixed number of samples to read instead of reading all available samples might correct the problem.
Property:  DAQmx_Read_RelativeTo
Corresponding Value:  DAQmx_Val_CurrReadPos

Property: DAQmx_Read_Offset
Corresponding Value:


Task Name: _unnamedTask<0>

Status Code: -200279

This problem occurs even when i am debugging step by step in VC++ leave alone Borland.

0 Kudos
Message 5 of 11
(4,294 Views)
I tried increasing the buffer size but but it did not help.
0 Kudos
Message 6 of 11
(4,288 Views)
I even tried incresing the rate of aquisition but no LUCK can somebody please help me out.
0 Kudos
Message 7 of 11
(4,288 Views)
Hello perk_bud,

The error you are seeing indicates that you are not reading the data out of the buffer fast enough.  When you start a DAQmx task with a call to the DAQmxStartTask function, the device begins acquiring data into a circular buffer in system memory.  In order to prevent the buffer overwrite error you are seeing, you need to read data out of that buffer in system memory and into your application's memory space with calls to the DAQmxReadAnalogF64 function.  Your program needs to call this function often enough to prevent a buffer overwrite error from occuring.  The reason the error occurs when you begin step-by-step debugging of your application is because you drastically slow down the rate that your program is executing and the buffer fills up and overwrites.  Some things you can try are:
  1. Reducing the sampling rate of your acquisition task to slow down the rate at which data is acquired into the buffer in system memory
  2. Call the DAQmxReadAnalogF64 function more often by making your program run faster, i.e. optimizing your code, closing background programs
  3. Increase the size of the buffer in system memory (generally this will only delay the problem from occuring)
  4. Increase the number of samples read with each call to the DAQmxReadAnalogF64 function
Really what your running into here is a limitation of the speed at which your code can execute and your system can process.  To make sure that your code is actually working properly, I would suggest drastically reducing the sampling rate for troubleshooting.

I hope this helps,

Travis G.
Applications Engineering
National Instruments
www.ni.com/support
0 Kudos
Message 8 of 11
(4,252 Views)
 

Hi perk_bud,

The error you are receiving is due to an overflow of the circular buffer of the device, and it is caused because data cannot be transferred fast enough from the device to PC memory.  A couple of options to try and overcome this overflow error are to increase the buffer size as you already attempted and also to decrease the sample rate.  When the error specifies "reading the data more frequently" this means reading the data already sampled into the buffer.  Increasing the sample rate actually speeds up the process of causing the buffer to overflow.  If there is any timing in your application that delays transfer of data from the device buffer to PC memory, this should be reduced as well.

There is a KnowledgeBase article here that addresses the -200279 error.  This KB is speaking in terms of LabVIEW, but the equivalent C call would be DAQmxGetReadAvailSampPerChan.  Information about this property can be found in the NI-DAQmx C Reference Help.

Regards,
Andrew W
National Instruments

 
0 Kudos
Message 9 of 11
(4,251 Views)
Thank You for your support Travis & Woost
Here is what I did ---it works for acquiring 1000 samples after which it gives the same error although previously it would crash at 100.


    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
    DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,500));
      DAQmxErrChk(DAQmxCfgInputBuffer (taskHandle, 100000));
    /*********************************************/
    // DAQmx Start Code
    /*********************************************/
    DAQmxErrChk (DAQmxStartTask(taskHandle));
    
    
    DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,-1,10.0,DAQmx_Val_GroupByScanNumber,data,100,&read,NULL));
    return data[0];
    fflush(stdout);

Decreasing the rate did help but  i dont know weather increasing the last parameter in DAQmxCfgSampClkTiming i think is also helping.


I have a few questions
1> how do i Increase the number of samples read with each call to the DAQmxReadAnalogF64 function.
2>Is the number of samples has anything to do with array size of data.
3> since right now i'm reading from only one channel in my case it's data[0] if i have configure a second channel will the value be stored @ data[1].

Message Edited by perk_bud on 01-16-2007 11:30 AM

0 Kudos
Message 10 of 11
(4,241 Views)