INTERNET-ENABLED DATA ACQUISITION USING VB6 PART I
Keithley Instruments, Inc.
Building a T&M Revolution
The test and measurement industry is primed for a revolution in the way data is collected and used. The change agents are the Internet and related software that instrument manufacturers can use to allow remote access to test systems. The technology is now in place for engineers to share their test systems and data with collaborators around the world. In addition, operators can easily command remote systems, which could reside in harsh environments.
Not so long ago, expensive third-party platforms were needed to do this. Now it can be done from any computer with a web browser and an Internet connection, using only a development platform most engineers are familiar with: Visual Basic. With Microsofts introduction of Visual Basic 6, designing and implementing a web-enabled test system has never been easier or cheaper.
Although the project described in this 2-part article is a simple one, the methods presented can easily be extended to more complicated systems and more sophisticated interfaces. The articles example involves the creation of VB6 code that links input controls, such as text boxes and check boxes, to the users test system. These controls allow a remote user to configure a data acquisition (DAQ) card by entering a channel number, channel gain, sampling frequency, and number of samples on an HTML form. The test system then acquires the requested data and displays it on a web page as a JPEG graph, and (optionally) in text form.
Hardware and Software Requirements
The test system in this example uses an analog/digital I/O board installed in a computer PCI slot. The VB6 code was tested with a Keithley Model KPCI-3108 board and its associated driver software, called DriverLINX. However, the code presented here is valid for most PC plug-in DAQ cards of recent vintage, regardless of the driver used, and the basic techniques described are applicable to any Windows®-based test systems.
Besides the PC, DAQ board, and driver software, there are a few other prerequisites. The PC operating system (OS) must be Windows NT/2000 or 95/98/Me. Of course, you will need Microsoft Visual Basic 6.0. If the features of your specific test system require additional third-party software, that would still be included in the design. Besides Keithleys DriverLINX, the only third-party tool used to implement the example is Intels JPEG Library, a free download that makes it easier to create and save JPEG files in Windows (more on this later). Since the intent of this design exercise is to configure the DAQ board and collect data from a remote web browser environment, you must set up the test system PC as a web server. The software component required for this depends on which version of Windows you use.
If you are running Windows 9x or Me, you will need Microsoft Personal Web Server (PWS) 3.0 or later. PWS 4.0 is available on the Windows 98 CD (under the addons\pws directory), and as a free download from Microsofts web site. If you want to download PWS 4.0, be aware that it is bundled (confusingly enough) with the NT Option Pack. If, on the other hand, you are running Windows NT, you need Internet Information Server (IIS) 3.0 or higher. IIS 4.0 is included in the downloadable NT Option Pack just mentioned. Fortunately, IIS 5.0 has been integrated into Windows 2000, so those using the Windows 2000 OS need not worry about installing extra components.
This web browser application requires two separate projects. When combined, they form a complete test application program. The first project is an ActiveX DLL that encapsulates the test system. The second project is an IIS application that provides the web interface to the test system DLL. Our design example takes a bottom-up approach, starting with the test system DLL project. The steps are:
Test System DLL Creation
Data acquisition Start Visual Basic 6 and create a new ActiveX DLL project. VB automatically creates a new class for you and names it Class1. Lets rename it clsTestSystem. There could be any number of functions a test system executes, but for simplicitys sake we will use only one, which uses the DAQ board to acquire data. This is done by creating a VB6 Public Function, as shown in the code snippet of Table 1.
Table 1. AcquireData() function header.
This function takes the channel number, gain, frequency, and number of samples as parameters, makes the necessary service request to the hardware, populates the Data() array with the acquired data, and returns an error code. Implementation of this function in a designers test application will vary depending on the DAQ board and driver used.
Creating the graph By implementing the Public Function above, we have succeeded in encapsulating our simple test system, and we could consider the test system DLL project complete. Before we move on to the IIS interface, however, we should consider one very important fact: VB does not allow forms to reside in IIS applications. Standard ActiveX DLLs like this one, on the other hand, are not limited by such restrictions. So, if a form is useful in implementing any functionality of the IIS interface, we should add that functionality here.
Drawing a graph is a perfect example of how a form can help implement an interface function. The standard PictureBox control has many drawing methods built in, and it requires a form as its parent. So, if you have not already done so, add a form (named Form1 by default) to the project, then add a PictureBox to Form1, and add the Public Function in Table 2 to clsTestSystem.
Table 2. CreateGraph() function header.
This function takes an array of data and the size of that array, then plots the data in the PictureBox, saves the picture as a JPEG file, and returns the name of the file. Completion of this step is left to the reader, but it should be noted that while VB offers native support for loading JPEG files, saving them is another matter. Thankfully, free third party tools have been developed specifically for this purpose. In this implementation of clsTestSystem, for example, the author used Intels JPEG Library, which includes a function SaveJPG that is similar to the familiar VB function SavePicture.
We are now ready to compile the DLL. Go into Project Properties and change the project description to "Test System," then make the DLL from the File menu (Make Project1.dll).
Creating the IIS Application
A simplified model of VBs IIS Application contains three components. The first is an HTML template with nonstandard HTML tags; these tags are replaced when the page is loaded by the second component, the WebClass. The final component is the Active Server Page (ASP) script, which provides the link between end-user web browsers and the DLL; Visual Basic automatically generates this component behind-the-scenes, so we dont have to worry about it here.
HTML Template Creation of the HTML template can be accomplished in Windows Notepad or your favorite text editor. Generally, it is better to avoid using HTML editors of the WYSIWYG variety, which can make it tricky to insert custom tags. Copy and paste the code shown in Table 3 into the text editor and save the file as "test.html".
Table 3. HTML template for IIS Application.
If you examine the text of this template, it may appear that our form doesnt do anything, because its action string (in the fifth line) has been left empty. Dont worry; VB will fill in this action string for us. Also note that we added three nonstandard HTML tags: <wc@graph>, <wc@parms>, and <wc@data>. These are the custom tokens that our WebClass will replace when the time comes; as long as the HTML tag starts with wc@, it is recognized and replaced by the WebClass.
WebClass Creation To create the WebClass, we open a new instance of Visual Basic. When the New Project dialogue pops up, select "New IIS Application." Before you forget, add a reference to the encapsulated test system DLL that we have already created: Select Project→References and check the box next to "Test System." Notice that in the Project Explorer window, VB has put a new WebClass module and called it WebClass1. Double-click on the WebClass1 icon and the WebClass Designer window will pop up. At this point we need to save the project, because VB wont let us add our HTML template until we do. Save the WebClass as wcTest and the project as TestInterface.
If you right-click on the "HTML Template WebItems" entry in the WebClass Designer, and select "Add HTML Template" from the pop-up menu, you can select our HTML template "test.html." VB calls this Template1 and populates the left-hand side of the designer with the components specified in our HTML code. Double-click the Form1 component to add the Form1 event to our design.
You should now be in the code window of the Template1_Form1 event subroutine. We will implement this subroutine later. For now, we will just add the global variables shown in Table 4.
Table 4. Global variables for IIS application.
Note that Channel, Gain, NumSamples, and Frequency have been declared as Variants (instead of Integers or Longs). This declaration is used because they are variables that a user inputs via text boxes, and as such may not necessarily be numerical.
When the code window appeared, you may have noticed that the Template1_Form1 event wasnt the only one there. To get you started, VB automatically generated sample code for the WebClass_Start event. This event is equivalent to a Form_Load or Main subroutine; it is the first subroutine encountered when the ASP is activated, and the perfect place to initialize our global variables. So replace the sample code with the code in Table 5.
Table 5. WebClass_Start() event subroutine.
The last line of this subroutine is the one that invokes our template. Since we havent yet defined the Template1_Respond event, lets do so now. Add the lines in Table 6 to the code window:
Table 6. Template1_Respond() event subroutine.
This subroutine defines the templates response, which is to write itself, replacing our custom HTML tags along the way. In Part II of this article, we will define the replacements for these custom tags and present some code that implements the WebClass/User Interaction. Results will be illustrated with screen-captured examples.
About the Author
Joseph Frese joined Keithley Instruments in Cleveland after receiving a bachelors degree in Computer Engineering from Case Western Reserve University. He currently is a Development Engineer for Keithleys Data Acquisition product line.
# # #