Supported Languages
Profinity scripting supports three languages: C#, Python (through IronPython) and Lua (through NLua).
Each language runs on .NET, the Microsoft software framework Profinity is written in, so scripts can use .NET libraries directly.
Each language supports the same script types (Run, Receive, Service, Tag Change and Rule Script, the last being a script set to Run On Alert mode) and the same host operations, exposed through the Profinity script variable, so the choice of language is a matter of preference and background.
| Language | Typing and style | Main limits |
|---|---|---|
| C# | Statically typed classes with full .NET access | More verbose, and needs some .NET knowledge |
| Python | Procedural functions with Python 3 syntax | Runs on IronPython, so libraries that need compiled CPython extensions cannot be used |
| Lua | Procedural functions with a small syntax | Smaller standard library, and fewer third-party packages |
Functionality beyond what Profinity Scripting provides is available by calling the Profinity APIs from separate tools.
C# scripting
C# is a statically typed, object-oriented language. C# scripts in Profinity are classes that inherit from a base class or implement a specific interface, and they have full access to the .NET framework. Because Profinity is itself written in C#, C# gives the deepest integration with Profinity types and suits large automation projects and performance-critical work. See the C# Language Reference and the .NET API Documentation.
Python scripting
Python scripts in Profinity use a procedural style, with functions that Profinity calls at defined points. Profinity runs them through IronPython, a .NET implementation of Python, so a Python script can also call .NET libraries directly. IronPython is not the standard CPython interpreter, so Python packages that depend on compiled CPython extensions, such as numpy, cannot be imported, whereas the Python standard library modules that IronPython provides and .NET types can.
Profinity runs IronPython with Python 3 compatibility enabled, so all Python scripts use Python 3 syntax, including:
print()as a function, not a statement- f-strings, such as
f"Value: {value}" /for true division and//for floor division- Unicode literals
- Absolute imports
See the IronPython Documentation and the Python Language Reference.
Lua scripting
Lua scripts in Profinity use a procedural style, with functions that Profinity calls at defined points, such as receive() for a Receive script or on_start() for a Service script.
Profinity embeds the Lua 5.4 runtime through NLua, and a Lua script can call .NET types directly, including the same Profinity script variable that C# and Python scripts use.
The General Purpose Input/Output (GPIO) and serial port types GpioController, PinMode, PinValue, SerialPort, Parity, StopBits and Handshake are available as globals without any import.
Profinity also registers four Lua host functions:
| Function | What it does |
|---|---|
print(...) |
Writes its arguments to the script output stream |
stderr(...) |
Writes its arguments to the script error stream, the Lua equivalent of Python's sys.stderr |
sleep(seconds) |
Pauses the script for the given number of seconds. Each call is limited to 30 seconds, so a loop that needs to wait longer calls sleep() again |
CanBusPacket(address) |
Creates a new CAN packet for the given CAN address, for use with the operations described in CAN Bus |
Lua scripts follow Lua syntax, including these points that differ from Python and C#:
- Native Lua tables use 1-based indexing, whereas .NET collections such as
TriggeredTagskeep their own 0-based indexing. niltakes the place ofnullorNone.- Strings are joined with
..rather than+. - Blocks are written as
function name(...) ... endrather than with indentation or braces.
See the Lua 5.4 Reference Manual and the NLua Documentation.