Applicable Versions
NetSim Standard
NetSim Pro


Applicable Releases
v11
v12


Software Tools used:

  • NetSim v11.0 (or higher versions) 64-bit 
  • MATLAB R2016a (or higher versions) 64-bit
  • SIMULINK


NetSim can be interfaced with MATLAB, which allows users to interact with MATLAB during run-time. MATLAB can interact and control the behavior of  SIMULINK Models. In order to interface NetSim with SIMULINK the existing MATLAB Interfacing can be utilized. NetSim can interact with SIMULINK Models and control its behavior via MATLAB.

SIMULINK - Water Supply System:

In this example we consider a Water Supply System Model in SIMULINK as shown below:

Reference: https://in.mathworks.com/help/physmod/hydro/examples/water-supply-system.html

This model consists of four tanks and the readings of the volume of water in each of the tanks are taken periodically and sent to MATLAB workspace. 

NetSim - Simulink Interfacing:

Upon interfacing NetSim with MATLAB the following tasks are performed during simulation start:

  • MATLAB Engine process is initialized
  • MATLAB Desktop window is loaded
  • SIMULINK Model is loaded

Upon simulating a network created in NetSim the following tasks are performed periodically:

  • SIMULINK Model is simulated
  • SIMULINK Model is paused
  • NetSim reads the data generated by SIMULINK from MATLAB workspace
  • Appends the readings to the packet payload as packets are formed


During the Simulation the SIMULINK Model is started and paused several times for NetSim and SIMULINK simulations to run synchronously. The readings obtained from SIMULINK are read from MATLAB workspace and appended to the payload of the packets generated in NetSim. In this example readings are taken every one second and appended to the packet payload. 


Output/Metrics specific to this example:

  • NetSim Event Trace - MATLAB event is registered to periodically interact with MATLAB
  • Wireshark Packet Capture - Can be enabled in the source nodes or in the destination to view the packet payload containing the readings taken from SIMULINK Model.


Modifications done to NetSim Source codes:

Projects: Application

Files

  • Application.c, 
  • Application.h, 
  • Application.vcxproj (Project file)

Sections of source code modified:

  • Application.c
    • fn_NetSim_Application_Init() - Initializes MATLAB, Loads SIMULINK Model, starts and pauses SIMULINK simulation, registers MATLAB_EVENT
    • fn_NetSim_Application_Run() -  Handles and registers a MATLAB_EVENT 
    • copy_payload() -  Appends SIMULINK readings to the packet payload 
    • fn_NetSim_Application_Trace() - Adds MATLAB_EVENT to NetSim Event Trace log file
    • fn_NetSim_Application_Finish() -  Stops SIMULINK simulations and terminates the MATLAB Engine process
  • Application.h
    • MATLAB Engine variable - Used to initiate and interact with MATLAB Engine process
    • MATLAB Event definition - Used to trigger periodic calls to MATLAB
  • Application.vcxproj - This is a Visual Studio project file used to load and manage the source codes related to Application layer in NetSim
    • path to MATLAB application
    • path to MATLAB include directory
    • path to MATLAB lib directory
    • information related to dependent MATLAB library files


Procedure to setup MATLAB for this example:

  • Create a system environment variable with name as "MATLAB_PATH" and value as "<path of MATLAB Root directory>"

Note: The procedure to create an environment variable and the path of the MATLAB root directory will vary based on the Windows OS version and the MATLAB software version respectively.

  • Place the downloaded water_supply_system.slx file in <MATLAB_ROOT_DIRECTORY>. For example: C:\Program Files\MATLAB\R2016a


Procedure to setup NetSim for this example:

  • Download the files attached in this article.
  • Go to <Netsim_Install_Directory>/src/Simulation path
  • Copy the folders - Application, IP, lib and Include

  • Create a new folder in your system and paste the copied folders and file in it.

  • Place the downloaded NetSim.sln solution file inside the New folder along with the previously copied Application, Include, IP and lib folders.
  • Replace the downloaded Application.c, Application.h and Application.vcxproj inside the Application folder present inside your new folder. 
  • Double click on the NetSim.sln file present in your new folder to load the source codes in Visual Studio
  • Change the solution platform from win32 to x64 for 64 bit NetSim.

  • Expand the Application project and you will be able to find the modifications done to Application.c and Application.h files as explained above. Further, the modifications done to the Application project settings (Application.vcxproj) can be viewed in the Application Project properties window.
  • Now to obtain a new DLL file for running this example in NetSim, right-click on the application project and select Rebuild. This will start the process of building the source codes and upon successful build, you will get a libApplication.dll file in your new folder inside the DLL directory.
  • Copy the newly built libApplication.dll file from your new folder and place it in <NetSim_Install_Directory>/bin folder after suitably renaming the already existing libApplication.dll file suitably as a backup.

(NOTE: above step can be ignored in v11.1 and above as Netsim automatically takes care of updating DLL files according to the code modification)

  • Open the Configuration.NetSim file from the attached Config_file zip folder after extracting it

  • In the above example there are four nodes corresponding to four Tank Stations in the SIMULINK model. 
  • Traffic is configured from each Tank Station to the Control Station. Packets are generated every one second.
  • Run Simulation for 100 Seconds.
  • As simulation starts, MATLAB gets initialized, SIMULINK model gets loaded, Wireshark is initialized and displays the packets captured in the destination device (Wired_Node_7).


Analyzing the output Metrics specific to this example:

  • During the Simulation NetSim and SIMULINK simulations run synchronously as shown below:



  • Wireshark displays the packets received at the destination and the payload of the packets contain the readings taken in SIMULINK as shown below:

  • The throughput plot associated with the wireless link between the Tank Stations and the Sink is shown below:

  • The Application throughput for transmission from each of the four tank stations is shown below:


 

  • After the Simulation NetSim Event Trace log file can be opened using the Open Event Trace link available in the Results Dashboard. Event trace contains entries related to MATLAB_EVENT trigger as shown below:

  • Source codes can be further modified, to analyze the readings obtained from SIMULINK in the APPLICATION_IN Event which is part of fn_NetSim_Application_Run() function.


Note: For different versions of MATLAB, SIMULINK *.slx file can be found as explained in https://in.mathworks.com/help/physmod/hydro/examples/water-supply-system.html