LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Is it better to use channels or shared variables to communicate between loops?

Solved!
Go to solution

Alrighty. So I just started labview core 2 and am learning about transferring data between parallel loops. However, before I started core 2, I was going through the Real-Time 1 course where they show you how to use shared variables to communicate between parallel loops.

 

They both seem to be fairly similar. I believe a stream channel uses a FIFO, and before I was creating shared variable buffer's with the FIFO enabled. Is it just preference? Or is there a reason why I should use channels over a shared variable when I'm NOT doing a real-time project? Thanks

0 Kudos
Message 1 of 12
(1,580 Views)

Unless it is a distributed application, go for channel wires:

https://zone.ni.com/reference/en-XX/help/371361R-01/lvconcepts/channel_transfer_data/

 

 

Certified LabVIEW Architect
Message 2 of 12
(1,539 Views)

@David99999 wrote:

 

They both seem to be fairly similar. I believe a stream channel uses a FIFO, and before I was creating shared variable buffer's with the FIFO enabled. Is it just preference? Or is there a reason why I should use channels over a shared variable when I'm NOT doing a real-time project? Thanks


Well there are several types of stream channels, most are a FIFO (Stream), but some are not, Tag Channel and "Lossy Stream" for instance.

 

What is the better or best way? It all comes down to using the right tool for the job.

 

I have to say I have recently become a fan of the "Stream Message Handler" design (although not NI's implementation) and prefer Channels over Queues and Notifiers.

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 3 of 12
(1,487 Views)

Since Channel Wires were (secretly) introduced in LabVIEW 2015, I've been an enthusiastic user, as much as anything for their mnemonic value -- they show the "flow" of "asynchronous data" (they are called, after all, "Asynchronous Channel Wires" by following the well-established left-to-right "flow of data" that LabVIEW uses (except in a Queue or Notifier, where it "appears" to flow "backwards") as well as showing the "asynchronous" ability to "jump over the output side of a structure".

 

I use the Stream Channel when I want to "stream" data from, say, a Producer to a Consumer.  Stream Channels are designed as "single output" to "single input".  When I want to communicate "Messages", which might involve a Feedback loop, I use Messenger Channels, which accept branches (so a Message Handler can send a Message "back to itself", explicitly "showing" the Feedback by the Channel leaving the right edge of the While Loop, looping over the top, and entering to the Channel Reader just inside the left edge of the same While Loop.  Furthermore, if a parallel Event Loop wants to send a Message, it sends it through another Message Writer, out the right side of the Event Loop, and joins the Feedback "Pipe" carrying the Message Handler's "Message".  While Stream Channels are designed as 1-to-1, Messenger Channels are designed Many-to-Many.

 

Bob "Channel-Enthusiast" Schor

Message 4 of 12
(1,464 Views)

In your opinion, is there ever a time when it's more appropriate to use a shared variable instead of a channel wire? Because I noticed that there are real-time specific channel wires as well. So I'm trying to figure out what use case a shared variable still has. Although, if I'm remembering correctly, shared variables can used throughout multiple VI's at once or something. Probably can't do that with a channel wire.

0 Kudos
Message 5 of 12
(1,455 Views)
Solution
Accepted by topic author David99999

@David99999 wrote:

In your opinion, is there ever a time when it's more appropriate to use a shared variable instead of a channel wire? Because I noticed that there are real-time specific channel wires as well. So I'm trying to figure out what use case a shared variable still has. Although, if I'm remembering correctly, shared variables can used throughout multiple VI's at once or something. Probably can't do that with a channel wire.


Shared variable is for communication between applications/computers, channel wires only works within an application.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
Message 6 of 12
(1,442 Views)

@David99999 wrote:

In your opinion, is there ever a time when it's more appropriate to use a shared variable instead of a channel wire? Because I noticed that there are real-time specific channel wires as well. So I'm trying to figure out what use case a shared variable still has. Although, if I'm remembering correctly, shared variables can used throughout multiple VI's at once or something. Probably can't do that with a channel wire.


I really like @Yamaeda's excellent and clear distinction between Channel Wires (which work within an application and (Network) Shared Variables, which work between applications using Network communications).  Apples vs Oranges.

 

One of the things I especially like about Channel Wires is their mnemonic representation of Asynchronous communication between and among multiple components within and between VIs.

 

Here is an example from one of my Channel Message Handler VIs.  This, in fact, has 4 CMHs inside the VI.  The main CMH involves the central While Loop, and has the label "Station CMH".  The other three are contained in parallel VIs running in "little Blue Boxes" under the left-hand edge of the Station CMH -- the right-most one has the label "Video CMH".  The Green (Boolean) Channel Wire is a Tag Channel, while the Pinkish (Cluster) Channel Wires are all Messenger Channels.  The "Station" is running two hardware devices, a Balance and a Video Camera (managed by the Balance and Camera CMHs), and interact with the Video CMH to make 3-to-10 second Videos.  

Multi-Messanger.png

Bob Schor

Message 7 of 12
(1,355 Views)

Thanks Bob. When you say a Network Shared Variable works between applications, are you saying they are literally for communicating between completely separate Labview projects? Like having two compact rio's talking to each other?

 

And when I was comparing channel wires to shared variables, I was mostly talking about single process shared variables. From what I can tell, single process shared variables do the exact same thing as a basic channel wire. Correct?

0 Kudos
Message 8 of 12
(1,348 Views)

@David99999 wrote:

Thanks Bob. When you say a Network Shared Variable works between applications, are you saying they are literally for communicating between completely separate Labview projects? Like having two compact rio's talking to each other?


Yup. 🙂

They're some wrapped tcp/ip implementation under the hood so different applications on the same or different computers can communicate.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
Message 9 of 12
(1,339 Views)

Again, @Yamaeda said it best.  I've had a few projects that used Network Streams for communicating between different processes (LabVIEW Real-Time), and have also tried using Network Shared Variables, but (probably due to my incompetance) never got them to work reliably and "glitch-free", so I haven't used them in at least a decade.  But I use Channel Wires all the time (my favorites are the Stream and Messenger).

 

Bob Schor

0 Kudos
Message 10 of 12
(1,298 Views)