掌握 IEC 编程中的复杂 GDS 端口定义与连接
摘要
本文解释如何在 C++ 中定义复杂 GDS 端口(如数据结构)并将其连接到 IEC 代码中的 GDS 端口。
数据结构
如何声明数据结构。看 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
在程序类头文件中,此类型现可定义为 GDS 端口。
#pragma once ... #include "MyData.hpp" namespace MyComponent { //#program //#component(MyComponent::MyComponent) class MyProgramm ... { ... public: //#port //#name(TheData) //#attributes(Input) MyData GdsPortMyData; }; } // namespace MyComponent
要将 GDS 端口连接到 IEC 代码中的 GDS 端口,IEC 变量须为相同类型。即 IEC 结构须与 C++ 数据类型具有相同数据布局,且所有元素名须与 C++ 类型中的元素匹配。
TYPE UDT_PROJECT_MY_DATA : STRUCT Valid BOOL; IntegerValue : INT; StringValue : STRING; Data : ARRAY [0..7] OF BYTE; END_STRUCT; END_TYPE
类型 UDT_PROJECT_MY_DATA 的 GDS Out Port 现可连接到 C++ 程序的 GDS In Port TheData。
STRING 类型特别说明
STRING 数据类型特别注意。目前 IEC 代码不识别 UTF-8 编码。字符串字面量使用 ISO-8859-1 编码。因此在与 C++ 代码字符串字面量比较前,可能需转为 UTF-8。可用 boost-locale 库在 ISO-8859-1 与 UTF-8 间转换。
Arp::String toUtf8(const Arp::String& s) { return boost::locale::conv::to_utf<char>(s.CStr(), "ISO-8859-1"); }
工业技术