![National Instruments NI-CAN User Manual Download Page 46](http://html1.mh-extra.com/html/national-instruments/ni-can/ni-can_user-manual_3506864046.webp)
Chapter 4
Application Examples
©
National Instruments Corporation
4-3
plays the role of the simulated speed display, and the other port plays the
role of the simulated speed sensor.
Example 1 configures one CAN Object to receive data, and configures
another CAN Object to transmit data. Both CAN Objects use arbitration
ID 5. The data is transmitted every second, so the test engineer can view
each period’s data as well as its timestamp. Once the engineer completes
the example, he can change it for testing of each device by using one CAN
Object at a time.
The following steps correspond to the program flow chart in Figure 4-1.
1.
The application calls
ncConfig
to configure the CAN Network
Interface Objects for both ports (
CAN0
and
CAN1
). Normally, this
configuration would be handled using the NI-CAN Configuration
utility, but
ncConfig
is used instead to keep the example
self-contained.
2.
The application calls
ncConfig
to configure the CAN Objects for
both ports (
CAN0::STD5
and
CAN1::STD5
). Once again, such
configuration would normally be handled using the NI-CAN
Configuration utility.
3.
The application calls
ncOpenObject
to open the two CAN Objects.
4.
The application calls
ncWrite
for
CAN1::STD5
. This call starts the
periodic transmission of data. For this example, the same data is
transmitted every period.
5.
The application calls
ncWaitForState
for
CAN1::STD5
in order to
wait for the
NC_ST_WRITE_SUCCESS
state. This state is set when the
first CAN frame is successfully transmitted to the other CAN Object.
6.
The application calls
ncWaitForState
for
CAN0::STD5
in order to
wait for the
NC_ST_READ_AVAIL
state. This state is set when a CAN
frame is received from the other CAN Object.
7.
The application calls
ncRead
to read data for
CAN0::STD5
. The data
contains the value written in step 4, as well as a timestamp of when the
value arrived.
8.
The application loops back to step 6 for a total of 10 periods. Each
period, step 6 waits one second before the next data value is received.
9.
When all 10 loops complete, both CAN Objects are closed using
ncCloseObject
.