4. View & Write Variable Data

TwinCAT ADS Client Project Data

Data values of Variables can be viewed and modified through the Variable Data table accessible through the () Data button on the TwinCAT ADS Client project main page.

This guide describes:

  1. How to view and chart TwinCAT ADS Client real time data.
  2. Viewing a Group’s data payload with the MQTT Client.
  3. How to write values into one or more TwinCAT ADS Client Variables.

Back to Top

Viewing and Charting Variable Data

The Data tab of a TwinCAT ADS Client project is used to display the real time values of the Variables configured. The Data tab is only visible when the project is running.

To see the real time values of the Variables configured for a Project:

  1. Open the target TwinCAT ADS Client project from the Projects list.
  2. Confirm that the Project is running. If it is not Running, please Start or Restart your Project.
  3. When the Project is Running, Click on the () Data button on the TwinCAT ADS project’s main page to display the Variables page.
Open Data page
  1. Select the Group whose Variables you want to view from the dropdown menu. This opens the Variable table.

  2. The table displays the following columns:

    • Name is the name given to the Variable being displayed.
    • ID is the Variable’s address.
    • Last Value displays the Variable’s last value.
    • Timestamp displays the timestamp of the last value written to the Variable.
    • New Value displays a new value written to the Variable.
    • Chart displays switches on every row. Turning the switches on will display a real time chart for the Variable selected.
Data Chart
  1. To view near real-time chart(s) of one or more Variables, turn on the corresponding switch(es) in the Chart column. The values of the Variable(s) will be displayed on chart(s) below. Note that you can toggle the display of the charts when there are multiple values by clicking on their respective legends.
Data Chart

Back to Top

Viewing Data with the MQTT Client

The Edge One™ MQTT Client can also be used to see the payloads of real-time data published by the TwinCAT PLC.

Note that the MQTT Client only operates in real-time and must be opened when the data is published. For example, if you want to see the data from a Group and the Client is closed when the data is published, you will not see any data after opening the MQTT Client.

To view the data payload of a data Group:

  1. Open the MQTT Client.

  2. Click on Topic

  3. Enter the topic name as:

     data/myprojectname/groupID
    

Where myprojectname is the name of your ADS TwinCAT project and GroupID is the GroupID of the Group whose payload you want to see.

  1. The data published will start flowing and you can use the features as described in the MQTT Client documentation.

Back to Top

Writing Variables

There are two ways to write data to TwinCAT ADS Variables:

  1. Using the Data user interface.
  2. Publishing data into cmd channels.

Write Using Data Interface

To write values into one or more Variables using the graphical user interface:

  1. Open the target TwinCAT ADS Client project from the Projects list.
  2. Confirm that the Project is running. If it is not Running, please Start or Restart your Project.
  3. When the Project is Running, Click on the () Data button on the TwinCAT ADS project’s main page to display the Variables page.
Open Data page
  1. Select the Group whose Variables you want to view. This opens the Variable table.
Select Group

The table displays the Variables of the Group and their values as follows.

Group variables
  1. In the New Value column of the Variable(s) you want to write, enter the value(s) you want to write using the data format required by the Variable. In the example below, the Variable is an integer. We enter the new value as 170.

The Write values button will appear indicating the number of Variables whose values you will write.

Written data
  1. Click on the Write values button to complete the process.

  2. The Last Value column will display the value(s) you wrote on the respective Variables, and any chart being displayed will also be updated as shown below. The last value shows as 170 and the chart was also automatically updated. Note that if the sampling rate is fast, the chart will show the value manually written as a past data point, and the Last Value will be the last value read from the PLC.

Written data

Back to Top

Writing Variables using cmd channels

This section describes how to write data to a single Variable or to multiple Variables by publishing data to the cmd/ or to the write/ channel. Both channels are equivalent.

The payload must include:

  • ts the timestamp.
  • mod which is the container module used. For TwinCAT ADS it is ads.
  • pid which is the Project ID.
  • cid which is the command id - an arbitrary string that identifies the command.
  • type which identifies they type of command: write or read.
  • data which is the data of the payload. Data includes:
    • g the Group ID, and
    • vals the JSON with the Variables and their values.

Writing data to a single Variable

In the example below, we write data to the Group with Group ID 1 and Variables Temp.int of data type int, and Temp.uint of data type uint. We publish the data to channel cmd/2, where 2 is the project ID, as follows:

	{ 	
        "ts":1522917350646,
        "mod":"ads",
        "pid":2,
        "cid":"edge-one-core-0a2d9649c66857a6114fc24008681d74",
        "type":"write",
        "data": {
	       "g":1,
	        "vals":[{"k":"Temp.int","v":"2"},{"k":"Temp.uint","v":"3"}]
	     	}
    }

the TwinCAT ADS Client replies with:

	{
		"ts":1522917353594,
		"mod":"ads",
		"pid":2,
		"cid":"edge-one-core-0a2d9649c66857a6114fc24008681d74",
		"status":true,
	}

indicating the data was valid.

If we try to write a value out of range, for example, 40000 in Temp.int whose type’s valid data range is (-32768 to 32767), the sequence is as follows:

	{ 	
		"ts":1522917350646,
		"mod":"ads",
		"pid":2,
		"cid":"edge-one-core-0a2d9649c66857a6114fc24008681d74",
		"type":"write",
		"data": {
			"g":1,
			"vals":[{"k":"Temp.int”,”v”:”40000”},{“k”:”Temp.uint","v":"3"}]
			}
	}

The reply is :

	{
		"ts":1522917948491,
		"mod":"ads",
		"pid":2,
		"cid":"edge-one-core-fcd8ef2c40e1bf4a1399722193227ff1",
		"status":false,
		"errors":[{"Temp.int":"ERR_VALUE_OUT_OF_RANGE"}]
	}

Writing to a Variable that does not exist has the following outcome:

	{
		"ts":1522920072156,
		"mod":"ads",
		"pid":2,
		"cid":"edge-one-core-5128a708f754df3b7f5f7e8e965d5873",
		"type":"write",
		"data":{"g":1,"vals":[{"k":"testWrong”,”v”:”4”}]}}
	}

The reply is:

	{
		"ts":1522920072142,
		"mod":"ads",
		"pid":2,
		"cid":"edge-one-core-5128a708f754df3b7f5f7e8e965d5873",
		"status":false,
		"errors":[{"testWrong":"ERR_VAR_NOT_FOUND"}]}
	}
Still need help? Get in touch!
Last updated on 6th May 2021