Choose Which Objects To Use
An application written for the NI-CAN Frame API communicates on the network by using various objects. Which Frame API objects to use depends largely on the needs of the application. The following sections discuss the objects provided by the Frame API, and reasons why you might use each class of object.
Using CAN Network Interface Objects
The CAN Network Interface Object encapsulates a physical interface to a CAN network, usually a CAN port on an AT, PCI, PCMCIA, PXI, or USB card.
You use the CAN Network Interface Object to read and write complete CAN frames. As a CAN frame arrives from over the network, it can be placed into the read queue of the CAN Network Interface Object. You can retrieve CAN frames from this read queue using the ncRead or ncReadMult function. The read functions provide a timestamp of when the frame was received, the arbitration ID of the frame, the type of frame (data, remote, or RTSI), the data length, and the data bytes. You also can use the CAN Network Interface Object to write CAN frames using the ncWrite function.
Some possible uses for the CAN Network Interface Object include the following:
- You can use the read queue to log all CAN frames transferred across the network. This log is useful when you need to view CAN traffic to verify that all CAN devices are functioning properly.
- You can use the write queue to transmit a sequence of CAN frames in quick succession.
- You can read and write CAN frames for access to configuration settings within a device. Because such settings generally are not accessed during normal device operation, a dedicated CAN Object is not appropriate.
- For higher level protocols based on CAN, you can use sequences of write/read transactions to initialize communication with a device. In these protocols, specific sequences of CAN frames often need to be exchanged before you can access the data from a device. In such cases, you can use the CAN Network Interface Object to set up communication, then use CAN Objects for actual data transfer with the device.
- The USB-845x hardware uses only the Network Interface Objects for communication.
In general, you use CAN Network Interface Objects for situations in which you need to transfer arbitrary CAN frames.
Using LIN Network Interface Objects
The LIN Network Interface Object encapsulates a physical interface to a LIN network, such as a LIN port on a USB-LIN device. You use the LIN Network Interface Object to read and write NI LIN frame types. As a LIN frame arrives from over the network, it can be placed into the read queue of the LIN Network Interface Object.
You can retrieve LIN frames from this read queue using the ncRead or ncReadMult function. The read functions provide a timestamp of when the frame was received, the arbitration ID of the frame, the type of frame (full, bus inactive, wakeup received, or bus error), the data length, and the data bytes. You also can use the LIN Network Interface Object to write any of three NI LIN frame types (response entry, header, or full), using the ncWrite or ncWriteMult function.
Some possible uses for the LIN Network Interface Object include the following:
- You can use the read queue and the LIN device in slave mode, to log all LIN frames transferred across the network. This log is useful when you need to view LIN traffic to verify that all LIN devices are functioning properly.
- You can use the write and read queues and the LIN device in slave mode, to emulate one or more slave nodes.
- You can use the write queue and the LIN device in master mode, to transmit a sequence of LIN frames in quick succession.
- You can loop individual writes with a variable delay between each, and the LIN device in master mode, to emulate LIN descriptor file (LDF) control of scheduling of LIN traffic.
Using CAN Objects
The CAN Object encapsulates a specific CAN arbitration ID and its associated data.
Every CAN Object is always associated with a specific CAN Network Interface Object, used to identify the physical interface on which the CAN Object is located. The application can use multiple CAN Objects in conjunction with their associated CAN Network Interface Object.
The CAN Object provides high-level access to a specific arbitration ID. You can configure each CAN Object for different forms of background access. For example, you can configure a CAN Object to transmit a data frame every 100 milliseconds, or to periodically poll for data by transmitting a remote frame and receiving the data frame response. The arbitration ID, direction of data transfer, data length, and when data transfer occurs (periodic or unsolicited) are all preconfigured for the CAN Object. When you have configured and opened the CAN Object, data transfer is handled in the background using read and write queues. For example, if the CAN Object periodically polls for data, the NI-CAN driver automatically handles the periodic transmission of remote frames, and stores incoming data in the read queue of the CAN Object for later retrieval by the ncRead function. For CAN Objects that receive data frames, the ncRead function provides a timestamp of when the data frame arrived, and the data bytes of the frame. For CAN Objects that transmit data frames, the ncWrite function provides the outgoing data bytes.
Some possible uses for CAN Objects include the following:
- You can configure a CAN Object to periodically transmit a data frame for a specific arbitration ID. The CAN Object transmits the same data bytes repetitively until different data is provided using ncWrite.
- You can configure a CAN Object to watch for unsolicited data frames received for its arbitration ID, then store that data in the read queue of the CAN Object. A watchdog timeout is provided to ensure that incoming data is received periodically. This configuration is useful when you want to apply a timeout to data received for a specific arbitration ID and store that data in a dedicated queue. If you do not need to apply a timeout for a given arbitration ID, it is preferable to use the CAN Network Interface Object to receive that data.
- You can configure a CAN Object to periodically poll for data by transmitting a remote frame and receiving the data frame response. This configuration is useful for communication with devices that require a remote frame to transmit their data.
- You can configure a CAN Object to transmit a data frame whenever it receives a remote frame for its arbitration ID. You can use this configuration to simulate a device which responds to remote frames.
In general, you use CAN Objects for data transfer for a specific arbitration ID, especially when that data transfer needs to occur periodically.