Data Type Conversion

There are times when a user has to convert between Data Types. For example a sensor may have 16-bit data in the form of two(2) 16-bit Words that are to be read, yet the data eventually needs to be used in a Float or Real data type format. This could be in an HMI or maybe a SCADA system.

This project demonstrates how to easily convert the data via Unions, Arrays, Structures and IO Mapping techniques.

Introduction

This example is based upon an actual physical device, the SOCOMEC DIRIS A-40 Meter.

The Current readings (I1 - I3) for the various phases are shown below (in WORD format).

alternate text

The sensor’s total Current data field Data Type value is an Unsigned 32-bit value which is comprised of two(2) separate 16-bit Words.

In the program example, we will use the variable names CuAW1 and CuAW2 to hold the Current A phase two(2) Words of data.

This project will convert the data into a final 32-bit Real format that is required by our fictional SCADA system.

System Requirements and Installation

This section requires the following:
  • P2CDS-622 CPU

  • P2000 Base (any variant)

  • P2000 PowerSupply (any variant)

  • Latest version of CODESYS installed on Host PC

  • Familiarity with the “Structured Text” programming language.

Recommendation for Data Protection

In order to minimize the risk of data security breaches, we recommend the following organizational and technical measures for the system that will run your applications:

  • Whenever possible, avoid exposing PLCs and controller networks to public networks and Internet.

  • Use additional data link layers for protection, such as a VPN for remote access, and install firewall mechanisms.

  • Restrict access to authorized persons only, change any existing default passwords during the initial commissioning, and change passwords regularly.

If you want to publish a web visualization, then we strongly recommend utilizing HTTPS and assigning simple password protection to prevent access to the functionality of your PLC over the Internet.

Help

Help is provided in the Help menu, as well as the context-sensitive <F1> key when in the IDE whether a project is open or not.

Currently, the web-based online help opens by default. This can be disabled in the CODESYS options so that the offline help (CHM format) is used.

Caution

To do this project, the steps outlined in the previous section Preparation have to be completed successfully.

Tip

NOTE: There is a completed project that can be downloaded entitled Demo_datatype_convert. The following instructions on this page provide details to the generation of this project.

Download the project here: Data Type Conversion project

Creating a Project

The following sections outline the steps taken to create the downloadable project.

The project can be made up of many program/design PRG files. These are referred to as Programmable Object Units or POUs.

Start CODESYS and Create a Project

Start CODESYS

(1). Start CODESYS from the Start menu (by default, the path is Programs > CODESYS (version).

You can also click the CODESYS icon that is located on the desktop after installation.

Create a project

(2). Click File > New Project

The example project here will be named datatype_convert

(3). In the New Project window, Templates section, select the Standard Project template.

(4). Specify a name and a storage location for the project and click OK

The project opens in the CODESYS Standard Project frame window.

(5). In the Device drop-down location, select the P2CDS622 device.

The next step will be to define the language for the initial program you will create (default name is PLC_PRG). We will rename this file later to my preference of “Main”.

(6). In the PLC_PRG in drop-down location, select Structured Text (ST). This will be the language we will use for the first program.

The default Base (P2-04B) is inserted in the tree with no modules added yet, i.e. <Empty>. If your hardware topology is a different Base, see the section below on changing the Base type.

The elements of the project tree can be investigated further by going to the Help section online by pressing <F1> in the device tree area and searching for a specific item of interest.

Caution

If Ethernet ports have not been configured yet, it is required that they be setup per section the Ethernet section!

Rename Design File and Add Fieldbusses

It is not required, but for this example we will change the name of the PLC_PRG to Main.

(7). Right Click on PLC_PRG and in the drop-down select Properties. In the Common dialog box, rename PLC_PRG to Main. Click OK.

(8). A dialog box will prompt regarding the name change to be applied over the entire project. Click Yes.

Another dialog box entitled Refactoring will pop up displaying all the areas in the project where this new name will be applied. Click OK.

If a Fieldbus (Modbus or EtherNet/IP) is to be used, this is the point where you would add these.

Add Fieldbus

(Not required )

Change Base Type (if req’d)

If you are using a Base topology other than the P2-04B, you can change it now.

(9). In the device tree, highlight with a single click the Base (P2-04B) element, then Right-click. Select Plug Device.

The Plug Device dialogue box should appear.

(10). Under the Miscellaneous name, find the Base unit device you want and double-click. Close the box.

The updated Base should be showing.

Write the Control Program

This project will be comprised of the following key aspects:

  1. Create a UNION that takes in the ARRAY values and converts to a Double Word (DWORD).

  2. Create a UNION that takes in the DWORD values and converts them to a Real/Float (REAL).

  3. Create an Object or Structure ( STRUCT ) that represents the Meter’s registers and other variables desired.

  4. Map the Meter values into WORD size variables.

  5. Map these variables into an ARRAY so the Words can be appended together to create a DWORD.

Create a UNION to convert to DWORD

This Union is a custom, user defined DataType that will take in an ARRAY variable and create a DWORD representation of it.

(11). Add a Union by right-clicking on Application > Add Object > DUT….

A pop-up will appear entitled Add DUT (Device Unit Type)

(12). Select the Union item and name it UT_CONVERTDWORD (“Unit Type-Convert to DWORD”)

(13). Fill in the file as shown below.

alternate text

Create a UNION to convert to REAL

This Union is a custom, user defined DataType that will take in an ARRAY variable and create a DWORD representation of it.

(14). Add a Union by right-clicking on Application > Add Object > DUT….

A pop-up will appear entitled Add DUT (Device Unit Type)

(15). Select the Union item and name it UT_CONVERTREAL (“Union Type-Convert to REAL”)

(16). Fill in the file as shown below.

alternate text

Create the STRUCTURE for the Powermeter Object

A Structure ( STRUCT ) is a user-defined data type, which combines multiple variables of any data type into a logical unit. The variables declared within a structure are called members.

For Object Oriented Programming, a Structure can be referred to as an Object for example a Motor and the attributes of the Motor like Speed, RPM and Temperature are all members of this object.

In our example of a Meter that measures 3-phase current, voltage, etc., we have have a structure that contains the Current related members only:

  • Current Phase A Word 1 (CuAW1)

  • Current Phase A Word 2 (CuAW1)

  • Current Phase B Word 1 (CuBW1)

  • Current Phase B Word 2 (CuBW2)

  • Current Phase C Word 1 (CuCW1)

  • Current Phase C Word 2 (CuCW2)

  • Total Current Phase A (CurrentA) - Two Phase A WORDs –> DWORD

  • Total Current Phase B (CurrentB)

  • Total Current Phase C (CurrentC)

(17). Add a Structure by right-clicking on Application > Add Object > DUT….

A pop-up will appear entitled Add DUT (Device Unit Type)

(18). Select the Structure item and name it ST_POWERMETER (“Structure Type-Power Meter”)

(19). Fill in the file as shown below.

alternate text

Below is a view of the project tree with the Programmable Object Unit (POU) entitled Main and DUTs.

alternate text

Create the Variables in Main

(20). In the Main program, enter the following variables:

alternate text

Create the Main Control Code

The main area of the code is explained in each functional section.

(21). In the Main program, enter the following control code.

The first section shown below, is emulating a possible current reading from the Meter’s two data Words for Current A - Word 1 and Current A - Word 2. The values chosen in Hex were selected so when the two Words are appended, it produces the 16-bit value 12345678 in decimal.

alternate text

The second section, takes each Word and assigns it to an ARRAY variable that can easily be appended to create a DWORD.

alternate text

The next line of code writes this Array variable into the Union (.awValue) location and in parallel a DWORD is generated in the Union as the .dwValue assignment.

alternate text

The fourth section copies this DWORD value and puts it into a new variable named dwCurrentPhaseA which gives us the decimal value of “12345678”.

alternate text

To convert this value to a Real or Floating Point values, the urREsult (datatype Union to convert to Real) is copied into the Union as shown below.

alternate text

And finally, the SCADA unit wants the data in Amps and since the value is in mA from the Meter i.e. 12345678 mA, we need to convert this to Amps by dividing by the mA value by 1000 to get 12345.678 amps.

alternate text

A View of the “Task”

Another very important area in the Device Tree is the Task Configuration section which defines the program tasks are defined (which POUs are to run on a scan) and the characteristics of the task (priority, speed etc.).

The online help goes into detail for this area. The defaults are good for this project.

Compile the Project

Next, we will compile and build the project to download to the P2CDS-622 target.

(22). Select from the drop-down menus- Build > Generate Code.

Connect to PLC and Download

The project needs to be downloaded to the target but we must first connect to it.

(23). Physically connect the Host PC to one of the Ethernet ports on the P2CDS-622.

Configure Connection Channel to PLC

(24). In the Device Tree, Double-click on the Device(P2CDS622).

A Device tab should appear with the first tab on the left selected entitled Communication Settings selected. Now we need to scan the network to find our device.

(25). In the Communication Settings section, click on the Scan network.

A dialog box should pop up with all the CODESYS PLCs available to connect to on this network.

Note

If you are unsure which unit is the one you want to connect to, on the right side is a Wink select box. Click on this and this P2CDS-622 front panel RUN light should start to flash for 5 seconds.

(26). Select the applicable P2CDS-622 unit and Click OK.

(27). Toggle the Run/Stop switch.

Now the following should be visible- both the Gateway and the P2CDS-622 have the green indicators present indicating the IDE is connected to the P2CDS-622.

alternate text

Download Application to PLC

(28). Click Online > Login.

A dialog opens up if there is an existing application on the PLC, if so Click Yes to continue.

Successful Download. Ready to Run

The system should show green highlighted Device and Application as shown below:

The alternate text icon indicates connection is active (but program is not executing).

alternate text

Run and Watch the Application

This section will start the application running and using the debugger features, allow a user to watch the variables in real time, set breakpoints and force variables to a specific value.

Start the Application

(29). Click Debug > Start.

The IDE should now be showing a green “Run” in the lower portion of the status area. Also, the Main Task has the running icon alternate text next to it.

Watch the Application

There are several ways to monitor the variables of the application program and to influence them in the watch view:

  • Online views of individual POUs

  • Write and force variable values

  • Defined variable lists in separate watch lists

Online View of the POU

This view of a POU shows the current values of the watchable expressions contained in a table in the declaration part, and if enabled, also in the implementation section in the form of “inline monitoring”.

(30). To open the online view, in the device tree double-click Main, or select it and in the context menu click Edit Object.

In the center part of the view, you see the lines of code as specified in offline mode. They are supplemented by the orange inline monitoring views after each variable which show the current value.

In the top part, a table shows the watchable expressions of the POU- the corresponding variables with Type and current Value.

alternate text

Debug the Application

One of the most powerful features of CODESYS is its debugging features. For example, you can set multiple breakpoints and use different step-through commands around lines of code.

Set Breakpoints

In Online/Login mode (not “Started” yet), you can set breakpoints where any program execution should be halted. When a breakpoint is reached, the program can be processed in steps. At each breakpoint and in each step, you can check the current value of the variables in the watch views.

(31).To demonstrate, “Main”, set the cursor in line 7. In the menu, select Debug > Toggle Breakpoint to insert a Breakpoint where your cursor is.

The breakpoint is displayed in the program.

(32). Click Debug > Start to run the program.

The application will run to line 7 where the breakpoint is and halt. It is currently in the stop status, it will look like:

alternate text

Note

The line where the breakpoint is executed/stops has NOT been executed yet.

Stepping Through the Program

Now you can press <F8> repeatedly to execute the Step Into command in the Debug menu and run the program in single-step mode. This also steps into the function block instance.

(33). To skip this function block processing, instead of <F8> you can press <F10> to execute the Step Over command.

The current variable values are displayed at the processing position just reached.

Tip

The debugger can be single stepped also with the use of the Hot Keys that can be found in the following link: Hot Keys

(34). See also the Breakpoints dialog View > Breakpoints The currently defined breakpoints are listed here and can be edited or new ones can be added.

Note

Note that the breakpoint positions are saved even when you log out of the PLC. The next time you log in, they will be displayed as light red markers and can be reactivated.

Execute Code Back to Beginning

If you hit a breakpoint and are stepping through and want to restart from the beginning of your program while still Online.

(35). Select Online > Reset Warm.

This will take you to the beginning of the program in Stop mode, and wait for you to initiate another Start.