Skip to content

How to write your first script

Create your first Profinity script to automate tasks and interact with your CAN bus system. This guide adds a script component, writes a run script that prints a message, runs it and finds the output.

Before you start, you need:

  • Profinity 2.3 installed and running.
  • A Desktop, Server, or Enterprise licence. An unlicensed instance cannot add or run scripts. Refer to Licensing.
  • An active profile with components configured.
  • Basic understanding of C# or Python. Profinity scripting uses IronPython for Python, which limits the Python syntax available, and also supports Lua. Refer to Supported Languages.
  • Scripting enabled in Profinity configuration, as in the first step below.

Scripts run with application permissions

A script runs inside Profinity with the same operating system permissions as the application, so review any script before running it. Refer to the Scripting overview for the security context and behaviour.

Enable scripting

  1. Select ADMIN in the side menu and open the System Configuration pill. Refer to System Configuration for detail.
  2. Find the Optional Capabilities section.
  3. Enable Enable Scripting.
  4. Select Save.

Add a script component and set its mode

A script is a component in your profile, and the language is set by which script component you add. For a first script use a run script, whose type is set by the Script Mode of the script component. The main types are Run scripts, which run on demand or on a schedule (time interval or cron), Receive scripts, which run when selected CAN messages are received, and Service scripts, which are long-running background services with lifecycle control. Tag Change and Alert modes also exist, and Script Types describes them.

  1. Select ADD COMPONENT. Refer to Adding Components to Your Profile.
  2. Select the CSharp Script, Python Script or Lua Script component. This opens the script component settings and the script editor together.
  3. Set the component name, and add a description if wanted.
  4. Set Script Mode to Run On Demand, which runs the script manually from the UI. Run On Time Interval runs it at a fixed interval, for example every few minutes, and Run On CRON Schedule runs it on a Quartz cron expression.
  5. Optional: enable Auto Start Script to start the script automatically when Profinity starts, and enable Log Script Output, which is off by default, so that the script's output appears in the Profinity log.

Write the script

Use the same patterns as in Run Scripts. C# implements IProfinityRunnableScript with a Run() method, and Python defines functions and calls them from the top level, with no import profinity because the Profinity host object is provided. In C#, use Profinity.Console.WriteLine for output, and in Python use print(). Refer to Console.

C

using System;

public class MyFirstRunScript : ProfinityScript, IProfinityRunnableScript
{
    public bool Run()
    {
        Profinity.Console.WriteLine("This is my first Profinity script.");
        Profinity.Console.WriteLine(Profinity.Message);
        return true;
    }
}

Python

def main():
    print("This is my first Profinity script.")
    print(Profinity.Message)


main()

Run the script

Select Save to save the script and the component settings, which are saved together. The script component appears in your profile. Run the script from the script component's menu by selecting Run Script (available when Script Mode is Run On Demand), and use Cancel Script to stop a script that is still running.

With Log Script Output enabled, the output appears in the Profinity log (select ADMIN, then Logs) at the Info level as Script <script file name>: This is my first Profinity script., followed by a line for Profinity.Message. Compile errors and script errors are always written to the log, whether or not Log Script Output is on, so review the error message, fix the script, and run it again. A script with a time interval or cron schedule runs according to the schedule that was set.

For more examples, refer to Script examples.

Troubleshooting

The script does not run

A script that does not run usually has scripting disabled or a component language that does not match the source. Confirm that scripting is enabled in System Configuration and that the script component's language matches the language of the source.

The DBC signal is null or missing

A signal that returns nothing or raises an error has a component, message, or signal name that does not match the DBC, because the names are case-sensitive. Check the names against the DBC viewer. The Script examples page shows a script that reads a DBC signal.

A syntax error appears

Fix compile errors (C#) or IronPython and Python 3 syntax errors (Python) before running, and add one API (DBC, CAN bus or state) at a time so that a fault is easy to find. The Script examples page shows each API in turn.

Next steps