Chapter 4
Application Examples
©
National Instruments Corporation
4-5
CAN as their in-vehicle network. The utility monitors the car’s CAN
network traffic to test for defective devices, incorrect sensor data, and so on.
In developing the utility, the test engineer wants two threads. One thread
receives data from the CAN network and places it into a buffer. The other
thread processes the data in the buffer in order to check for erroneous
network traffic.
The following steps correspond to the program flowchart in Figure 4-2.
1.
The application calls
ncConfig
to configure the CAN Network
Interface Object used for bus analysis (
CAN0
). The masks and
comparators are configured such that all CAN data frames are
received. 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
ncOpenObject
to open the CAN Network
Interface Object.
3.
The application calls
ncCreateNotification
to create the
notification thread, which is used to receive frames into the buffer. This
is done by registering a callback function for the
NC_ST_READ_AVAIL
state. After creating the notification thread, the main thread proceeds
to Step 7.
4.
The notification thread remains idle until its callback function is
invoked by the NI-CAN driver.
5.
If the callback function detects the
NC_ST_READ_AVAIL
state,
ncRead
is called to read the frame, and the frame is placed into the
buffer for processing by the main thread.
6.
If the callback function detects a timeout or error, it indicates the
problem to the main thread, then proceeds to Step 9. If no timeout or
error is detected, the callback function re-enables the notification and
returns to Step 4 (idle).
7.
If the main thread detects a new frame in the buffer (placed there by the
notification thread in Step 5), it processes the frame. For this example,
processing the frame merely entails printing it to the screen.
8.
If the main thread does not detect a timeout or error, it loops back to
Step 7 to wait for more frames.
9.
If a timeout or error occurs, the main thread prints it to the screen.
The timeout occurs if no frame is received within 30 seconds.
10. The application calls
ncCloseObject
to close the CAN Network
Interface Object.