Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Basic syntax for DAQ under MS6.0 for Visual C++.

I'm still struggling with the syntax under MS6.0. I'm using the CW tools so I have to use variants. When I say:

CCWAOPoint m_analog;
m_analog.SingleWrite(5.0);

I get the error message (SingleWrite takes more then 1 parameter.)

So I try:
COleVariant vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

m_analog.SingleWrite(5.0,vOptional);

and I get

"Can't convert parameter 1 from const double to const struct tagVARIANT &. How do I create a struct tagVARIANT &? I just want to output 5.0V to my NI PXI card.

What really is a variant? I see that Microsoft has some ideas about what they are and that NI has a class to try and handle them better but I don't appreciate what they are for. Do I reall
y need to use a scale factor for my second parameter in SingleWrite? Any help would be great.

Grant
Grant M. Johnson
Project Engineer
LECO Corporation
0 Kudos
Message 1 of 5
(4,355 Views)
CNiVariant is designed to help you use VARIANTs easily within a Measurement Studio for C++ program. The following code compiles and has the same behavior as the equivalent Visual Basic call that takes only 5.0 as a parameter.

m_analog.SingleWrite(CNiVariant(5.0), CNiVariant(true));

The VARIANT type is a structure that contains (more or less) a union of many types and an enum value that indicates which type is in the union. As such, it is not the easiest thing to use from C++. Its use is further complicated by the fact that the union can contain SAFEARRAY and BSTR types which you also need to convert into the appropriate C++ type. For more detail on VARIANTs, search in MSDN for "Article 4. The Ultimate Data Type".

CNiVariant handles all of the conversi
ons for you. You can construct them in-place as the above code demonstrates. You also can use them as output parameters. For example, you can use CNiVariant to get a waveform from the analog input control's AcquireData function.

Check out MeasurementStudio\VC\Examples\Apps\SpectrumAnalyzer to see how this kind of thing is done.

Regarding the second parameter, I think the easiest thing to do would be to look at the Visual Basic help to determine what you need to pass for each parameter. You can access the help directly from the dialog editor. Right-click on the CWAOPoint control to pull up the context menu and go to CWAOPoint Control (National Instruments) Object > Help. This will bring up MSRef. At the top of the page, click Visual Basic Reference to get to the page for CWAOPoint. When you go to the SingleWrite method, you'll see that the second parameter is Optional. You have to pass this explicitely to the C++ wrapper. The help shows you what to pass if you want the default beh
avior. Alternatively, you cand construct and pass a COleVariant with type VT_ERROR and value DISP_E_PARAMNOTFOUND as you did.


David Rohacek
National Instruments
Message 2 of 5
(4,355 Views)
Thanks David. This is just what I needed. It sure seems like your solution would be a good article to add to your resource library or somewhere to refer others to. Sure seemed like a great summary to me. Thanks again.

Grant
Grant M. Johnson
Project Engineer
LECO Corporation
0 Kudos
Message 4 of 5
(4,355 Views)
You aren't out of your league, it's just some starting pains in programming an ActiveX control. The SingleWrite function is prototyped as:

CCWAOPoint::SingleWrite(VARIANT value, VARIANT Scaled);

The reason you are getting an error is that the 5.0 is not a variant, it's a double value. Pass it as CNiVariant(5.0) and it will work.

The reason this value is a variant in Visual C++ is that it could be an array, double, or int depending on how you use the function. Visual C++ will change the variants to basic C data types where it can, but it can't here so it leaves it as a variant. We give you the CNiVariant class to easily deal with variants in Visual C++. It has constructors for all the basic data types and you can easily put our Vector and
Matrix class in them as well. Just to clarify, here are your steps for creating a DAQ app with Visual C++.

1) Create the application and set it up with the MS AppWizard.

2) Add the DAQ controls you need (CWAI, CWAO, CWAOPoint, etc.)

3) Usually, you will add a DAQ control to your dialog resource. This allows you to use the property pages to setup the DAQ settings (real time saver).

4) Go to ClassWizard and declare a member variable for your DAQ control (i.e. m_AOPoint).

5) Program with the control. In your case,

COleVariant vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

m_AOPoint.SingleWrite(CNiVariant(5.0), vOptional);


There is documentation on all of the functions and properties of the DAQ ActiveX controls in Visual C++ under Tools->Measurement Studio Reference. The documentation is for Visual Basic, but once you get used to variants, the VB help is usually enough.

Best Regards,

Chris Matthews
National Instruments
Message 3 of 5
(4,355 Views)
Thanks Chris. It works great. I especially appreciate the procedure breakdown.

Grant
Grant M. Johnson
Project Engineer
LECO Corporation
0 Kudos
Message 5 of 5
(4,355 Views)