ncCreateNotification (Create Notification)

NI-DNET Programmer

ncCreateNotification (Create Notification)

Purpose

Create a notification callback for an object (C only).

Format

LabVIEW

Not applicable

C

NCTYPE_STATUS
ncCreateNotification  (NCTYPE_OBJH ObjHandle,
		    NCTYPE_STATE DesiredState,
		    NCTYPE_DURATION Timeout,
		    NCTYPE_ANY_P RefData,
		    NCTYPE_NOTIFY_CALLBACK 
			 Callback)

Input

ObjHandle Object handle of an open Explicit Messaging Object or I/O Object
DesiredState States for which notification is called
Timeout Number of milliseconds to wait for one of the desired states
RefData Pointer to user-specified reference data
Callback Address of your callback function

Output

None

Function Description

ncCreateNotification creates a notification callback for the object specified by ObjHandle. The NI-DNET driver uses the notification callback to communicate state changes to your application. The ncCreateNotification function does not apply to LabVIEW programming.

You commonly use ncCreateNotification to receive notifications when new input data is available for an I/O Object. Within your notification callback function, you call ncReadDnetIO to read the new input data, perform any needed calculations for that data, call ncWriteDnetIO to provide output data, then return from the callback function.

You normally use ncCreateNotification when you want to let other code to execute while waiting for NI-DNET states, especially when the other code does not call NI-DNET functions. If you do not need such background execution, ncWaitForState offers better overall performance. You cannot use ncWaitForState at the same time as ncCreateNotification.

This function is not supported for Visual Basic 6.

The Status parameter of your callback function indicates any error detected by NI-DNET. You should always check this Status parameter prior to checking the CurrentState parameter of your callback function.

When ncCreateNotification returns successfully, NI-DNET calls your notification callback function whenever one of the states specified by DesiredState occurs in the object. If DesiredState is 0, NI-DNET disables notifications for the object specified by ObjHandle.

Parameter Descriptions

ObjHandle

Description ObjHandle must contain an object handle returned from ncOpenDnetExplMsg or ncOpenDnetIO.
Values The encoding of ObjHandle is internal to NI-DNET.

DesiredState

Description States for which notification is called. So that notification can be enabled for multiple states simultaneously, a single bit represents each state. For example, if NI-DNET provides states with values of hex 1 and hex 4, DesiredState of hex 5 enables notification for both states.

ReadAvail for the I/O Object

For the I/O Object, the ReadAvail state sets when a new input message is received from the network. The ReadAvail state clears when you call ncReadDnetIO. For example, for a change-of-state (COS) I/O connection, the notification occurs when a COS input message is received.

The typical behavior for your callback function is to call ncReadDnetIO to read the new input data, perform any calculations needed, call ncWriteDnetIO to provide output data, then return from the callback function.

ReadAvail for the Explicit Messaging Object

For the Explicit Messaging Object, the ReadAvail state sets when an explicit message response is received from the network. The ReadAvail state clears when you call ncReadDnetExplMsg. An explicit message response is received only after you send an explicit message request using ncWriteDnetExplMsg.

Although using a notification for an explicit message response allows for execution of other code while waiting, it is often more straightforward to use the following sequence of calls: ncWriteDnetExplMsg, ncWaitForState, and ncReadDnetExplMsg. This is the sequence used internally by ncGetDnetAttribute and ncSetDnetAttribute.

The ReadAvail state is not needed when using the explicit messaging functions ncGetDnetAttribute and ncSetDnetAttribute because both of these functions wait for the explicit message response internally.

Established for the Explicit Messaging Object

For the Explicit Messaging Object, the Established state is clear (not established) before you start communication using ncOperateDnetIntf. After you start communication, the Established state remains clear until the explicit message connection has been successfully established with the remote DeviceNet device. After the explicit message connection has been established, the Established state sets and remains set for as long as the explicit message connection is open.

Until the Established state is set for the Explicit Messaging Object, all calls to ncGetDnetAttribute, ncSetDnetAttribute, or ncWriteDnetExplMsg return the error CanErrNotStarted. Before you call any of these functions in your application, you must first wait for the Established state to set.

After the Established state is set, unless communication problems occur with the device (CanErrFunctionTimeout), it remains set until you stop communication using ncOperateDnetIntf.

While waiting for one of the above states, if an error occurs (such as a communication error or an initialization error), the notification returns immediately with the appropriate error code. For example, if you call ncCreateNotification with DesiredState of ReadAvail, the notification function will return when data is available for a read, or when a DeviceNet communication error (such as connection timeout) is detected.

Values A combination of the following bit values:

1 hex (ReadAvail state, constant NC_ST_READ_AVAIL)

8 hex (Established, constant NC_ST_ESTABLISHED)

In the LabWindows™/CVI™ function panel, to facilitate combining multiple states, you can select a combination from an enumerated list of all valid combinations. This list contains the names of each state in the combination, such as ReadAvail or Established.

Timeout

Description Number of milliseconds to wait for one of the desired states. If the timeout expires before one of the desired states occurs, your notification function is called with CurrentState of 0 and Status of CanErrFunctionTimeout. Use the special timeout value of FFFFFFFF hex to wait indefinitely.
Values 1 to 200000
or
FFFFFFFF hex (infinite duration, constant NC_DURATION_INFINITE)

RefData

Description RefData provides a pointer that is passed to all calls of your notification callback function. It is typically used to provide the address of globally declared reference data for use within the notification callback. For example, for the ReadAvail state, RefData is often the data buffer which you pass to ncReadDnetIO to read available data. If the notification callback does not need reference data, you can set RefData to NULL.
Values Pointer to any globally declared data variable

or

NULL

Callback

Description This is the address of a callback function within your application source code. Within the code for the callback function, you can call any of the NI-DNET functions except for ncCreateNotification and ncWaitForState.

Declare this function using the following C language prototype.
NCTYPE_STATE	_NCFUNC_ Callback(
   NCTYPE_OBJH	ObjHandle, 
   NCTYPE_STATE	CurrentState, 
   NCTYPE_STATUS	Status, 
   NCTYPE_ANY_P	RefData);
In the declaration for your callback, the constant _NCFUNC_ is required for your compiler to declare the function such that it can be called by the NI-DNET device driver.

Parameter descriptions for Callback

ObjHandle

Object handle originally passed to ncCreateNotification. This identifies the object generating the notification, which is useful when you use the same callback function for multiple objects.

CurrentState

Current state of the object. If one of the desired states occurs, it provides the current value of the ReadAvail and Established states. If the Timeout expires before one of the desired states occurs, it has the value 0.

Status

Current status of the object. If one of the desired states occurs, it has the value 0 (DnetSuccess). If the Timeout expires before one of the desired states occurs, it has the value BFF62001 hex (CanErrFunctionTimeout).

RefData

Pointer to your reference data as originally passed to ncCreateNotification.

Return Value from Callback

The value you return from the callback indicates the desired states to re-enable for notification. If you want to continue to receive notifications, return the same value as the original DesiredState parameter. If you no longer want to receive notifications, return a value of 0.

If you return a nonzero value from the callback, and one of those states is still set, the callback is invoked again immediately after you return. For example, if you return ReadAvail from the callback without calling ncReadDnetIO to read the available data, the callback is invoked again.

Information Specific to LabWindows/CVI

When the NI-DNET device driver calls your notification callback, it does so in a separate thread within the LabWindows/CVI process. Your application's front panel indicators and controls can only be accessed within the main thread of the LabWindows/CVI process. Although you can call NI-DNET functions and perform generic C calculations in your notification callback, you cannot call LabWindows/CVI functions which access the front panel (the User Interface Library). To use the LabWindows/CVI User Interface Library, save any data needed for front panel indicators using global variables, then register a deferred callback using the LabWindows/CVI PostDeferredCall function. Since a LabWindows/CVI deferred callback executes in the main thread of the LabWindows/CVI process, you can call any LabWindows/CVI function, including the User Interface Library.

Information Specific to Microsoft, Borland, and Other C Compilers

When the NI-DNET device driver calls your notification callback, it does so in a separate thread within your process. Therefore, it has access to any process global data, but not thread local data. If your callback function needs to access global variables, you must protect that access using synchronization primitives (such as semaphores) because your callback is running in a different thread context. For an explanation of these concepts and other multithreading issues, refer to the online help of the Microsoft Win32 Software Development Kit (SDK).

Values Address of a callback function within your application source code. For example, if your function is declared with the name MyReadCallback, you would pass MyReadCallback as the Callback parameter.

Example

C

Create a notification for the ReadAvail state. Use a timeout of 10 seconds.

NCTYPE_UINT8	DataBuffer[20];
NCTYPE_STATE	_NCFUNC_ MyReadCallback (
			NCTYPE_OBJH ObjHandle,
			NCTYPE_STATE CurrentState,
			NCTYPE_STATUS Status,
			NCTYPE_ANY_P RefData) {
   if (Status == DnetSuccess) {
	Status = ncReadDnetIO(ObjHandle>, 20, RefData);
	.
	.
	.
   }
   .
   .
   .
   return(NC_ST_READ_AVAIL);
}

void main() {
   NCTYPE_STATUS	status;
   NCTYPE_OBJH	objh;

   .
   .
   .
   status = ncCreateNotification(objh, NC_ST_READ_AVAIL,
		10000, DataBuffer, MyReadCallback);
   .
   .
   .
}