Establishing a TCP/IP Network Communication Between LabVIEW and Simulink®

Updated Sep 30, 2024

Environment

Software

  • LabVIEW
  • MATLAB
  • Simulink

This article provides step by step guidance on how to establish TCP/IP communication between LabVIEW and Simulink®. Whether your application requires LabVIEW or Simulink® to function as a server or a client, this guide will provide the necessary configuration details to meet your specific requirements.

Configure LabVIEW as Server and Simulink as Client

Configure LabVIEW:
To configure LabVIEW to act as a Server, you need to create a VI. You can create a simple VI using the steps in Basic TCP/IP Communication in LabVIEW or refer to the Simple TCP.lvproj in the labview\examples\Data Communication\Protocols\TCP\Simple TCP directory for an example of using the TCP VI and functions. Here you can find Simple TCP - Server.vi. Configure the port number for TCP/IP communication.

Server. VI.jpg

Configure Simulink:

Step 1: Create a New Model

  • On the Simulink start page, click Blank Model>>Create Model. An empty Editor window opens.
  • In the toolstrip, click Save to assign a name to your new model.


Step 2: Open the Block Library

  • In the toolstrip, click Library Browser in the Simulation tab.
  • The Simulink Library Browser opens. The left pane contains a tree of available block libraries in alphabetical order. Click Instrument Control Toolbox.
  • To use a block, add it to an existing model or create a new model.


blocklibbrsr.png

Step 3: Drag the Instrument Control Toolbox Blocks into the Model
  • To use a block in a model, drag the block into the Simulink Editor. For this model, you need one instance of the TCP/IP Receive block in your model. 

Send and Receive blocks.png
 
  • Make the connection between the output port of the TCP/IP Receive block and the input port of the Scope block.

Simulink receiver.jpg

Step 4: Specify the Block Parameter Values
  • Double-click the Receive block to open its parameters dialog box. 
  • Set the Remote address to localhost and fill the Port field. The port number in Simulink must be the same as configured in LabVIEW.
  • Click OK.
View the Results:
After configuration steps, run the LabVIEW Server vi and after that run Simulink Client simulation. The number sent via LabVIEW will be visible in Simulink as well.

Results.jpg

Configure Simulink as Server and LabVIEW as Client

Configure Simulink:
Step 1: Install Simulink Desktop Real-Time Kernel
The Simulink Desktop Real-Time Kernel needs to be installed in Simulink. To do that go to Matlab Workplace and in the Command Window type sldrtkernel -setup

command.png
After the successful installation, there is a need to restart the PC.

Step 2: using the Simulink Stream Output block
The Simulink Stream Output function is located in Simulink Library Browser>>Simulink Desktop Real-Time - 1 section.

Stream Output.png


In the Block Parameters section, there is a need to create a new board by clicking on the Install new board button.

Install new Board.png

In the opened drop-down menu go to Standard Devices>>TCP Protocol

TCP protocol.png

In the opened window there is a need to configure the Connection mode: Server, select the Local TCP port: (the one there is a need to use), the Remote TCP port: (can be any port), and the Remote host name or IP address: localhost.

local and port.png
In the Block Parameters section, the Format string: represents the data type to transfer. In this case, it is '%g' meaning the representation of numeric value is Integer. If there is a need to transfer a double value, you should use '%f'.

string format.png

Configure LabVIEW:
In LabVIEW a simple VI needs to be created as a TCP client. You can create a simple VI using the steps in Basic TCP/IP Communication in LabVIEW or refer to the Simple TCP.lvproj in the labview\examples\Data Communication\Protocols\TCP\Simple TCP directory for an example of using the TCP VI and functions. Here you can find Simple TCP - Client.vi. Configure the port number for TCP/IP communication. 

LabVIEW Client.png
The bytes to read value should represent the digits of the numeric data. As LabVIEW receives the data in string values, it represents each digit as one byte, so if you are sending a two-digit value, you should read it in two bytes.

View the results:
When starting the transfer of the data, you should run the Simulink model first and then the LabVIEW VI.

Result.png

Please see attached example of the LabVIEW simple VI and Simulink simulation to establish a TCP/IP communication, where Simulink is a server and LabVIEW acts as client.