-
Notifications
You must be signed in to change notification settings - Fork 7
/
Server_Protocol_Mercury.pas
645 lines (527 loc) · 21.9 KB
/
Server_Protocol_Mercury.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
unit Server_Protocol_Mercury;
{
Ìîäóëü îïðîñà ñ÷åò÷èêà ìåðêóðèé 230-R ñ òðàíñôîìàòîðàìè òîêà è íàïðÿæåíèÿ
Ïàðîëü: 111111 (ñòðîêà 299)
- êîýô. òðàíñôîðìàöèè íàïðÿæåíèÿ
- êîýô. òðàíñôîðìàöèè òîêà
- íàïðÿæåíèå, Â *0,01
- òîê, À *0,001
- ìîùíîñòü, Âò *0,01
- àêòèâíà ýíåðãèÿ êÂò*÷ *0,001
- ðåàêòèâíàÿ ýíåðãèÿ êÂÀð*÷ *0,001
}
//------------------------------------------------------------------------------
interface
//------------------------------------------------------------------------------
uses
Server_Devices, cport, sysutils, Windows, SyncObjs, Classes, variants, Contnrs,
ComCtrls, Server_UDP, XMLIntf, msxmldom;
type
TMomentValue = class
handle: TObject; // óêàçàòåëü
cmd: array [0..5] of Byte; // ïîäãîòîâëåííûé ïàêåò êîìàíäû
factor: Integer;
end;
type
TMercury = class(TRealDev)
private
password: array [0..5] of byte; // "111111" - äëÿ ïåðâîãî óðîâíÿ äîñòóïà;
queryOpen: array [0..10] of byte; // Çàðîñû - îòêðûòü ñåññèþ
queryEnergy: array [0..5] of byte; // àêòèâíàÿ ðåàêòèâíàÿ ýíåðãèÿ
queryClose: array [0..3] of byte; // çàêðûòü ñåññèþ
momentValue: array of TMomentValue; // çàïðîñû íàïðÿæåíèÿ è òîêà
factorVoltage : Integer; // êîýô. òðàíñôîðìàöèè íàïðÿÿæåíèÿ
factorCurrent : Integer; // êîýô. òðàíñôîðìàöèè òîêà
procedure queryBild(); // ïîñòðîèòåëü çàïðîñîâ íà îòêðûòèå è çàâåðøåíèè ñåññèè, ïîëó÷åíèÿ ýíåðãèè
function Byte2143ToDword (inp:PByteArray; offset: integer = 0):longword; // Ïîëó÷àåì çíà÷åíèå ïåðåñòàíîâêîé
function Byte21ToDword (inp:PByteArray; offset: integer = 0):word;
procedure SetEnergyValue(inp:PByteArray); // óñòàíàâëèâàåì çíà÷åíèå àòèâíîé è ðåàêòèâíîé ýíåðãèè
procedure SetEnergyBadValue(); // óñòàíàâëèâàåì ôëàã îøèáêè ÷òåíèÿ ýíåðãèé
public
constructor Create(xmlfile: IXMLNode); overload;
destructor Destroy; override;
procedure draw(inlist: tlistview); override;
procedure Read(port: TComPort); override;
procedure Write(port: TComPort); override;
end;
//------------------------------------------------------------------------------
implementation
//------------------------------------------------------------------------------
uses
UHelper;
const
srCRCHi: array[0..255] of byte = (
$00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40,
$01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41,
$01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41,
$00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40,
$01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41,
$00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40,
$00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40,
$01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41,
$01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41,
$00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40,
$00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40,
$01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41,
$00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40,
$01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41,
$01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41,
$00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40);
srCRCLo: array[0..255] of byte = (
$00, $C0, $C1, $01, $C3, $03, $02, $C2, $C6, $06, $07, $C7, $05, $C5, $C4, $04,
$CC, $0C, $0D, $CD, $0F, $CF, $CE, $0E, $0A, $CA, $CB, $0B, $C9, $09, $08, $C8,
$D8, $18, $19, $D9, $1B, $DB, $DA, $1A, $1E, $DE, $DF, $1F, $DD, $1D, $1C, $DC,
$14, $D4, $D5, $15, $D7, $17, $16, $D6, $D2, $12, $13, $D3, $11, $D1, $D0, $10,
$F0, $30, $31, $F1, $33, $F3, $F2, $32, $36, $F6, $F7, $37, $F5, $35, $34, $F4,
$3C, $FC, $FD, $3D, $FF, $3F, $3E, $FE, $FA, $3A, $3B, $FB, $39, $F9, $F8, $38,
$28, $E8, $E9, $29, $EB, $2B, $2A, $EA, $EE, $2E, $2F, $EF, $2D, $ED, $EC, $2C,
$E4, $24, $25, $E5, $27, $E7, $E6, $26, $22, $E2, $E3, $23, $E1, $21, $20, $E0,
$A0, $60, $61, $A1, $63, $A3, $A2, $62, $66, $A6, $A7, $67, $A5, $65, $64, $A4,
$6C, $AC, $AD, $6D, $AF, $6F, $6E, $AE, $AA, $6A, $6B, $AB, $69, $A9, $A8, $68,
$78, $B8, $B9, $79, $BB, $7B, $7A, $BA, $BE, $7E, $7F, $BF, $7D, $BD, $BC, $7C,
$B4, $74, $75, $B5, $77, $B7, $B6, $76, $72, $B2, $B3, $73, $B1, $71, $70, $B0,
$50, $90, $91, $51, $93, $53, $52, $92, $96, $56, $57, $97, $55, $95, $94, $54,
$9C, $5C, $5D, $9D, $5F, $9F, $9E, $5E, $5A, $9A, $9B, $5B, $99, $59, $58, $98,
$88, $48, $49, $89, $4B, $8B, $8A, $4A, $4E, $8E, $8F, $4F, $8D, $4D, $4C, $8C,
$44, $84, $85, $45, $87, $47, $46, $86, $82, $42, $43, $83, $41, $81, $80, $40);
const
InitCRC: word = $FFFF;
function UpdCRC(C: byte; oldCRC: word): word;
var
i: byte;
arrCRC: array[0..1] of byte absolute oldCRC;
begin
i := arrCRC[1] xor C;
arrCRC[1] := arrCRC[0] xor srCRCHi[i];
arrCRC[0] := srCRCLo[i];
UpdCRC := oldCRC;
end;
procedure CalcCRCMercury (Length:word; inp:PByteArray);
var
i:integer;
crc:word;
begin
Crc:=UpdCRC(inp[0],InitCrc);
For i:=1 to Length-3 do Crc:=UpdCRC(inp[i],Crc);
inp[Length-2]:=crc div 256;
inp[Length-1]:=crc mod 256;
end;
{ TMercury }
constructor TMercury.Create(xmlfile: IXMLNode);
var
confNode: IXMLNode;
tmpitm: Tservitem;
handle: integer; // ñåòåâîé àäðåñ
netaddress: word; // ñåòåâîé àäðåñ
i: Integer;
nameTag: string; // èìÿ óçëà
listObjectPos: Integer; // ïîçèöèÿ äîáàâëåíîãî ýëåìåíòà â ñïèñêå
momentValueCount: Integer; // êîëè÷åñòâî momentValue
sendCmd: array [0..5] of byte; // êîìàíäà äëÿ momentValue
begin
FillChar(password, 6*SizeOf(Byte), 1); // äëÿ ïåðâîãî óðîâíÿ äîñòóïà ïàðîëü: 111111
handle := 0;
listObjectPos := 0;
momentValueCount := 0;
try
self.adress := xmlfile.Attributes['address'];
Self.devtype := 'mercury';
inherited Create( xmlfile.Attributes['name'], strtoint(xmlfile.Attributes['portnum']));
itemlist := TObjectList.Create;
factorVoltage := 1;
factorCurrent := 1;
// --- node factor
confNode := xmlfile.ChildNodes.FindNode('factor');
if confNode <> nil then
try
factorVoltage := StrToInt(confNode.Attributes['voltage']);
factorCurrent := StrToInt(confNode.Attributes['current']);
log.debug('TMercury.Create. Ñonfigured factor success.');
except
on E: Exception do
log.error('TMercury.Create. Ñonfigured factor. ' + E.ClassName + ': ' + E.Message);
else
log.error('Unknown error. TMercury.Create. Ñonfigured factor.' );
end;
// --- node energy
confNode := xmlfile.ChildNodes.FindNode('energy');
if confNode <> nil then
try
// active
handle := AddItem('active', 4, StrToInt(confNode.Attributes['active_idopc']), false );
listObjectPos := itemlist.Add( TDevItem.Create(handle, 'active', 0, 0, False ) );
tmpitm := serv.items[handle];
// reactive
handle := AddItem('reactive', 4, StrToInt(confNode.Attributes['reactive_idopc']), false );
listObjectPos := itemlist.Add( TDevItem.Create(handle, 'reactive', 0, 0, False ) );
tmpitm := serv.items[handle];
// ñîçäàíèå çàïðîñîâ íà îòêðûòèå, çàêðûòèå, ïîëó÷åíèå àòèíâîé è ðåàêòèâíîé ýíåðãèé
queryBild();
log.debug('TMercury.Create. Ñonfigured energy success.');
except
on E: Exception do
log.error('TMercury.Create. Ñonfigured energy. '+ E.ClassName + ': ' + E.Message);
else
log.error('Unknown error. TMercury.Create. Ñonfigured energy.' );
end;
// --- node voltage
confNode := xmlfile.ChildNodes.FindNode('voltage');
if confNode <> nil then
try
for i := 1 to 3 do
begin
nameTag := 'u'+IntToStr(i);
handle := AddItem(nameTag, 4, StrToInt(confNode.Attributes[nameTag+'_idopc']), False);
listObjectPos := itemlist.Add( TDevItem.Create(handle, nameTag, 0, 0, False ) );
tmpitm := serv.items[handle];
sendCmd[0]:= StrToInt(Self.adress);
sendCmd[1]:= $8;
sendCmd[2]:= $11; // ìíîãîâåííûõ çíà÷åíèé
case i of
1: sendCmd[3]:= $11; // Íàïðÿæåíèå ôàçà 1
2: sendCmd[3]:= $12; // Íàïðÿæåíèå ôàçà 2
3: sendCmd[3]:= $13; // Íàïðÿæåíèå ôàçà 3
end;
momentValueCount:= momentValueCount + 1;
SetLength( momentValue, momentValueCount);
momentValue[momentValueCount-1] := TMomentValue.Create;
momentValue[momentValueCount-1].handle := itemlist.Items[listObjectPos] ;
momentValue[momentValueCount-1].factor := factorVoltage;
CalcCRCMercury(6, @sendCmd);
Move(sendCmd[0],momentValue[momentValueCount-1].cmd[0],6 );
end;
log.debug('TMercury.Create. Ñonfigured voltage success.');
except
on E: Exception do
log.error('TMercury.Create. Ñonfigured voltage. '+ E.ClassName + ': ' + E.Message);
else
log.error('Unknown error. TMercury.Create. Ñonfigured voltage.' );
end;
// --- node current
confNode := xmlfile.ChildNodes.FindNode('current');
if confNode <> nil then
try
for i := 1 to 3 do
begin
nameTag := 'i'+IntToStr(i);
handle := AddItem(nameTag, 4, StrToInt(confNode.Attributes[nameTag+'_idopc']), False );
listObjectPos := itemlist.Add( TDevItem.Create(handle, nameTag, 0, 0, False ) );
tmpitm := serv.items[handle];
sendCmd[0]:= StrToInt(Self.adress);
sendCmd[1]:= $8;
sendCmd[2]:= $11; // ìíîãîâåííûõ çíà÷åíèé
case i of
1: sendCmd[3]:= $21; // Òîê ôàçà 1
2: sendCmd[3]:= $22; // Òîê ôàçà 2
3: sendCmd[3]:= $23; // Òîê ôàçà 3
end;
momentValueCount:= momentValueCount + 1;
SetLength( momentValue, momentValueCount);
momentValue[momentValueCount-1] := TMomentValue.Create;
momentValue[momentValueCount-1].handle := itemlist.Items[listObjectPos] ;
momentValue[momentValueCount-1].factor := factorCurrent;
CalcCRCMercury(6, @sendCmd);
Move(sendCmd[0],momentValue[momentValueCount-1].cmd[0],6 );
end;
log.debug('TMercury.Create. Ñonfigured current success.');
except
on E: Exception do
log.error('TMercury.Create. Ñonfigured current. '+ E.ClassName + ': ' + E.Message);
else
log.error('Unknown error. TMercury.Create. Ñonfigured current.' );
end;
// --- node power
confNode := xmlfile.ChildNodes.FindNode('power');
if confNode <> nil then
try
for i := 1 to 3 do
begin
nameTag := 'p'+IntToStr(i);
handle := AddItem(nameTag, 4, StrToInt(confNode.Attributes[nameTag+'_idopc']), False );
listObjectPos := itemlist.Add( TDevItem.Create(handle, nameTag, 0, Null, False ) );
tmpitm := serv.items[handle];
sendCmd[0]:= StrToInt(Self.adress);
sendCmd[1]:= $8;
sendCmd[2]:= $11; // ìíîãîâåííûõ çíà÷åíèé
case i of
1: sendCmd[3]:= $1; // Ìîùíîñòü ôàçà 1
2: sendCmd[3]:= $2; // Ìîùíîñòü ôàçà 2
3: sendCmd[3]:= $3; // Ìîùíîñòü ôàçà 3
end;
momentValueCount:= momentValueCount + 1;
SetLength( momentValue, momentValueCount);
momentValue[momentValueCount-1] := TMomentValue.Create;
momentValue[momentValueCount-1].handle := itemlist.Items[listObjectPos] ;
momentValue[momentValueCount-1].factor := factorCurrent * factorVoltage;
CalcCRCMercury(6, @sendCmd);
Move(sendCmd[0],momentValue[momentValueCount-1].cmd[0],6 );
end;
log.debug('TMercury.Create. Ñonfigured power success.');
except
on E: Exception do
log.error('TMercury.Create. Ñonfigured power. '+ E.ClassName + ': ' + E.Message);
else
log.error('Unknown error. TMercury.Create. Ñonfigured power.' );
end;
except
on E: Exception do
log.error('TMercury.Create. Ñonfigured. '+ E.ClassName + ': ' + E.Message);
else
log.error('Unknown error. TMercury.Create. Ñonfigured.' );
end;
end;
destructor TMercury.Destroy;
var
i: Integer;
begin
for i:= Low(momentValue) to High(momentValue) do
momentValue[i].Free;
SetLength( momentValue, 0);
inherited Destroy;
end;
procedure TMercury.queryBild();
var
i: Integer;
sendCmd: array [0..10] of byte;
begin
// Çàïðîñ íà îòêðûòèå êàíàëà ñâÿçè
sendCmd[0]:= StrToInt(Self.adress); // Àäðåñ
sendCmd[1]:= 1; // Êîä çàïðîñà = 1h
sendCmd[2]:= 1; // Óðîâåíü äîñòóïà - ïîëüçîâàòåëü
for i:=0 to 5 do sendCmd[i+3]:= password[i]; // ïàðîëü
CalcCRCMercury(11, @sendCmd); // CRC
Move(sendCmd[0],queryOpen[0],11 );
// çàïðîñ íà ÷òåíèå ìàññèâîâ ðåãèñòðîâ íàêîïëåííîé ýíåðãèè
sendCmd[1]:= 5;
sendCmd[2]:= 0;
sendCmd[3]:= 0;
CalcCRCMercury(6, @sendCmd);
Move(sendCmd[0],queryEnergy[0],6 );
// close session
sendCmd[1]:= 2;
CalcCRCMercury(4, @sendCmd);
Move(sendCmd[0],queryClose[0],4 );
end;
procedure TMercury.draw(inlist: tlistview);
var
i: integer;
drawindex: tlistitem;
begin
inlist.clear;
for i := 0 to itemlist.Count - 1 do
begin
drawindex := inlist.Items.Add();
drawindex.ImageIndex:= 0;
drawindex.Caption := TDevItem(itemlist.Items[i]).name;
case TDevItem(itemlist.Items[i]).name[1] of
'a': drawindex.Caption := 'Àêòèâíàÿ';
'r': drawindex.Caption := 'Ðåàêòèâíàÿ';
'u': drawindex.Caption := 'Íàðÿæåíèå Ô'+TDevItem(itemlist.Items[i]).name[2];
'i': drawindex.Caption := 'Òîê Ô'+TDevItem(itemlist.Items[i]).name[2];
'p': drawindex.Caption := 'Ìîùíîñòü Ô'+TDevItem(itemlist.Items[i]).name[2];
else
drawindex.Caption := TDevItem(itemlist.Items[i]).name;
end;
if (TDevItem(itemlist.Items[i]).Value <> null) and (not VarIsEmpty(TDevItem(itemlist.Items[i]).Value)) then
begin
case TDevItem(itemlist.Items[i]).name[1] of
'a': drawindex.SubItems.Add(FloatToStrF(TDevItem(itemlist.Items[i]).Value/1000, ffFixed, 10, 1)+ ' êÂò*÷' );
'r': drawindex.SubItems.Add(FloatToStrF(TDevItem(itemlist.Items[i]).Value/1000, ffFixed, 10, 1)+ ' êÂÀð*÷' );
'u': drawindex.SubItems.Add(FloatToStrF(TDevItem(itemlist.Items[i]).Value/100, ffFixed, 4, 2)+ ' Â' );
'i': drawindex.SubItems.Add(FloatToStrF(TDevItem(itemlist.Items[i]).Value/1000, ffFixed, 4, 2)+ ' À' );
'p': drawindex.SubItems.Add(FloatToStrF(TDevItem(itemlist.Items[i]).Value/100, ffFixed, 10, 1)+ ' Âò' );
else
drawindex.SubItems.Add(intToStr(TDevItem(itemlist.Items[i]).Value) );
end;
drawindex.SubItems.Add(QualityToStr(serv.items[TDevItem(itemlist.Items[i]).handle].Quality));
drawindex.SubItems.Add(inttostr(timer));
end
else
begin
drawindex.SubItems.Add('NULL');
drawindex.SubItems.Add(QualityToStr(serv.items[TDevItem(itemlist.Items[i]).handle].Quality));
drawindex.SubItems.Add(inttostr(timer));
end;
end;
end;
procedure TMercury.Write(port: TComPort);
begin
// empty
end;
procedure TMercury.Read(port: TComPort);
var
responseToCmd: array [0..20] of byte;
event: TEvent;
count: integer;
Events: TComEvents;
i: Integer;
begin
PurgeComm(port.Handle, PURGE_RXABORT or PURGE_RXCLEAR);
Event := TEvent.Create(nil, // ïàðàìåòðû çàùèòû, áåðóòñÿ îò ðîäèòåëÿ
True, // ðåæèì óïðàâëåíèÿ
False, // íà÷àëüíîå ñîñòîÿíèå, íåñèãíàëüíîå ñîñòîÿíèå - çàíÿòî
''); // èìÿ îáúåêòà
Events := [evRxChar];
// Çàïðîñ íà îòêðûòèå êàíàëà ñâÿçè
port.Write(queryOpen,11);
port.WaitForEvent(Events, Event.Handle, 350);
count := port.InputCount;
// ïûòàåìñÿ ïîâòîðíî îòêðûòü êàíàë ñâÿçè
if count <> 4 then
begin
log.debug('Mercury. Repeated send auth paket for open channel. Send raw:'+ByteToHexStr(queryOpen));
PurgeComm(port.Handle, PURGE_RXABORT or PURGE_RXCLEAR);
Event := TEvent.Create(nil, True, False, '');
Events := [evRxChar];
port.Write(queryOpen,11);
port.WaitForEvent(Events, Event.Handle, 350);
count := port.InputCount;
end;
if count = 4 then
begin
port.Read(responseToCmd[0], count);
// ïðîâåðÿåì àäðåñ è êîä îòâåòà, êîíòðîëüíóþ ñóììó èãíîðèðóåì
if (responseToCmd[0] = queryOpen[0]) and (responseToCmd[1] = 0) then
begin
// ãàðàíòèðîâàííûé òàéì-àóò
sleep(25);
// çàïðîñ íà ÷òåíèå ìàññèâîâ ðåãèñòðîâ íàêîïëåííîé ýíåðãèè
{ Îïðîñ
êîìàíäà: 1áàéò - àäðåñ
1áàéò = 05 êîìàíäà
2áàéòà = 00 00 ïåðèîä
2áàéòà - êîíòðîëüíàÿ ñóììà
îòâåò: 1áàéò - àäðåñ
4áàéòà - À+
4áàéòà - À-, åñëè íå ïîäåðæèâàåòñÿ òî FF FF FF FF
4áàéòà - R+
4áàéòà - R-, åñëè íå ïîäåðæèâàåòñÿ òî FF FF FF FF
2áàéòà - êîíòðîëüíàÿ ñóììà
}
PurgeComm(port.Handle, PURGE_RXABORT or PURGE_RXCLEAR);
Event := TEvent.Create(nil, True, False, '');
Events := [evRxChar];
port.Write(queryEnergy,6);
port.WaitForEvent(Events, Event.Handle, 150);
port.WaitForEvent(Events, Event.Handle, 150); // TODO: Ëîæíîå ñðàáàòûâàíèå, äëèíà ïàêåòà 14. FFFFFFFF?
sleep(25); // TODO: Ëîæíîå ñðàáàòûâàíèå, äëèíà ïàêåòà 14
count := port.InputCount;
port.Read(responseToCmd[0], count);
if count > 13 then // TODO: Ëîæíîå ñðàáàòûâàíèå, äëèíà ïàêåòà 14
begin
if (responseToCmd[0] = queryEnergy[0]) then
begin
SetEnergyValue(@responseToCmd);
// ïîëó÷àåì ìãíîâåííûå çíà÷àíèÿ
for i:= Low(momentValue) to High(momentValue) do
begin
PurgeComm(port.Handle, PURGE_RXABORT or PURGE_RXCLEAR);
Event := TEvent.Create(nil, True, False, '');
Events := [evRxChar];
port.Write(momentValue[i].cmd,6);
port.WaitForEvent(Events, Event.Handle, 150);
count := port.InputCount;
port.Read(responseToCmd[0], count);
{ îòâåò
1 áàéò - àäðåñ
1 áàéò = 0
2 áàéòà = çíà÷åíèå
2 áàéòà - êîíòðîëüíàÿ ñóììà
}
if (count = 6 ) and (momentValue[i].cmd[0] = responseToCmd[0]) then
begin
TDevItem(momentValue[i].handle).value := Byte21ToDword(@responseToCmd,2) * momentValue[i].factor;
serv.items[TDevItem(momentValue[i].handle).handle].Quality := OPC_QUALITY_GOOD;
//log.debug('Mercury. Response moment data. Send: '+ByteToHexStr(momentValue[i].cmd,6) + 'Response raw:' +ByteToHexStr(responseToCmd,count) );
end
else
begin
TDevItem(momentValue[i].handle).value := Null;
serv.items[TDevItem(momentValue[i].handle).handle].Quality := OPC_QUALITY_UNCERTAIN;
log.error('Mercury. Error response moment data. Send: '+ByteToHexStr(momentValue[i].cmd,6) + 'Response raw:' +ByteToHexStr(responseToCmd,count) );
end;
serv.Items[TDevItem(momentValue[i].handle).handle].value := TDevItem(momentValue[i].handle).value;
end;
end
else
begin
log.error('Mercury. Error response energy data. Response raw:' +ByteToHexStr(responseToCmd,count) );
SetEnergyBadValue();
end;
end
else
begin
// error response energy
log.error('Mercury. Error response energy data. Send raw:' +ByteToHexStr(queryEnergy)+'. Response raw:'+ByteToHexStr(responseToCmd,count) );
// close session
port.Write(queryClose,4);
SetEnergyBadValue();
end;
end
else
begin
// error response
log.error('Mercury. Error response. Response raw:' +ByteToHexStr(responseToCmd,4));
// close session
port.Write(queryClose,4);
SetEnergyBadValue();
end;
end
else
begin
// error timeout
log.error('Mercury. Error timeout. Send raw:'+ByteToHexStr(queryOpen) );
SetEnergyBadValue();
end;
Event.Free;
Sleep(50);
end;
procedure TMercury.SetEnergyValue(inp:PByteArray);
var
i: Integer;
begin
for i:=0 to itemlist.count-1 do
begin
if TDevItem(itemlist.Items[i]).name = 'active' then
begin
TDevItem(itemlist.Items[i]).value := Byte2143ToDword(inp,1) * factorVoltage * factorCurrent;
serv.Items[TDevItem(itemlist.Items[i]).handle].value := TDevItem(itemlist.Items[i]).value;
serv.items[TDevItem(itemlist.Items[i]).handle].Quality := OPC_QUALITY_GOOD;
Continue;
end;
if TDevItem(itemlist.Items[i]).name = 'reactive' then
begin
TDevItem(itemlist.Items[i]).value := Byte2143ToDword(inp,9) * factorVoltage * factorCurrent;
serv.Items[TDevItem(itemlist.Items[i]).handle].value := TDevItem(itemlist.Items[i]).value;
serv.items[TDevItem(itemlist.Items[i]).handle].Quality := OPC_QUALITY_GOOD;
Continue;
end;
end;
end;
procedure TMercury.SetEnergyBadValue();
var
i: Integer;
begin
for i:=0 to itemlist.count-1 do
begin
TDevItem(itemlist.Items[i]).value := null;
serv.Items[TDevItem(itemlist.Items[i]).handle].value := TDevItem(itemlist.Items[i]).value;
serv.items[TDevItem(itemlist.Items[i]).handle].Quality := OPC_QUALITY_UNCERTAIN;
end;
end;
function TMercury.Byte2143ToDword (inp:PByteArray; offset: integer = 0):longword;
var
value:longword;
begin
Result := (inp[offset+3] shl 8)
or inp[offset+2]
or (inp[offset+1] shl 24)
or (inp[offset] shl 16) ;
end;
function TMercury.Byte21ToDword (inp:PByteArray; offset: integer = 0):word;
var
value:longword;
begin
Result := (inp[offset+1] shl 8) or inp[offset];
end;
end.