Makers Blog

How to define complex GDS Ports and connect them to GDS Port in IEC code

Bjoern PLCnext Team 07 December 2020 min. read
616 views 0 comments

Abstract

In this article I will explain how to define complex GDS ports like data structures in C++ and connect them to GDS port in IEC code.

Data Structures

How to declare data structures. Lets look at C++.

#pragma once
#include "Arp/System/Core/Arp.h"
#include "Arp/Plc/Commons/PlcTypes.h"

namespace MyComponent
{

class MyData
{
public:
    MyData() = default;
    ~MyData() = default;

public:
    Arp::boolean Valid = false;
    Arp::int16 IntegerValue = 0;
    Arp::Plc::Commons::Gds::StaticString<80> StringValue = "";
    Arp::uint8 Data[8] = {0x00};
};

} // namespace MyComponent

And in the Header of the programm class this type can now be defined as GDS Port.

#pragma once
...
#include "MyData.hpp"

namespace MyComponent
{

//#program
//#component(MyComponent::MyComponent)
class MyProgramm ...
{

...

public:
    //#port
    //#name(TheData)
    //#attributes(Input)
    MyData GdsPortMyData;

};

} // namespace MyComponent

To be able to connect the GDS Port to a GDS Port in IEC code the variable in the IEC code has to be of the same type. This means the IEC structure has to have same data layout as the C++ data type. In addition all element names have to match the elements in the C++ type.

TYPE

UDT_PROJECT_MY_DATA : STRUCT
    Valid BOOL;
    IntegerValue : INT;
    StringValue : STRING;
    Data : ARRAY [0..7] OF BYTE;
END_STRUCT;

END_TYPE

A GDS Out Port of the type UDT_PROJECT_MY_DATA can now be connected to the GDS In Port TheData of the C++ program.

Special note to STRING

A special note to STRING data type. Be aware that the IEC code for now is not aware of UTF-8 encoding. The string literals are using ISO-8859-1 encoding. So it may be necessary to convert them to UTF-8 before comparing them with string literals in C++ code. You can use the boost-locale library for converting between ISO-8859-1 to UTF-8 and back.

Arp::String toUtf8(const Arp::String& s)
{
    return boost::locale::conv::to_utf<char>(s.CStr(), "ISO-8859-1");
}

Note:

The Makers Blog shows applications and user stories of community members that are not tested or reviewed by Phoenix Contact. Use them at your own risk.

Discussion

Please login/register to comment

Login/Register

Leave a Reply

Newsletter
Never miss a new article
Sign up for the newsletter
Never miss news about PLCnext Technology
Get interesting content via newsletter four times a year
Receive exclusive information before all other users