-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOPCClient.cpp
150 lines (126 loc) · 3.38 KB
/
OPCClient.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
#include "OPCClient.h"
OPCClient::OPCClient()
{
StartCOM();
}
OPCClient::~OPCClient()
{
AbortCOM();
}
void OPCClient::StartCOM()
{
CoInitialize(0);
}
void OPCClient::AbortCOM()
{
CoUninitialize();
}
void OPCClient::GetCLSID()
{
CLSIDFromProgID(this->ServerName, &this->CLSIDServer);
}
void OPCClient::CreateInstance()
{
CoCreateInstance(this->CLSIDServer, NULL, CLSCTX_LOCAL_SERVER, IID_IOPCServer, (void**) &this->ServerConnected);
}
void OPCClient::OPCConnect(_bstr_t ServerName)
{
this->ServerName = ServerName;
GetCLSID();
CreateInstance();
StartGroup();
}
void OPCClient::StartGroup()
{
OPCHANDLE clientHandle = 1;
DWORD DefaultUpdTime = 5000, RevisedUpdTime, LanguageCode = 0x416;
IOPCItemMgt *Item = NULL;
OPCHANDLE GroupHandle = NULL;
this->ServerConnected->AddGroup(L"",
FALSE,
DefaultUpdTime,
clientHandle,
NULL,
NULL,
LanguageCode,
&this->OPCGroup,
&RevisedUpdTime,
IID_IOPCItemMgt,
(LPUNKNOWN*) &this->TypeReadItem);
this->TypeReadItem->QueryInterface(IID_IOPCSyncIO, (void**)&this->TypeReadSync);
}
void OPCClient::InsertItens(_bstr_t tags[], int QtdTags)
{
OPCITEMDEF *TagDef = new OPCITEMDEF[QtdTags];
OPCITEMRESULT *AddResult = NULL;
HRESULT *Errors = NULL;
this->QtdTags = QtdTags;
this->TagValues = new _variant_t[QtdTags];
this->TagNames = new _bstr_t[QtdTags];
for(int i = 0; i < this->QtdTags; i++)
this->TagNames[i] = tags[i];
for (int i = 0; i < this->QtdTags; i++)
{
TagDef[i].szAccessPath = _bstr_t("");
TagDef[i].szItemID = tags[i];
TagDef[i].bActive = FALSE;
TagDef[i].hClient = 1;
TagDef[i].dwBlobSize = 0;
TagDef[i].pBlob = NULL;
TagDef[i].vtRequestedDataType = 0;
}
this->TypeReadItem->AddItems(this->QtdTags, TagDef, &AddResult, &Errors);
this->OPCGroupTag = new OPCHANDLE[this->QtdTags];
for (int i = 0; i < this->QtdTags; i++)
this->OPCGroupTag[i] = AddResult[i].hServer;
if(Errors != NULL)
CoTaskMemFree(Errors);
if(TagDef != NULL)
delete [] TagDef;
}
_variant_t *OPCClient::ReadItens()
{
HRESULT *Errors = NULL;
OPCITEMSTATE *Values = NULL;
this->TypeReadSync->Read(OPC_DS_DEVICE, this->QtdTags, this->OPCGroupTag, &Values, &Errors);
for (int i = 0; i < this->QtdTags; i++)
this->TagValues[i] = Values[i].vDataValue;
for (int i = 0; i < this->QtdTags; i++)
VariantClear(&Values[i].vDataValue);
if (Values != NULL)
CoTaskMemFree(Values);
if (Errors != NULL)
CoTaskMemFree(Errors);
return this->TagValues;
}
void OPCClient::WriteItens(int TagIndex[], _variant_t Values[], int QtdValues)
{
using namespace std;
HRESULT *Errors = NULL;
OPCHANDLE *Items = new OPCHANDLE[QtdValues];
for (int i = 0; i < QtdValues; i++)
Items[i] = this->OPCGroupTag[TagIndex[i]];
this->TypeReadSync->Write(QtdValues, Items, Values, &Errors);
if (Errors != NULL)
CoTaskMemFree(Errors);
if (Items != NULL)
delete [] Items;
}
void OPCClient::Show(int ShowTime)
{
using namespace std;
for(int i=0;i < ShowTime;i++)
{
ReadItens();
cout<<"##################\n";
for(int i = 0; i < this->QtdTags; i++)
{
cout<<"# "<<this->TagNames[i]<<"| ";
printf("%6.0f", this->TagValues[i].dblVal);
cout<<" #\n";
}
cout<<"##################\n";
cout<<"\n";
Sleep(1000);
}
}