事例とベストプラクティス CODESYS Trace スクリプト API
CODESYS Scripting から CODESYS Trace
以下の例は、自動生成されたAPIドキュメントの補足として意図されています。 CODESYS Scripting に CODESYS Trace。
トレースを作成する
あなたは CODESYS Trace 新しいトレースを作成するためのスクリプト API。
myApplication = projects.primary.find("Device", "PLC Logic", "Application")[0]
traceObject = trace.create(myApplication, "MyTraceName")
タスク名 Task すでにここに存在している必要があります。存在しない場合は、この割り当てによって ValueError。
traceObject.task_name = "Task"
スクリプトでは、新しく作成されたトレースをどのタスクで実行するかを指定できます。
myApplication = projects.primary.find("Device", "PLC Logic", "Application")[0]
traceObjectWithTaskSet = trace.create(myApplication, "NameOfMySecondTrace", "MainTask")
assert traceObjectWithTaskSet.task_name == "MainTask"既存のトレースの検索と検証
既存のトレースを検索するには、 project.find 方法。
その他のすべてのオブジェクトについては、 IScriptObject タイプ、 .is_trace_object 等しい FALSE。
foundObjects = projects.primary.find("Device", "PLC Logic", "Application", "MyTraceObject")
expectedTraceObject = foundObjects[0]
assert expectedTraceObject.is_trace_object, "Could not find trace object with the expected name"
トレース変数の追加と変更
の add_variable() メソッドは新しいものを追加する簡単な方法です ScriptTraceVariable 作成時にプロパティを設定します。
myTrace = projects.primary.find("Device", "PLC Logic", "Application", "MyTraceObject")[0]
addedVariable = myTrace.add_trace_variable(variableName="PLC_PRG.a1",
graphColor= 0xff000000,
graphType=GraphType.LINES,
activateMaxWarning=True,
maxWarningArea=9.0,
maxColor= 0xffff0000)
addedVariable.enabled = False
for i in range(2,11):
newVariable = myTrace.variable_list.add()
newVariable.variable_name = "PLC_PRG.a"+str(i)
newVariable.graph_type = GraphType.LINES_CROSSES
assert len(myTrace.variable_list) == 10ScriptTrace 変数リストの変更
特定の ScriptTrace 変数を削除する
次のコードスニペットでは、指定された名前の変数が最初に検索され、次に ScriptTraceVariableList。
def remove_variable_by_name(traceObject, variableName):
index_to_remove = -1
for variable in traceObject.variable_list:
if variable.variable_name == variableName:
index_to_remove = traceObject.variable_list.index_of(variable)
break
if index_to_remove != -1:
traceObject.variable_list.remove(index_to_remove) 録画設定の変更
トレース記録の解像度(ms/µs)の設定
次のコードスニペットは「トレースを作成する「スニペット」。
traceObject.resolution = Resolution.MicroSeconds
使用 Resolution.MilliSeconds ミリ秒単位で記録したい場合。
録音条件の設定
次のコードスニペットは「トレースを作成する「スニペット」。
traceObject.record_condition = "PLC_PRG.bDoRecord"
コメントを設定する
次のコードスニペットは「トレースを作成する「スニペット」。
traceObject.comment = "This trace records the ..."
自動開始オプションの設定
次のコードスニペットは「トレースを作成する「スニペット」。
traceObject.auto_start = True
n サイクルごとにのみ記録するオプションを設定する
次のコードスニペットは「トレースを作成する「スニペット」。
traceObject.every_n_cycles = 10
図表とその変数
すべての ScriptTrace 変数を含む 1 つの ScriptTrace ダイアグラムを作成する
次のスクリプトは、すべての変数が使用される単純な図を作成する例です。
まず、ScriptTrace オブジェクトといくつかの ScriptTrace 変数が作成されます。
traceObject = projects.primary.find("Device","PLC Logic","Application","Trace")[0]
assert traceObject.is_trace_object
for i in range(5):
traceObject.add_trace_variable(variableName="PLC_PRG.a"+str(i))
次に、ScriptTrace ダイアグラムが作成され、すべての ScriptTrace 変数が追加されます。
traceDiagram = traceObject.diagrams.add()
for traceVar in traceObject.variable_list:
traceDiagram.add_diagram_variable(traceVar)ScriptTrace 変数ごとに 1 つの ScriptTrace ダイアグラムを作成する
次の Python メソッドは、1 つの ScriptTrace オブジェクトを受け取り、ScriptTrace 変数ごとにダイアグラムを追加します。
def one_diagram_per_variable(traceObject):
assert traceObject.is_trace_object
for traceVar in traceObject.variable_list:
diagram = traceObject.diagrams.add(traceVar)
diagram.name = traceVar.variable_name
ScriptTrace ダイアグラムの設定のコピー
次のスクリプトは、ScriptTrace ダイアグラムの設定を変更およびコピーする方法を示しています。
traceObject = projects.primary.find("Device","PLC Logic","Application","Trace")[0]
assert traceObject.is_trace_object
# apply settings for the first diagram
traceObject.diagrams[0].y_axis.mode = AxisScaleMode.FixedLength
traceObject.diagrams[0].y_axis.range = 10.0
traceObject.diagrams[0].y_axis.color = 0xffee0000
traceObject.diagrams[0].y_axis.draw_grid = True
traceObject.diagrams[0].y_axis.grid_color = 0xffee0000
traceObject.diagrams[0].tickmark_fixed_spacing = True
traceObject.diagrams[0].tickmark_fixed_distance = 5.0
traceObject.diagrams[0].tickmark_fixed_subdivisions = 4
# copy the settings from the first diagrams to all other diagrams
for i in range(1,len(traceObject.diagrams))
traceObject.diagrams[i].y_axis.copy_from(traceObject.diagrams[0])
トレースのオンライン処理
Trace のダウンロードと起動
次のコードスニペットは、 Trace の痕跡 Application アプリケーションをダウンロードして起動します。
proj = projects.open(r"<Path to the project file>")
app = proj.find("Device", "PLC Logic", "Application")[0]
with online.create_online_application(app) as onlineapp:
traceObject = proj.find("Device", "PLC Logic", "Application", "Trace")[0]
onlineapp.login(OnlineChangeOption.Never, True)
onlineapp.start()
editor = traceObject.open_editor()
editor.download()
editor.start()トレースをファイルに保存する
次のコードスニペットは「Trace のダウンロードと起動「スニペット」。
トレースは保存する前に停止する必要があります。これは、トリガー条件に到達するか、明示的に次の呼び出しを行うことによって行うことができます。 stop()。
トレースがすでに実行されているものと想定されます。
editor.stop() editor.save(r"<File path to csv file>")
トレース記録に関するさまざまな情報の照会
次のコードスニペットは「Trace のダウンロードと起動「スニペット」。
トレースがすでに実行されているものと想定されます。
print("Packet State: {}".format(editor.get_packet_state()))
print("Trace started at {} (absolute timestamp)".format(editor.get_trace_start_timetamp()))トリガー処理
ブールトリガーの設定
traceObject = proj.find("Device", "PLC Logic", "Application", "Trace")[0]
traceObject.trigger_variable = "PLC_PRG.bVar"
traceObject.trigger_edge = TriggerEdge.Positive
traceObject.post_trigger_samples = 20
トリガーレベルは数値トリガー変数にのみ必要です。ただし、トリガーを明示的に設定することは良いコーディングスタイルです。 None。
traceObject.trigger_level = None
数値トリガーの設定
traceObject = proj.find("Device", "PLC Logic", "Application", "Trace")[0]
traceObject.trigger_variable = "PLC_PRG.iTemperature"
traceObject.trigger_edge = TriggerEdge.Positive
traceObject.post_trigger_samples = 20トリガー変数が REAL または LREALトリガーレベルは浮動小数点数で指定することもできます(例: 80.5)。
traceObject.trigger_level = 80
トリガーを待って記録データを保存する
次のコードスニペットは「Trace のダウンロードと起動「」スニペットと「* トリガーの構成」を参照してください。
triggerstate = editor.get_trigger_state()
while triggerstate != TriggerState.TriggerReached:
system.delay(200)
triggerstate = editor.get_trigger_state()
editor.save(r"<File path to csv file>")トリガーに到達した後のタイムスタンプ情報のクエリ
次のコードスニペットは、「Trace のダウンロードと起動「」スニペットと「* トリガーの構成」を参照してください。
print("Trigger reached at {} (absolute timestamp)".format(editor.get_trigger_timetamp()))
print("Trigger reached at {}".format(editor.get_trigger_startdate()))
print("Trace was running {}ms until trigger has been reached".format(editor.get_trigger_timetamp() - editor.get_trace_start_timetamp()))トリガーに到達した後、トレース記録を再開する
次のコードスニペットは、「Trace のダウンロードと起動「」スニペットと「* トリガーの構成」を参照してください。
editor.reset_trigger()
デバイストレース
デバイストレースの作成
myDevice = projects.primary.find("Device")[0]
devicetrace = trace.create(myDevice, "DeviceTrace")デバイス上の現在のトレース記録を照会する
次のコードスニペットは「デバイストレースの作成「スニペット」。
traceObject = projects.primary.find("Device", "DeviceTrace")[0]
アプリケーショントレースの場合、 get_online_traces アプリケーションの痕跡のみが含まれます。
# traceObject = projects.primary.find("Device", "PLC Logic", "Application", "Trace")[0]
editor = traceObject.open_editor()
for tracepacket in editor.get_online_traces():
print(tracepacket)既存のトレースをDeviceTraceとしてアップロードする
次のコードスニペットは「デバイストレースの作成「スニペット」。
traceObject = projects.primary.find("Device", "DeviceTrace")[0]
editor = traceObject.open_editor()
editor.upload_to_device_trace("CpuCoreLoad")