Frame API Basic Programming Model for CAN
The following steps demonstrate how to use the Frame API functions in an application. The steps are shown in the following flowchart.
- Configure Objects
Prior to opening the objects used in the application, you must configure the objects with their initial attribute settings. Each object is configured within the application by calling the ncConfig function. This function takes the name of the object to configure, along with a list of configuration attribute settings.
- Open Objects
You must call the ncOpen function to open each object you use within the application.
The ncOpen function returns a handle for use in all subsequent Frame API calls for that object. When you are using the LabVIEW function library, this handle is passed through the upper left and right terminals of each Frame API function used after the open.
- Start Communication
You must start communication on the CAN network before you can use the objects to transfer data.
If you configured the CAN Network Interface Object to start on open, that object and all of its higher level CAN Objects are started automatically by the ncOpen function, so nothing special is required for this step.
If you disabled the start-on-open attribute, when the application is ready to start communication, use the CAN Network Interface Object to call the ncAction function with the Opcode parameter set to NC_OP_START. This call is often useful when you want to use ncWrite to place outgoing data in write queues prior to starting communication. This call is also useful in high bus load situations, because it is more efficient to start communication after all objects have been opened.
- Communicate Using Objects
After you open the 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.
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 read queue of the object.
When receiving data from the device, if the 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.
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.
- Close Objects
When you are finished accessing the CAN devices, close all objects using the ncClose function before you exit the application.