Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Multithreaded Visual C++ application for NI 6008 USB.

Hi,

  I am currently having a problem with reading two analog inputs from my NI 6008 device simultaenously in my visual C++ application. I currently have two threads, where each thread contains the create task, create channel and read functions for an input channel. However, when i run the application only one read function retrieves the correct values from the device and the other recieves completely random values (off to infinity).

Hope some can help.

Thanks.

James.

 

The below is an example of one of the threads, the other is almost the same.

//DC Thread
UINT DC_Work(LPVOID pParam)
{
    CDialog* dlgPtr1 = static_cast<CDialog*>(pParam);
   
    DAQmxCreateTask("DC_Task",&taskHandle_2);
    DAQmxCreateAIVoltageChan(taskHandle_2,chan_2,"DC_Channel",DAQmx_Val_Cfg_Default,min,max,DAQmx_Val_Volts,NULL);
    DAQmxStartTask(taskHandle_2);

    //Create File with filename Date.txt
    DWORD wmWritten;

    DC_File = CreateFile(DC_file,GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);    
    char str[10];
   
    //Data Variables
    float64     DC_data[bufferSize];

    while(runThread == true)
    {
        //Computes Yaw Value
        DAQmxReadAnalogF64(taskHandle_2,pointsToRead_1,timeout,DAQmx_Val_GroupByChannel, DC_data,samplesPerChan,&pointsRead_1,NULL);   
        double a = DC_data[0];
        int aa = (int)(a*1000);
        //Print Yaw Value to File
        sprintf_s(str,"%1.2f",a);
        WriteFile(DC_File,str,(DWORD)(4),&wmWritten,NULL);
        WriteFile(DC_File,"\r\n",(DWORD)(2),&wmWritten,NULL);
        //Send and sleep
        Sleep(100);
        ::SendMessage(dlgPtr1->GetSafeHwnd(), WM_DCMESSAGE, aa,0);
    }
   
    return 0;   

0 Kudos
Message 1 of 4
(3,287 Views)
You cannot have two separate tasks running at the same time using the same hardware resource. You should be getting an error. You can have a single task that acquires data from two channels or run the tasks sequentially.
0 Kudos
Message 2 of 4
(3,268 Views)

Thanks for your help. Although being new to the function of NI DAQmx, how would I do that? Say for example I had two different analog inputs into my device, and I would like to take one sample of them both at the same time then read the values of independently of course.

So I'd create one task and two Analog input voltage channels? Then call two Read functions? 

 

Thanks again.

 

 

0 Kudos
Message 3 of 4
(3,255 Views)

Ok it doesn't matter I worked it all out. My answer was deep in the C Help library.

Thanks for you help anyway!

0 Kudos
Message 4 of 4
(3,247 Views)