Labview - 6 10 2023

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

LabVIEW

2023-06-10
LabVIEW

Contents
Tutorial: Creating and Publishing a LabVIEW Web Service to the Application Web Server
............................................................................... 3

2 ni.com
LabVIEW

Tutorial: Creating and Publishing a LabVIEW Web


Service to the Application Web Server (Real-
Time, Windows)
Complete the following steps to create, publish, and exchange data between a
LabVIEW Web service and a web client, in this case, a web browser.

Note  For more information about concepts in


this topic, refer to the Web services
introduction. LabVIEW Web services are
available only in the LabVIEW Full Development
System and the LabVIEW Professional
Development System.
This tutorial contains the following sections:

1. Creating a Web Service


2. Testing and Debugging the Web Service
3. Configuring How Clients Access the Web Service
4. Publishing the Web Service to the Application Web Server
5. Accessing the Web Service with a Client
6. Monitoring the Web Service

Creating a Web Service


Develop a Web service in a LabVIEW project to group together and configure the VIs
and other files that make up the Web service. Complete the following steps to create
a Web service project and two HTTP method VIs.

1. Create a LabVIEW project to organize the Web service files. Save the project as
Tutorial.lvproj.
2. Right-click My Computer and select New»Web Service. LabVIEW adds a
Web service project item and folders under the target.
Note  

© National Instruments 3
LabVIEW

■ In this tutorial, you create a Web service


and publish it on the host computer. You
also can publish a Web service to a Real-
Time target by creating a Web service
project under the RT target in a LabVIEW
project.
■ (Windows) NI recommends using the NI
Web Server to host your Web service on a
Windows computer. Refer to Tutorial:
Creating and Publishing a LabVIEW Web
Service to the NI Web Server for detailed
instructions.

3. Right-click the Web service project item , select Rename, and name the
Web service TutorialService. The project appears as follows:

4. Right-click Web Resources and select New VI to create a new HTTP method
VI, which is a VI that receives HTTP requests from clients and returns data to
clients. LabVIEW opens a new VI from a template.
Note  By default, an HTTP method VI returns
data to clients via the connector pane. In this
tutorial, the HTTP method VIs return the
default JSON-formatted text responses.

4 ni.com
LabVIEW

When you create an HTTP method VI outside


this tutorial, you also can stream data to a
client in a custom format, such as a
customized HTML page or image data.
Configure the output format type on the
HTTP Method VI Settings page of the
Web Service Properties dialog box.

5. In the new VI, build the block diagram, front panel, and connector pane as
shown in the following figures.

Block Diagram Front Panel Connector Pane

When you publish this VI as part of a Web service, a web client can send an
HTTP request to invoke it. When invoked, this VI performs the following
actions:
■ Receives the values of any controls assigned to the connector pane
(except LabVIEW Web Service Request) from the HTTP request the client
made. LabVIEW Web Service Request acts like a refnum by identifying
the current HTTP request.
■ Computes the sum of a and b.
■ Returns the sum of those values to the client via the c indicator assigned
to the connector pane.
■ Executes the Read Request Variable VI and returns the query string the
client included in the current HTTP request, which LabVIEW Web Service
Request identifies.
Tip  In a more complex Web service, you
might use other VIs on the Web Services

© National Instruments 5
LabVIEW

palette to enable features such as HTTP


sessions, authentication, HTTP headers,
and cookies.

6. Notice that each control and indicator appears on the connector pane. With
the default Terminal output type, Web services can receive data from clients
or send data to clients only through inputs and outputs assigned to the
connector pane of an HTTP method VI.
Tip  Assign recognizable names to each
object. These names become part of the URL
that web clients use to exchange data with
the VI.
7. Save the VI as Add.vi.
8. Select File»Save As and select Copy»Open additional copy in the dialog
box that prompts. Ensure the Add copy to Tutorial.lvproj checkbox
contains a checkmark, and then click Continue.
9. Save the new copy as Subtract.vi.
10. Open the block diagram of Subtract.vi and replace the Add function with
the Subtract function. Rewire the controls and indicators.
11. Return to the project, right-click Web Resources, and select New Web
Resource. Rename the new project item AdditionalOperations.
12. Drag Subtract.vi under the AdditionalOperations project item. Save
the project, which should appear as in the following figure.

6 ni.com
LabVIEW

Notice the following features in the project tree:

■ LabVIEW appends an HTTP request type to the name of each HTTP method
VI, which by default is GET. You can change the type of HTTP request that
clients send to a specific HTTP method VI later in this tutorial.
■ The Web service project item also contains a Startup VIs folder. This
tutorial does not use startup VIs, but you can find more information about
them at the end of the tutorial.

Related Information
Components of a Web Service
Assigning Terminals to Controls and Indicators
Developing HTTP Method VIs: Choosing the Format of Output Data
Configuring HTTP Headers, Streaming, and Buffering
Managing a Project in LabVIEW

© National Instruments 7
LabVIEW

Testing and Debugging the Web Service


Before you publish the Web service to a target, you might want to test that the HTTP
method VIs communicate with clients as expected. You can place the Web service on
a debugging server directly from the LabVIEW project to allow a client to send
requests to HTTP method VIs. The debugging server provides a sandbox-like
environment.

1. (Windows) Right-click the Web service project item under My Computer


and select Application Web Server»Start (Debug Server).

(Real-Time) Right-click the Web service project item under the RT target
and select Start (Debug Server).
2. Open Add.vi. Notice that the Run button indicates the VI is reserved for
execution because LabVIEW loads the Web service and makes it available
on the debugging server for communication with clients.
3. Close Add.vi.
4. Complete the following steps to get the URL that clients use to invoke Add.v
i:
a. Right-click Add.vi and select Show Method URL to display the HTTP
Method URL dialog box.
b. In the Available Servers pull-down menu, select the item that
contains Local Debugging, and then click the Copy URL button.
Clients must communicate with Web services on the debugging server
through a different port than if you publish the service to a web server.
c. Close the dialog box.
Note  The Web service does not respond
to any requests from the client if the
HTTP Method URL dialog box is open.

5. Paste the URL from step 4 into a standard web browser and replace the {val
ue} variables with numeric values, such as: http://127.0.0.1:8001/T
utorialService/Add?b=2&a=3.

8 ni.com
LabVIEW

6. Browse to the URL. Add.vi receives the HTTP request from the browser,
computes the sum of the values for a and b, and returns a response with the
name and output value of each indicator assigned to the connector pane of A
dd.vi. In this tutorial, the response is a JSON string, which is the default
Terminal output format. Recall that Add.vi has two indicators assigned to
its connector pane, c and value, whose values the HTTP method VI returns in
the response.
{"c":5,"value":"b=2&a=3"}

Note  If you notice unexpected behavior,


you must stop the debugging session before
you can edit the Web service files because
you cannot edit the files while LabVIEW
reserves them for execution.
7. Repeat steps 4 and 5 for Subtract.vi, or update the URL from step 5 to
reflect the unique URL mapping for Subtract.vi: http://127.0.0.1:
8001/TutorialService/AdditionalOperations/Subtract?b=2
&a=3.
8. Repeat step 6 for Subtract.vi.
9. (Windows) Right-click the Web service project item and select Application
Web Server»Stop (Debug Server).

(Real-Time) Right-click the Web service project item and select Stop (Debug
Server).

Related Information
Testing and Debugging a Web Service on the Application Web Server

Configuring How Clients Access the Web Service


Web clients use URLs to exchange data with HTTP method VIs. Later in this tutorial,
you will enter a specific URL in a web browser to send data directly to the controls
on the connector pane of the HTTP method VIs.

© National Instruments 9
LabVIEW

Complete the following steps to define the URLs clients can use to access the HTTP
method VIs:

1. Right-click the Web service project item and select Properties to display
the Web Service Properties dialog box.
2. On the HTTP Method VI Settings page, notice that HTTP method VIs from the
Web Resources folder in the LabVIEW project appear in the table of Web
service VIs. Further, notice how placing Subtract.vi under the Addition
alOperations project item affects the URL mapping compared to Add.vi.
3. Select Subtract.vi in the table and notice the following features on the
URL Mapping tab at the bottom of the page:
■You can change the HTTP request type in the Method pull-down menu.
Use the default GET value for Subtract.vi.
■ You can choose to disable the standard format LabVIEW uses to map a
URL to the VI and instead customize the part of the URL that appears after
the service name. For this tutorial, use the standard URL mapping and
include the VI name.
■The URL field displays the base URL LabVIEW creates for the HTTP
method VI. For example, for Subtract.vi, LabVIEW creates the default
URL mapping /AdditionalOperations/Subtract.

10 ni.com
LabVIEW

Note  In a more complex Web service, you


might use the remaining tabs on this page to
configure the format that the VI returns
output data to the web client, as well as
security settings like permissions.
4. Click OK to close the Web Service Properties dialog box.
5. Save Tutorial.lvproj.

Related Information
Understanding URL Mappings and Query Strings in Web Services

Publishing the Web Service to the Application Web Server


You can publish the Web service you create to the Application Web Server. In this
tutorial, you publish a stand-alone Web service, but in a more complex application,
you might publish a Web service through a LabVIEW stand-alone application or an
installer. The Application Web Server hosts Web services on a network and provides
multiple security-related features to protect the network data exchange, including
Secure Socket Layer (SSL) encryption.

© National Instruments 11
LabVIEW

Note  (Windows) You can also publish your Web


service to the NI Web Server. Refer to Tutorial:
Creating and Publishing a LabVIEW Web Service
to the NI Web Server for detailed instructions.
Complete the following steps to publish the Web service to the Application Web
Server running on the local computer:

1. Right-click the Web service project item and select Application Web
Server»Publish. The Deployment Progress dialog box appears.
2. If LabVIEW publishes the Web service without errors, click the Close button.
Web clients can now exchange data with the HTTP method VIs.

Related Information
Publishing Web Services
Setting up the Application Web Server for Web Services

Accessing the Web Service with a Client


Complete the following steps to send data to the published Web service from a web
browser via a URL:

1. Right-click Add.vi and select Show Method URL to display the HTTP
Method URL dialog box.
2. In the Available Servers pull-down menu, select the item that contains
Application, and then click the Copy URL button. This URL contains a
different port number than the URL for the debugging server. LabVIEW uses
this port number for communication with the Web service files running on the
Application Web Server.
3. Close the dialog box.
4. Paste the URL into a standard web browser and replace the {value}
variables with numeric values, such as: http://127.0.0.1:8080/Tuto
rialService/Add?b=2&a=3.
Note  If the client is located on a different
system than where you publish the Web

12 ni.com
LabVIEW

service, you must replace 127.0.0.1 with


the actual IP address of the hosting system
or the hostname you customize in NI Web-
based Configuration & Monitoring.
5. The browser returns a JSON response with the output values from each
indicator assigned to the connector pane.

Related Information
Sending Data to a Deployed Application Using URLs

Monitoring the Web Service


Complete the following steps to check the status of Web services published to the
Application Web Server:

1. Right-click the Web service project item and select Application Web
Server»Manage Web Server. NI Web-based Configuration & Monitoring
opens in a web browser.
2. Click the Web Services Management button in the left-hand sidebar .

3. Select TutorialService in the Published Web Services list. If the list is


empty, click the Refresh button.
4. Notice that the status of the Web service is Running. To pause, resume,
restart, and unpublish Web services, use the buttons at the bottom of the
page.
5. On the right side of the page, notice that the URL mappings for the HTTP
method VIs, Add and AdditionalOperations/Subtract, appear with
the HTTP method they perform.

Where to Go Next

Communicating with a LabVIEW Application from Web Clients
■ Overview: Web-based Communication with a LabVIEW Application—An
overall introduction to LabVIEW Web Service

© National Instruments 13
LabVIEW

■ Publishing a Web Service through a Stand-Alone Application—Include a Web


service in a LabVIEW stand-alone application, so that the Web service runs
when the application runs.
■ Publishing a Web Service through an Installer—Include a Web service in a
Windows installer, so that the Web service runs when you finish the
installation process.

Developing HTTP Method VIs or Developing Startup VIs to Initialize and
Communicate with HTTP Method VIs—Develop more complex HTTP method
VIs, as well as startup VIs that run as part of a Web service application but do
not exchange data with a web client.
■ Integrating Static Content into a Web Service—Publish static content such
as HTML pages or images with a Web service.

Example Code
Refer to the following LabVIEW projects for examples of using Web services:

■examples\Connectivity\Web Services\Weather Monitor\W


eb Services — Weather Monitor.lvproj
 Open example   Find related examples

14 ni.com © 2023 National Instruments Corporation.

You might also like