Choose the Programming Language

NI-CAN

Choose the Programming Language

The programming language you use for application development determines how to access the NI-CAN APIs.

LabVIEW

NI-CAN functions and controls are available in the LabVIEW palettes. In LabVIEW 7.0 or later, the NI-CAN palette is located within the top-level NI Measurements palette. In earlier LabVIEW versions, the NI-CAN palette is located at the top-level. The top level of the NI-CAN function palette contains subpalettes for the Channel API and Frame API. Each subpalette of an API contains the most commonly used functions, with subpalettes for advanced functions.

The reference for each NI-CAN Channel API function is in Channel API for LabVIEW. The reference for each NI-CAN Frame API function is in Frame API for LabVIEW. To access the reference for a function from within LabVIEW, press <Ctrl-H> to open the help window, click on the NI-CAN function, and then follow the link.

The NI-CAN software includes a full set of examples for LabVIEW. These examples teach basic NI-CAN programming as well as advanced topics. The example help describes each example and includes a link you can use to open the VI.

The NI-CAN example help is in Help»Find Examples»Hardware Input and Output»CAN.

LabWindows™/CVI™

Within LabWindows/CVI, the NI-CAN function panel is in Libraries»NI-CAN. Like other LabWindows/CVI function panels, the NI-CAN function panel provides help for each function and the ability to generate code.

The reference for each NI-CAN Channel API function is in Channel API for C. The reference for each NI-CAN Frame API function is in Frame API for C. You can access the reference for each function directly from within the function panel.

The header file for both NI-CAN APIs is nican.h. The library for both NI-CAN APIs is nican.lib.

The NI-CAN software includes a full set of examples for LabWindows/CVI. The NI-CAN examples are installed in the LabWindows/CVI directory under samples\nican.

Each example provides a complete LabWindows/CVI project ( .prj file). A description of each example is provided in comments at the top of the .c file.

Visual C++ 6

The NI-CAN software supports Microsoft Visual C/C++ version 6.

The header file and library for Visual C/C++ 6 are in the MS Visual C folder of the NI-CAN folder. The typical path to this folder is \Program Files\National Instruments\NI-CAN\MS Visual C.

To use either NI-CAN API, include the nican.h header file in the code, then link with the nicanmsc.lib library file.

For C applications (files with .c extension), include the header file by adding a #include to the beginning of the code, such as:

#include "nican.h"

The reference for each NI-CAN Channel API function is in Channel API for C. The reference for each NI-CAN Frame API function is in Frame API for C.

You can find examples for the C language in the MS Visual C subfolder of the NI-CAN folder. Each example is in a separate folder. A description of each example is in comments at the top of the .c file.

At the command prompt, after setting MSVC environment variables (such as with MS vcvars32.bat), you can build each example using a command such as:

cl -I..singin.c..\nicanmsc.lib

Borland C/C++

The header file and library for Borland C/C++ are in the Borland C folder of the NI-CAN folder. The typical path to this folder is \Program Files\National Instruments\NI-CAN\Borland C.

To use either NI-CAN API, include the nican.h header file in the code, then link with the nicanbor.lib library file.

For C applications (files with .c extension), include the header file by adding a #include to the beginning of the code, such as:

#include "nican.h"

The reference for each NI-CAN Channel API function is in Channel API for C. The reference for each NI-CAN Frame API function is in Frame API for C.

You can find examples for the C language in the Borland C subfolder of the NI-CAN folder. Each example is in a separate folder. A description of each example is in comments at the top of the .c file.

Microsoft Visual Basic

The NI-CAN software supports Microsoft Visual Basic 6.0 or later.

To create an application in Visual Basic, add the NI-CAN Channel API.BAS or NI-CAN Frame API.BAS file with the WIN32 API.BAS file to the project. WIN32 API.BAS defines API calls to the Windows system which are called by functions defined in the NI-CAN Channel API.BAS and NI-CAN Frame API.BAS files. Adding these files to the project allows you to call any of the functions declared in them from the code.

The .BAS files are located in the MS Visual Basic folder of the NI-CAN folder. The typical path to this folder is \Program Files\National Instruments\NI-CAN\MS Visual Basic.

The reference for each NI-CAN Channel API function is Channel API for C. The reference for each NI-CAN Frame API function is in Frame API for C.

If you plan to use the ReadMult or WriteMult functions, refer to the examples, which explain how to use the Read Multiple or Write Multiple functions instead.

You can find examples for Visual Basic in the Channel API examples and Frame API examples subfolders of the MS Visual Basic folder. Each example is in a separate folder. A .vbp file with the same name as the example opens the Visual Basic project. A description of the example is located in a Help form within the project.

Other Programming Languages

The NI-CAN software does not provide formal support for programming languages other than those described in the preceding sections. Nevertheless, you may find libraries and examples for other programming languages on the National Instruments Web site, ni.com.

If the programming language provides a mechanism to call a Dynamic Link Library (DLL), you can create code to call NI-CAN functions. All functions for the Channel API and Frame API are in nican.dll.

If the programming language supports the Microsoft Win32 APIs, you can load pointers to NI-CAN functions in the application. The following text demonstrates use of the Win32 functions for C/C++ environments other than Visual C/C++ 6. For more detailed information, refer to Microsoft documentation.

The following C language code fragment shows how to call Win32 LoadLibrary to load the DLL for the NI-CAN Channel API:

#include <windows.h>

#include "nican.h"

HINSTANCE NicanLib = NULL;

NicanLib = LoadLibrary("nican.dll");

Next, the application must call the Win32 GetProcAddress function to obtain a pointer to each NI-CAN function that the application will use. For each NI-CAN function, you must declare a pointer variable using the prototype of the function. For the prototypes of each NI-CAN function, refer to the C language sections of this help file.

static nctTypeStatus (NCT_FUNC * PnctInitStart)

(const str TaskList, i32 Interface, i32 Direction,

f64 SampleRate, nctTypeTaskRef * TaskRef);

static nctTypeStatus (NCT_FUNC * PnctRead)

(nctTypeTaskRef TaskRef, u32 NumberOfSamplesToRead, nctTypeTimestamp * StartTime, nctTypeTimestamp * DeltaTime, f64 * SampleArray, u32 * NumberOfSamplesReturned);

static nctTypeStatus (NCT_FUNC * PnctClear)

(nctTypeTaskRef TaskRef);

PnctInitStart = (nctTypeStatus (NCT_FUNC *)

(const str, i32, i32, f64, nctTypeTaskRef *)) GetProcAddress(NicanLib, (LPCSTR)"nctInitStart");

PnctRead = (nctTypeStatus (NCT_FUNC *)

(nctTypeTaskRef, u32, nctTypeTimestamp *, nctTypeTimestamp *, f64 *, u32 *)) GetProcAddress(NicanLib, (LPCSTR)"nctRead");

PnctClear = (nctTypeStatus (NCT_FUNC *)

(nctTypeTaskRef)) GetProcAddress(NicanLib, (LPCSTR)"nctClear");

The application must de-reference the pointer to call the NI-CAN function, as shown by the following code:

nctTypeStatus status;

nctTypeTaskRef TaskRef;

status = (*PnctInitStart)("mychannel1, mychannel2", 0, nctModeInput, 1000.0, &TaskRef);

Before exiting the application, you must unload the NI-CAN DLL as follows:

FreeLibrary(NicanLib);