Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

daqmx and threading

This might be a bit of a simple question, but I'm totally new to NI software.

I'm trying to create a visual basic program (vb2005) to aquire data from a USB DAQ for feedback purposes.  I created a task for my device in MAX, and then imported it into vb with the measurment studio (measurment studio v8.1, DAQmx v8.5) template wizzard. I've put the call events for getting data into a seperate thread, but all it does is kick up errors. It won't let me call the ReadAsync function for a thread other than the form thread.  I would use LabView to do all this, but I've got about 500 lines of serial comms code for the other half of this program already written in VB and I'm laith to re-write all that in LabView too. Any tips greatly appreciated...

As a speperate issue, I think there's a problem with my measurment studio help files, in aobut 70% of the help pages, the example code is missing! It keeps talking about the example code below, only there is no code...which is really not helpng my problems!

Thank you for any help you can give me!
EDF.
0 Kudos
Message 1 of 5
(4,475 Views)

Hi EDF

Can you pls give us a step by step guide of what you are trying to do? I will try to replicate the problem.

Thank you,

KostasB

Applications Engineering

National Instruments

 

0 Kudos
Message 2 of 5
(4,461 Views)
Hi KostasB, thank you for the reply.

The program is to control a thermal enclosure I have built. The half I have already written controls a heat-pump through the RS-232 port.

I have a USB DAQ with temperature sensors atached. The program is supposed to gather data from the DAQ (the number of channels sampled and aquisition rate being defined in the MAX task), average the samples to an seperate aquisition frequency defined form within the program (ie 100Hz sampling of the DAQ averages out to 0.2Hz sampling at the program level). The averaged date is then both displayed in the GUI and logged to disk. Further, the values are used as feedback parameters to the heat-pump. Becasue of the complexity of the program, it has to be threaded. The sampling of the DAQ, logging data and serial comms is just too much for a single form level thread and the UI is largley unresponsive.

At the moment the DAQ side of the project exists as a seperate vbproject (I'll combine the two together once I have them both working, and then code the feedback system). It's only at a simple level where I have merley used the background worker to aquire data.

The MAX task is configured for N samples, taking 500 samples at a rate of 100Hz. Clicking the 'aquire' sets a boolean variable 'isrunning' to true, this is passed to a public sub in a seperate class and set up as:

        Dim input As New ThreadInput(isrunning)
        BackgroundWorker1.RunWorkerAsync(input)

Under DoWork even I have:

        Dim input As ThreadInput = CType(e.Argument, ThreadInput)

        'Start Process
        While isrunning = True
            Try
                DaqTaskComponent1.ReadAsync()
            'Catch ex As NationalInstruments.DAQmx.DaqException
                'MessageBox.Show(ex.Message, "DAQ Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                'btn_read.Enabled = True
            End Try
        End While

(The error handing was removed for thread safety, i'll set up an Invoke Method on it later on)
Ok, there are a couple of problems with this that I can see:

1. It complains that 'ReadAsync' is not a member of DaqTaskComponent1 (which it doesnt if I just call it form the form level thread, so an import is missing I presume)
2. I'm pretty sure that my While loop will not function as I want, ie once one set of N smaples is taken, it'll go get another and loop gathering sets of smaples (which I can then average and use) untill I tell uit otherwise. Trouble is, I can't think of another method to acheive this...


Bit of a mamoth post I know, but I wanted to get all the info across, and in all likleyhood I've done somethign stupid which is stopping it working, so i'll appologise for that in advance!

Thank you for any help you can give me!
EDF.
0 Kudos
Message 3 of 5
(4,456 Views)

Hi edf,

As far as the missing DAQ code snippets, this was fixed in DAQmx 8.6 which is released. 

With regards to your main question, based on your comments about using a while loop for your data acquisition, you need to be doing continuous sampling instead of finite sampling.  Since you are using the DAQ assistant, this can be easily changed. Open up the DAQ Assistant by double-clicking on your xxx.mxb file located in the Solution Explorer. Then modify the Acquisition Mode item to be Continuous Samples

When you use continuous sampling with the DAQ component, our DAQ component is firing off a worker thread to do the actual data acquisition.  This is perfect in your case since you want to free up your UI and make it more responsive.  We even go a step further in that case and marshal the data back to the UI thread so you can plot the data to your UI components. 

Now you didn't mention if you used our DAQ Component to generate a UI for you. If you are just starting out, the resultant code should look something like the attached "Generated Code.jpg" snapshot. If you notice, the code is calling StartRead method which starts the continuous input acquisition. The resultant acquisition will be done on a worker thread. Then we hook up to the DataReady event and grab the data when it’s ready via the GetData method. Now we are back on the main UI thread and you notice we can plot the data to the graph. This is where you can do your own plotting to your graphs or spawn a new thread to do some analysis or data logging.  This is the recommended approach.

Now if you already have some code written and don't want to generate the UI, you basically need to re-write what our UI code generated with respect to starting the reads and hooking up to the appropriate events.  For example, you would need the StartRead and StopRead calls and you would need to hook up to the DataReady event. In your event handler for the DataReady event, you could grab the data, and do whatever you want. See the snapshot, "Manual Typing.jpg".

You can learn how threading, reading and writing with DAQ works in the NI Measurement Studio Help under the topics "Using the Measurement Studio DAQmx .NET Library". That has sections on reading and writing as well as a section entitled "Events, Callbacks, and Thread Safety in Measurement Studio .NET Class Libraries"

Hope this helps!

Best Regards,



Message Edited by Jonathan N on 11-20-2007 02:34 PM
Jonathan N.
National Instruments
Download All
0 Kudos
Message 4 of 5
(4,446 Views)
Hi Jonathan,

That was really helpfull, thank you! I've only just upgrqded from vb6 to vb2005 (get with the times, I know!), so all this new fangled threading and .NET language commands are casuing me some problems, not helped by the fact that I'm totally new to NI stuff too, but i'm gettign there now. And especially thanks for letting me know the help bug is fixed in DAQmx 8.6, i'm downloading it just now.

Cheers,
EDF.
0 Kudos
Message 5 of 5
(4,412 Views)