How to use Python scripts?

Script Manager

To open the Script Manager, click on the Script button in the main interface. View of the Script Manager dialog

Adding a new script

Click on the Add button, and browse the filesystem to select of script file. Python scripts should have the .py extension.

When the script is added to the project, a new entry appears in the Script Manager: A script added into the script manager

Executing a script

Click on the Execute button of a script in order to execute it.

Removing a script

Click on the Delete button of a script in order to remove it from the project.

Opening a script

Click on the Open button of a script in order to open it into the editor defined by the system for Python scripts.

Script execution classes

Each script has an execution class, which defines when the script should be executed. The classes are:

On demand
A script with the On demand attribute is not executed automatically by ModbusPal. The user has to click on the Execute button of this script.
Before init
A script with the Before init attribute will be automatically executed when the project is loaded, right before the MODBUS slaves and automations information are processed. Examples of scripts that should be executed before init are: scripts registering new generators, bindings, functions.
After init
A script with the After init attribute will be automatically executed when the project is loaded, only after the MODBUS slaves and automations information have been processed. Examples of scripts that should be executed after init are: scripts creating a user interface.

Example

HelloWorldFrame.py
from javax.swing import *
from java.awt import *

class HelloWorldFrame(JFrame):

  def buttonPushed(self,event):

    self.setVisible(False);

  def __init__(self):

    self.setTitle("Hello world");
    self.setSize(300, 100);
    self.setLayout(BorderLayout());
    self.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    # create the Label
    self.label = JLabel('Hello, world !');
    self.add(self.label, BorderLayout.CENTER);

    # create the button
    self.button = JButton('OK',actionPerformed=self.buttonPushed);
    self.add(self.button, BorderLayout.SOUTH);

# Create the Hello world frame:
frame = HelloWorldFrame();

# Make it visible:
frame.setVisible(True);

# Make it the top window:
frame.toFront();
      

Special variables

ModbusPal
Instance of the current ModbusPalProject java class.
mbp_script_path
A String containing the complete pathname of the current script file. Ex: "C:/directory/script.py"
mbp_script_directory
A String containing only the path of the current script file. Ex: "C:/directory"
mbp_script_file
A String containing only the name of the current script file. Ex: "script.py"

API

Please consult the Javadoc of ModbusPal in order to get more information on all the classes introduced in this page.