Table Of Contents

Sending Commands Using a Network Stream

Last Modified: February 19, 2020

Network streams enable high-throughput, but you can use network streams for lossless, low-throughput communication to send and receive commands.

To send commands using network streams, you must develop code to write and read data on the host and target applications.

What to Use

What to Do

  1. Create the controls to send commands on the panel of the host application. The controls you use are entirely dependent on the goals and requirements of your application.
  2. Create the following diagram in a VI for the host application. This diagram illustrates the network stream code on the host application.

    Customize the gray sections for your unique programming goals.

    The Create Network Stream Writer Endpoint node creates the writer endpoint in host application.

    The writer endpoint establishes a connection with the reader endpoint using the reader URL. Refer to the Details tab of the Create Network Stream Writer Endpoint or the Create Network Stream Reader Endpoint nodes for help specifying endpoint URLs.

    The Event Structure has one or more subdiagrams, or event cases. Only one event case executes when the structure executes to handle an event. You must place the Event Structure in a While Loop to handle multiple events. Configure the Event Structure to send commands to the target application. The code you write is entirely dependent on the goals and requirements of your application.

    spd-note-note
    Note

    To configure event cases and events, refer to Executing Code Based on an Event for more information.

    The Write Single Element to Stream node will write the command to the buffer, depending on the event case the user selects, which the reader endpoint on the target application reads from.

    spd-note-note
    Note

    You can use the Write Multiple Elements to the Stream node to add more than one data point at a time.

    If you want to minimize latency and ensure that commands propagate over the network immediately, place the Flush Stream node directly after the Write Single Element to Stream node. If low-latency is not a goal or requirement for your application, you can place the Flush Stream node outside of the While Loop.

    spd-note-note
    Note

    Use this node before using the Destroy Stream Endpoint node to ensure that the writer endpoint buffer is empty before you destroy it. This prevents data loss.

    If you want to shut down the network stream, call the Destroy Stream Endpoint to destroy the writer endpoint. To destroy a stream completely, however, you must destroy both the reader and writer endpoints.

  3. Create the following diagram in a VI for the target application. This diagram illustrates the entire network stream code on the target application.

    Customize the gray sections for your unique programming goals.

    The Create Network Stream Reader Endpoint node creates the reader endpoint on the target application. Once the reader endpoint exists, the writer endpoint will attempt to connect with it.

    The Read Single Element from Stream node continuously consumes commands from the buffer originally sent from the writer loop.

    spd-note-note
    Note

    The Read Multiple Elements from Stream node consumes batches of data from the buffer. You can use this node even if you used the Write Single Element to Stream node in the writer loop.

    A Case Structure contains one or more subdiagrams, or cases, exactly one of which executes when the structure executes. Case Structures behave similarly to switch statements or if-then-else statements in other programming languages. The data type of the value you wire to the selector terminal defines various cases that are available in the Case Structure. Use the case selector label to define the various cases in which different code executes. Place the code you want to execute in each case on the corresponding subdiagram of the Case Structure.

    spd-note-note
    Note

    In this example, the first Case Structure handles the data out and error out outputs of the Read Single Element from Stream node. The second Case Structure parses the commands.

    If you want to shut down the network stream, call the Destroy Stream Endpoint to destroy the writer endpoint. To destroy a stream completely, however, you must destroy both the reader and writer endpoints.

After you configure your target VI to meet your application's needs, you can use a network stream to send and receive commands. After you finish developing your host and target applications, your applications can send and receive commands. Be sure to run the target application first to ensure that the writer endpoint can locate the reader endpoint.

Troubleshooting

  • If you have trouble establishing a connection, try the following troubleshooting strategies:
    • Make sure you are using the recommended firewall settings.
    • (Windows 7) Make sure your Ethernet adapters are set to a "Home" or "Work" setting. If your network is set to Private, you may receive an error.
    • Make sure your endpoint URL is specified properly. Refer to the Details tab of the Create Network Stream Writer Endpoint or the Create Network Stream Reader Endpoint nodes for help specifying endpoint URLs.
    • Make sure you are not reusing endpoint names. If a name collision occurs, your intended endpoints will not connect and will error out. To avoid name collisions, consider using globally unique identifiers (GUID).
    • Make sure the endpoints you want to use are not connected to other endpoints. An endpoint can only connect to one other endpoint at a time.
    • Make sure you are specifying an endpoint that exists.
    • Make sure you are using compatible endpoints.
      • You cannot connect two reader endpoints to each other nor two writer endpoints to each other. You must use a writer endpoint and a reader endpoint.
      • The endpoints must have the same data type.
  • If you have trouble streaming data after establishing a connection, try the following troubleshooting strategies:
    • Make sure the nodes are compatible with one another. You cannot wire a Read Multiple Elements from Stream node or a Read Single Element from Stream node to a Create Network Stream Writer Endpoint node. You also cannot wire a Write Multiple Elements to Stream node, a Write Single Element to Stream node, or a Flush Stream node to a Create Network Stream Reader Endpoint node.
    • Make sure you are not reading or writing a number of elements larger than the endpoint buffer size.

Examples

Search within the programming environment to access the following installed example:

  • Simple Network Streams

Recently Viewed Topics