Applicable Versions
NetSim Standard
NetSim Pro


Applicable Releases
v14.1


Software: Python 3.11.1 or higher with NumPy and Matplotlib modules installed.


Download link: https://github.com/NetSim-TETCOS/NetSim_Python_Interface_v14.1/archive/refs/heads/main.zip


Follow the instructions specified in the following link to download and setup the Project in NetSim:

https://support.tetcos.com/support/solutions/articles/14000128666-downloading-and-setting-up-netsim-file-exchange-projects


The provided workspace allows users to interface NetSim with Python scripts via a socket interface. This is achieved by establishing a socket connection between NetSim and a Python Script during runtime, which is used to send and receive data. Lot of work related to machine learning, artificial intelligence and specialized mathematical algorithms which can be used for networking research, can be carried out using this workspace.


NetSim API’s to interact with Python

Description

init_python_interface_socket()

This function initializes winsock library and setup the TCP listening socket.

send_value_to_python(struct send_to_python* Var)

This function is used to send data from NetSim to python.

receive_value_from_python(struct get_from_python* Var)

This function is used to receive data from python to NetSim.

close_python_interface_socket()

This function used for closing socket connection.


Steps to Initialize variable for socket interface with Python:

  • Open Source Code, under NetSim_Python_Interfacing Project, in the Python Interface.h file, 
  • In struct send_to_python declare variables of specific data type to send to python.
  • In struct get_from_python declare variables of specific data type to receive from python.
  • Here users can declare the datatypes such as single variable int , 1d array of type double or 2d array of type int etc as per requirements shown in the image below.

  • In the Python Interface.c file,
  • serialize_send_to_python(unsigned char* buffer, struct send_to_python* value) function converts any datatype variables to bytes and sends to python.
  • deserialize_send_to_python(unsigned char* buffer, struct send_to_python* value) function converts any datatype variables to bytes and sends back to netsim.
  • Users must initialize size of array and convert to bytes for each variables declared as shown in figure below.

  • Rebuild the NetSim_Python_Interfacing project.

Example:

Select the section of code where you want to interface NetSim with Python. Consider Application.c file.

  • Include the header and lib for calling the NetSim Python Interfacing API’s.
    • #include "../NetSim_Python_Interfacing/Python_Interface.h"
    • #pragma comment(lib,"NetSim_Python_Interfacing.lib")
  • Call the init_python_interface_socket() in the int fn_NetSim_Application_Init() function

  • Call the close_python_interface_socket() in the fn_NetSim_Application_Finish() function

  • Example of passing current time and packet Id, from APPLICATION_IN event (from line 95) to python.

  • Open NetSimInterface.py, to extract the values from NetSim to python use the following code.

  • To send values back to NetSim, For eg: a=5 and b =10

  • To receive the values from python to NetSim, from APPLICATION_IN event (line 104)

  • Now Rebuild Application project.
  • Now create a simple scenario in NetSim and run.
  • You will get the below message in NetSim console window.

  • Now run the NetSimInterface.py file in the command prompt from the saved location.

  • Values sent from NetSim are printed on Python console, as well as values sent from Python are printed to NetSim console.



Related examples:

5G-DL-Power-Control-to-Maximize-Sum-Throughput-Using-RL_v14.1.pdf (tetcos.com)