forked from PLCnext/CppExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataAccessComponent.cpp
371 lines (313 loc) · 14 KB
/
DataAccessComponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
///////////////////////////////////////////////////////////////////////////////"
//
// Copyright PHOENIX CONTACT Electronics GmbH
//
///////////////////////////////////////////////////////////////////////////////
#include "DataAccessComponent.hpp"
#include "Arp/Plc/Commons/Domain/PlcDomainProxy.hpp"
#include "DataAccessLibrary.hpp"
#include "Arp/System/Rsc/ServiceManager.hpp"
#include "Arp/System/Rsc/Services/RscArrayReader.hpp"
#include "Arp/System/Rsc/Services/RscArrayWriter.hpp"
#include "Arp/System/Rsc/Services/RscStructReader.hxx"
#include "Arp/System/Rsc/Services/RscStructWriter.hxx"
#include "Arp/System/Rsc/Services/RscString.hxx"
void ReadPortNames(Arp::System::Rsc::Services::IRscWriteEnumerator<RscString<512>>& portNames)
{
// In this case the values of three GDS variables will be read.
// The names of these variables are specified using the write enumerator.
portNames.BeginWrite(3);
try
{
portNames.WriteNext("Arp.Plc.Eclr/DataAccessInstance.Int_OUT");
portNames.WriteNext("Arp.Plc.Eclr/DataAccessInstance.Array_OUT");
portNames.WriteNext("Arp.Plc.Eclr/DataAccessInstance.Struct_OUT");
portNames.EndWrite();
}
catch (std::exception& e)
{
Arp::System::Commons::Diagnostics::Logging::Log::Error("Error occurred in {0}:\n{1}", __FUNCTION__, e.what());
Arp::System::Commons::Diagnostics::Logging::Log::Info("<- ReadPortNames()");
}
}
void ReadResult(Arp::System::Rsc::Services::IRscReadEnumerator<Arp::Plc::Gds::Services::ReadItem>& dataItems)
{
Arp::Plc::Gds::Services::ReadItem item;
auto elements = dataItems.BeginRead();
try
{
// The items in the ReadEnumerator appear in the order that the variable names were added in the "Port Names" delegate.
// The names of the variables are not available here, so it must be known what order the variable names were added.
// In this case, because each variable happens to have a different type, the variables can be identified by the type.
while (dataItems.ReadNext(item))
{
switch (item.Value.GetType())
{
case RscType::Int16:
{
Arp::int16 value;
item.Value.CopyTo(value);
Arp::System::Commons::Diagnostics::Logging::Log::Info("Int_OUT from DataAccess Read() = {0}", value);
break;
}
case RscType::Array:
{
RscArrayReader arrayReader{item.Value};
// Use the following RscArrayReader methods to get information about the array:
// - GetElementType()
// - GetSize()
// - GetDimensions();
RscVariant<512> current;
arrayReader.ReadNext(current);
Arp::int16 value;
current.CopyTo(value);
Arp::System::Commons::Diagnostics::Logging::Log::Info("Array_OUT[0] from DataAccess Read() = {0}", value);
// Use the ReadNext() method to iterate through all array elements.
break;
}
case RscType::Struct:
{
RscStructReader<512> structReader{item.Value};
// Use the following RscStructReader method to get information about the struct:
// - GetFieldCount()
// Struct element values will appear in the order that the elements are declared.
// The names of the struct elements are not available.
RscVariant<512> current;
structReader.ReadNextField(current);
// Process the MyBool field if necessary.
structReader.ReadNextField(current);
Arp::int16 intValue;
current.CopyTo(intValue);
Arp::System::Commons::Diagnostics::Logging::Log::Info("Struct_OUT.MyInt16 from DataAccess Read() = {0}", intValue);
structReader.ReadNextField(current);
// Process the MyFloat32 field if necessary.
structReader.ReadNextField(current);
const char * stringValue = current.GetChars();
Arp::System::Commons::Diagnostics::Logging::Log::Info("Struct_OUT.MyString from DataAccess Read() = {0}", stringValue);
break;
}
default:
{
Arp::System::Commons::Diagnostics::Logging::Log::Info("Unhandled type = {0}", item.Value.GetType());
break;
}
}
}
dataItems.EndRead();
}
catch (std::exception& e)
{
Arp::System::Commons::Diagnostics::Logging::Log::Error("Error occurred in {0}:\n{1}", __FUNCTION__, e.what());
Arp::System::Commons::Diagnostics::Logging::Log::Info("<- ReadResult()");
}
}
void WriteData(Arp::System::Rsc::Services::IRscWriteEnumerator<Arp::Plc::Gds::Services::WriteItem>& dataItems)
{
// This function writes values to three GDS variables - an Int16, a struct, and an array.
dataItems.BeginWrite(3);
try
{
// 1. Write the Integer port name and value.
Arp::Plc::Gds::Services::WriteItem intItem;
intItem.PortName="Arp.Plc.Eclr/DataAccessInstance.Int_IN";
intItem.Value=(Arp::int16)42;
dataItems.WriteNext(intItem);
// 2. Write the Array port name and value
// Create the RscVariant representing the array
Arp::Plc::Gds::Services::WriteItem arrayItem;
arrayItem.PortName="Arp.Plc.Eclr/DataAccessInstance.Array_IN";
arrayItem.Value=RscVariant<512>::CreateArrayVariant(10,RscType::Int16);
// Write the array item to the enumerator *before* creating the RscArrayWriter
dataItems.WriteNext(arrayItem);
// Create an RscArrayWriter for the array
// This must be done *after* the array WriteItem has been written to the enumerator,
// otherwise this error appears: "WriteElementFunction in RscVariant has to be defined"
RscArrayWriter arrayWriter{arrayItem.Value};
// Fill the RSC Array Variant with RscType::Int16 values
arrayWriter.WriteNext((Arp::int16)42);
arrayWriter.WriteNext((Arp::int16)142);
arrayWriter.WriteNext((Arp::int16)242);
arrayWriter.WriteNext((Arp::int16)342);
arrayWriter.WriteNext((Arp::int16)442);
arrayWriter.WriteNext((Arp::int16)542);
arrayWriter.WriteNext((Arp::int16)642);
arrayWriter.WriteNext((Arp::int16)742);
arrayWriter.WriteNext((Arp::int16)842);
arrayWriter.WriteNext((Arp::int16)942);
// 3. Write the Struct port name and value
// Create the RscVariant representing the struct, with four fields
Arp::Plc::Gds::Services::WriteItem structItem;
structItem.PortName="Arp.Plc.Eclr/DataAccessInstance.Struct_IN";
structItem.Value=RscVariant<512>::CreateStructVariant(4);
// Write the struct item to the enumerator *before* creating the RscStructWriter
dataItems.WriteNext(structItem);
// Create an RscStructWriter for the struct
RscStructWriter<512> structWriter{structItem.Value};
// Fill the RSC Struct Variant with values
// The fields are written in the order that they are declared
structWriter.WriteNextField((Arp::boolean)true);
structWriter.WriteNextField((Arp::int16)42);
structWriter.WriteNextField((Arp::float32)42.31);
// Create an RscString variable to write to the next struct element
// IMPORTANT: The maximum size of the RscString MUST be the same as the maximum string size of the RscStructWriter object,
Arp::System::Rsc::Services::RscString<512> myString("String from C++");
structWriter.WriteNextField(myString);
dataItems.EndWrite();
}
catch (std::exception& e)
{
Arp::System::Commons::Diagnostics::Logging::Log::Error("Error occurred in {0}:\n{1}", __FUNCTION__, e.what());
Arp::System::Commons::Diagnostics::Logging::Log::Info("<- WriteData()");
}
}
void WriteResult(Arp::System::Rsc::Services::IRscReadEnumerator<Arp::Plc::Gds::Services::DataAccessError>& errors)
{
Arp::Plc::Gds::Services::DataAccessError error;
auto elements = errors.BeginRead();
try
{
while (errors.ReadNext(error))
{
// TODO: Handle the error.
}
errors.EndRead();
}
catch (std::exception& e)
{
Arp::System::Commons::Diagnostics::Logging::Log::Error("Error occurred in {0}:\n{1}", __FUNCTION__, e.what());
Arp::System::Commons::Diagnostics::Logging::Log::Info("<- WriteResult()");
}
}
namespace DataAccess
{
using Arp::System::Rsc::ServiceManager;
using namespace Arp::Plc::Commons::Domain;
DataAccessComponent::DataAccessComponent(IApplication& application, const String& name)
: ComponentBase(application, ::DataAccess::DataAccessLibrary::GetInstance(), name, ComponentCategory::Custom)
, MetaComponentBase(::DataAccess::DataAccessLibrary::GetInstance().GetNamespace())
, dataAccessThread(this, &DataAccessComponent::AccessData, 1000, "DataAccessThread")
{
}
void DataAccessComponent::Initialize()
{
// never remove next line
PlcDomainProxy::GetInstance().RegisterComponent(*this, false);
// initialize singletons here, subscribe notifications here
PlcDomainProxy& plcDomainProxy = PlcDomainProxy::GetInstance();
// register all Plc event handler
plcDomainProxy.PlcLoaded += make_delegate(this, &DataAccessComponent::OnPlcLoaded);
plcDomainProxy.PlcStarted += make_delegate(this, &DataAccessComponent::OnPlcStarted);
plcDomainProxy.PlcStopping += make_delegate(this, &DataAccessComponent::OnPlcStopping);
plcDomainProxy.PlcUnloading += make_delegate(this, &DataAccessComponent::OnPlcUnloading);
plcDomainProxy.PlcChanging += make_delegate(this, &DataAccessComponent::OnPlcChanging);
plcDomainProxy.PlcChanged += make_delegate(this, &DataAccessComponent::OnPlcChanged);
}
void DataAccessComponent::SubscribeServices()
{
// gets the IDataAccessService pointer
this->dataAccessServicePtr = ServiceManager::GetService<IDataAccessService>();
}
void DataAccessComponent::LoadSettings(const String& /*settingsPath*/)
{
// load firmware settings here
}
void DataAccessComponent::SetupSettings()
{
// never remove next line
MetaComponentBase::SetupSettings();
// setup firmware settings here
}
void DataAccessComponent::PublishServices()
{
// publish the services of this component here
}
void DataAccessComponent::LoadConfig()
{
// load project config here
}
void DataAccessComponent::SetupConfig()
{
// setup project config here
}
void DataAccessComponent::ResetConfig()
{
// implement this inverse to SetupConfig() and LoadConfig()
}
void DataAccessComponent::Dispose()
{
// never remove next line
MetaComponentBase::Dispose();
// implement this inverse to SetupSettings(), LoadSettings() and Initialize()
PlcDomainProxy& plcDomainProxy = PlcDomainProxy::GetInstance();
// unregister all Plc event handler
plcDomainProxy.PlcLoaded -= make_delegate(this, &DataAccessComponent::OnPlcLoaded);
plcDomainProxy.PlcStarted -= make_delegate(this, &DataAccessComponent::OnPlcStarted);
plcDomainProxy.PlcStopping -= make_delegate(this, &DataAccessComponent::OnPlcStopping);
plcDomainProxy.PlcUnloading -= make_delegate(this, &DataAccessComponent::OnPlcUnloading);
plcDomainProxy.PlcChanging -= make_delegate(this, &DataAccessComponent::OnPlcChanging);
plcDomainProxy.PlcChanged -= make_delegate(this, &DataAccessComponent::OnPlcChanged);
}
void DataAccessComponent::PowerDown()
{
// implement this only if data must be retained even on power down event
}
void DataAccessComponent::OnPlcLoaded()
{
}
void DataAccessComponent::OnPlcStarted()
{
this->StartDataAccess();
}
void DataAccessComponent::OnPlcStopping()
{
this->StopDataAccess();
}
void DataAccessComponent::OnPlcUnloading(bool)
{
}
void DataAccessComponent::OnPlcChanging()
{
this->StopDataAccess();
}
void DataAccessComponent::OnPlcChanged(bool /*success*/)
{
this->StartDataAccess();
}
void DataAccessComponent::StartDataAccess()
{
this->dataAccessThread.Start();
}
void DataAccessComponent::StopDataAccess()
{
this->dataAccessThread.Stop();
}
void DataAccessComponent::AccessData()
{
// 1. Read a single variable
// "ReadSingle" can only be used with primitive types
int16 Int_IN = 0;
ReadItem readPortData = this->dataAccessServicePtr->ReadSingle("Arp.Plc.Eclr/DataAccessInstance.Int_OUT");
if (readPortData.Error == DataAccessError::None)
{
readPortData.Value.CopyTo(Int_IN);
this->log.Info("Int_OUT '{0}' read from ReadSingle()", Int_IN);
}
else this->log.Info("Error reading single Int from GDS");
// 2. Write a single variable
// "WriteSingle" can only be used with primitive types
WriteItem writePortData;
writePortData.PortName = "Arp.Plc.Eclr/DataAccessInstance.Int_IN";
writePortData.Value = this->Int_OUT++;
if (this->dataAccessServicePtr->WriteSingle(writePortData) == DataAccessError::None)
{
// Success
}
else this->log.Info("Error writing single Int to GDS");
// 3. Read multiple variables
// "Read" can be used with both primitive and complex types (e.g. arrays and structs)
this->dataAccessServicePtr->Read(make_delegate(ReadPortNames), make_delegate(ReadResult));
// 4. Write multiple variables
// "Write" can be used with both primitive and complex types (e.g. arrays and structs)
this->dataAccessServicePtr->Write(make_delegate(WriteData), make_delegate(WriteResult));
}
} // end of namespace DataAccess