Debugging Type3157 Python scripts with Visual Studio Code

TRNSYS Type3157 is a convenient component that allows you to code processes in Python. However, it does not provide an integrated development environment. You will have to code using an editor, but if you want to create a complicated script, you will want to use a proper development tool.

Calling Python from TRNSYS with CFFI

Type3157 is a component of TRNSYS, but it is a Python embedded program. I thought it would be possible to debug it by attaching it to the TRNSYS process from Visual Studio Code.

I thought so and tried it, and was able to debug it successfully.

Prerequisite.

It is assumed that all files, including TRNSYS Dck files and Python scripts, are stored in the same folder.

The package debugpy is used for debugging. Install it beforehand on the command line as follows.

pip install debugpy

This article explains debugging using Type 3157’s Example, 01-SimplePolynomial.

Preparation for debugging

Start Visual Studio Code and configure the following settings.

Open the project folder

First of all, open the 01-SimplePolynomial folder.

Select [File]-[Open Folder] from the menu and open the folder “C:\TRNSYS18\TRNLib\CallingPython-Cffi\Examples\01-SimplePolynomial”.

Editing the Python script

Open the Python script to be debugged (Polynomial.py) and add a process to wait for attachments from Visual Studio Code when TRNSYS runs.

Add the following code to the beginning of the script

import sys
import debugpy  # a debugger for Python
python_path = os.path.join(sys.prefix,'python.exe')
debugpy.configure(python=str(python_path))
debugpy.listen(('localhost', 5678))
debugpy.wait_for_client()  # blocks execution until client is attached

E.g. Polynomial.py with debugging process

# Python module for the TRNSYS Type calling Python using CFFI
# Data exchange with TRNSYS uses a dictionary, called TRNData in this file (it is the argument of all functions).
# Data for this module will be in a nested dictionary under the module name,
# i.e. if this file is called "MyScript.py", the inputs will be in TRNData["MyScript"]["inputs"]
# for convenience the module name is saved in thisModule
#
# MKu, 2022-02-15

import numpy
import os

# --- Added the following lines ---
import sys
import debugpy  # a debugger for Python
python_path = os.path.join(sys.prefix,'python.exe')
debugpy.configure(python=str(python_path))
debugpy.listen(('localhost', 5678))
debugpy.wait_for_client()  # blocks execution until client is attached
# ---------------------------------

thisModule = os.path.splitext(os.path.basename(__file__))[0]

Define debug settings in launch.json

Open the debug configuration file (launch.json) and add two settings.

  • “TRNSYS:launch” Configuration for launching TRNSYS
  • “Python-debug” configuration for attaching to Type3157 Python scripts

Just copy and paste the following contents into the launch.json file.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "TRNSYS:launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "C:\\TRNSYS18\\Exe\\TrnEXE64.exe", // path to TRNSYS
            "cwd":"${workspaceFolder}",
            "console": "integratedTerminal",
            "args": ["Polynomial.dck"],
        },
        {
            "name": "Python-debug",
            "type": "python",
            "request": "attach",
            "port":5678,
            "host":"localhost"
        }
    ]
}

If the launch.json does not yet exist, please follow these steps to create it.

  1. Click on the debug button
  2. Click on “create a launch.json file”
  3. Select “Python File” to create launch.json

Add a breakpoint

Open Polynomial.py and set a breakpoint. In this example, the breakpoint is set by clicking on the line where the output value is calculated (line 62).

Start debugging

Note: C/C++ and Python extensions are required for Visual Studio Code to perform debugging. If they are not already installed, please refer to the instructions below to install them.

Run TRNSYS first, followed by the Python script.

  1. Select “TRNSYS:Launch
  2. Click “Start-debugging” to start debugging

TRNSYS will launch. In this example, the Online Plotter appears on the screen, but the calculation stops immediately and the Python script is waiting for debugging.

Then, run the Python script.

  1. Select “Python-debug
  2. Click on the “Start-debugging” button to start debugging. (Be sure to run the script with Polynomial.py open.)

When debugging starts, the process stops at the breakpoint you have just set.

After this, the script can be debugged using Visual Studio Code features.

CAUTION!!!

If you are running the script in Simulation Studio without Visual Studio Code, be sure to comment out the debug server code. If it is not commented out, the calculation will not proceed from this line because it is waiting for the debugger to run.

import numpy
import os

# # --- Added the following lines ---
# import sys
# import debugpy  # a debugger for Python
# python_path = os.path.join(sys.prefix,'python.exe')
# debugpy.configure(python=str(python_path))
# debugpy.listen(('localhost', 5678))
# debugpy.wait_for_client()  # blocks execution until client is attached
# # ---------------------------------

thisModule = os.path.splitext(os.path.basename(__file__))[0]

# Initialization: function called at TRNSYS initialization

Here is a video showing how it works.

Installing Extensions to Visual Studio Code

Installing C/C++ and Python extensions to Visual Studio Code for debugging.

  1. Click on the “Extension” icon
  2. Type “C/C++” from the keyboard to search for the extension
  3. Click “Install” to install.

Install Python extensions as well.

  1. Click on the “Extension” icon
  2. Type “Python” from the keyboard to search for the extension
  3. Click “Install” to install.

Test Environment

The article is confirmed to work properly under the following conditions.

  • Windows11 Pro(64bit, 22H2)
  • TRNSYS18.05.001(64bit)
  • Visual Studio Code Version: 1.77.3
  • Python 3.10.7
  • debugpy 1.6.7
Pocket

Leave a Comment

Your email address will not be published. Required fields are marked *