-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
362 lines (336 loc) · 14.6 KB
/
test.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
/*=============================================================================|
| PROJECT SNAP7 1.4.0 |
|==============================================================================|
| Copyright (C) 2013, 2014 Davide Nardella |
| All rights reserved. |
|==============================================================================|
| SNAP7 is free software: you can redistribute it and/or modify |
| it under the terms of the Lesser GNU General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| It means that you can distribute your commercial software linked with |
| SNAP7 without the requirement to distribute the source code of your |
| application and without the requirement that your application be itself |
| distributed under LGPL. |
| |
| SNAP7 is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| Lesser GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License and a |
| copy of Lesser GNU General Public License along with Snap7. |
| If not, see http://www.gnu.org/licenses/ |
|==============================================================================|
| |
| Client Example |
| |
|=============================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include "snap7.h"
#include "Common/S7Utils.hxx"
#ifdef OS_WINDOWS
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
TS7Client *Client;
byte Buffer[65536]; // 64 K buffer
int SampleDBNum = 1000;
char *Address; // PLC IP Address
int Rack=0,Slot=2; // Default Rack and Slot
int ok = 0; // Number of test pass
int ko = 0; // Number of test failure
bool JobDone=false;
int JobResult=0;
//------------------------------------------------------------------------------
// Async completion callback
//------------------------------------------------------------------------------
// This is a simply text demo, we use callback only to set an internal flag...
void S7API CliCompletion(void *usrPtr, int opCode, int opResult)
{
JobResult=opResult;
JobDone = true;
}
//------------------------------------------------------------------------------
// Usage Syntax
//------------------------------------------------------------------------------
void Usage()
{
printf("Usage\n");
printf(" client <IP> [Rack=0 Slot=2]\n");
printf("Example\n");
printf(" client 192.168.1.101 0 2\n");
printf("or\n");
printf(" client 192.168.1.101\n");
getchar();
}
//------------------------------------------------------------------------------
// Check error
//------------------------------------------------------------------------------
bool Check(int Result, const char * function)
{
printf("\n");
printf("+-----------------------------------------------------\n");
printf("| %s\n",function);
printf("+-----------------------------------------------------\n");
if (Result==0) {
printf("| Result : OK\n");
printf("| Execution time : %d ms\n",Client->ExecTime());
printf("+-----------------------------------------------------\n");
ok++;
}
else {
printf("| ERROR !!! \n");
if (Result<0)
printf("| Library Error (-1)\n");
else
printf("| %s\n",CliErrorText(Result).c_str());
printf("+-----------------------------------------------------\n");
ko++;
}
return Result==0;
}
TS7DataItem RAMS7200Read(std::string RAMS7200Address, void* val)
{
TS7DataItem item = Common::S7Utils::TS7DataItemFromAddress(RAMS7200Address, true);
int memSize = ( Common::S7Utils::DataSizeByte(item.WordLen )*item.Amount);
printf("-------------read RAMS7200Address=>(Area, Start, WordLen, Amount): memSize : %s =>(%d, %d, %d, %d) : %dB", RAMS7200Address.c_str(), item.Area, item.Start, item.WordLen, item.Amount, memSize);
if(Client->ReadMultiVars(&item, 1) == 0){
if(item.WordLen == S7WLWord){
uint16_t wordVal = Common::Utils::CopyNSwapBytes<uint16_t>(item.pdata);
std::memcpy(val, &wordVal, memSize);
}
else{
std::memcpy(val, item.pdata, memSize);
}
printf(Common::S7Utils::DisplayTS7DataItem(&item, Common::S7Utils::Operation::READ).c_str());
}
else{
printf("--> read NOK!\n");
}
return item;
}
TS7DataItem RAMS7200Write(std::string RAMS7200Address, void* val)
{
TS7DataItem item = Common::S7Utils::TS7DataItemFromAddress(RAMS7200Address, true);
int memSize = ( Common::S7Utils::DataSizeByte(item.WordLen )*item.Amount);
if(item.WordLen == S7WLWord){
uint16_t wordVal = Common::Utils::CopyNSwapBytes<uint16_t>(val);
std::memcpy(item.pdata , &wordVal , memSize);
}
else{
std::memcpy(item.pdata , val , memSize);
}
printf("-------------write RAMS7200Address=>(Area, Start, WordLen, Amount): memSize : %s =>(%d, %d, %d, %d) : %dB", RAMS7200Address.c_str(), item.Area, item.Start, item.WordLen, item.Amount, memSize);
if(Client->WriteMultiVars(&item, 1) == 0){
printf("--> write OK!\n");
}
else{
printf("--> write NOK!\n");
}
return item;
}
//------------------------------------------------------------------------------
// Unit Connection
//------------------------------------------------------------------------------
bool CliConnect()
{
Client->SetConnectionParams(Address, 0x1101, 0x1100);
printf("Trying to connect to %s", Address);
int res = Client->Connect();
if (Check(res,"UNIT Connection")) {
printf(" Connected to : %s (Rack=%d, Slot=%d)\n",Address,Rack,Slot);
printf(" PDU Requested : %d bytes\n",Client->PDURequested());
printf(" PDU Negotiated : %d bytes\n",Client->PDULength());
};
return res==0;
}
//------------------------------------------------------------------------------
// Unit Disconnection
//------------------------------------------------------------------------------
void CliDisconnect()
{
Client->Disconnect();
}
//------------------------------------------------------------------------------
// Perform readonly tests, no cpu status modification
//------------------------------------------------------------------------------
void PerformTests()
{
std::string addresses[] = {
"VW1984", //VW1984=13220
"VB2978.20", //VB2978.20='29 1-035',
"VW2000",
"VW2002",
"VB1604", //VB1604=8
"V1604.2", //V1604.2=0
"V1604.3", //V1604.3=1
"V2640.0",
"V2640.02",
"V2640.04",
"V2640.06",
"V2641.0",
"V2641.02",
"V2641.04",
"V2641.06",
//"M10.0", //M10.0=1
//"M10.1", //M10.1=0 Read-only?
//"E0.0", //E0.0=1 Read-only?
//"E1.1", //E0.0=0 Read-only?
//"A0.2", //A0.2=1 Read-only?
//"A0.3", //A0.3=0 Read-only?
//"I0.2", //I0.2=1 Read-only?
//"I0.3", //I0.3=0 Read-only?
//"Q0.2", //Q0.2=1 Read-only?
//"Q0.3" //Q0.3=0 Read-only?
};
for(std::string address : addresses){
TS7DataItem item = Common::S7Utils::TS7DataItemFromAddress(address, true);
switch (item.WordLen){
case S7WLByte:
if(item.Amount >1){
printf("Item.amount is : %d\n", item.Amount);
char valInitR[256], valInitW[256], valChangedW[256], valChangedR[256];
RAMS7200Read(address, &valInitR);
std::memcpy(valChangedW , &valInitR , 256);
valChangedW[0] = valChangedW[0]+1;
RAMS7200Write(address, &valChangedW);
RAMS7200Read(address, &valChangedR);
Check(strcmp(valChangedW,valChangedR), ("IO :" + address).c_str());
std::memcpy(valInitW , &valInitR , 256);
RAMS7200Write(address, &valInitW);
RAMS7200Read(address, &valInitR);
Check(strcmp(valInitW,valInitR), ("IO re-init :" + address).c_str());
printf("valInitR: %s\n",valInitR);
printf("valInitW: %s\n",valInitW);
printf("valChangedW: %s\n",valChangedW);
printf("valChangedR: %s\n",valChangedR);
}
else{
uint8_t valInitR, valInitW, valChangedW, valChangedR;
RAMS7200Read(address, &valInitR);
valChangedW = valInitR == 1 ? 0 : 1;
RAMS7200Write(address, &valChangedW);
RAMS7200Read(address, &valChangedR);
Check((valChangedW==valChangedR) ? 0 : -1, ("IO :" + address).c_str());
valInitW = valInitR;
RAMS7200Write(address, &valInitW);
RAMS7200Read(address, &valInitR);
Check((valInitW==valInitR) ? 0 : -1, ("IO re-init :" + address).c_str());
printf("valInitR: %d\n",valInitR);
printf("valInitW: %d\n",valInitW);
printf("valChangedW: %d\n",valChangedW);
printf("valChangedR: %d\n",valChangedR);
}
break;
case S7WLBit:{
uint8_t valInitR, valInitW, valChangedW, valChangedR;
RAMS7200Read(address, &valInitR);
valChangedW = valInitR == 0x0001 ? 0x0000 : 0x0001;
RAMS7200Write(address, &valChangedW);
RAMS7200Read(address, &valChangedR);
Check((valChangedW==valChangedR) ? 0 : -1, ("IO :" + address).c_str());
valInitW = valInitR;
RAMS7200Write(address, &valInitW);
RAMS7200Read(address, &valInitR);
Check((valInitW==valInitR) ? 0 : -1, ("IO re-init :" + address).c_str());
printf("valInitR: %d\n",valInitR);
//hexdump(&valInitR , sizeof(valInitR));
printf("valInitW: %d\n",valInitW);
//hexdump(&valInitW , sizeof(valInitW));
printf("valChangedW: %d\n",valChangedW);
//hexdump(&valChangedW , sizeof(valChangedW));
printf("valChangedR: %d\n",valChangedR);
//hexdump(&valChangedR , sizeof(valChangedR));
}
break;
case S7WLWord:{
uint16_t valInitR, valInitW, valChangedW, valChangedR;
RAMS7200Read(address, &valInitR);
valChangedW = valInitR == 1 ? 0 : 1;
RAMS7200Write(address, &valChangedW);
RAMS7200Read(address, &valChangedR);
Check((valChangedW==valChangedR) ? 0 : -1, ("IO :" + address).c_str());
valInitW = valInitR;
RAMS7200Write(address, &valInitW);
RAMS7200Read(address, &valInitR);
Check((valInitW==valInitR) ? 0 : -1, ("IO re-init :" + address).c_str());
printf("valInitR: %d\n",valInitR);
printf("valInitW: %d\n",valInitW);
printf("valChangedW: %d\n",valChangedW);
printf("valChangedR: %d\n",valChangedR);
}
break;
case S7WLReal:{
float valInitR, valInitW, valChangedW, valChangedR;
RAMS7200Read(address, &valInitR);
valChangedW = valInitR == 1.0 ? 0.0 : 1.0;
RAMS7200Write(address, &valChangedW);
RAMS7200Read(address, &valChangedR);
Check((valChangedW==valChangedR) ? 0 : -1, ("IO :" + address).c_str());
valInitW = valInitR == 1 ? 0 : 1;
RAMS7200Write(address, &valInitW);
RAMS7200Read(address, &valInitR);
Check((valInitW==valInitR) ? 0 : -1, ("IO re-init :" + address).c_str());
printf("valInitR: %.3f\n",valInitR);
printf("valInitW: %.3f\\n",valInitW);
printf("valChangedW: %.3f\\n",valChangedW);
printf("valChangedR: %.3f\\n",valChangedR);
}
break;
default:
break;
}
if(item.pdata != nullptr){
delete[] item.pdata;
}
}
}
//------------------------------------------------------------------------------
// Tests Summary
//------------------------------------------------------------------------------
void Summary()
{
printf("\n");
printf("+-----------------------------------------------------\n");
printf("| Test Summary \n");
printf("+-----------------------------------------------------\n");
printf("| Performed : %d\n",(ok+ko));
printf("| Passed : %d\n",ok);
printf("| Failed : %d\n",ko);
}
//------------------------------------------------------------------------------
// Main
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
// Get Progran args (we need the client address and optionally Rack and Slot)
if (argc!=2 && argc!=4)
{
Usage();
return 1;
}
Address=argv[1];
if (argc==4)
{
Rack=atoi(argv[2]);
Slot=atoi(argv[3]);
}
// Client Creation
Client= new TS7Client();
Client->SetAsCallback(CliCompletion,NULL);
// Connection
if (CliConnect())
{
PerformTests();
CliDisconnect();
};
// Deletion
delete Client;
Summary();
return 0;
}