Global TMW:
Login  |  Register          Free Newsletter Subscription
Subscribe
Email
Print
Reprint
Learn RSS

Which text language works best?

You can employ C, C++, Delphi, Java, or Visual Basic in your test-automation applications.

Jeff Laney, National Instruments, Austin, TX -- Test & Measurement World, 4/15/2002

Even though graphical development tools (Ref. 1) have become prevalent in the test industry, you shouldn't abandon your skills as a text programmer. At times, you may find it's easier to write a program in a text language you already know than to take time to learn a special graphical tool. And you may want to consider expending the effort to learn a new text language: Most likely, you can use the language to write programs for a variety of applications, not just for test setups.

If you want to use a text-based language, look for one with a future—one that will continue to find use in a variety of applications. Five such languages can handle test tasks: C, C++, Java, Delphi (Pascal), and Visual Basic. The new .NET languages, such as C# (pronounced C-sharp), offer promise, but they haven't yet entered the mainstream (see What the future holds ).

C remains a powerhouse

Listing 1. Java example
Listing 2. Visual Basic example
What the future holds
Because of the cachet of C++, many programmers call themselves C++ developers even if they only use C. In truth, most developers who are creating simple test modules do not need the complexity of C++ and its object-oriented-programming (OOP) approach. Furthermore, C is faster than C++, because the C++ objects result in more indirection in your code, which makes your code more difficult for a compiler to handle. When speed is an issue, such as in a production scenario employing slow legacy host computers, simple C is best. With the introduction of test executives (Ref. 2) that let you build complex applications by sequencing small modules, C becomes even more valuable for its speed and simplicity; the test executive takes over object interaction and data handling.

Key benefits of C include the number of libraries available and the number of developers in the world who know the language. Almost every hardware company offers drivers with C interfaces, and you can find a C routine to do almost anything, including playing MP3s if you want some background music for your test application. Many engineers take some C programming classes in college, and others learn it on the job to become more efficient or to advance their careers.

Of course, not everyone should revert to programming in C—it does have some pitfalls. Its lack of object-oriented features, while an advantage for short programs, can make it unsuitable for developing extremely complex applications. Furthermore, you can't expect new features to be added to it.

C++: The object of desire

In contrast, new features will be added to C++. C++ is a superset of C that takes the best of C's structured-programming features and extends it to embody OOP. C++ is the most advanced development language you can use today; it lets you develop complex applications while maintaining low-level access to hardware resources on every available computer platform.

The OOP of C++ forgoes the traditional approach of code working on data. Instead, OOP encapsulates state and behavior information into individual functional objects. For instance, in a test application, an object may represent an entire instrument, with properties and methods associated with the instrument's functionality, or an object may represent an aspect of that instrument, such as the data the instrument is acquiring. Each object can communicate with and respond to other objects, which you can piece together to create applications with complex interactions, and you can reuse your code with minimal rewrite.

C++ isn't as fast as C, but by no means is it slow. With each new release, C++ compilers are shortening the execution time of run-time code. If you're employing new computers with processor speeds pushing 2 GHz, you'll find the difference in execution speeds between C and C++ is negligible. The more important time factor is development time.

Object technology is great for dealing with complex application interactions, but it requires you to do a lot of planning before you can write a successful application. Many C++ environments try to make up for the language's complexity by providing wizards and boilerplate code to simplify development. With the openness and extensibility of the C++ language, you run the risk of making many mistakes and making your code unreadable. As an example, you could overload the "=" operator—that is, give it different meanings depending on context—thereby making it difficult for another person to understand the code.

In addition, not many true C++ class libraries exist. Most companies claim that they support C++, but they do so with a C library that can also run in C++. You do not get any advantages of the object-oriented model when using these libraries, and usually, you'll need to do some extra work to wrap these libraries so they behave as objects within your design pattern.

Java becomes the hot language

Java (www.sun.com/java) has become the most popular language for the Internet and for doing any kind of Web commerce, because you can write a Java application (Listing 1) once, and it will run on almost all PCs and workstations. Java, like C++, is an object-oriented language in which you build applications by combining different objects that interact with each other to create the desired output. Java not only extends OOP to the Web, but it solves many of the problems with C++. It eliminates some of the complexities of C++, making it easier for you to manage memory and work with pointers, and it improves error handling.

Java does have some drawbacks, though. Because Java is an interpreted language that gets just-in-time compiled, some operations can be noticeably slower than they would be with fully compiled code, even on the fastest processors. Also, Java is touted as being hardware independent—without a strong connection to any specific hardware—but the test-and-measurement industry depends on making physical measurements with specific pieces of hardware. There are almost no Java libraries for interacting with hardware and no clear way to interact with standard libraries such as C DLLs. To get around this limitation, you could employ a separate interface that exposes the hardware through some transfer mechanism. For example, you could set up a TCP/IP or UDP (User Datagram Protocol) connection that would buffer Java from the actual hardware, and you could write a client for the networking protocol in Java.

Pascal for today

Delphi (www.borland.com/delphi) mixes OOP with an easy-to-use development environment that makes everything possible and some things easy. To create Delphi, Borland took the standard Pascal and left it open so you can tweak it and do low-level operations, as you can with C or C++. Then, Borland created a development environment that simplifies most common programming tasks.

Delphi includes a set of programming tools that work with all platforms and standards, so you can develop and integrate your application for Microsoft, Sun, and Linux platforms. Delphi has its own component package called visual component library (VCL), which is both good and bad. It's good because the VCL is optimized for the compiler; it's bad because there is not a lot of support for the VCL. Delphi also works with ActiveX controls, which is a plus because most companies do have some sort of ActiveX libraries available. Of course, because Delphi is built upon Pascal—which is not the most popular language—you'll find a smaller community of Delphi developers than you'll find with C and C++.

More than Basic

Visual Basic (msdn.microsoft.com/vbasic ) gets its roots from the old Basic language, but other than retaining a simple syntax it leaves the original far behind. Visual Basic (Listing 2) is essentially a development environment that makes it easy to develop common Windows applications. Visual Basic handles memory management, event programming, and Web-page creation. Microsoft has even gone as far as trying to make data type unnecessary, by having a variant type that can be used as any data type. (It's best to avoid that variant, though, because it can make errors difficult to find at compile time and because it can cause problems during interactions with non-Visual Basic code. In fact, the new .NET version of Visual Basic does not permit such variants.) The ease of development of the environment has enabled poor programmers to successfully hack together common applications.

Microsoft, of course, gets the lion's share of attention in nearly any market it participates in, so it's not surprising that whole communities have been built around using Visual Basic as the native environment for developing COM/ActiveX controls. You will likely find at least two suppliers of controls for anything you want to do, from instrument connectivity to voice recognition.

There are some drawbacks to this ease of development. Microsoft hides much of what is going on underneath. The abstraction may make something easy to do at a very high level, but if you need some granular control, you might be out of luck. Also, Visual Basic is not truly object oriented. It's a close approximation that takes many of the concepts from OOP and integrates them into the environment.

Visual Basic is limited to development for Windows; the language offers no hope of platform independence. Visual Basic is based upon an interpreted language, and therefore has a stigma for being slow. But with processors becoming so fast, the difference between Visual Basic and C/C++ is unnoticeable in most cases.

What's best for you?

Your selection of a language or programming environment should be based on two factors: which language you already know and which is the best language for the project.

Even if you are working on a small project, if you already know how to develop in C++, then use C++. You can shorten the development time for a simple project by not planning your use of objects as you would on a larger project. Using the powerful C++ makes more sense than taking the time to learn a simpler language. Stick with the language you know if it can do the job.

If you do not have a preference or extensive knowledge in one language over the other, then you should choose based on what you are trying to accomplish. If you need to do a Web project, you should choose Java to take advantage of all of the Java-enabled servers in the world. Ultimately, choose whatever tool—whether it be a language, a spreadsheet, or a complete application—that makes your project the most successful.


Author Information
Jeff Laney is Measurement Studio development engineer at National Instruments, Austin, TX.


References
  1. Nelson, Rick, "Code behind the icons drives test languages," Test & Measurement World, April 15, 2001. p. 13. www.tmworld.com/archives.
  2. Nelson, Rick, "Test executives keep programs in line," Test & Measurement World, January 2002. p. 43. www.tmworld.com/archives.
 

Listing 1. Java example

class myDUTtest {

public static void main (string[] args) throws IOException{

//import necessary libraries (notice fake data acquisition)

import com.ms.wfc.ui*;

import fake.dataacquisition;

//declare a waveform and fill it with a sine wave

Double[] wfm = {mysinefunc(1000,5.0, 0.0, 10)};

private void bNew_click(Object source, Event e)

{

//prompt user to insert new DUT and get information

MessageBox.Show("Insert new DUT and information!", "New DUT", MessageBox.OK);

edit1.clear();

edit2.clear();

}

private void bGo_click(Object source, Event e)

{

String serial, batch;

//Get Information

serial = edit1.getText();

batch = edit2.getText();

//configure,synchronize, and go

DAQ(1).Signals.Synchronize (AnalogInput(1), AnalogOutput(1));

DAQ(1).Synchronize();

AnalogInput.Start();

AnalogOutput.Start(wfm);

}

private void data_acquired (Object source, Event e)

{

//display data and log to spreadsheet

display.plot(e.data);

datasource1(Excel).append (serial,",",batch,",",DATE,",",e.data);

}

}

}

Listing 2. Visual Basic example

Option Explicit

'declare variable and fill waveform with a sine wave

Dim mwfm as Variant

Dim serial,batch as Variant

m_vWfm1 = CWDSP1.SinePattern(NPointsPerWfmNumEdit.Value, 4.5, 0, 10)

Private Sub InsertNew_Click()

'ask for new DUT and data

dummy = MsgBox("Insert New DUT and Data", vbOKOnly, "New DUT")

serialText.Text = ""

Batchtext.Text = ""

End Sub

Private Sub Go_Click()

'synchronize,configure, and start

'This assumes that at design time the configuration was inserted into the property pages

CWDAQTools1.RouteSignal CWAO1.Device, cwrsPinRTSI0, cwrsSourceAOStartTrigger

CWAO1.Configure

CWAO1.Write m_Wfm

CWAI1.Configure

CWAI1.Start

CWAO1.Start

End Sub

Private Sub CWAI1_AcquiredData(Voltages As Variant, BinaryCodes As Variant)

'plot data and append to spreadsheet

CWGraph1.PlotY Voltages

LogData Voltages

End Sub

Sub LogData(ScaledData As Variant)

' log the data to a file

Dim FileNumber As Integer

Dim iChannel As Integer

Dim Chan As CWAIChannel

Dim iScans As Integer

Dim sData As String

' open the file

FileNumber = FreeFile

If Overwrite Then

Open txtFilename.Text For Output As #FileNumber

Else

Open txtFilename.Text For Append As #FileNumber

End If

'print the title of each column

For Each Chan In CWAI1.Channels

Print #FileNumber, vbTab & "Ch " & RTrim(Chan.ChannelString);

Next

Print #FileNumber, 'carriage return/linefeed

For iChannel = LBound(ScaledData) To UBound(ScaledData)

sData = Format(ScaledData(iChannel), ".###0")

'the semi-colon after a Print command means that a CR is not

'placed at the end of the line

Print #FileNumber, vbTab & sData;

Next iChannel

Print #FileNumber,

' close the file

Close #FileNumber

End Sub

What the future holds

Microsoft Visual Studio .NET (msdn.microsoft.com/vstudio/default.asp) is the latest version of Visual Studio (version 7), but it's a significant revision that aims to simplify development for the .NET framework. By using the new language called C# (pronounced C-sharp), you can easily develop native .NET applications.

Code now compiles to an intermediate language that may eventually run on multiple platforms; a new Common Language Runtime (CLR) program compiles the intermediate code to specific machine code for different platforms. The CLR also simplifies development by automating memory management. Visual Studio .NET is built on XML (eXtensible Markup Language), an open standard that promotes flexibility and integration into any platform.

All of Microsoft's languages and third-party languages, such as COBOL, can be used from the same .NET environment, and they communicate seamlessly with each other. With .Net, languages are just syntax to the bigger goal, which is successfully completing your project, on time and under budget..

Email
Print
Reprint
Learn RSS

Talkback

We would love your feedback!

Post a comment

» VIEW ALL TALKBACK THREADS

Related Content

Related Content

 

By This Author

There are no other articles written by this author.

Sponsored Links



 
Advertisement
SPONSORED LINKS

More Content

  • Blogs
  • Podcasts

Blogs

  • Martin Rowe
    ROWE'S AND COLUMNS

    July 8, 2008
    Introducing...Test ideas
    Beginning in the T&MW August print issue, we’ll replace the “Project Profile” ...
    More
  • Martin Rowe
    Rowe's and Columns

    July 8, 2008
    They finally figured it out
    I just received a press release from one of those companies that analyzes industries. This one is ab...
    More
  • » VIEW ALL BLOGS RSS

Podcasts

Advertisements





NEWSLETTERS

Click on a title below to learn more.

Test Industry News (3 Times Per Month)
Machine-Vision & Inspection (Monthly)
Communications Test (Monthly)
Design, Test & Yield (Monthly)
Automotive, Aerospace & Defense (Monthly)
Instrumentation (Monthly)
Resource Center E-Alert (Monthly)
©2008 Reed Business Information, a division of Reed Elsevier Inc. All rights reserved.
Use of this Web site is subject to its Terms of Use | Privacy Policy
Please visit these other Reed Business sites