-
Notifications
You must be signed in to change notification settings - Fork 1
/
MailslotOperations.cpp
256 lines (226 loc) · 7.42 KB
/
MailslotOperations.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
// Mailslot Operations
//
// Thess functions are a modified version of the "Reading from a Mailslot"
// and "Writing from a Mailslot", originally available at Microsoft
// Developer Network Platforms (MSDN).
//
// Some functions that wasn't necessary for this project were removed and
// some adaptations were done.
//
// The Mailslots tutorial used can be found in
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa365794.aspx
//
//s
// Gustavo Barreto Garcia
// Rafael Gilmar Ribeiro Gurgel
#include <iostream>
#include <tchar.h>
#include "MailslotOperations.h"
BOOL WriteSlot(HANDLE hSlot, LPTSTR lpszMessage)
{
BOOL fResult;
DWORD cbWritten;
fResult = WriteFile(hSlot,
lpszMessage,
(DWORD) (lstrlen(lpszMessage)+1)*sizeof(TCHAR),
&cbWritten,
(LPOVERLAPPED) NULL);
if (!fResult)
{
return FALSE;
}
return TRUE;
}
//Return FALSE if no message is available
//Return TRUE and write the output to message in case of no error.
BOOL ReadSlot(HANDLE hSlot, char* message)
{
DWORD cbMessage, cMessage, cbRead;
BOOL fResult;
LPTSTR lpszBuffer;
TCHAR achID[80];
DWORD cAllMessages;
HANDLE hEvent;
OVERLAPPED ov;
//Clear message contents
message[0] = '\0';
cbMessage = cMessage = cbRead = 0;
hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
if( NULL == hEvent )
return FALSE;
ov.Offset = 0;
ov.OffsetHigh = 0;
ov.hEvent = hEvent;
fResult = GetMailslotInfo( hSlot, // mailslot handle
(LPDWORD) NULL, // no maximum message size
&cbMessage, // size of next message
&cMessage, // number of messages
(LPDWORD) NULL); // no read time-out
if (!fResult)
{
std::cout << "GetMailslotInfo failed." << std::endl;
return FALSE;
}
if (cbMessage == MAILSLOT_NO_MESSAGE)
{
//std::cout << "Waiting for a message..." << std::endl;
return FALSE;
}
cAllMessages = cMessage;
if (cMessage != 0)
{
while (cMessage != 0) // retrieve all messages
{
// Allocate memory for the message.
lpszBuffer = (LPTSTR) GlobalAlloc(GPTR,
lstrlen((LPTSTR) achID)*sizeof(TCHAR) + cbMessage);
if( NULL == lpszBuffer )
return FALSE;
lpszBuffer[0] = '\0';
fResult = ReadFile(hSlot,
lpszBuffer,
cbMessage,
&cbRead,
&ov);
if (!fResult)
{
std::cout << "ReadFile failed." << std::endl;
GlobalFree((HGLOBAL) lpszBuffer);
return FALSE;
}
// Write the message.
strcpy_s(message, 1024, lpszBuffer);
//std::cout << message << std::endl;
GlobalFree((HGLOBAL) lpszBuffer);
fResult = GetMailslotInfo(hSlot, // mailslot handle
(LPDWORD) NULL, // no maximum message size
&cbMessage, // size of next message
&cMessage, // number of messages
(LPDWORD) NULL); // no read time-out
if (!fResult)
{
std::cout << "GetMailslotInfo failed." << std::endl;
return FALSE;
}
}
CloseHandle(hEvent);
return TRUE;
}
CloseHandle(hEvent);
return FALSE;
}
//Parse the information sent by the Sockets client and write on the OPC registers.
void parseAndSend(char* information, Opcclass *OpcSd)
{
int tempValue = 0; int aux = 0; int index = 0;
unsigned int tempUValue = 0;
float tempFloat;
double tempDouble;
char tempBuff[6];
//POG! TROCAR ISSO AQUI POR UM ENVIO PARA O MAILSLOT DO MAIN!
std::cout << information << std::endl;
//ENDPOG
////////////////////////////////////////////////////////////////////////////////
//Send the VT_I1 data
for (aux=0;aux<6;aux++)
{
tempBuff[aux]=information[aux+index];
}
sscanf(tempBuff, "%d", &tempValue);
//Set max and min values
if (tempValue > 127)
tempValue = 127;
else if (tempValue < -128)
tempValue = -128;
OpcSd->WriteItem(1,&tempValue,VT_I1);
////////////////////////////////////////////////////////////////////////////////
//Send the VT_I2 data
index=7*1;
for (aux=0;aux<6;aux++)
{
tempBuff[aux]=information[aux+index];
}
//Set max and min values
if (tempValue > 32767)
tempValue = 32767;
else if (tempValue < -32768)
tempValue = -32768;
sscanf(tempBuff, "%d", &tempValue);
OpcSd->WriteItem(2,&tempValue,VT_I2);
////////////////////////////////////////////////////////////////////////////////
//Send the VT_R4 data
index=7*2;
for (aux=0;aux<6;aux++)
{
tempBuff[aux]=information[aux+index];
}
sscanf(tempBuff, "%f", &tempFloat);
//Set max and min values
OpcSd->WriteItem(3,&tempFloat,VT_R4);
////////////////////////////////////////////////////////////////////////////////
//Send the VT_R8 data
index=7*3;
for (aux=0;aux<6;aux++)
{
tempBuff[aux]=information[aux+index];
}
sscanf(tempBuff, "%f", &tempDouble);
//Set max and min values
OpcSd->WriteItem(4,&tempDouble,VT_R8);
////////////////////////////////////////////////////////////////////////////////
//Send the VT_I4 data
index=7*4;
for (aux=0;aux<6;aux++)
{
tempBuff[aux]=information[aux+index];
}
sscanf(tempBuff, "%d", &tempValue);
//Set max and min values
if (tempValue > 2147483647)
tempValue = 2147483647;
else if (tempValue < -2147483648)
tempValue = -2147483648;
OpcSd->WriteItem(5,&tempValue,VT_I4);
////////////////////////////////////////////////////////////////////////////////
//Send the VT_UI1 data
index=7*5;
for (aux=0;aux<6;aux++)
{
tempBuff[aux]=information[aux+index];
}
sscanf(tempBuff, "%d", &tempUValue);
//Set max and min values
if (tempUValue > 256)
tempUValue = 256;
else if (tempUValue < 0)
tempUValue = 0;
OpcSd->WriteItem(6,&tempUValue,VT_UI1);
////////////////////////////////////////////////////////////////////////////////
//Send the VT_UI2 data
index=7*6;
for (aux=0;aux<6;aux++)
{
tempBuff[aux]=information[aux+index];
}
sscanf(tempBuff, "%d", &tempUValue);
//Set max and min values
if (tempUValue > 65535)
tempUValue = 65535;
else if (tempUValue < 0)
tempUValue = 0;
OpcSd->WriteItem(7,&tempUValue,VT_UI2);
////////////////////////////////////////////////////////////////////////////////
//Send the VT_UI4 data
index=7*7;
for (aux=0;aux<6;aux++)
{
tempBuff[aux]=information[aux+index];
}
sscanf(tempBuff, "%d", &tempUValue);
//Set max and min values
if (tempUValue > 4294967295)
tempUValue = 4294967295;
else if (tempUValue < 0)
tempUValue = 0;
OpcSd->WriteItem(8,&tempUValue,VT_UI4);
}