-
Notifications
You must be signed in to change notification settings - Fork 0
/
MBusAddIn.cs
401 lines (369 loc) · 13.2 KB
/
MBusAddIn.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using Gurux.Device.Editor;
using Gurux.Device;
using Gurux.Communication;
using Gurux.Common;
namespace Gurux.MBus.AddIn
{
[GXCommunicationAttribute(typeof(MBusPacketHandler), typeof(MBusPacketParser))]
public class MBus : GXProtocolAddIn
{
public MBus() : base("MBus", false, true, true)
{
base.WizardAvailable = VisibilityItems.None;
}
public override VisibilityItems ItemVisibility
{
get
{
return VisibilityItems.Categories | VisibilityItems.Tables;
}
}
public override Functionalities GetFunctionalities(object target)
{
if (target is GXCategoryCollection)
{
return Functionalities.None;
}
else if (target is GXTableCollection)
{
return Functionalities.None;
}
return Functionalities.Remove | Functionalities.Edit;
}
public override void ImportFromDevice(Control[] addinPages, GXDevice device, IGXMedia media)
{
media.Open();
GXMBusDevice dev = device as GXMBusDevice;
object data;
//Handshake
ShortFrame sf = new ShortFrame();
sf.AddressField = dev.DeviceAddress;
sf.ControlField = (byte)CFieldFunctions.SendSlaveInit;
sf.CountChecksum();
data = sf.ToByteArray();
ReceiveParameters<byte[]> recParams = new ReceiveParameters<byte[]>()
{
Count = 1,
Peek = false,
WaitTime = device.WaitTime
};
lock (media.Synchronous)
{
media.Send(data, null);
if (!media.Receive(recParams))
{
throw new Exception("Failed to receive reply from the device in given time.");
}
if (recParams.Reply.Length < 1 || recParams.Reply[0] != 0xE5)
{
throw new Exception("Handshake failed.");
}
bool morePacketsAvailable = false;
byte fcb = 1;
List<MBusRegister> items = new List<MBusRegister>();
do
{
sf = new ShortFrame();
sf.AddressField = dev.DeviceAddress;
if (fcb == 1)
{
sf.ControlField = (byte)CFieldFunctions.RequestClass2Data;
}
else
{
sf.ControlField = (byte)CFieldFunctions.RequestClass2Data | (byte)CFieldRequest.FrameCountBit;
}
sf.CountChecksum();
recParams.AllData = true;
recParams.Count = 1;
recParams.Eop = (byte)0x16;
recParams.Reply = null;
int cnt = 0;
//Some meters can't answer right a way.
do
{
media.Send(sf.ToByteArray(), null);
if (++cnt == 6)
{
throw new Exception("Failed to receive reply from the device in given time.");
}
}
while (!media.Receive(recParams));
while (!VerifyReadReplyCheckSum(recParams.Reply))
{
recParams.Count = 0;
recParams.Reply = null;
bool gotData = media.Receive(recParams);
if (!gotData)
{
break;
}
}
morePacketsAvailable = recParams.Reply.Length > 5 && (recParams.Reply[4] & 0x10) != 0;
items.AddRange(ParseReadReply(recParams.Reply));
}
while (morePacketsAvailable);
GXMBusCategory defaultCategory = null;
if ((defaultCategory = (GXMBusCategory)device.Categories.Find("Default")) == null)
{
defaultCategory = new GXMBusCategory();
defaultCategory.Name = "Default";
device.Categories.Add(defaultCategory);
}
for (int pos = 0; pos < items.Count; ++pos)
{
MBusRegister item = items[pos];
GXMBusProperty prop = new GXMBusProperty();
prop.Ordinal = pos;
if (item.IsVariableData)
{
string name = item.MBusType;
int len = name.IndexOf('_');
if (len != -1)
{
name = name.Substring(0, len);
}
name += " " + item.Function.ToString();
if (item.Tariff != 0)
{
name += " Tariff " + item.Tariff.ToString();
}
prop.Name = name;
prop.Unit = item.Unit;
prop.DataLength = item.DataLength;
item.Mask.Reverse();
prop.InfoMask = item.Mask.ToArray();
prop.Type = item.Type;
if (item.MBusType.ToLower().Contains("date") || item.MBusType.ToLower().Contains("timepoint"))
{
prop.ValueType = typeof(DateTime);
}
else
{
prop.ValueType = typeof(string);
}
}
else
{
prop.Name = item.MBusType;
prop.Unit = item.Unit;
prop.ValueType = typeof(string);
prop.DataLength = 4;
}
prop.InfoBytes = item.InformationBytes.Reverse().ToArray();
defaultCategory.Properties.Add(prop);
}
}
}
private bool VerifyReadReplyCheckSum(byte[] p)
{
if (p.Length < 2)
{
return false;
}
if (p.Length < p[1] + 6)
{
return false;
}
int checkSum = 0;
for (int i = 4; i <= p.Length - 3; ++i)
{
checkSum += p[i];
}
checkSum &= 0xFF;
return checkSum == p[p.Length-2];
}
internal static List<MBusRegister> ParseReadReply(byte[] reply)
{
List<MBusRegister> results = new List<MBusRegister>();
LongFrame lf = new LongFrame(reply);
if ((lf.ControlInformationField & 0x73) == 0x72) //Variable data
{
VariableData vd = new VariableData();
vd.Header = new FixedDataHeader(lf.UserData);
byte[] tmpArray = new byte[lf.UserData.Length - 12];
Array.Copy(lf.UserData, 12, tmpArray, 0, lf.UserData.Length - 12);
while (tmpArray.Length > 0)
{
VariableDataBlock vdb = new VariableDataBlock(ref tmpArray);
vd.DataBlocks.Add(vdb);
}
foreach (VariableDataBlock vdb in vd.DataBlocks)
{
MBusRegister reg = new MBusRegister();
reg.IsVariableData = true;
if (vdb.DataInformationField.Data != VariableDataType.SpecialFunctions)
{
reg.Unit = vdb.VariableInformationField.Unit;
}
reg.Type = vdb.DataInformationField.Data;
reg.DataLength = vdb.DataInformationField.GetDataLength();
string unitStr = vdb.VariableInformationField.UnitInfo.ToString();
if (unitStr.Contains(' '))
{
reg.MBusType = unitStr.Substring(0, unitStr.IndexOf(' '));
}
else
{
reg.MBusType = unitStr;
}
reg.Function = vdb.DataInformationField.Function;
reg.StorageNumber = vdb.DataInformationField.StorageNumberLSB;
reg.Mask.Add(0xBF); //DIF storage number may change
List<byte> infoBytes = new List<byte>();
infoBytes.AddRange(vdb.DataInformationField.ToByteArray());
for (int i = 0; i < vdb.DataInformationFieldExtensions.Count; ++i)
{
var dife = vdb.DataInformationFieldExtensions[i];
infoBytes.AddRange(dife.ToByteArray());
reg.StorageNumber += dife.StorageNumber << (i * 4 + 1);
reg.Tariff += dife.Tariff << (i * 2);
reg.DeviceUnit += dife.DeviceUnit << i;
reg.Mask.Add(0xC0); //DIFE tariff and storage number may change
}
infoBytes.AddRange(vdb.VariableInformationField.ToByteArray());
reg.Mask.Add(vdb.VariableInformationField.Mask);
reg.Multiplier = vdb.VariableInformationField.Multiplier;
if (vdb.VariableInformationField.ToByteArray()[0] == 0xFD)
{
if (vdb.VariableInformationFieldExtensions[0].ExtensionBit == 1)
{
VariableValueAlternateInformation0xFD vvai;
byte multiBits, mask;
string unit;
double multi;
vdb.VariableInformationFieldExtensions[1].GetVariableInfo0xFD(out vvai, out mask, out multiBits, out unit, out multi);
reg.Unit = unit;
reg.MultiplierBits = multiBits;
reg.Multiplier = multi;
reg.Mask.Add(mask);
unitStr = vvai.ToString();
if (unitStr.Contains(' '))
{
reg.MBusType = unitStr.Substring(0, unitStr.IndexOf(' '));
}
else
{
reg.MBusType = unitStr;
}
}
}
else if (vdb.VariableInformationField.ToByteArray()[0] == 0xFB)
{
if (vdb.VariableInformationFieldExtensions[0].ExtensionBit == 1)
{
if (vdb.VariableInformationFieldExtensions[0].ExtensionBit == 1)
{
VariableValueAlternateInformation0xFB vvai;
byte multiBits, mask;
string unit;
double multi;
vdb.VariableInformationFieldExtensions[1].GetVariableInfo0xFB(out vvai, out mask, out multiBits, out unit, out multi);
reg.Unit = unit;
reg.MultiplierBits = multiBits;
reg.Multiplier = multi;
reg.Mask.Add(mask);
unitStr = vvai.ToString();
if (unitStr.Contains(' '))
{
reg.MBusType = unitStr.Substring(0, unitStr.IndexOf(' '));
}
else
{
reg.MBusType = unitStr;
}
}
}
}
else if (vdb.VariableInformationField.ExtensionBit == 1)
{
if (vdb.VariableInformationFieldExtensions[0].ExtensionBit == 1)
{
VariableValueInformationExtensionType vviet;
byte mask;
string unitExt;
double multi;
vdb.VariableInformationFieldExtensions[1].GetExtendedVariableInfo(out vviet, out mask, out unitExt, out multi);
reg.Multiplier = multi;
reg.Unit += unitExt;
reg.Mask.Add(mask);
}
}
for (int i = 0; i < vdb.VariableInformationFieldExtensions.Count; ++i)
{
var vife = vdb.VariableInformationFieldExtensions[i];
infoBytes.AddRange(vife.ToByteArray());
}
reg.InformationBytes = infoBytes.ToArray();
reg.Value = vdb.DataPayload;
results.Add(reg);
}
}
else //Fixed data
{
FixedData fd = new FixedData(lf.UserData);
MBusRegister reg1 = new MBusRegister();
reg1.IsVariableData = false;
reg1.MBusType = fd.MediumAndStatus.Counter1Medium.ToString();
reg1.Unit = MathEnumToString(fd.MediumAndStatus.Counter1Unit.ToString());
reg1.InformationBytes = fd.MediumAndStatus.ToByteArray();
MBusRegister reg2 = new MBusRegister();
reg1.IsVariableData = false;
reg2.MBusType = fd.MediumAndStatus.Counter2Medium.ToString();
if (fd.MediumAndStatus.Counter2Unit == FixedDataPhysicalUnit.SameButHistoric)
{
reg2.Unit = reg1.Unit + " (historic)";
}
else
{
reg2.Unit = MathEnumToString(fd.MediumAndStatus.Counter2Unit.ToString());
}
reg2.InformationBytes = fd.MediumAndStatus.ToByteArray();
results.Add(reg1);
results.Add(reg2);
}
return results;
}
internal static string MathEnumToString(string enumToString)
{
enumToString = enumToString.Replace("Times", " * ");
enumToString = enumToString.Replace("Per", "/");
enumToString = enumToString.Replace("m3", "m^3");
enumToString = enumToString.Replace("To", "^");
enumToString = enumToString.Replace("Minus", "-");
enumToString = enumToString.Replace("Degrees", "°");
enumToString = enumToString.Replace("Celcius", "C");
return enumToString;
}
public override void ModifyWizardPages(object source, GXPropertyPageType type, List<Control> pages)
{
if (type == GXPropertyPageType.Import)
{
pages.Insert(1, new ImportSelectionDlg(source as GXMBusDevice));
}
}
public override Type GetDeviceType()
{
return typeof(GXMBusDevice);
}
public override Type[] GetPropertyTypes(object parent)
{
return new Type[] { typeof(GXMBusProperty) };
}
public override Type[] GetCategoryTypes(object parent)
{
return new Type[] { typeof(GXMBusCategory) };
}
public override Type[] GetTableTypes(object parent)
{
return new Type[] { typeof(GXMBusTable) };
}
}
}