How to Programmatically Generate Global Virtual Channels with LabVIEW

How to Programmatically Generate Global Virtual Channels with LabVIEW

How many times have you had to create numerous global virtual channels for a project? It’s the tedious task of going into NI MAX and creating a channel one by one based on an IO sheet.

In this tutorial, I will show you how to programmatically create and save a global virtual channel in LabVIEW. I will tie it all together by generating the same signals from an IO sheet in Excel.


I wanted to reuse a library DMC developed that controls relays and reads their feedback. I preffered not to modify this library because we were referencing the repo in another DMC project; modifying the library would change it for another team’s project. The inputs to this library were global virtual channels connecting to the control and feedback of each relay. I could have manually created global channels for our 100+ relays, but instead, I let LabVIEW do all the work.

NI Max Setup

In this example, I added a simulated cDAQ to NI Max. I also added two NI 9403 DIO module and gave it the names below:

screenshot of a computer screen

The Code

The below code is what we will use to create 6 global virtual channels. This code will iterate between a relay control (Digital Control) and relay feedback (Digital Feedback).

Screenshot of code on a computer

First, I do modulo two to iterate between the control and feedback enum I made. I then use “format into string” to create the name of the channel. 

To prevent errors, clear the task name. I also like to delete the task in case it exists. If the task doesn’t exist, an error will be thrown, so make sure to clear errors coming out of that VI.

  • Factoid: You can feed a string into a task name and it will typecast automatically for you.

Create the task by feeding the task coming out of the previous VI into the “New Task Name” input of “DAQmx Create Task”. 

  • Factoid: You can feed a task into a string input as well*, and it will be typecast automatically.

Create the hardware line you want to connect the channel to. I do this by using format string again. Make sure to use the name of the module you have in NI Max.

  • Factoid: If you want a list of all available channels, you can drop a “DAQmx Physical Channel” onto the block diagram and browse for available lines.

Image of code on a screen

Create the DAQmx virtual channel. I use a Digital Output for the controls and Digital Input for feedback.

Save the global channel using "DAQmx Save Global".

Below, are the channels that were created in NI Max.

screenshot of code on a laptop

Reading From an IO Sheet

Now that you can programmatically create global virtual channels, you can take it a step further and create channels from information contained in an IO sheet like the one below.

screenshot of spreadsheet on a computer

You can read this information by using “Read Delimited” and removing the top row of headers. After that, feed the inputs into the code from before, like so.

Screenshot of code on a laptop

And viola! Four new global virtual channels have been made from your spreadsheet.

Screenshot of four global virtual channels

Deleting Channels

You can delete the channels programmatically just as easy as you made them by using “DAQmx Delete Saved Global”. 

Screenshot of laptop screen

Why is this useful?

This is useful because you don’t have to manually type in global virtual channels to NI Max. This prevents fat fingering a wrong IO channel. This will also save you time depending on how many channels you have. It also prevents errors from happening by pulling straight from the IO list. When collaborating with coworkers on an IO list, you don’t have to worry about someone else making a change in the list that doesn’t get propagated into the code. The code will pick up on it!

Another benefit of note is you can decrease the amount of fluff in an NI Max file. All you will need in your NI Max file are the hardware and module names. If a coworker needs to add a channel, they can add it to the IO sheet, and it will automatically be imported into your code when you pull their code. No more pushing and pulling the NI Max file nor importing and exporting channels causing less committing and merging of NI Max channels between developers.

Learn more about DMC’s LabVIEW Programming and contact us today for your next project!


There are currently no comments, be the first to post one.

Post a comment

Name (required)

Email (required)

Enter the code shown above:

Related Blog Posts