Script examples
This page holds worked script examples that read a DBC signal, send a CAN message and persist script state. To add a script component and run a first script, refer to How to write your first script. Scripting needs a Desktop, Server, or Enterprise licence. Refer to Licensing.
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.
The following examples use the Example Profile, so load it and replay example_log.csv as described in the Quick Start Guide to give them data.
Use names from your own DBC and profile in other profiles: Component, Message and Signal are case-sensitive, and a name that does not exist raises an error.
Confirm the names in the DBC viewer in Profinity. Refer to DBC.
Reading a DBC signal value
using System;
try
{
var signal = Profinity.DBC.GetDbcSignal("Prohelion BMU", "PackStateOfCharge", "SOCPercent");
double currentValue = signal.Value;
string ts = DateTime.Now.ToString("HH:mm:ss");
if (double.IsNaN(currentValue))
{
Profinity.Console.WriteLine($"{ts} - No valid value for SOCPercent yet");
}
else
{
Profinity.Console.WriteLine($"{ts} - State of charge: {currentValue} {signal.Unit}");
}
}
catch (Exception ex)
{
Profinity.Console.WriteLine("DBC read failed: " + ex.Message);
}
import math
import sys
from datetime import datetime
try:
signal = Profinity.DBC.GetDbcSignal("Prohelion BMU", "PackStateOfCharge", "SOCPercent")
current_value = signal.Value
ts = datetime.now().strftime("%H:%M:%S")
if math.isnan(current_value):
print(f"{ts} - No valid value for SOCPercent yet")
else:
print(f"{ts} - State of charge: {current_value} {signal.Unit}")
except Exception as ex:
print(f"DBC read failed: {ex}", file=sys.stderr)
Prefixing log lines with a timestamp, from DateTime.Now in C# or datetime.now() in Python, helps when correlating script output with bus or application events.
Sending a CAN message
Payload data can be built from a byte array (in C#) or set as typed properties on CanBusPacket (for example Int32Pos0 and Int32Pos1) when the layout matches your frames.
The CanBusPacket type is available to C# scripts without an extra using statement, and Python scripts import it from Profinity.Sdk.Models.CANBus.
A sent message goes onto the connected CAN bus, so use IDs that are safe for the devices on that bus.
// Method 1: Byte array
var packetBytes = new CanBusPacket(0x123, new byte[] { 0x01, 0x02, 0x03, 0x04 });
Profinity.CAN.Send(packetBytes);
// Method 2: Property-based (common when filling structured data)
var packetProps = new CanBusPacket(0x100)
{
Int32Pos0 = 100,
Int32Pos1 = 200
};
Profinity.CAN.Send(packetProps);
Persisting script state
For the full operations, refer to Script Operations: CAN bus, DBC, state, and console.