-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOPCClient.cpp
120 lines (102 loc) · 4.07 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
#include <iostream>
#include <process.h>
#include <iostream>
#include "OPCClient.h"
#include "Opcclass.h"
#include "SocketClient.h"
#include "MailslotOperations.h"
typedef unsigned (WINAPI *CAST_FUNCTION)(LPVOID);
typedef unsigned *CAST_LPDWORD;
//Main function of the OPC_Thread
DWORD WINAPI dwOPC()
{
HANDLE hCommunication;
MSG msg; //Used by the windows message buffer
int bRet; //Return values
Opcclass *Opc_tp = new Opcclass(); //Instantiate OPC Class
Opc_tp->AddGroup(); //Add the main group
cout << "Adding a group in the INACTIVE state for the moment...\n";
//Add the random Items that will be read
Opc_tp->AddItem((wchar_t *)L"Random.Boolean");
cout<< "Added item Random.Boolean to the group\n";
Opc_tp->AddItem((wchar_t *)L"Random.Int1");
cout<< "Added item Random.Int1 to the group\n";
Opc_tp->AddItem((wchar_t *)L"Random.Int2");
cout<< "Added item Random.Int2 to the group\n";
Opc_tp->AddItem((wchar_t *)L"Random.Real4");
cout<< "Added item Random.Real4 to the group\n";
//Add the default Items that will be Writen
Opc_tp->AddItem((wchar_t *)L"Bucket Brigade.Int1");
cout<< "Added item Bucket Brigade.Int1 to the group\n";
Opc_tp->AddItem((wchar_t *)L"Bucket Brigade.Int2");
cout<< "Added item Bucket Brigade.Int2 to the group\n";
Opc_tp->AddItem((wchar_t *)L"Bucket Brigade.Real4");
cout<< "Added item Bucket Brigade.Real4 to the group\n";
Opc_tp->AddItem((wchar_t *)L"Bucket Brigade.Real8");
cout<< "Added item Bucket Brigade.Real8 to the group\n";
Opc_tp->AddItem((wchar_t *)L"Bucket Brigade.Int4");
cout<< "Added item Bucket Brigade.Int4 to the group\n";
Opc_tp->AddItem((wchar_t *)L"Bucket Brigade.UInt1");
cout<< "Added item Bucket Brigade.UInt1 to the group\n";
Opc_tp->AddItem((wchar_t *)L"Bucket Brigade.UInt2");
cout<< "Added item Bucket Brigade.UInt2 to the group\n";
Opc_tp->AddItem((wchar_t *)L"Bucket Brigade.UInt4");
cout<< "Added item Bucket Brigade.UInt4 to the group\n";
//Set the callback configuration
Opc_tp->ConfigCallback();
//Activate the callbacks
Opc_tp->ActivateAsyncRead();
hCommunication = (HANDLE) _beginthreadex(
NULL,
0,
(CAST_FUNCTION)dwCommunication,
Opc_tp,
0,
NULL);
//Keep processing the message loops
do {
bRet = GetMessage( &msg, NULL, 0, 0 );
if (!bRet){
printf ("Failed to get windows message! Error code = %d\n", GetLastError());
exit(0);
}
TranslateMessage(&msg); // This call is not really needed ...
DispatchMessage(&msg); // ... but this one is!
} while(1);
CloseHandle (hCommunication);
//Ending the Thread
_endthreadex((DWORD) 0);
return 0;
}
DWORD WINAPI dwCommunication (LPVOID opc)
{
char bigBuffer[1024];
//Set Mailslot
HANDLE hMailslotOPC = CreateMailslot(
(LPCSTR)"\\\\.\\mailslot\\OPCTCP",
0,
MAILSLOT_WAIT_FOREVER,
NULL);
HANDLE hSyncOPC=OpenEvent(EVENT_MODIFY_STATE,FALSE,(LPCSTR)"SyncOPCTCP");
SetEvent(hSyncOPC);
//Set a basic config to be able to write an opc item
Opcclass *Opc_send = new Opcclass(); //Instantiate OPC Class
Opc_send->AddGroup(); //Add the main group
//Add the default Items that will be Writen
Opc_send->AddItem((wchar_t *)L"Bucket Brigade.Int1");
Opc_send->AddItem((wchar_t *)L"Bucket Brigade.Int2");
Opc_send->AddItem((wchar_t *)L"Bucket Brigade.Real4");
Opc_send->AddItem((wchar_t *)L"Bucket Brigade.Real8");
Opc_send->AddItem((wchar_t *)L"Bucket Brigade.Int4");
Opc_send->AddItem((wchar_t *)L"Bucket Brigade.UInt1");
Opc_send->AddItem((wchar_t *)L"Bucket Brigade.UInt2");
Opc_send->AddItem((wchar_t *)L"Bucket Brigade.UInt4");
while (true)
{
if (ReadSlot(hMailslotOPC, bigBuffer)==TRUE)
parseAndSend(bigBuffer,Opc_send);
}
CloseHandle (hMailslotOPC);
_endthreadex((DWORD) 0);
return 0;
}