![National Instruments NI-CAN User Manual Download Page 36](http://html1.mh-extra.com/html/national-instruments/ni-can/ni-can_user-manual_3506864036.webp)
Chapter 2
Developing Your Application
©
National Instruments Corporation
2-9
Step 4. Communicate Using Objects
After you open your objects and start communication, you are ready to
transfer data on the CAN network. The manner in which data is transferred
depends on the configuration of the objects you are using. For this example,
assume that you are communicating with a CAN device that periodically
transmits a data frame. To receive this data, assume that a CAN Object is
configured to watch for data frames received for its arbitration ID and store
that data in its read queue.
Step 4a. Wait for Available Data
To wait for the arrival of a data frame from the device, you can call
ncWaitForState
with the
DesiredState
parameter set to
NC_ST_READ_AVAIL
. The
NC_ST_READ_AVAIL
state tells you that data
for the CAN Object has been received from the network and placed into the
object’s read queue. Another way to wait for the
NC_ST_READ_AVAIL
state
is to call the
ncCreateNotification
function so you receive a callback
when the state occurs. For more information on
ncWaitForState
and
ncCreateNotification
, refer to the
NI-CAN Programmer Reference
Manual
.
When receiving data from the device, if your only requirement is to
obtain the most recent data, you are not required to wait for the
NC_ST_READ_AVAIL
state. If this is the case, you can set the read queue
length of the CAN Object to zero during configuration, so that it only holds
the most recent data bytes. Then you can use the
ncRead
function as
needed to obtain the most recent data bytes received.
Step 4b. Read Data
Read the data bytes using
ncRead
. For CAN Objects that receive data
frames,
ncRead
returns a timestamp of when the data was received,
followed by the actual data bytes (the number of which you configured in
Step 1).
Steps 4a and 4b should be repeated for each data value you want to read
from the CAN device.
Step 5. Close Objects
When you are finished accessing the CAN devices, close all objects using
the
ncCloseObject
function before you exit your application.