Sie sind auf Seite 1von 6

1 /* Curve store in files, source file: CURVE.

C */
2 /* Last modified 27.06.2000 by Kriwet (secstamp = counter * interval) */
3
4 #include <stdio.h>
5 #include <errno.h>
6 #include <copro.h> /* Needed for copro functions */
7 #include <strings.h>
8
9 /* Init read and write PLC data */
10
11 #define P_LIST1A "N41:0,256,word,read" /* PLC BASE A N41: Word 0-255*/
12 #define P_LIST2A "N42:0,256,word,read" /* PLC BASE A N42: Word 0-255 */
13
14 #define P_LIST1B "N61:0,256,word,read" /* PLC BASE B N61: Word 0-255*/
15 #define P_LIST2B "N62:0,256,word,read" /* PLC BASE B N62: Word 0-255 */
16
17 #define COUNT_1A "N41:120,7,word,modify" /* PLC BASE A N41: Word 120-126*/
18 #define COUNT_1B "N61:120,7,word,modify" /* PLC BASE B N61: Word 120-126*/
19
20 unsigned p1a, p2a; /* handle ID's for base A */
21 unsigned p1b, p2b; /* handle ID's for base B */
22 unsigned c1a, c1b; /* handle ID's for counter A+B */
23
24 short bc1a[7], bp1a[256], bp2a[256]; /* buffer baseA */
25 short bc1b[7], bp1b[256], bp2b[256]; /* buffer baseB */
26 int cpa=0; /* curve pointer for curve A data */
27 int cpb=0; /* curve pointer for curve B data */
28
29 long secstampA=0; /* Timestamp, seconds startA */
30 long secstampB=0; /* Timestamp, seconds startB */
31 long jsecy;
32
33 long intervala=0, recorda=0; /* curve A */
34 long intervalb=0, recordb=0; /* curve B */
35 char fnamea[18] [9]; /* for Name of Dat Files Base A*/
36 char fnameb[18] [9]; /* for Name of Dat Files Base B*/
37 FILE *wf; /* file pointer */
38
39
40 print_error(err) /* funct: print DTL error's */
41 unsigned err;
42 {
43 char *errptr;
44
45 errptr = CC_ERROR(err);
46 printf ("\n curve: DTL ERROR = %4d - %s\n", err, errptr);
47 }
48
49 cfile(wn) /* funct: createfile (filename) */
50 char wn[9];
51 {
52 short p, ret=0;
53
54 if ((wf=fopen(wn,"w")) ==NULL) /* create new file */
55 { printf("curve:can't open file for create %s \n",wn); ret=-1; }
56
57 for (p=0; p<=1000; p++) /* write 'p' times '0' to file (init)*/
58 { if (putw(0,wf) ==EOF)
59 { printf ("curve:write error while creating file %s \n",wn); ret=-1; }
60 }
61 if (fclose(wf) ==EOF)
62 { printf("curve:can't close file %s \n",wn); ret=-1; }
63
64 return(ret);
65 }
66
67 init_cc() /* funct: init CC, DTL Libary */
68 {
69 unsigned ret_val; /* Return value variable */
70 unsigned iostat; /* Status variable DTL functions */
71 int j;
72
73 CC_INIT(); /* initialize the copro */
74
75 ret_val = DTL_INIT(7); /* Setup dtl definition table */
76 if (ret_val != DTL_SUCCESS)
77 { print_error(ret_val); exit(-1); } /* print error ... and exit */
78
79 /*
80 ret_val = DTL_CLOCK();
81 if (ret_val != DTL_SUCCESS)
82 { print_error(ret_val); exit(-1); }
83 */
84
85 ret_val=DTL_C_DEFINE(&p1a, P_LIST1A); /* Define DTL */
86 if (ret_val != DTL_SUCCESS)
87 { print_error(ret_val); exit(-1); } /* print error ... and exit */
88
89 ret_val=DTL_C_DEFINE(&p2a, P_LIST2A); /* Define DTL */
90 if (ret_val != DTL_SUCCESS)
91 { print_error(ret_val); exit(-1); } /* print error ... and exit */
92
93 ret_val=DTL_C_DEFINE(&p1b, P_LIST1B); /* Define DTL */
94 if (ret_val != DTL_SUCCESS)
95 { print_error(ret_val); exit(-1); } /* print error ... and exit */
96
97 ret_val=DTL_C_DEFINE(&p2b, P_LIST2B); /* Define DTL */
98 if (ret_val != DTL_SUCCESS)
99 { print_error(ret_val); exit(-1); } /* print error ... and exit */
100
101 ret_val=DTL_C_DEFINE(&c1a, COUNT_1A); /* Define DTL */
102 if (ret_val != DTL_SUCCESS)
103 { print_error(ret_val); exit(-1); } /* print error ... and exit */
104
105 ret_val=DTL_C_DEFINE(&c1b, COUNT_1B); /* Define DTL */
106 if (ret_val != DTL_SUCCESS)
107 { print_error(ret_val); exit(-1); } /* print error ... and exit */
108
109
110 /* init Filename of stored Value BASE A: */
111 strcpy(fnamea[0], "c00a.dat");
112 strcpy(fnamea[1], "c01a.dat");
113 strcpy(fnamea[2], "c02a.dat");
114 strcpy(fnamea[3], "c03a.dat");
115 strcpy(fnamea[4], "c04a.dat");
116 strcpy(fnamea[5], "c05a.dat");
117 strcpy(fnamea[6], "c06a.dat");
118 strcpy(fnamea[7], "c07a.dat");
119 strcpy(fnamea[8], "c08a.dat");
120 strcpy(fnamea[9], "c09a.dat");
121 strcpy(fnamea[10], "c10a.dat");
122 strcpy(fnamea[11], "c11a.dat");
123 strcpy(fnamea[12], "c12a.dat");
124 strcpy(fnamea[13], "c13a.dat");
125 strcpy(fnamea[14], "c14a.dat");
126 strcpy(fnamea[15], "c15a.dat");
127 strcpy(fnamea[16], "c16a.dat");
128 strcpy(fnamea[17], "c17a.dat");
129
130 for (j=0; j<18; j++) /* create 'j' Dat Files for Base A */
131 { cfile(fnamea[j]);
132 }
133
134 /* init Filename of stored Value BASE B: */
135 strcpy(fnameb[0], "c00b.dat");
136 strcpy(fnameb[1], "c01b.dat");
137 strcpy(fnameb[2], "c02b.dat");
138 strcpy(fnameb[3], "c03b.dat");
139 strcpy(fnameb[4], "c04b.dat");
140 strcpy(fnameb[5], "c05b.dat");
141 strcpy(fnameb[6], "c06b.dat");
142 strcpy(fnameb[7], "c07b.dat");
143 strcpy(fnameb[8], "c08b.dat");
144 strcpy(fnameb[9], "c09b.dat");
145 strcpy(fnameb[10], "c10b.dat");
146 strcpy(fnameb[11], "c11b.dat");
147 strcpy(fnameb[12], "c12b.dat");
148 strcpy(fnameb[13], "c13b.dat");
149 strcpy(fnameb[14], "c14b.dat");
150 strcpy(fnameb[15], "c15b.dat");
151 strcpy(fnameb[16], "c16b.dat");
152 strcpy(fnameb[17], "c17b.dat");
153
154 for (j=0; j<18; j++) /* create 'j' Dat Files for Base B */
155 { cfile(fnameb[j]);
156 }
157
158 jsecy=get_jul_sec(); /* init jsecy for first secstamp */
159 }
160
161
162 wfile(wn,p,wv) /* funct: writefile (filename, seekpos, value) */
163 char wn[9];
164 short p, wv;
165 {
166 short ret=0;
167
168 if ((wf=fopen(wn,"r+")) ==NULL)
169 { printf("can't open file for update %s \n",wn); ret=-1; }
170
171 if (fseek(wf, p, 0) !=NULL)
172 { printf("fileseek error %s \n",wn); ret=-1; }
173
174 if ((putw(wv,wf) == EOF) && (wv != -1))
175 { printf ("write error %s \n",wn); ret=-1; }
176
177 if (fclose(wf) ==EOF)
178 { printf("can't close file %s \n",wn); ret=-1; }
179
180 return(ret);
181 }
182
183 updatea() /* funct: update curvefile A */
184 {
185 unsigned ret_val; /* Return value variable */
186 unsigned iostat; /* Status variable DTL functions */
187 short pos,j; /* filepointer */
188 short value[18]; /* stored values */
189 short high_scc, low_scc; /* 2*16 Bit timestamp for SCC */
190 long help;
191
192 ret_val = DTL_READ_W (c1a, bc1a, &iostat); /*read counter data*/
193 if (ret_val != DTL_SUCCESS) print_error(ret_val);
194
195 if (bc1a[1] < 0) bc1a[1]=0; /* check curvepointerA */
196
197 if ((bc1a[6]<=0)||(bc1a[6]>3600)) bc1a[6]=360; /* check curve interval */
198
199 intervala=bc1a[6]; /* update curve interval BaseA */
200 secstampA=bc1a[1] * intervala; /* update secstamp */
201
202 recorda=secstampA + intervala; /* build time for next record */
203
204 cpa=bc1a[1]; /* get actual curvepointer p1_121 */
205 if (cpa > 999) cpa=cpa % 1000; /* pointer = pointer modulo 1000 */
206 pos=cpa*2; /* actual position for fileseek (bytes) */
207 bc1a[1]++; /* next position curvepointerA (1...) */
208
209 ret_val = DTL_WRITE_W (c1a, bc1a, &iostat); /*write back counter data*/
210 if (ret_val != DTL_SUCCESS) print_error(ret_val);
211
212 /*
213 printf("\n...recorda:(%d) ",recorda);
214 printf("interva:(%d) ",intervala);
215 printf("high:(%d) ",high_scc);
216 printf("low(%d) ",low_scc);
217 printf("p1_121a:(%d) ",bc1a[1]);
218 printf("cpa:(%d) ",cpa);
219 printf("fileseek posa:(%d) \n",pos);
220 */
221
222 ret_val = DTL_READ_W (p1a, bp1a, &iostat); /*read plc data*/
223 if (ret_val != DTL_SUCCESS) print_error(ret_val);
224
225 ret_val = DTL_READ_W (p2a, bp2a, &iostat); /*read plc data*/
226 if (ret_val != DTL_SUCCESS) print_error(ret_val);
227
228 help = secstampA; /* seconds since programstart */
229 help = help >> 16;
230 high_scc = help; /* highword for scc */
231 low_scc = secstampA; /* lowword for scc */
232
233 /* Value to store: */
234 value[0] = high_scc; /* high for SCC */
235 value[1] = low_scc; /* low for SCC */
236 value[2] = bp2a[28];
237 value[3] = bp2a[29];
238 value[4] = bp2a[27];
239 value[5] = bp1a[125];
240 value[6] = bp2a[32];
241 value[7] = bp2a[30];
242 value[8] = bp2a[31];
243 value[9] = bp2a[33];
244 value[10] = bp2a[35];
245 value[11] = bp2a[34];
246 value[12] = bp2a[37];
247 value[13] = bp2a[38];
248 value[14] = bp2a[41];
249 value[15] = bp2a[42];
250 value[16] = bp2a[182];
251 value[17] = bp2a[183];
252
253 for (j=0; j<18; j++) /* write 'j' Dat Files */
254 {
255 if ((wfile(fnamea[j],pos,value[j]))!=0) /* write Value to file */
256 { cfile(fnamea[j]); /* on write error create file again */
257 }
258 }
259 }
260
261 updateb() /* funct: update curvefile B */
262 {
263 unsigned ret_val; /* Return value variable */
264 unsigned iostat; /* Status variable DTL functions */
265 short pos,j; /* filepointer */
266 short value[18]; /* stored values */
267 short high_scc, low_scc; /* 2*16 Bit timestamp for SCC */
268 long help;
269
270 ret_val = DTL_READ_W (c1b, bc1b, &iostat); /*read counter data*/
271 if (ret_val != DTL_SUCCESS) print_error(ret_val);
272
273 if (bc1b[1] < 0) bc1b[1]=0; /* check curvepointerB */
274
275 if ((bc1b[6]<=0)||(bc1b[6]>3600)) bc1b[6]=360; /* check curve interval */
276
277 intervalb=bc1b[6]; /* update curve interval BaseB */
278 secstampB=bc1b[1] * intervalb; /* update secstamp */
279
280 recordb=secstampB + intervalb; /* time for next record */
281
282 cpb=bc1b[1]; /* get actual curvepointer p1_121 */
283 if (cpb > 999) cpb=cpb % 1000; /* pointer = pointer modulo 1000 */
284 pos=cpb*2; /* actual position for fileseek (bytes) */
285 bc1b[1]++; /* next position curvepointerB (1...) */
286
287 ret_val = DTL_WRITE_W (c1b, bc1b, &iostat); /*write back counter data*/
288 if (ret_val != DTL_SUCCESS) print_error(ret_val);
289
290
291 ret_val = DTL_READ_W (p1b, bp1b, &iostat); /*read plc data*/
292 if (ret_val != DTL_SUCCESS) print_error(ret_val);
293
294 ret_val = DTL_READ_W (p2b, bp2b, &iostat); /*read plc data*/
295 if (ret_val != DTL_SUCCESS) print_error(ret_val);
296
297 help = secstampB;
298 help = help >> 16;
299 high_scc = help; /* highword for scc */
300 low_scc = secstampB; /* lowword for scc */
301
302 /* Value to store: */
303 value[0] = high_scc; /* high for SCC */
304 value[1] = low_scc; /* low for SCC */
305 value[2] = bp2b[28];
306 value[3] = bp2b[29];
307 value[4] = bp2b[27];
308 value[5] = bp1b[125];
309 value[6] = bp2b[32];
310 value[7] = bp2b[30];
311 value[8] = bp2b[31];
312 value[9] = bp2b[33];
313 value[10] = bp2b[35];
314 value[11] = bp2b[34];
315 value[12] = bp2b[37];
316 value[13] = bp2b[38];
317 value[14] = bp2b[41];
318 value[15] = bp2b[42];
319 value[16] = bp2b[182];
320 value[17] = bp2b[183];
321
322 for (j=0; j<18; j++) /* write 'j' Dat Files */
323 {
324 if ((wfile(fnameb[j],pos,value[j]))!=0) /* write Value to file */
325 { cfile(fnameb[j]); /* on write error create file again */
326 }
327 }
328 }
329
330 long get_jul_sec() /* funct: julian sec. (seconds after midnight) */
331 {
332 int date, ti, tick;
333 short day;
334 long jsec;
335
336 if (_sysdate(1,&ti,&date,&day,&tick) == -1) return(jsecy);
337 jsec=ti;
338 return(jsec);
339 }
340
341 sec_stamp() /* funct: build secstamp */
342 {
343 long jsecn, delta;
344
345 jsecn=get_jul_sec(); /* get new seconds */
346 delta=jsecn-jsecy; jsecy=jsecn;
347 if (delta<0) delta=delta+86400; /* seconds turn midnight, correct delta*/
348 secstampA=secstampA+delta; /* update secstampA */
349 secstampB=secstampB+delta; /* update secstampB */
350
351 /*
352 printf("_jsecn:(%d) ",jsecn);
353 printf("_jsecy:(%d) ",jsecy);
354 printf("_delta:(%d) ",delta);
355 printf("_secstamp:(%d)\n",secstamp);
356 */
357 }
358
359 main()
360 {
361 init_cc(); /* call funct: init access to PLC */
362
363 while(1) /* this is the main loop after startup */
364 {
365 sec_stamp(); /* call funct: build actual secstamp */
366
367 if (secstampA >= recorda) updatea(); /* call funct: write curve A */
368 if (secstampB >= recordb) updateb(); /* call funct: write curve B */
369
370 }
371

Das könnte Ihnen auch gefallen