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 package net.sf.openv4j.protocolhandlers;
26
27 import java.util.Calendar;
28 import java.util.Date;
29
30 import net.sf.openv4j.CycleTimeEntry;
31 import net.sf.openv4j.CycleTimes;
32 import net.sf.openv4j.ErrorListEntry;
33 import net.sf.openv4j.Holiday;
34
35
36
37
38
39
40 public abstract class MemoryImage {
41
42
43
44
45
46
47
48 public abstract int getRawByte(int address);
49
50
51
52
53
54
55
56 public abstract void setRawByte(int addr, byte theData);
57
58
59
60
61
62
63
64 public void addTime(int addr, Date d) {
65 Calendar c = Calendar.getInstance();
66 c.setTime(d);
67 c.set(Calendar.HOUR_OF_DAY, decodeBCD(getRawByte(addr)));
68 c.set(Calendar.MINUTE, decodeBCD(getRawByte(addr + 1)));
69 c.set(Calendar.SECOND, decodeBCD(getRawByte(addr + 2)));
70 d.setTime(c.getTimeInMillis());
71 }
72
73
74
75
76
77
78
79
80 public Boolean getBool(int addr) {
81 final int value = getRawByte(addr);
82
83 return (value == 0x00ff) ? null : (value != 0);
84 }
85
86
87
88
89
90
91
92
93 public Byte getByte(int addr) {
94 int result = getRawByte(addr);
95
96 if (result == 0xff) {
97 return null;
98 }
99
100 return (byte) result;
101 }
102
103
104
105
106
107
108
109
110
111 public CycleTimes getCycleTimes(int addr, int numberOfCycleTimes) {
112 CycleTimes result = new CycleTimes(numberOfCycleTimes / 2);
113
114 for (int i = 0; i < (numberOfCycleTimes / 2); i++) {
115 int dataByte = getRawByte(addr + (i * 2));
116
117 if (dataByte == 0xff) {
118 result.setEntry(i, null);
119 } else {
120 result.setEntry(i, new CycleTimeEntry());
121 result.getEntry(i).setStart((dataByte & 0xF8) >> 3, (dataByte & 7) * 10);
122 dataByte = getRawByte(addr + (i * 2) + 1);
123 result.getEntry(i).setEnd((dataByte & 0xF8) >> 3, (dataByte & 7) * 10);
124 }
125 }
126
127 return result;
128 }
129
130
131
132
133
134
135
136
137 public ErrorListEntry getErrorListEntry(int addr) {
138 return new ErrorListEntry(getRawByte(addr), getTimeStamp_8(addr + 1));
139 }
140
141
142
143
144
145
146
147
148 public Holiday getHoliday(int addr) {
149 Holiday result = new Holiday();
150 result.setStart(getTimeStamp_Date(addr));
151 result.setStartFlag((byte) getRawByte(addr + 4));
152 addTime(addr + 5, result.getStart());
153 result.setEnd(getTimeStamp_Date(addr + 8));
154 result.setEndFlag((byte) getRawByte(addr + 12));
155 addTime(addr + 13, result.getEnd());
156
157 return result;
158 }
159
160
161
162
163
164
165
166
167
168
169 public Integer getInteger(int addr) {
170 int result = getRawByte(addr);
171 result |= (getRawByte(addr + 1) << 8);
172 result |= (getRawByte(addr + 2) << 16);
173 result |= (getRawByte(addr + 3) << 24);
174
175 if (result == 0xffffffff) {
176 return null;
177 }
178
179 return result;
180 }
181
182
183
184
185
186
187
188
189
190
191 public Short getShort(int addr) {
192 int result = getRawByte(addr);
193 result |= (getRawByte(addr + 1) << 8);
194
195 if (result == 0xffff) {
196 return null;
197 }
198
199 return (short) result;
200 }
201
202
203
204
205
206
207
208
209
210
211 public Short getShortHex(int addr) {
212 int result = getRawByte(addr) << 8;
213 result |= getRawByte(addr + 1);
214
215 return (result == 0xffff) ? null : (short) result;
216 }
217
218
219
220
221
222
223
224
225 public Date getTimeStamp_8(int addr) {
226 Calendar c = Calendar.getInstance();
227 int year = (decodeBCD(getRawByte(addr)) * 100) + decodeBCD(getRawByte(addr + 1));
228 int month = decodeBCD(getRawByte(addr + 2) - 1);
229 int day = decodeBCD(getRawByte(addr + 3));
230
231
232 int h = decodeBCD(getRawByte(addr + 5));
233 int min = decodeBCD(getRawByte(addr + 6));
234 int s = decodeBCD(getRawByte(addr + 7));
235 c.set(year, month, day, h, min, s);
236
237 return c.getTime();
238 }
239
240
241
242
243
244
245
246
247 public Date getTimeStamp_Date(int addr) {
248 Calendar c = Calendar.getInstance();
249 int year = (decodeBCD(getRawByte(addr)) * 100) + decodeBCD(getRawByte(addr + 1));
250 int month = decodeBCD(getRawByte(addr + 2) - 1);
251 int day = decodeBCD(getRawByte(addr + 3));
252
253 c.set(year, month, day);
254
255 return c.getTime();
256 }
257
258
259
260
261
262
263
264
265 public Short getUByte(int addr) {
266 int result = getRawByte(addr);
267
268 if (result == 0xff) {
269 return null;
270 }
271
272 return (short) result;
273 }
274
275
276
277
278
279
280
281
282 public Integer getUShort(int addr) {
283 int result = getRawByte(addr);
284 result |= (getRawByte(addr + 1) << 8);
285
286 if (result == 0xffff) {
287 return null;
288 }
289
290 return (int) result;
291 }
292
293
294
295
296
297
298
299 public void setBool(int addr, boolean value) {
300 setRawByte(addr, (byte) (value ? 1 : 0));
301 }
302
303
304
305
306
307
308
309 public void setByte(int addr, byte value) {
310 setRawByte(addr, value);
311 }
312
313
314
315
316
317
318
319 public void setBytes(int addr, byte[] theData) {
320 for (int i = 0; i < theData.length; i++) {
321 setRawByte(addr + i, theData[i]);
322 }
323 }
324
325
326
327
328
329
330
331 public void setInteger(int addr, int value) {
332 setRawByte(addr, (byte) value);
333 setRawByte(addr + 1, (byte) ((value >> 8) & 0x00FF));
334 setRawByte(addr + 2, (byte) ((value >> 16) & 0x00FF));
335 setRawByte(addr + 3, (byte) ((value >> 24) & 0x00FF));
336 }
337
338
339
340
341
342
343
344 public void setShort(int addr, short value) {
345 setRawByte(addr, (byte) value);
346 setRawByte(addr + 1, (byte) ((value >> 8) & 0x00FF));
347 }
348
349
350
351
352
353
354
355 public void setShortHex(int addr, short value) {
356 setRawByte(addr, (byte) ((value >> 8) & 0x00FF));
357 setRawByte(addr + 1, (byte) (value & 0x00FF));
358 }
359
360 private int decodeBCD(int bcdByte) {
361 return (bcdByte & 0x0F) + (((bcdByte & 0x00F0) >> 4) * 10);
362 }
363 }