Communicating Data between Parallel Sections of Code Using Channel Wires
- Updated2025-03-11
- 5 minute(s) read
Communicating Data between Parallel Sections of Code Using Channel Wires
A channel wire is a wire in LabVIEW that expresses an asynchronous communication between two parallel sections of code without forcing an execution order. Using a channel wire is similar to using a refnum or a variable that one loop writes into and the other loop reads from. Instead of using a refnum or a variable, you represent the data transfer graphically as a wire.
The following figure illustrates that you can use channel wires to build your block diagram to achieve the same functionality as using queue refnums.
Every channel wire must have at least two endpoints: a writer and a reader. Endpoints are places in the code that exchange data. Writer endpoints write data to the channel. Reader endpoints read data from the channel. By connecting the writer endpoint to the reader endpoint using a channel wire, you enable asynchronous data communication between parallel sections of code.
LabVIEW provides the following channel templates that you can use to build applications. Each template expresses a different communications protocol. You can choose from the following templates based on your communications and application needs:
LabVIEW | Real-Time Module | FPGA Module* |
---|---|---|
Stream Tag Messenger Accumulator Tag Event Messenger High Speed Stream Lossy Stream One Element Stream | Stream Tag Messenger Accumulator Tag Event Messenger High Speed Stream Lossy Stream One Element Stream Real Time Stream | Stream Tag Accumulator Tag Lossy Stream One Element Stream |
*For each supported channel template, the FPGA Module supports only a subset of the endpoints. |
You can create a channel writer endpoint or a channel reader endpoint by right-clicking a terminal or a wire and selecting Create»Channel Writer or Create»Channel Reader. In the Select Channel Endpoint dialog box that appears, you can switch between writer or reader endpoints by clicking Switch Read/Write. When you create an endpoint from a terminal, you instantiate the channel template with that data type as its transmission type.
All Stream channels are not forkable, which means they connect a single writer endpoint to a single reader endpoint. All the other channels are forkable to allow multiple writers or multiple readers on the same channel.
Understanding the Lifetime of Channels
The calling block diagram of a channel governs the channel execution. A calling block diagram is the lowest level block diagram that contains all endpoints of a channel or all the nodes that call those endpoints.
A channel initializes when its calling block diagram begins running. A channel closes when the last element? or abort input on one of its endpoints is TRUE. If a channel closes, it ignores further writes to the channel. The channel reinitializes when its calling block diagram begins another run. A calling block diagram can be a structure node, the top-level block diagram of a VI, or a higher level block diagram that calls the current top-level VI.
In the following figure, the calling block diagram of the channel is the top-level diagram of this VI. The channel initializes when the calling block diagram begins running, which means the channel initializes each time the top-level VI begins running or each time the top-level VI is called as a subVI.
In the following figure, the calling block diagram of the channel is the While Loop. The channel initializes when the While Loop begins running, which means the channel initializes on each iteration of the While Loop.
In the following figure, the calling block diagram of the channel is not the top-level VI but is higher than the current top-level block diagram. The channel initializes when the higher level block diagram begins running, which means the channel initializes each time the higher level block diagram begins running or each time the higher level block diagram is called as a subVI. The channel does not act upon the iterations of the While Loop to initialize because the While Loop does not contain all the endpoints of the channel.
In all the channel instances above, the Write endpoint writes the first three values to the channel but ignores the final two values because the last element? input is TRUE when the number of elements left unwritten in the array equals to 2.
Stopping a Replicate Endpoint
When you create a channel reader or writer, select With Abort to create an endpoint with a boolean input abort. When TRUE, abort alerts all endpoints to produce an aborted? output. Any endpoint can send the abort signal. If you have loops that include channel wires, wire all loops to the aborted? output of the endpoints to stop the loops when an endpoint sends the abort signal. No local is necessary for this mechanism. If you use a Replicate endpoint, the original channel must supply the abort value. The Replicate endpoint stops its internal loop and propagate the abort value to the replicated channels.
Advantages of Channel Wires Compared to Refnums and Variables
Using channel wires provides the following benefits:
- Channel wires do not require you to create and destroy refnums like queues.
- Channel wires require fewer nodes on the block diagram than queues.
- Channel wires show where the data flows to and from, which makes them more visible and easier to debug than refnums and variables.
- Channel wires can incorporate stop and abort signals with your data, which makes your application less error prone than creating such signals using refnums and variables.
- Channel wires allow you to create co-routines that fill the role of closures in other programming languages. Refer to the labview\examples\Channels\Replacing The Function At The Heart Of An Algorithm At Run Time\Replacing The Function At The Heart Of An Algorithm At Run Time.lvproj for an example of using channel wires to create co-routines.