-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathtestPPIload.c
373 lines (346 loc) · 10.7 KB
/
testPPIload.c
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
/*
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.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "nodave.h"
#include "setport.h"
#include <fcntl.h>
#include "log2.h"
#ifdef LINUX
#include <unistd.h>
#define UNIX_STYLE
#endif
#ifdef BCCWIN
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <time.h>
#define WIN_STYLE
#endif
void usage(void)
{
printf("Usage: testPPIload [-d] [-w] <serial port> <block file name>.\n");
printf("-d will produce a lot of debug messages.\n");
printf("--debug=<number> will set daveDebug to number.\n");
printf("--ppi=<number> will use number as the PPI adddres of the PLC. Default is 2.\n");
printf("--mpi=<number> will use number as the PPI adddres of the PLC.\n");
printf("--local=<number> will set the local PPI adddres to number. Default is 0.\n");
printf("Example: testPPIload /dev/ttyS0 OB1.mc7\n");
}
void wait(void) {
#ifdef UNIX_STYLE
uc c;
printf("Press return to continue.\n");
read(0,&c,1);
#endif
}
int main(int argc, char **argv) {
int i, a, adrPos;
int len, number, blockCont, localPPI, plcPPI;
#ifdef UNIX_STYLE
int fd, res;
#endif
#ifdef WIN_STYLE
HANDLE fd;
unsigned long res;
#endif
int blockNumber, rawLen, netLen;
unsigned int maxPBlockLen;
char blockType;
daveInterface * di;
daveConnection * dc;
_daveOSserialType fds;
PDU p,p2;
uc pup[]= { // Load request
0x1A,
0,1,0,0,0,0,0,9,
0x5F,0x30,0x42,0x30,0x30,0x30,0x30,0x34,0x50, // block type code and number
// _ 0 B 0 0 0 0 4 P
// SDB
0x0D,
0x31,0x30,0x30,0x30,0x32,0x30,0x38,0x30,0x30,0x30,0x31,0x31,0x30, // file length and netto length
// 1 0 0 0 2 0 8 0 0 0 1 1 0
0 //extra byte
};
uc pablock[]= { // parameters for parts of a block
0x1B,0
};
uc progBlock[1260]= {
0, 0, 0, 0xFB, // This seems to be a fix prefix for program blocks
};
uc paInsert[]= { // sended after transmission of a complete block,
// I this makes the CPU link the block into a program
0x28,0x00,0x00,0x00,
0x00,0x00,0x00,0xFD,
0x00,0x0A,0x01,0x00,
0x30,0x42,0x30,0x30,0x30,0x30,0x34,0x50, // block type code and number
0x05,'_','I','N','S','E',
};
adrPos=1;
localPPI=0;
plcPPI=2;
if (argc<3) {
usage();
exit(-1);
}
while (argv[adrPos][0]=='-') {
if (strcmp(argv[adrPos],"-d")==0) {
daveSetDebug(daveDebugAll);
} else
if (strncmp(argv[adrPos],"--local=",8)==0) {
localPPI=atol(argv[adrPos]+8);
printf("setting local PPI address to:%d\n",localPPI);
} else
if (strncmp(argv[adrPos],"--mpi=",6)==0) {
plcPPI=atol(argv[adrPos]+6);
printf("setting PPI address of PLC to:%d\n",plcPPI);
} else
if (strncmp(argv[adrPos],"--ppi=",6)==0) {
plcPPI=atol(argv[adrPos]+6);
printf("setting PPI address of PLC to:%d\n",plcPPI);
}
adrPos++;
if (argc<=adrPos+1) {
usage();
exit(-1);
}
}
fds.rfd=setPort(argv[adrPos],"9600",'E');
fds.wfd=fds.rfd;
if (fds.rfd>0) {
di =daveNewInterface(fds,"IF1", localPPI, daveProtoPPI, daveSpeed187k);
dc =daveNewConnection(di, plcPPI, 0, 0); // insert your PPI address here
daveConnectPLC(dc);
maxPBlockLen=dc->maxPDUlength-22;
// maxPBlockLen=90;
progBlock[1]=maxPBlockLen;
printf("Trying to read 64 bytes (32 words) from data block 1.\n This V memory of the 200.\n");
wait();
res=daveReadBytes(dc,daveDB,1,0,64,NULL);
if (res==0) {
a=daveGetU16(dc);
printf("VW0: %d\n",a);
a=daveGetU16(dc);
printf("VW2: %d\n...\n",a);
}
printf("About to open: %s\n",argv[adrPos+1]);
#ifdef UNIX_STYLE
fd=open(argv[adrPos+1],O_RDONLY);
read(fd, progBlock+4, maxPBlockLen);
close(fd);
#endif
#ifdef WIN_STYLE
// fd=openFile(argv[adrPos+1],O_RDONLY);
fd = CreateFile(argv[adrPos+1],
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_FLAG_WRITE_THROUGH,
0);
printf("fd is: %d. About to read.\n",fd);
ReadFile(fd, progBlock+4, maxPBlockLen, &res, NULL);
printf("Read result: %d\n",res);
CloseHandle(fd);
#endif
#ifdef UNIX_STYLE
fd=open(argv[adrPos+1],O_RDONLY);
read(fd, progBlock+4, maxPBlockLen);
close(fd);
#endif
#ifdef WIN_STYLE
// fd=openFile(argv[adrPos+1],O_RDONLY);
fd = CreateFile(argv[adrPos+1],
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_FLAG_WRITE_THROUGH,
0);
printf("fd is: %d. About to read.\n",fd);
ReadFile(fd, progBlock+4, maxPBlockLen, &res, NULL);
printf("Read result: %d\n",res);
CloseHandle(fd);
#endif
blockNumber=daveGetU16from(progBlock+10);
blockType=progBlock[9];
rawLen=daveGetU16from(progBlock+14);
netLen=daveGetU16from(progBlock+38);
printf("Block number: %d\n",blockNumber);
printf("Block type: %0X\n",blockType);
printf("Size 1: %d\n",rawLen);
printf("Size 2: %d\n",netLen);
printf("1%06d%06d\n",rawLen,netLen);
printf("0%X%05d\n",blockType,blockNumber);
sprintf((char*)pup+10,"0%X%05d",blockType,blockNumber);
sprintf((char*)paInsert+12,"0%X%05d",blockType,blockNumber);
pup[17]='P';
paInsert[19]='P';
sprintf((char*)pup+19,"1%06d%06d",rawLen,netLen);
if (daveGetDebug() & daveDebugPDU) {
_daveDump("pup:",pup,sizeof(pup)-1);
}
p.header=dc->msgOut+dc->PDUstartO;
_daveInitPDUheader(&p, 1);
_daveAddParam(&p, pup, sizeof(pup)-1);
res=_daveExchange(dc, &p);
// res=
_daveSetupReceivedPDU(dc, &p2);
printf("res1: %04X\n",res);
if (res==daveResOK) {
res=_daveSetupReceivedPDU(dc, &p2);
if (daveGetDebug() & daveDebugPDU) {
_daveDumpPDU(&p2);
}
res=daveGetPDUerror(&p2);
printf("load request:%04X\n",res);
_daveSendRequestData(dc, 1);
#ifdef UNIX_STYLE
fd=open(argv[adrPos+1],O_RDONLY);
#endif
#ifdef WIN_STYLE
fd = CreateFile(argv[adrPos+1],
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_FLAG_WRITE_THROUGH,
0);
printf("fd is: %d\n",fd);
#endif
// res=_daveGetResponsePPI(dc);
// res=_daveSetupReceivedPDU(dc, &p2);
blockCont=1;
do {
res=_daveGetResponsePPI(dc);
if (res==0) {
res=_daveSetupReceivedPDU(dc, &p2);
if (daveGetDebug() & daveDebugPDU) {
_daveDumpPDU(&p2);
}
number=((PDUHeader*)p2.header)->number;
if (p2.param[0]==0x1B) {
if (daveGetDebug() & daveDebugPDU) {
printf("Beginning block transmission\n");
}
#ifdef UNIX_STYLE
res=read(fd, progBlock+4, maxPBlockLen);
#endif
#ifdef WIN_STYLE
ReadFile(fd, progBlock+4, maxPBlockLen, &res, NULL);
printf("Read result: %d\n",res);
#endif
p.header=dc->msgOut+dc->PDUstartO;
_daveInitPDUheader(&p, 3);
if ((unsigned int)res==maxPBlockLen) pablock[1]=1; //more blocks
else {
pablock[1]=0; //last block
blockCont=0;
}
progBlock[1]=res;
_daveAddParam(&p, pablock, sizeof(pablock));
_daveAddData(&p, progBlock, res+4 /*sizeof(progBlock)*/);
if (daveGetDebug() & daveDebugPDU) {
_daveDumpPDU(&p);
}
((PDUHeader*)p.header)->number=number;
// _daveSendRequestData(dc, 1);
// _daveExchange(dc,&p);
dc->msgOut[0]=dc->MPIAdr; //address ?
dc->msgOut[1]=dc->iface->localMPI;
// dc->msgOut[2]=108;
dc->msgOut[2]=0x5C;
len=3+p.hlen+p.plen+p.dlen; // The 3 fix bytes + all parts of PDU
_daveSendLength(dc->iface, len);
_daveSendIt(dc->iface, dc->msgOut, len);
printf("Block sended\n");
res=0;
// i = _daveReadChars(dc->iface, dc->msgIn+res, 2000000, daveMaxRawLen);
i = _daveReadChars2(dc->iface, dc->msgIn+res, daveMaxRawLen);
if ((daveGetDebug() & daveDebugByte)!=0) {
LOG3("i:%d res:%d\n",i,res);
}
_daveSendRequestData(dc, 1);
printf("Your turn sended\n");
}
}
} while (blockCont);
#ifdef UNIX_STYLE
close(fd);
#endif
#ifdef WIN_STYLE
CloseHandle(fd);
#endif
res=_daveGetResponsePPI(dc);
if (res==0) {
res=_daveSetupReceivedPDU(dc, &p2);
if (daveGetDebug() & daveDebugPDU) {
_daveDumpPDU(&p2);
}
number=((PDUHeader*)p2.header)->number;
if (p2.param[0]==0x1C) {
printf("Got end of block transmission\n");
p.header=dc->msgOut+dc->PDUstartO;
printf("1\n");
_daveInitPDUheader(&p, 3);
printf("2\n");
_daveAddParam(&p, p2.param,1);
printf("3\n");
if (daveGetDebug() & daveDebugPDU) {
_daveDumpPDU(&p);
}
printf("4\n");
((PDUHeader*)p.header)->number=number;
printf("5\n");
dc->msgOut[0]=dc->MPIAdr; //address ?
printf("6\n");
dc->msgOut[1]=dc->iface->localMPI;
dc->msgOut[2]=0x5C;
len=3+p.hlen+p.plen+p.dlen; // The 3 fix bytes + all parts of PDU
_daveSendLength(dc->iface, len);
_daveSendIt(dc->iface, dc->msgOut, len);
printf("ACK for end sended\n");
res=0;
// i = _daveReadChars(dc->iface, dc->msgIn+res, 2000000, daveMaxRawLen);
i = _daveReadChars2(dc->iface, dc->msgIn+res, daveMaxRawLen);
if ((daveGetDebug() & daveDebugByte)!=0) {
LOG3("i:%d res:%d\n",i,res);
}
p.header=dc->msgOut+dc->PDUstartO;
_daveInitPDUheader(&p, 1);
_daveAddParam(&p, paInsert, sizeof(paInsert));
res=_daveExchange(dc, &p);
res=_daveSetupReceivedPDU(dc, &p2);
res=daveGetPDUerror(&p2);
printf("block insert:%04X\n",res);
}
}
}
} else
printf("Couldn't open serial port %s\n",argv[adrPos]);
return 0;
}
/*
Changes:
03/20/05 Max. block length depends on max. PDU length.
04/09/05 removed CYGWIN defines. As there were no more differences against LINUX, it should
work with LINUX defines.
*/