Receiving data from TwinCAT PLC
As mentioned bfore, the OPC-Server gives the possibility to read/write process values from ADS devices and therefore also from the TwinCAT PLC.
The following chapter gives you an overview about two different modes for accessing PLC symbols.
General
To configure the OPC-Server for PLC Data Access, you have two options. Both options can be configured via OPC-Configurator, which will be explained later in Step 2.
- AutoCfg "7" : All PLC variables are available via OPCAlle SPS Variablen sind über OPC verfügbar.
Please note: This could result in a very big OPC namespace, depending on the amount of PLC variables, structures, etc.. As this setting may be sufficient for smaller PLC project, we highly recommend to use AutoCfg "8" for bigger projects. - AutoCfg "8" : A subset of PLC variables is available via OPC.
The subset will be defined by special PLC comments directly in the PLC project. We recommend to use this setting.
When using AutoCfg "8", the following steps need to be taken to make PLC variables accessible via OPC:
- Step 1: Configuration of variables in the PLC
- Step 2: Configuration of the OPC-Server (this is a one-time step)
When using AutoCfg "7", step 1 will become obsolete and you can directly move on to step 2.
Step 1: Configuration of variables in the PLC
To make a PLC variable accessible via OPC, this variable needs to be explicitely configured. This happens via special PLC comments directly behind a variable, a structure or an instance. Two better understand the behavior of these comments, here are two examples:
Sample 1:
In this sample, the PLC variables bMemFlag1, bMemFlag2 and iReadOnly are configured for OPC. The PLC variable bMemAlarm1 should not be accessible via OPC. The PLC program may look as follows:
bMemFlag1 AT%MX10.0 : BOOL; (*~ (OPC:1:available for OPC Clients) *)
bMemFlag2 AT%MX10.1 : BOOL; (*~ (OPC:1:available for OPC Clients) *)
bMemAlarm1 AT%MX10.2 : BOOL;
bMemAlarm2 AT%MX10.3 : BOOL; (*~ (OPC:1:available for OPC Clients) *)
iReadOnly : INT; (*~ (OPC:1:available for OPC Clients)
(OPC_PROP[0005]:1:available for OPC Clients but ReadOnly) *)
The comment OPC_PROP[0005]:1 causes that the variable is read only.
Sample 2:
This sample makes the instances fbTest1 and fbTest2 of the function block FB_BLOCK1 accessible via OPC. If an instance is made available via OPC, all contained variables are also published into the OPC namespace. The PLC program may look as follows:
The instance fbTest1 has been made available via OPC, therefore all symbols within are also available via OPC, e.g. fbTest.ni1, fbTest.ni2, ... . The instance fbTest2 has not been configured for OPC, however, the function block defines the three variables ni1, no1 and nx1 for OPC. These are therefore OPC-enabled in all instances.
After the first PLC project has been compiled, the project directory contains a TPY-File, which needs to be configured in the OPC-Server, see Step 2. This TPY-File includes information about all PLC variables and whether a variable has been configured for OPC or not.
Please note: When making structural changes to the PLC program, this TPY-File may also change and you may need to re-start the OPC-Server.
Step 2: Configuration of the OPC-Server (one-time step)
In the second step you need to configure the OPC-Server. This is a one-time configuration.
Start the TwinCAT-OPC-Configurator "Start - All Programs - TwinCAT System - TwinCAT OPC - TwinCAT OPC Configurator"
Select "I/O Devices" with right click and then menu "New".
For "Name" please enter a name for this alarm definition. This name has to be OPC conform, no special characters allowed. Sample: The TwinCAT-PLC runtime-system 1 has a PLC variable "temperatur". Definition of devicename with "Plc1" will publish the PLC variable via OPC as "Plc1tTemperatur" later for OPC-Clients.
The new device has been added and you can now configure it according to your system environment.
The following table gives an overview about all possible configuration settings.
Parameter |
Beschreibung |
---|---|
Name |
Name of the device, e.g. PLC1. This will be the name under which the device will be accessible for the OPC-Client. |
Description [optional] |
Optional description for the device. |
AutoCfg |
7 : All PLC variables are available via OPC 8 : Only a subset of PLC variables is available via OPC |
AutoCfgSymFile |
Path to TPY-File, which is by default located in the PLC project directory. |
AdsPort |
ADS port numer of the device, e.g. 851 for the first PLC runtime. |
AdsNetId [optional] |
Address of the ADS device. By default this is 0.0.0.0.0.0 for local system. |
AdsTimeout [optional] |
Timeout for ADS connection to the device, measured in [ms]. If the device is not reachable within that timeframe, the OPC-Server returns BAD_QUALITY to the OPC-Client. |
AdsTimeSuspend [optional] |
Suspend time for the ADS device, measured in [ms]. If the ADS connection breaks, the OPC-Server waits this time before sending the next ADS request to the runtime. |
Disable [optional] |
Deactivates the device. |
Save the configuration via "File" --> "Save As". After the configuration has been saved, you will be asked if this configuration should be set as the startup-configuration.
The configuration will be automatically activated on next restart of the OPC-Server.