Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Using External Code in LabVIEW.pdf
Скачиваний:
48
Добавлен:
29.05.2015
Размер:
1.85 Mб
Скачать

Chapter 3

CINs

Correct example: foo(Path path) {

Str255 buf; /* allocated buffer of 256 chars */

File fd;

MgErr err;

err = FNamePtr(path, buf);

err = FMOpen(&fd, path, openReadOnly, denyWriteOnly);

}

Incorrect example: foo(Path path) {

PStr

p;

/*

an

uninitialized pointer */

File

*fd;

/*

an

uninitialized pointer */

MgErr err;

 

 

 

err = FNamePtr(path, p);

err = FMOpen(fd, path, openReadOnly denyWriteOnly);

}

In the correct example, buf contains space for the maximum-sized Pascal string (whose address is passed to FNamePtr), and fd is a local variable (allocated space) for a file descriptor.

In the incorrect example, p is a pointer to a Pascal string, but the pointer is not initialized to point to any allocated buffer. FNamePtr expects its caller to pass a pointer to an allocated space, and writes the name of the file referred to by path into that space. Even if the pointer does not point to a valid place, FNamePtr writes its results there, with unpredictable consequences. Similarly, FMOpen writes its results to the space to which fd points, which is not a valid place because fd is uninitialized.

Debugging External Code

LabVIEW has a debugging window you can use with external code to display information at run time. You can open the window, display arbitrary print statements, and close the window from any CIN or external subroutine.

To create this debugging window, use the DbgPrintf function. The format for DbgPrintf is similar to the format of the SPrintf function, described

Using External Code in LabVIEW

3-18

www.ni.com

Chapter 3

CINs

in Chapter 6, Function Descriptions. DbgPrintf takes a variable number of arguments, where the first argument is a C format string.

DbgPrintf

syntax int32 DbgPrintf(CStr cfmt, ..);

The first time you call DbgPrintf, LabVIEW opens a window to display the text you pass to the function. Subsequent calls to DbgPrintf append new data as new lines in the window. You do not need to pass in the new line character to the function. If you call DbgPrintf with NULL instead of a format string, LabVIEW closes the debugging window. You cannot position or change the size of the window.

The following examples show how to use DbgPrintf.

DbgPrintf("");

/* print an empty line, opening

 

the window if necessary */

DbgPrintf("%H", var1);

/* print the contents of an

 

LStrHandle (LabVIEW string),

 

opening the window if necessary

 

*/

DbgPrintf(NULL);

/* close the debugging window

 

*/

Windows

Windows supports source-level debugging of CINs using Microsoft’s Visual C environment. To debug CINs in Windows, complete the following steps.

1.Modify your CIN to set a debugger trap. You must do this to force Visual C to load your debugging symbols. The trap call must be done after the CIN is in memory. The easiest way to do this is to place it in the CINLoad procedure. After the debugging symbols are loaded, you can set normal debug points inside Visual C. Windows 9x has a single method of setting a debugger trap, while Windows 2000/NT can use the Windows 95 method or another.

The method common to Windows is to insert a debugger break using an in-line assembly command:

_asm int 3;

© National Instruments Corporation

3-19

Using External Code in LabVIEW

Chapter 3

CINs

Adding this to CINLoad gives you the following:

CIN MgErr CINLoad(RsrcFile reserved)

{

Unused(reserved);

_asm int 3; return noErr;

}

When the debugger trap is hit, Visual C++ invokes a debug window highlighting that line.

In Windows 2000/NT, you can use the DebugBreak function. This function exists in Windows 9x, but does not produce suitable results for debugging CINs. To use DebugBreak, include <windows.h> at the top of your file and place the call where you want to break:

#include <windows.h>

CIN MgErr CINLoad(RsrcFile reserved)

{

Unused(reserved);

DebugBreak(); return noErr;

}

When that line runs, you will be in assembly. Step out of that function to get to the point of the DebugBreak call.

2.Rebuild your CIN with debugging symbols.

If you built your CIN from the command line, add the following lines to the .lvm file of your CIN to add debug information to the CIN:

DEGUG = 1

cinLibraries = Kernel32.lib

If you built your CIN using the IDE, build a debug version of the DLL. Select Projects»Settings, the Debug tab, and the General category. Type your LabVIEW executable in Executable for debug session.

3.Run LabVIEW.

If you built your CIN from the command line, start LabVIEW normally. When the debugger trap is run, a message appears:

A Breakpoint has been reached. Click OK to terminate application. Click CANCEL to debug the application.

Using External Code in LabVIEW

3-20

www.ni.com

Chapter 3

CINs

Click the Cancel button to launch the debugger, which attaches to LabVIEW, searches for the DLLs, then asks for the source file of your CIN. Point it to your source file, and the debugger loads the CIN source code. You can then debug your code.

If you built your CIN using the IDE, open your CIN project and click the GO button. Visual C launches LabVIEW.

UNIX

You can use standard C printf calls or the DbgPrintf function described in the previous section. You also can use gdb, the Gnu debugger, to debug the CIN. You must load the VI that contains the CIN before you add breakpoints; the CIN is not loaded until the VI is loaded.

© National Instruments Corporation

3-21

Using External Code in LabVIEW

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]