UltimaSerial How to use ActiveX in VC (Visual C++)
 

Windaq add-ons
Windaq Add-ons


 

The following is a step-by-step lesson on how to use ActiveX in Visual C++ (VC++). If you wish to use the free Visual C++ 2008 Express Edition, check this out

In this lesson, we will use Ultimaserial ActiveX to develop a data acquisition application with DATAQ's Starter kit. 

Specification of the sample program:

a) Acquire voltage signal between -10 to 10V via Channel 1 of DATAQ's data logger DI-194 at a rate of 240 Samples/second 

b) The data is streamed back to the PC via COM 1

c) The input signal is displayed on a scrolling waveform window

Here, we will create a  Windows dialog-based Visual C++ program to acquire data from the DI-194 via COM 1. The program will start to acquire data as soon as the it is started, you can push the Cancel button to exit the program.

Instructions

1) Start Visual C++, and follow File->New

2) In the dialogue box New, choose Projects Tab

3) Select MFC AppWizard (exe), Fill in the name "Ultima" in project name and hit OK

4) Select Dialogue-based for "What type of application would you like to create?", then hit Finish.

5) Hit OK to create the project files.

6) If you see a dialogue box template labeled as Ultima on your screen, you can proceed to Step 11)

7) From Workspace pane, select Resource

8) Click on the + sign in front of Ultima resources and you should see the folder labeled as Dialog

9) Click on the + sign in front of the folder Dialog, and you should see IDD_ULTIMA_DIALOG.

10) Double-click on IDD_ULTIMA_DIALOG and you will see the dialogue box template labeled as Ultima on your screen

11) Move the cursor to the dialog box template Ultima, and click the right button on your mouse, and follow Insert ActiveX Control ...

12) In several seconds, you will see a dialog box labeled as Insert ActiveX Control

13) Double click on DQChart Control in the list

14) Repeat 11-13) to insert UltimaSerial Control

15) Remove the OK button and the "TODO Place dialog controls here" text from the dialog box. Also, resize/re-arrange the objects on the dialog box if you like

16) Follow View->ClassWizard ... to bring up the MFC ClassWizard

17) From the Member Variables tab of the MFC ClassWizard, Double-click on IDC_DQCHARTCTRL1, and the Wizard will ask if it is OK to generate a C++ wrapper class for it

18) Select OK to create the wrapper class for DQChart Control. When the Add Member Variable dialog box comes up, set the member variable name to m_dqchart

19) Repeat 16-18) to add a member variable m_ultima for IDC_ULTIMASERIALCTRL1

20) Hit OK to exit MFC ClassWizard

21) On the dialog box template Ultima, right-click on UltimaSerial control and follow Events...

22) You will see a dialog box labeled as New Windows Message and Event Handlers for CUltimaDlg.

23) Double-click on NewData under New Windows messages/events list, then hit OK to use the default name

24) Hit OK to exit the dialog box New Windows Message and Event Handlers for class CUltimaDlg

25) From the Workspace pane, follow FileView tab, and open UltimaDlg.cpp

26) Add the follow codes to BOOL CUltimaDlg::OnInitDialog(), right after the comment // TODO: Add extra initialization here (If you decide to type in the codes, you will notice the properties and methods will be displayed in a drop-down menu once you typed in m_ultima.)

m_ultima.SetDevice(194);
m_ultima.SetCommPort(1);
m_ultima.SetEventLevel(2);
m_ultima.SetSampleRate(240);
m_ultima.SetChannelCount(1);
m_ultima.Start();

27) Add the follow codes OnNewDataUltimaserialctrl1(short Count)

m_dqchart.Chart (m_ultima.GetData());

28) If you wish to access the data returned by GetData. Here is the codes extracted from XChart::Chart so that you can search some keywords from MSDN to write your own codes.

void CXChartCtrl::Chart(const VARIANT FAR& Data) 
{
short * ptr;
...

if (Data.vt== VT_I2+VT_ARRAY){
    switch(Data.parray->cDims){
    case 2:
        iTotal=Data.parray->rgsabound[0].cElements-Data.parray->rgsabound[0].lLbound;
        iTotalChannel=iChannel=Data.parray->rgsabound[1].cElements-Data.parray->rgsabound[1].lLbound;
        break;
    case 1:
        iTotal=Data.parray->rgsabound[0].cElements-Data.parray->rgsabound[0].lLbound;
        iTotalChannel=iChannel=1;
        break;
    default:
    return;
    }
    ptr=(short HUGEP *)Data.parray->pvData;
}

Now, compile the project and run!

 

Copyright: 2000-2005  www.UltimaSerial.com