Creating and handling TwinCAT Measurement projects
The TwinCAT Automation Interface provides methods and properties to create and access TwinCAT Measurement projects. The following chapter describes how some basic tasks can be solved with such a TwinCAT project and includes information on the following topics:
- Requirements
- Creating a TwinCAT Measurement project
- Creating a TwinCAT Scope configuration
- Creating, accessing and handling diagrams
- Creating, accessing and handling axes
- Creating, accessing and handling channels
- Starting and stopping recordings
- Further information on TwinCAT Measurement can be found on the corresponding webpage in our information system.
Requirements
The integration of TwinCAT Measurement projects via the Automation Interface is possible from TwinCAT 3.1 Build 4013.
The interface definitions required for handling TwinCAT Measurement projects are located in the TwinCAT.Measurement.AutomationInterface.dll (this is located in the installation directory of the TE130x scope view)
IMeasurementScope:
int StartRecord(); | Starts the recording. The command is independent of the selected node, as the underlying scope project is always used. |
int StopRecord(); | Stops the recording. The command is independent of the selected node, as the underlying scope project is always used. |
int Disconnect(); | Disconnects the connection to the ScopeServer. The recording is discarded. |
int SaveSVD(string filePath); | Saves the recording to the specified file path. |
int ExportCSV(string filePath); | Exports the recorded data in CSV format under the specified file path. |
int ExportBinary(string filePath); | Exports the recorded data in binary format under the specified file path. |
int ExportTDMS(string filePath); | Exports the recorded data to the TDMS format under the specified file path. |
int ExportDAT(string filePath); | Exports the recorded data in DAT format under the specified file path. |
int CreateChild(out object item, string name = "", int elementType = 0); | Creates a new element under the currently selected one. The element types are listed in the table below. |
int ChangeName(string name); | Changes the name of the element. |
int ShowControl(); | Opens the editor of the project |
int CloseControl(); | Closes the editor of the project |
int LookUpChild(string name, out object childNode); | Searches for the element matching the name in the project tree. |
The following element types are available as an enum in TwinCAT.Measurement.AutomationInterface.dll for the CreateChild method:
CursorAlignment | Vertical = 1, |
MarkerType | TimeMarker = 100, |
ChartElement | AxisGroup = 0 |
ChartType | YT = 0, |
AcquisitionType | ADS = 0, |
ConditionType | Area = 0, |
AxisGroupMember | Channel = 0, |
TriggerSetType | ChannelSet = 0, |
ChannelMember | AcquisitionInterpreter = 0, |
Creating a TwinCAT Measurement project
TwinCAT Measurement is a global "container" that can host one or more measurement projects, e.g. a TwinCAT Scope configuration. Similar to a regular TwinCAT configuration, each project is first described with the aid of a template file. This template file is used when adding a new project to the solution, which can be done by calling the AddFromTemplate() method from the Visual Studio DTE object. Note that this procedure is the same when adding a regular TwinCAT project. The following code snippet assumes that you have already acquired a DTE instance and created a Visual Studio solution, as shown in our article about Accessing TwinCAT configuration. A reference to the DTE instance is stored in the "dte" object.
Code snippet (C#):
EnvDTE.Project scopeProject = dte.Solution.AddFromTemplate(template, destination, name);Definition of the parameters:
template | The standard template files are located in the installation directory of TE130X-Scope-View (e.g. C:\TwinCAT\Functions\TE130X-Scope-View\Templates\Projects\) and have the file type "tcmproj". |
destination | Path in which the new configuration is to be saved on the hard disk. |
name | Name of the project configuration. |
Creating a TwinCAT Scope configuration
A TwinCAT Scope project stands for a recording configuration. This means that all elements inserted into the project are subject to the same recording settings. You can add a Scope project via the Automation Interface by specifying the appropriate "TwinCAT Scope Project" template when adding a project using the AddFromTemplate() method, as described above.
Creating, accessing and handling diagrams
Several diagrams can exist in parallel in a scope configuration. To add diagrams to an existing scope project, simply use the CreateChild() method from the IMeasurementScope interface. The following code snippet assumes that a TwinCAT Measurement project has already been created and that a reference to this project has been saved in the "scopeProject" object.
Code snippet (C#):
Project MeasurementProject = dte.Solution.Projects.Item(1);
ProjectItem ScopeProjectItem = MeasurementProject.ProjectItems.Item(1);
IMeasurementScope ScopeObj = (IMeasurementScope)ScopeProjectItem.Object;
ScopeObj.CreateChild(out object chartObj); // Erzeugen eines neuen YT-Charts im Scope
ProjectItem piChart = chartObj as ProjectItem;
IMeasurementScope imsChart = piChart.Object as IMeasurementScope;
imsChart.ChangeName("New Chart");
imsChart.CreateChild(out object axisObj); // Erzeugen einer neuen Achsengruppe unterhalb des Charts
ProjectItem piAxis = axisObj as ProjectItem;
IMeasurementScope imsAxis = piAxis.Object as IMeasurementScope;
imsAxis.ChangeName("New Axis");
imsAxis.CreateChild(out object channelObj); // Erzeugen eines neuen Kanals unterhalb der Achsengruppe
ProjectItem piChannel = channelObj as ProjectItem;
IMeasurementScope imsChannel = piChannel.Object as IMeasurementScope;
imsChannel.ChangeName("New Channel");
IMeasurementScope DataPoolObj = (IMeasurementScope)ScopeProjectItem.ProjectItems.Item(1).Object;
DataPoolObj.CreateChild(out object acqObj); // Erzeugen einer neuen Acquisition im DataPool
ProjectItem piAcq = acqObj as ProjectItem;
IMeasurementScope imsAcq = piAcq.Object as IMeasurementScope;
imsAcq.ChangeName("New Acq");
foreach (Property prop in piChannel.Properties)
{
Debug.WriteLine(prop.Name + " - " + prop.Value); // Wir geben einmal alle Properties mit Nammen und aktuellem Wert aus um uns einen Überblick zu verschaffen.
if (prop.Name == "Y-Data.Acquisition")
prop.Value = imsAcq; // Wir setzen die Acquisition des Channels auf die zuvor angelegte Acquisition im DataPool
}Starting and stopping recordings
The corresponding methods of the IMeasurementScope interface can be used to start/stop a configured scope recording:
Code snippet (C#):
ScopeObj.StartRecord()
ScopeObj.StopRecord();