forked from netdata-be/libnodave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestISO_TCP.pas
executable file
·424 lines (387 loc) · 13 KB
/
testISO_TCP.pas
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
(*
Test and demo program for Libnodave, a free communication libray for Siemens S7.
**********************************************************************
* WARNING: This and other test programs overwrite data in your PLC. *
* DO NOT use it on PLC's when anything is connected to their outputs.*
* This is alpha software. Use entirely on your own risk. *
**********************************************************************
(C) Thomas Hergenhahn ([email protected]) 2002, 2003.
This is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This 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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Libnodave; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*)
uses nodave,tests
{$ifdef WIN32}
, windows
{$endif}
{$ifdef LINUX}
{$ifndef OLDFPC} (* I'm not shure when they changed that *)
,oldlinux
{$else}
,linux
{$endif}
{$define UNIX_STYLE}
{$endif}
;
{$ifdef CYGWIN}
{$define UNIX_STYLE}
{$endif}
procedure usage;
begin
writeln('Usage: testiso_tcp [-d] [-w] <IP address of CP>.');
writeln('-w will try to write to Flag words. It will overwrite FB0 to FB15 (MB0 to MB15) !');
writeln('-d will produce a lot of debug messages.');
writeln('-b will run benchmarks. Specify -b and -w to run write benchmarks.');
writeln('-m will run a test for multiple variable reads.');
writeln('-c will write 0 to the PLC memory used in write tests.');
writeln('-2 uses the protocol for CP243. You need to specify this, if you have such a CP.');
writeln('-s will put the CPU in STOP mode.');
writeln('-r will put the CPU in RUN mode.');
writeln('Example: testiso_tcp -w <IP address of CP>');
end;
procedure wait;
begin
writeln('Press return to continue.');
readln;
end;
//extern
var seconds,thirds:longint;
var i, a,b,c,adrPos, doWrite, doBenchmark, doMultiple, doClear, doNewfunctions,
doStop, doRun, res, saveDebug, proto: longint;
d: single;
di:pdaveInterface;
dc:pdaveConnection;
rs:daveResultSet;
fds: _daveOSserialType;
{$ifdef UNIX_STYLE}
t1,t2:timeval;
{$endif}
{$ifdef WIN32}
t1, t2:longint;
{$endif}
usec:double;
p:PDU;
pms:string;
COMname: array[0..20] of char;
begin
USEC:=0;
adrPos:=1;
doWrite:=0;
doBenchmark:=0;
doMultiple:=0;
doClear:=0;
doNewfunctions:=0;
doRun:=0;
doStop:=0;
proto:=daveProtoISOTCP;
if (paramcount<1) then begin
usage;
halt(1);
end;
writeln('1st parameter '+paramstr(1));
while (paramstr(adrPos)[1]='-') do begin
if paramstr(adrPos)='-d' then begin
(* daveDebug:=daveDebugAll; *)
daveSetDebug(daveDebugAll);
writeln('turning debug on');
end
else if paramstr(adrPos)='-w' then begin
doWrite:=1;
end
else if paramstr(adrPos)='-2' then begin
proto:=daveProtoISOTCP243;
end
else if paramstr(adrPos)='-b' then begin
doBenchmark:=1;
end
else if paramstr(adrPos)='-m' then begin
doMultiple:=1;
end
else if paramstr(adrPos)='-c' then begin
doClear:=1;
end
else if paramstr(adrPos)='-n' then begin
doNewfunctions:=1;
end else if paramstr(adrPos)='-r' then begin
doRun:=1;
end
else if paramstr(adrPos)='-s' then begin
doStop:=1;
end;;
adrPos:=adrPos+1;
if (paramcount<adrPos) then begin
usage;
halt(1);
end;
end;
writeln('fds',sizeof(fds));
pms:=paramstr(adrPos);
move(pms[1], COMname,length(paramstr(adrPos)));
COMname[length(paramstr(adrPos))+1]:=#0;
fds.rfd:=openSocket(102,COMname);
fds.wfd:=fds.rfd;
writeln('fds.rfd ',longint(fds.rfd));
writeln('fds.wfd ',longint(fds.wfd));
if (fds.rfd>0) then begin
di :=daveNewInterface(fds, 'IF1',0,daveProtoISOTCP, daveSpeed187k);
di^.timeout:=5000000;
(*
While initAdapter() is not necessary, it does no harm to call it. When the Interface is set
to work with ISO_TCP, it results in a call to a dummy function that returns allways 0.
This leads to a uniform structure.
*)
if (daveInitAdapter(di)=0) then begin
dc :=daveNewConnection(di,2,0,2); // insert your PPI address here
if (daveConnectPLC(dc)=0) then begin
(*
// just try out what else might be readable in an S7-200 (on your own risk!):
*)
writeln('Trying to read 64 bytes (32 words) from data block 1. This is V memory of the 200.');
wait;
res:=daveReadBytes(dc,daveDB,1,0,64,nil);
if (res=0) then begin
a:=daveGetU16(dc);
writeln('VW0: ',a);
a:=daveGetU16(dc);
writeln('VW2: ',a);
end;
(* a:=daveGetU16at(dc,62);
writeln('DB1:DW32: %d',a);
*)
writeln('Trying to read 16 bytes from FW0.');
wait;
(*
* Some comments about daveReadBytes():
*
* The 200 family PLCs have the V area. This is accessed like a datablock with number 1.
* This is not a quirk or convention introduced by libnodave, but the command transmitted
* to the PLC is exactly the same that would read from DB1 of a 300 or 400.
*
* to read VD68 and VD72 use:
* daveReadBytes(dc, daveDB, 1, 68, 6, nil);
* to read VD68 and VD72 into your applications buffer appBuffer use:
* daveReadBytes(dc, daveDB, 1, 68, 6, appBuffer);
* to read VD68 and VD78 into your applications buffer appBuffer use:
* daveReadBytes(dc, daveDB, 1, 68, 14, appBuffer);
* this reads DBD68 and DBD78 and everything in between and fills the range
* appBuffer+4 to appBuffer+9 with unwanted bytes, but is much faster than:
* daveReadBytes(dc, daveDB, 1, 68, 4, appBuffer);
* daveReadBytes(dc, daveDB, 1, 78, 4, appBuffer+4);
*)
res:=daveReadBytes(dc,daveFlags,0,0,16,nil);
if (res=0) then begin
(*
* daveGetU32(dc); reads a word (2 bytes) from the current buffer position and increments
* an internal pointer by 2, so next daveGetXXX() wil read from the new position behind that
* word.
*)
a:=daveGetU32(dc);
b:=daveGetU32(dc);
c:=daveGetU32(dc);
d:=daveGetFloat(dc);
writeln('FD0: ',a);
writeln('FD4: ',b);
writeln('FD8: ',c);
writeln('FD12:',d);
(*
d:=daveGetFloatAt(dc,12);
writeln('FD12:',d);
*)
end;
if(doNewfunctions<>0) then begin
(*
// saveDebug:=daveDebug;
// daveDebug:=daveDebugAll;
*)
res:=daveReadBits(dc, daveInputs, 0, 2, 1,nil);
writeln('function result:%d:=%s', res, daveStrerror(res));
// daveDebug:=0;
res:=daveReadBits(dc, daveDB, 1, 1, 2,nil);
writeln('function result:%d:=%s', res, daveStrerror(res));
res:=daveReadBits(dc, daveDB, 1, 1, 0,nil);
writeln('function result:%d:=%s', res, daveStrerror(res));
res:=daveReadBits(dc, daveDB, 1, 1, 1,nil);
writeln('function result:%d:=%s', res, daveStrerror(res));
a:=0;
res:=daveWriteBytes(dc, daveOutputs, 0, 0, 1, @a);
// daveDebug:=daveDebugAll;
a:=1;
res:=daveWriteBits(dc, daveOutputs, 0, 5, 1, @a);
writeln('function result:', res,' = ', daveStrerror(res));
res:=daveReadBytes(dc, daveAnaOut, 0, 0, 1,nil);
writeln('function result:%d:=%s', res, daveStrerror(res));
a:=2341;
res:=daveWriteBytes(dc, daveAnaOut, 0, 0, 2,@a);
writeln('function result:%d:=%s', res, daveStrerror(res));
// daveDebug:=saveDebug;
end;
if doRun<>0 then begin
daveStart(dc);
end;
if doStop<>0 then begin
daveStop(dc);
end;
if(doMultiple<>0) then begin
writeln('Now testing read multiple variables.''This will read 1 Byte from inputs,''4 bytes from flags, 2 bytes from DB6'' and other 2 bytes from flags');
wait;
davePrepareReadRequest(dc, @p);
daveAddVarToReadRequest(@p,daveInputs,0,0,1);
daveAddVarToReadRequest(@p,daveFlags,0,0,4);
daveAddVarToReadRequest(@p,daveDB,6,20,2);
daveAddVarToReadRequest(@p,daveSysInfo,0,0,24);
daveAddVarToReadRequest(@p,daveFlags,0,12,2);
daveAddVarToReadRequest(@p,daveAnaIn,0,0,2);
daveAddVarToReadRequest(@p,daveAnaOut,0,0,2);
res:=daveExecReadRequest(dc, @p, @rs);
write('Input Byte 0: ');
res:=daveUseResult(dc, @rs, 0); // first result
if (res=0) then begin
a:=daveGetU8(dc);
writeln(a);
end else
writeln('*** Error: ',daveStrerror(res));
write('Flag DWord 0: ');
res:=daveUseResult(dc, @rs, 1); // 2nd result
if (res=0) then begin
a:=daveGetS16(dc);
writeln(a);
end else
writeln('*** Error: ',daveStrerror(res));
write('DB 6 Word 20 (not present in 200): ');
res:=daveUseResult(dc, @rs, 2); // 3rd result
if (res=0) then begin
a:=daveGetS16(dc);
writeln(a);
end else
writeln('*** Error: ',daveStrerror(res));
write('System Information: ');
res:=daveUseResult(dc, @rs, 3); // 4th result
if (res=0) then begin
for i:=0 to 39 do begin
a:=daveGetU8(dc);
write(char(a));
end;
writeln('');
end else
writeln('*** Error: ',daveStrerror(res));
write('Flag Word 12: ');
res:=daveUseResult(dc, @rs, 4); // 5th result
if (res=0)then begin
a:=daveGetU16(dc);
writeln(a);
end else
writeln('*** Error: ',daveStrerror(res));
write('non existing result (we try to use 1 more than the number of items): ');
res:=daveUseResult(dc, @rs, 4); // 5th result
if (res=0)then begin
a:=daveGetU16(dc);
writeln(a);
end else
writeln('*** Error: %s',daveStrerror(res));
write('Analog Input Word 0:');
res:=daveUseResult(dc, @rs, 5); // 6th result
if (res=0)then begin
a:=daveGetU16(dc);
writeln(a);
end else
writeln('*** Error: ',daveStrerror(res));
write('Analog Output Word 0:');
res:=daveUseResult(dc, @rs, 6); // 7th result
if (res=0)then begin
a:=daveGetU16(dc);
writeln(a);
end else
writeln('*** Error: ',daveStrerror(res));
daveFreeResults(@rs);
(*
for (i:=0; i<rs.numResults;i++) begin
r2:=@(rs.results[i]);
writeln('result: %s length:%d',daveStrerror(r2->error), r2->length);
res:=daveUseResult(dc, @rs, i);
if (r2->length>0) _daveDump('bytes',r2->bytes,r2->length);
if (r2->bytes!:=nil) begin
_daveDump('bytes',r2->bytes,r2->length);
d:=daveGetFloat(dc);
writeln('FD12: %f',d);
end
end
*)
end;
if(doWrite>0) then begin
writeln('Now we write back these data after incrementing the first to by 1,2 and 3 and the first two floats by 1.1.');
wait;
(*
Attention! you need to daveSwapIed little endian variables before using them as a buffer for
daveWriteBytes() or before copying them into a buffer for daveWriteBytes()!
*)
a:=daveSwapIed_32(a+1);
daveWriteBytes(dc,daveFlags,0,0,4,@a);
b:=daveSwapIed_32(b+2);
daveWriteBytes(dc,daveFlags,0,4,4,@b);
c:=daveSwapIed_32(c+3);
daveWriteBytes(dc,daveFlags,0,8,4,@c);
d:=toPLCfloat(d+1.1);
daveWriteBytes(dc,daveFlags,0,12,4,@d);
daveReadBytes(dc,daveFlags,0,0,16,nil);
a:=daveGetU32(dc);
b:=daveGetU32(dc);
c:=daveGetU32(dc);
d:=daveGetFloat(dc);
writeln('FD0: ',a);
writeln('FD4: ',b);
writeln('FD8: ',c);
writeln('FD12:',d);
end; {doWrite}
if(doClear>0) then begin
writeln('Now writing 0 to the bytes FB0...FB15.');
wait();
a:=0;
daveWriteBytes(dc,daveFlags,0,0,4,@a);
daveWriteBytes(dc,daveFlags,0,4,4,@a);
daveWriteBytes(dc,daveFlags,0,8,4,@a);
daveWriteBytes(dc,daveFlags,0,12,4,@a);
daveReadBytes(dc,daveFlags,0,0,16,nil);
a:=daveGetU32(dc);
b:=daveGetU32(dc);
c:=daveGetU32(dc);
d:=daveGetFloat(dc);
writeln('FD0: %d',a);
writeln('FD4: %d',b);
writeln('FD8: %d',c);
writeln('FD12: %f',d);
end; { doClear}
if(doBenchmark>0) then begin
readBench(dc);
if(doWrite>0)then begin
writeBench(dc);
end; {// doWrite}
end; { // doBenchmark}
daveDisconnectPLC(dc);
daveDisconnectAdapter(di);
halt(0);
end;
end;
end else begin
writeln('Couldn''t establish connection to ',paramstr(adrPos));
halt(2);
end;
end.
(*
Changes:
07/19/04 added return values in main().
09/09/04 applied patch for variable Profibus speed from Andrew Rostovtsew.
09/09/04 removed unused include byteswap.h
09/10/04 removed SZL read, it doesn?t work on 200 family.
09/11/04 added multiple variable read example code.
01/01/05 fixed program name in usage.
01/01/13 fixed timeout, rack and slot number so it will work with standard settings.
*)