<< Chapter < Page Chapter >> Page >
The creation of this content was supported in some part by NSF grant 0538934.

G is a high level, data-flow graphical programming language designed to develop applications that are

  • Interactive
  • Execute in Parallel
  • Multicore

The program is a block diagram edited in the Block Diagram programming window.

A flow chart showing connections and movement between items like graphs, dice, arrows containing a plus-sign, and other objects in a G-block.
G Block Diagram

The program input data and results are manipulated and displayed in the Front Panel window.

A screen capture of the G user interface with a sinusoidal graph plotted on a horizontal axis, labeled time, and a vertical axis, amplitude.
G User Interface

Hello graphical world

The first program is to display the text “Hello graphical interactive parallel multicore world” in the Front Panel window.

Right click on the Block Diagram window and select String Constant from the Functions>>Programming>>String menu.

Drag and drop the String Constant onto the Block Diagram window as show in [link] .

A screen capture of a window titled functions, with buttons to search and view, a hierarchical list beginning with Programming, then String, and two objects, labeled string constant and empty string.
String Constant

Type in “Hello graphical interactive parallel multicore world.” in the String Constant .

A string constant that reads, Hello graphical interactive parallel multicore world.
"Hello…world" String Constant

Right click in the Front Panel window and select a String Indicator from the Controls>>Modern>>String&Path menu.

A screen capture of a window titled controls, with buttons to search and view, a hierarchical list beginning with Modern, then String and Path, and two objects, labeled string control and string indicator.
Select String Indicator

Drop it into the Front Panel window.

A beveled rectangle inside a gridded box labeled string.
String Indicator

Return to the programming window. Notice the string terminal corresponding to the string indicator in the Front Panel window. As you approach the string constant from the right, the wiring terminal is highlighted and the pointer turns to wire spooler.

A pink box containing the caption, hello graphical interactive parallel multicore world. To the right of the pink box is another pink box labeled string, containing a shaded grey rectangle containing the letters a, b, c. The boxes are not connected, but an object that resembles a spool of wire is connected to the box on the left.
Wiring the G Diagram

Click the “Hello graphical interactive parallel multicore world” terminal and then click on the String Indicator triangular terminal to wire the terminals.

A pink box containing the caption, hello graphical interactive parallel multicore world. To the right of the pink box is another pink box labeled string, containing a shaded grey rectangle containing the letters a, b, c. The boxes are connected by a zig-zag pink line.
Wired G Block Diagram

Save your program as Hello, World.vi . Return to the Front Panel window. Click the run button (➯). You have successfully completed and executed your first G program.

A grey grid labeled String containing a grey rectangle with a caption that reads, hello graphical interactive parallel multicore world.
Hello, World G Program Executed

Arithmetic expressions

The next program converts degrees from Fahrenheit to Celsius using the formula

C = 5 9 ( F 32 ) size 12{C= { {5} over {9} } \( F - "32" \) } {} .

In the Block Diagram window, select the subtract, multiply and divide from the Functions>>Mathematics>>Numeric menu

A screen capture of a window containing a hierarchical list, beginning with Mathematics, and below it, numeric. Below the list are four objects, which are yellow triangles containing operators, and they are labeled add, subtract, multiply, and divide.
Numeric Operations

Wire the subtract , multiply and divide functions as shown in [link] .

A diagram connecting three yellow triangles containing operations together. Subtract is connected to Multiply, and multiply is connected to divide.
Subtract, Multiply and Divide

Right click on the upper left terminal of the subtract function and select Create>>Control from the pop-up menu.

A screen capture of the pop-up menu out of the subtract triangle, with the option Create, then Control selected.
Create Control

Re-label x as Fahrenheit and wire the terminal as shown in [link] .

An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide.
Fahrenheit Input Control

Right click on the lower left terminal of the subtract function and select Create>>Constant and type 32.0 .

An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide. Also connected to subtract is an orange box containing the number 32.
Fahrenheit Numeric Constant

Repeat the process to generate numeric constants for the multiply and divide function with 5.0 and 9.0 respectively.

An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide. Also connected to subtract is an orange box containing the number 32. Also connected to multiply is an orange box containing the number 5. Also connected to divide is an orange box containing the number 9.
Fahrenheit Numeric Constants

To complete the program, right click on the right terminal of the divide function and select Create>>Indicator . Re-label x/y as Celsius . The final diagram is shown in [link] .

An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide. Also connected to subtract is an orange box containing the number 32. Also connected to multiply is an orange box containing the number 5. Also connected to divide is an orange box containing the number 9. Divide is then connected at the end to an orange box labeled celsius, containing the value 1.23.
Fahrenheit to Celsius G Diagram

Switch to the Front Panel window to run the program. Save the program as Celsius.vi . Try various Fahrenheit values to see the corresponding Celsius values. You have successfully finished a Fahrenheit to Celsius calculator.

A grey grid containing two boxes. The first is labeled Fahrenheit, and contains the value 100, with up-down arrows to the left of it allowing the user to adjust the value. The second is labeled celsius, and contains the value 37.7778.

Functions

Click on empty space and drag to select the entire diagram.

An orange box labeled Fahrenheit, with the orange outside line labeled DBL, and containing the value 1.23, is connected to subtract, which is connected to multiply, which is connected to divide. Also connected to subtract is an orange box containing the number 32. Also connected to multiply is an orange box containing the number 5. Also connected to divide is an orange box containing the number 9. Divide is then connected at the end to an orange box labeled celsius, containing the value 1.23. The entire diagram has a dashed box around it.
Select G Block Diagram

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to g programming. OpenStax CNX. Mar 15, 2010 Download for free at http://cnx.org/content/col11192/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to g programming' conversation and receive update notifications?

Ask