使用跟踪脚本接口
这个 CODESYS Trace 插件提供了脚本接口。您将在下面找到如何使用此接口的示例。
创建跟踪
# a new trace can be created using the toplevel trace api myApplication = projects.primary.find("Device", "PLC Logic", "Application")[0] traceObject = trace.create(myApplication, "MyTraceName") traceObject.task_name = "Task" # a task with the name "Task" should already exist here. If it does not exist, this assignment throws an ValueError.
# the task of the trace can be set during creation myApplication = projects.primary.find("Device", "PLC Logic", "Application")[0] traceObjectWithTaskSet = trace.create(myApplication, "NameOfMySecondTrace", "MainTask") assert traceObjectWithTaskSet.task_name == "MainTask"
查找和验证现有痕迹
# finding an existing trace can be done via the project.find method 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" # .is_trace_object is False for all other objects of type IScriptObject
添加和更改跟踪变量
myTrace = projects.primary.find("Device", "PLC Logic", "Application", "MyTraceObject")[0] # add_variable() is a convenience method to add a new ScriptTraceVariable and set its properties during creation 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) == 10
更改 ScriptTrace
变量列表
移除特定的 ScriptTrace
变量
以下代码片段查找具有指定名称的变量并将其从 ScriptTrace
变量列表。
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 # Use Resolution.MilliSeconds if you want to have a trace recording based on milliseconds
设置录制条件
以下代码片段基于 ??? 片段。
traceObject.record_condition = "PLC_PRG.bDoRecord"
设置评论
以下代码片段基于 ??? 片段。
traceObject.comment = "This trace records the ..."
设置自动启动选项
以下代码片段基于 ??? 片段。
traceObject.auto_start = True
将选项设置为仅每隔 n 个周期录制一次
以下代码片段基于 ??? 片段。
traceObject.every_n_cycles = 10
图表及其变量
创建一个 ScriptTrace
包含所有内容的图表 ScriptTrace
变量
以下脚本显示如何创建简单图及其变量。
# first we need a ScriptTraceObject and some ScriptTraceVariables 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)) # create one ScriptTraceDiagram and add all ScriptTraceVariables to it traceDiagram = traceObject.diagrams.add() for traceVar in traceObject.variable_list: traceDiagram.add_diagram_variable(traceVar)
创建一个 ScriptTrace
per 的图表 ScriptTrace
变量
以下 Python 方法需要一个 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
复制 a 的设置 ScriptTrace
示意图
以下脚本显示如何修改和复制的设置 ScriptTrace
图表。
# we assume, that the traceObject already has multiple diagrams 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
的踪迹 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()
将跟踪记录保存在文件中
以下代码片段基于 下载并开始追踪 片段。跟踪必须先停止,然后才能保存。这可以通过达到触发条件来完成,也可以通过显式调用 stop()
。
# We assume, that we have an already running trace editor.stop() editor.save(r"<File path to csv file>")
查询各种信息以进行跟踪记录
以下代码片段基于 下载并开始追踪 片段。
# We assume, that we have an already running trace print("Packet State: {}".format(editor.get_packet_state())) print("Trace started at {} (absolute timestamp)".format(editor.get_trace_start_timetamp()))
触发器处理
配置一个 BOOL
触发
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 # The trigger level is only needed for numeric trigger variables. It is a good coding style to set the trigger explicitly to "None" traceObject.trigger_level = None
配置数字触发器
traceObject = proj.find("Device", "PLC Logic", "Application", "Trace")[0] traceObject.trigger_variable = "PLC_PRG.iTemperature" traceObject.trigger_edge = TriggerEdge.PositivetraceObject.post_trigger_samples = 20 # In case of a trigger variable of REAL or LREAL type it is also possible to specify floating point trigger levels, e.g. 80.5 traceObject.trigger_level = 80
等待触发器并保存记录的数据
以下代码片段基于 “配置 BOOL
-triggers” 和 “配置*触发器” 片段。
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>")
到达触发器后查询时间戳信息
以下代码片段基于 “配置 BOOL
-Trigger” 和 “配置 * 触发器” 片段。
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()))
到达触发器后恢复跟踪记录
以下代码片段基于 “配置 BOOL
-Trigger” 和 “配置 * 触发器” 片段。
editor.reset_trigger()
设备跟踪
创建一个 DeviceTrace
myDevice = projects.primary.find("Device")[0] devicetrace = trace.create(myDevice, "DeviceTrace")
查询设备当前的跟踪记录
以下代码片段基于 创建一个 DeviceTrace
片段。
traceObject = projects.primary.find("Device", "DeviceTrace")[0] # for application traces the result of get_online_traces contains only the traces of the application # traceObject = projects.primary.find("Device", "PLC Logic", "Application", "Trace")[0] editor = traceObject.open_editor() for tracepacket in editor.get_online_traces(): print(tracepacket)
将现有录音下载到 DeviceTrace
以下代码片段基于 创建一个 DeviceTrace
片段。
traceObject = projects.primary.find("Device", "DeviceTrace")[0] editor = traceObject.open_editor() editor.upload_to_device_trace("CpuCoreLoad")
什么不适用于 DeviceTrace
访问权限
traceobject.variable_list
使用创建跟踪变量
traceobject.add_variable())
访问权限
traceobject.diagrams