Reading/Writing TwinCAT PLC Variables

Once a TwinCAT client device is configured as described in the Configure a TwinCAT Device topic, you are able to read/write TwinCAT PLC variables through the Selected Path property of Tags. To do so, perform the following steps:

  1. Ensure that the project has a TwinCAT client device which is connected to a TwinCAT 2 or 3 PLC on the network. If this is not the case, create a client device.
  2. Open the Tag Manager through the System > Tag Manager node of the Project Explorer.
  3. Add a new tag by filling all required cells (and optionally the Comment cell as well) in the row. When ready, link the tag to the specified PLC variable by updating the Selected Path property to the applicable PLC variable. The names of the TwinCAT PLC variables follow the [Devices.<TwinCat_ClientDevice_Name>]<PROGRAM>.<TwinCat_PLCVariableName> format, where <TwinCat_ClientDevice_Name> is the name of the configured Cognex Designer TwinCAT client device, while <TwinCat_PLCVariableName> is the name of the PLC variable you want to bound the tag to. See the below image for an example.

    Tip: If the Cognex Designer TwinCAT client device is connected to the specified PLC, you can also select the PLC variables via a drop-down list in the Selected Path column.

The TwinCAT client device supports reading and writing tags with the following Data Types:

  • Boolean
  • Byte
  • Double
  • Integer
  • String

Also, the TwinCAT client device supports reading (but not writing) array tags as well.

When configuring the tags in the Tag Manager, make sure that you set the correct Data Type applicable to the PLC variable you want to connect the tag to. For example, if you want to connect a tag to the MAIN.myBool variable of the PLC, ensure that the tag's data type is set to Boolean. See the official Beckhoff Data Type Comparison documentation for more information on the supported data types.

Once they are configured, reading and writing these tags have the same effect as directly reading and writing their bound PLC variables.

Note:

When deciding on whether to use tags or $ functions to modify PLC variables, consider the following:

  • When using tags to read/write PLC variables, the read/write operations are performed asynchronously in Cognex Designer, in line with the Update Rate property of the tags (this property is available in the Properties window after selecting the tag in the Tag Manager once its Selected Path cell is properly configured). This means that read/write requests are queued in a background thread, and are performed only when a thread becomes available. If you want to perform multiple PLC variable read/write operations synchronously while a Task is running, use the available TwinCAT $ functions instead of tags.
  • When using tags, you can read/write one PLC variable at a time (the one that is bound to the selected tag). On the other hand, the published TwinCAT $ functions allow you to read/write multiple PLC variables simultaneously as well, as they also support reading/writing an array of PLC variables.
  • Both the ReadMulti and WriteMulti $ functions use the TwinCAT ADS-sum command to read/write several variables with a single command.
  • Using an array tag that has a configured Selected Path to the corresponding PLC array variable will not update that variable on the PLC, and will result in an error in the Output window. Such PLC array variable updates are only supported by the WriteMulti $ function.