Controller Area Network CAN Tutorial
Controller Area Network CAN Tutorial
Topics
A. What You Need to Get Started
B. History of CAN
C. CAN Basics
D. Channel Configuration
E. CAN APIs
F. CAN Programming in LabVIEW (Channel API)
Tutorial Materials
Before you use this tutorial, ensure you have all the following items:
❑ A CAN Interface
❑ Exercises folder for saving VIs created during the tutorial and for
completing certain tutorial exercises
B. History of CAN
In the past few decades, the need for improvements in automotive
technology caused increased usage of electronic control systems for
functions such as engine timing, anti-lock brake systems, and distributorless
ignition.
CAN provides a cheap, durable network that allows the devices to speak
through the Electronic Control Unit (ECU). CAN allows the ECU to have
one CAN interface rather than analog inputs to every device in the system.
This decreases overall cost and weight in automobiles. Each of the devices
on the network has a CAN controller chip and is therefore intelligent. All
transmitted messages are seen by all devices on the network. Each device
can decide if the message is relevant or if it can be filtered.
By the mid-1990s, CAN was the basis of many industrial device networking
protocols, including DeviceNet and CANOpen.
Automotive Applications
Examples of CAN devices include engine controller (ECU), transmission,
ABS, lights, power windows, power steering, instrument panel, and so on.
Instrument Panel
Power Steering
Power Windows
Transmission
Lights
ABS
CAN
Farm
Avionics
Machinery
C. CAN Basics
Benefits of CAN
• Lower cost from reduced wiring compared to two wire, point-to-point
wiring
• Highly robust protocol
– Built-in determinism
– Fault tolerance
– Reliable—More than a decade of use in the automotive industry
CAN Specifications
• CAN data (up to 8 bytes in a frame)
• Maximum 1 Mbaud/s
• 40 Meters at 1 Mbaud/s
• 6 km at 10 kbaud/s
• Theoretical maximum of 2,032 nodes per bus
– Practical limit is approximately 100 nodes due to transceiver
– Most buses use 3–10 nodes
• Data fields—Arbitration ID (11 bit or 29 bit)
– Indicates message priority
Types of CAN
• High Speed CAN
– Up to 1 M bits/s transmission rate
High-speed CAN
• 1 and 2 ports
• Maximum baud rate of 1Mb/s
Low-speed CAN
• 1 and 2 ports
• Maximum baud rate of 125 kbaud/s
CAN Frame
CAN devices send data across the CAN Network on packets called frames.
A typical CAN frame contains an arbitration ID, a data field, a remote
frame, an error frame, and an overload frame.
Arbitration ID
The arbitration ID determines the priority of the messages on the bus. If
multiple nodes try to transmit a message onto the CAN bus at the same time,
the node with the highest priority (lowest arbitration ID) automatically gets
bus access. Nodes with a lower priority must wait until the bus becomes
available before trying to transmit again. The waiting devices wait until the
end of the frame section is detected.
Device A transmits
1 1 0 0 1 0 0 0 1 1 1
ID = 110 0100 0111 (647 hex)
Device B transmits
1 1 0 1
ID = 110 1100 0111 (6C7 hex)
Data Field
The data field contains the actual data being transmitted. The CAN protocol
supports two data field formats as defined in the Bosch Version 2.0
specifications, the essential difference being in the length of the arbitration
ID. In the standard frame format (also known as 2.0A), the length of the ID
is 11 bits. In the extended frame format (also known as 2.0B), the length of
the ID is 29 bits.
In NI-CAN terminology, a data field for which the individual fields are
described is called a message. A message can contain one or more channels,
4 bytes 2 bytes
Arbitration
ID Data Field (8 bytes)
D. Channel Configuration
You can configure Channels in MAX to enable LabVIEW to interpret your
data fields. MAX allows you to load a set of channels from a database file
or configure channels manually.
CAN Databases
To translate the data field into usable data, a CAN device comes with a
database that describes the channels contained in the message. A CAN
Database File (CANdb File) is a text file that contains this information.
It allows you to find the data in a frame and convert it to engineering units.
For each channel, CAN databases store the following data:
Channel name
Location (Start bit) and size (number of bits) of the
Channel within a given Message
Byte Order (Intel/Motorola)
Data type (signed, unsigned, and IEEE float)
Scaling and units string
Range
Default Value
Comment
With many software programs, you must manually convert the data field to
usable data using the information contained in the database file. However,
using National Instruments software, this conversion is done for you. You
can load the channel configuration from the file into MAX, and then use it
in your application through the NI-CAN driver.
Note You also can access a CAN database directly from the LabVIEW CAN API.
However, loading the channels into MAX prevents you from having to specify a path in
your LabVIEW programs and allows you to read and write from the channels using the
MAX test panels.
Saving the channel configuration creates a custom database file for your
device. The resulting NI-CAN database file uses file extension .ncd. You
can access the NI-CAN database just like any other CAN database. By
installing the NI-CAN database file along with your application, you can
deploy your application to a variety of users.
You can also test explicitly created channels using the Channel Test Panel.
E. CAN APIs
There are two APIs that you can use with NI-CAN hardware: Channel and
Frame.
• Channel API
– High level
For a single NI-CAN port such as CAN0, you can use only one API at a
time. For example, if you have one application that uses the Channel API
and another application that uses the Frame API, you cannot use CAN0 with
both at the same time. If you have a 2-port CAN card, you can connect
CAN0 and CAN1 to the same CAN network, then use CAN0 with one
application and CAN1 with the other. Alternately, you can use CAN0 for
both applications, but run each application at a different time. In most cases,
you should use the Channel API, because it simplifies programming and
reduces development time. However, there are some situations in which the
Frame API is necessary; for example:
• You are maintaining an application developed with NI-CAN version 1.6
or earlier. The Frame API is compatible with code developed in early
CAN versions.
• You need to implement a command/response protocol in which you send
a command to the device, and then the device replies by sending a
response. Command/response protocols typically use a fixed pair of IDs
for each device, and the ID does not determine the meaning of the data
bytes.
• Your devices require use of remote frames. The Channel API does not
provide support for remote frames, but the Frame API has extensive
features to transmit and receive remote frames.
• You are synchronizing the CAN communication with data acquisition
from a DAQ card. The Frame API provides lower-level RTSI features
than those provided with the Channel API, and therefore is better for
advanced synchronization.
• You are using one of the NI USB-847x CAN Interfaces. The USB-CAN
products do not support Channel API or CAN Objects. You can still use
Frame API but there are some limitations on the functions available for
USB-CAN. For a list of these functions, see the Related Links section
below.
The link between the functions is the task reference. A CAN task is a
collection of CAN channels that have identical timing and the same
communication direction—read or write. A task can encompass several
messages, but they must all be on the same interface or port.
To directly access a CAN channel from a CAN database, specify the channel
name with the database path as a prefix. For example, if you use a channel
named Switch0 in the C:\CAN Demo Box.DBC CAN Database, pass
C:\CAN Demo Box.DBC::Switch0 to the CAN Init Start VI, as shown in
Figure 1-10. This figure also demonstrates how to read a channel available
in MAX, and how to extract all channels from a database file.
CAN Write
The CAN Write VI writes samples to an output CAN task. Samples are
placed into transmitted CAN messages. Right-click the icon and select
Select Type from the shortcut menu to choose the write data type.
CAN Clear
The CAN Clear VI stops communication for the task and clears the
configuration.
2. NI-CAN channels are used for which one of the following reasons?
a. They allow associating bits/bytes of a CAN message/frame with
meaningful names and scaling information.
b. They allow access to the CAN message/frame in its entirety
c. They allow access to the CAN physical bus channel
d. They allow associating different types of CAN frames with user
defined names
3. Which of the following are ways to access CAN channels in your CAN
application?
a. Specify a channel name that has been imported into MAX
b. Specify a database file and channel name for channels not in MAX
c. Use the CAN Get Names VI to access all channels in a database file
2. NI-CAN channels are used for which one of the following reasons?
a. They allow associating bits/bytes of a CAN message/frame with
meaningful names and scaling information
b. They allow access to the CAN message/frame in its entirety
c. They allow access to the CAN physical bus channel
d. They allow associating different types of CAN frames with user
defined names
3. Which of the following are ways to access CAN channels in your CAN
application?
a. Specify a channel name that has been imported into MAX
b. Specify a database file and channel name for channels not in
MAX
c. Use the CAN Get Names VI to access all channels in a database
file
Notes
Description
The exercises in this tutorial use a 2-port CAN interface device and a CAN
Demo Box. In this exercise, you set up your hardware, observe the
configuration of the CAN device in MAX, and run a self-test to verify that
the device functions correctly.
❑ Connect the cable from your DAQ device to the 68-pin terminal on
the CAN Demo Box.
❑ Launch MAX.
❑ Select CAN0 and click the Properties button to display the Port
Properties dialog box.
Note The Port Properties dialog box allows you to change the baud rate and other
properties of the CAN Interface. The settings are already correct for the current
configuration. You also can access the Port Properties dialog box by right-clicking a
port and selecting Properties from the shortcut menu.
❑ Observe the value of the Test Status property after the test
completes. Also notice that a small blue circle with a white check
appears above the bottom right corner of the PCI-CAN/2 icon in the
Configuration tree, indicating that the device has passed the self-test.
Description
Load channels from either an NI CAN database file or a Vector CAN
Database file into MAX. Investigate the properties of messages and
channels. Read data on a specific channel using MAX Test Panels. Finally,
use the built-in CAN Bus Monitor feature in MAX to monitor the frame data
being sent along the CAN bus.
❑ Navigate to <Exercises>\CAN\
CAN Demo Box.dbc.
Tip You could choose to import the .ndc database files instead. To do so, select Load
Channel Configuration instead of Import from CAN db File.
❑ Click Import.
Tip You also can right-click the channel name and select Test Panel from the shortcut
menu.
Tip A periodic signal should appear. You can see the signal better by changing the
scaling to Auto.
❑ Experiment with changing the settings on the demo box itself. Use
the Menu Select button to scroll through the different options.
– Click the Menu Select button until you see Func Gen Output.
– Click the <+> and <-> buttons to change which type of signal
the Function Generator outputs. Select from the following
choices—Sine, Square, and Triangle. The screen displays which
type is currently being output.
– Click the Menu Select button again until you see Func Gen Freq.
– Click the <+> and <-> buttons to increase and decrease the
frequency of the signal the Function Generator outputs.
❑ Click the Stop button in the MAX test panel to finish reading.
❑ Unplug the connector between the CAN Interface and your demo
box. The monitor ceases to show any transmissions because there is
nothing being sent to or from the bus.
❑ Plug in the connector between the CAN Interface and your demo
box. Notice periodic transmissions because the demo box is
constantly transmitting to and receiving from the CAN Interface.
❑ To specify where to stream the data, click the File Name button in
the dialog box and navigate to a file path.
6. Open the log file that you specified in the previous steps and view its
contents.
Description
Complete a VI that reads a single value at a time from the CAN Interface
and graphs it on a chart. The channel being read is Analog Input on Channel
0 of the CAN Demo box.
Note Before beginning this exercise, load the CAN Demo Box.ncd or
CAN Demo Box.dbc database into MAX. Also, wire the Function Generator Gen
output to the Analog In To CAN Ch0 input on the CAN demo box. You completed these
steps in a previous exercise.
Implementation
❑ Add the CAN Init Start VI to the block diagram to the left of the
While Loop.
❑ Right-click the interface input of the CAN Init Start VI and select
Create»Control.
❑ Right-click the mode input of the CAN Init Start VI, select Create»
Constant.
❑ Add the CAN Read VI to the block diagram inside the While Loop.
Tip You also can accomplish this by right-clicking the VI and selecting Select Type»
Single Channel»Single Sample»DBL from the shortcut menu.
❑ Add the CAN Clear VI to the block diagram to the right of the
While Loop.
Tip You also can drag the name of the channel into the array element from MAX.
The channel is found in the message WAVEFORM0_SAW0_FROM_CDB (0x710).
❑ Experiment with CAN Demo Box. Change the signal time and
frequency. Notice the changes taking effect on your chart.
1. Add a second channel to the front panel of the Read CAN Channels VI.
❑ Enter Switch0 into the second array element of the channel list
control.
❑ Delete the wire between the output of the CAN Read VI and the
waveform chart.
This specifies that the VI should read an array of doubles, one from
each channel, every time that it executes.
❑ Using the cursor, drag the border of the Index Array function to
expand its size so that it returns two array elements instead of one.
❑ Right-click the index0 input of the Index Array function and select
Create»Constant.
❑ Right-click the index1 input of the Index Array function and select
Create»Constant.
❑ Connect the first output element of the Index Array function to the
waveform chart indicator terminal and connect the second output
element to the Switch0 indicator terminal.
Test
1. Test the VI.
❑ Ensure that the channel list has one element with the channel name
FunctionGeneratorFrequency.
❑ Run the CAN Read Channels VI and the CAN Write Channels VI.
Description
There are two CAN and DAQ acquisitions inputting data simultaneously.
Both the acquisitions are buffered, meaning that the timing is handled on the
interface devices. Explore a VI that synchronizes the inputs by routing the
clock signal from one of the devices to the other using a Real-Time Serial
Interface (RTSI) cable to connect the two.
3. Explore the block diagram shown in Figure 1-21. Notice that this VI
reads from both the CAN and DAQ interfaces and uses RTSI to
synchronize the timing.
❑ Interface: CAN0
6. On the CAN Demo Box, attach two wires to jumper between the output
of the Functions Generator, the input Analog In to CAN Ch0, and the
input Analog In to DAQ Ch0.
7. Run the VI. Notice that the signals read from the CAN interface and the
DAQ device are perfectly synchronized. To terminate the VI, click the
Stop button.