PLC Programming With CoDeSys - Using Data Unit Types (DUT) To Simplify Code

Updated on September 8, 2018
LiamBee profile image

10+ Years in Automation, PLC and HMIs. Working with most major Automation and Instrumentation vendors

What Is A Data Unit Type (DUT)?

A DUT is a special object that allows the definition of a Structure. A Structure works in the same way as a standard data type (such as Bool or Real) and allows variables to be defined as a type of a structure.

DUTs are defined in by using the following declaration:

TYPE <NAME>:
STRUCT

    <VARIABLE:TYPE>
    <VARIABLE:TYPE>

END_STRUCT
END_TYPE

The above would define a structure with 2 variables, defined in the usual method of <NAME>:<Type>;

These definitions would then be available through the defined structure, using the variable name that is defined as a structure and then the variable within the structure

<STRUCTURE VARIABLE>.<INNER VARIABLE>

Defining and Using DUTs

TYPE Thermostat:
STRUCT

	Current_Temp:REAL;
	Setpoint_Temp:REAL;

END_STRUCT
END_TYPE

The above code would define a DUT called Thermostat, the code should be placed in a DUT object under the Application object.

Once the DUT has been configured as above, you can define a variable as the structure type anywhere in your application

Var_Global

	Controller 1:Thermostat:=(Setpoint_Temp:=21);

End_Var

In the above example, the Controller1 is defined as a Thermostat (the DUT made earlier). It's defined in a Global variable space, so accessible anywhere in the program.

Data can be read and written from Controller1 using the following as the variable name

Controller1.Current_Temp:= 0;

This would set Controller1.Current_Temp to 0 (using structured text). This variable can then be read elsewhere with the same variable of Controller1.Current_Temp

CoDeSys Guide

Lets build the above example in CoDeSys and run it using the simulator. This will give a working example of a structure that can then be expanded on in the future.

Step 1 - Create The DUT Object

Firstly, ensure you are using a project that has a device added to it and an Application object.

  1. Right Click Application and choose Add Object and then DUT...
  2. Name the DUT "Thermostat" and choose the STRUCTURE option
  3. Open the new Thermostat DUT

Your navigation pane should look like the following by this point


Step 2 - Defining The Structure

In the opened Thermostat DUT, write the following code

TYPE Thermostat :
STRUCT
	Status_CurrentTemperature:REAL;
		
	Control_TargetTemperature:REAL;
	Control_Enabled:BOOL;
	Control_HeaterOutput:BOOL;
		
	Param_Deadband:REAL;
END_STRUCT
END_TYPE

There's a few more variables than previous examples, but they'll be used later in the code.

Now the structure is defined we can go ahead and start using it

Step 3 - Using The Structure

We need to define a variable that is the Thermostat type. We'll do that in the POU in which the Thermostat structure is needed.

  1. Create a new POU with the following configuration:, Name: Heating, Type: Program, Language: Ladder
  2. Between Var and Var_End add Controller1:Thermostat;

PROGRAM Heating
VAR
	Controller1:Thermostat;
END_VAR

Create the following ladder logic, using the Controller1 variable that has just been defined

For those on mobile or cannot see the image, here's a text base version to follow

Contact: Controller1.Control_Enabled

Box: LT (Less Than - Parameter1 = Controller1.Status_CurrentTemperature, Parameter 2 = Controller1.Control_SetpointTemperature - Controller1.Param_Deadband)

Coil: Controller1.Control_HeaterOutput

Add a Task Configuration to your Application and in Tasks add the POU Heating.

Build your application and ensure you have no errors. If you do have errors, go back and follow the previous steps again and see where you've gone wrong.

Step 4 - Initialise Variable

As with any variable, initial values should be given if being used in a controlling process.

In the declaration of Controller1, change Controller1:Thermostat; to the following

Controller1:Thermostat := (Control_TargetTemperature := 21, Param_Deadband := 0.5);

This will ensure that Controller1.Param_DeadBand & Controller1.Control_TargetTemperature are initialised with values when your application starts for the first time

Step 5 - Simulate

Now we're ready to run the application and make sure everything works as expected.

Login to the PLC (by clicking Online >> Login) Make sure your in simulate mode. Download your application to the simulated PLC.

Click Start, you should be seeing the below image

  • Control_Enabled = False
  • Status_CurrentTemperature = False
  • Control_TargetTemperature = 21
  • Param_DeadBand = 0.5
  • Control.HeaterOutput = False

Double click on Controller1.Current_Temperature and change the value to 15. Press Ctrl + F7 to download the new value to the PLC.

Double click on Controller1.Control_Enable and download a TRUE value to the PLC

Note that the Controller1.Control_HeaterOutput coil is now active.


In the Declaration pane (above the ladder logic) there is now a table with 1 entry - Controller1. Expand the variable and view the Structure variables of Controller1. These variables are the variables that are being used in the ladder logic, you can also manipulate them here.

Expanding DUT Usage

DUTs can be extremely complicated, but also extremely useful. To make better use of them, you could consider the following

  • Use DUTs with Function Blocks and pass an entire structure as an input to the function block instead of many individual variables
  • Build DUTs for common instrumentation used, such as pressure, temperature etc. You can pack these into a library and use them over and over
  • DUTs can be defined as Arrays also: Controller: Array [0..99] of Thermostat would create 100 thermostat structures, accessible via Controller[X], where X = the number controller you wish to access variables of.

Questions & Answers

    Comments

      0 of 8192 characters used
      Post Comment

      No comments yet.

      working

      This website uses cookies

      As a user in the EEA, your approval is needed on a few things. To provide a better website experience, owlcation.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

      For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://owlcation.com/privacy-policy#gdpr

      Show Details
      Necessary
      HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
      LoginThis is necessary to sign in to the HubPages Service.
      Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
      AkismetThis is used to detect comment spam. (Privacy Policy)
      HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
      HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
      Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
      CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
      Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
      Features
      Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
      Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
      Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
      Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
      Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
      VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
      PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
      Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
      MavenThis supports the Maven widget and search functionality. (Privacy Policy)
      Marketing
      Google AdSenseThis is an ad network. (Privacy Policy)
      Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
      Index ExchangeThis is an ad network. (Privacy Policy)
      SovrnThis is an ad network. (Privacy Policy)
      Facebook AdsThis is an ad network. (Privacy Policy)
      Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
      AppNexusThis is an ad network. (Privacy Policy)
      OpenxThis is an ad network. (Privacy Policy)
      Rubicon ProjectThis is an ad network. (Privacy Policy)
      TripleLiftThis is an ad network. (Privacy Policy)
      Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
      Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
      Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
      Statistics
      Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
      ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
      Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
      ClickscoThis is a data management platform studying reader behavior (Privacy Policy)