-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNXConversion.cs
161 lines (135 loc) · 5.66 KB
/
KNXConversion.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace uKNX_Configurator
{
public static class KNXConversion
{
public static int Hex2Dec(string hex, int maxDigits)
{
string hexNumber = "0123456789ABCDEF";
int address = 0;
hex = hex.ToUpper();
if (hex.Length > maxDigits)
return 0;
for (int byteCount = 0; byteCount < hex.Length; byteCount++)
{
if (hexNumber.IndexOf(hex.Substring(byteCount, byteCount + 1)) == -1)
return 0;
else
address = address * 16 + hexNumber.IndexOf(hex.Substring(byteCount, byteCount + 1));
}
return address;
}
public static string Dec2Hex(int dec, int hexDigits)
{
string hexNumber = "0123456789ABCDEF";
string hex = "";
if (dec > Math.Pow(16, hexDigits))
return "";
while (hexDigits > 0)
{
hex = hexNumber.Substring(dec % 16, dec % 16 + 1) + hex;
dec = dec / 16;
hexDigits = hexDigits - 1;
}
return hex;
}
public static UInt16 GroupETS2Addr(string etsString)
{
int address = 0;
if (etsString.IndexOf("/") == -1)
return 0;
address = Convert.ToInt32(etsString.Substring(0, etsString.IndexOf("/"))) * 2048;
etsString = etsString.Substring(etsString.IndexOf("/") + 1);
if (etsString.IndexOf("/") == -1)
{
address = address + Convert.ToInt32(etsString);
}
else
{
address = address + Convert.ToInt32(etsString.Substring(0, etsString.IndexOf("/"))) * 256;
address = address + Convert.ToInt32(etsString.Substring(etsString.IndexOf("/") + 1));
}
return (UInt16)address;
}
public static string GroupAddr2Ets(UInt16 address, int addrLevel)
{
string ets;
if (addrLevel == 2)
ets = Math.Floor((double)address / 2048).ToString() + "/" + Math.Floor((double)address % 2048).ToString();
else
ets = Math.Floor((double)address / 2048).ToString() + "/" + Math.Floor(((double)address % 2048) / 256).ToString() + "/" + Math.Floor((double)address % 256).ToString();
return ets;
}
public static byte Eis62Percent(byte eis6)
{
return (byte)(Math.Round((decimal)eis6 * 1000 / 255) / 10);
}
public static byte Percent2Eis6(Double percent)
{
return (byte)(Math.Round((decimal)percent * 255 / 100));
}
public static byte Eis62Angle(byte eis6)
{
return (byte)(Math.Round((decimal)eis6 * 3600 / 255) / 10);
}
public static byte Angle2Eis6(Double angle)
{
return (byte)(Math.Round((decimal)angle * 255 / 360));
}
public static Double Eis52Value(UInt32 eis5)
{
Double value = eis5 & 0x07ff;
if ((eis5 & 0x08000) != 0)
{
value = (UInt32)value | 0xfffff800;
value = -value;
}
value = (Int32)value << (((Int32)eis5 & 0x07800) >> 11);
if ((eis5 & 0x08000) != 0)
value = -value;
return value/100;
}
public static Int32 Value2Eis5(Double value)
{
value = value * 100;
Int32 eis5 = 0;
byte exponent = 0;
if (value < 0)
{
eis5 = 0x08000;
value = -value;
}
while (value > 0x07ff)
{
value = (Int32)value >> 1;
exponent++;
}
if (eis5 != 0)
value = -value;
eis5 |= (Int32)value & 0x7ff;
eis5 |= (exponent << 11) & 0x07800;
return (Int32)(eis5 & 0x0ffff);
}
public static void Test()
{
Debug.Write("Eis52Value 0xOCFB = " + (KNXConversion.Eis52Value(3323).ToString()) + "\r\n"); // expected to give 25.5
Debug.Write("Value2Eis5 25.5 = " + (KNXConversion.Value2Eis5(25.5).ToString()) + "\r\n"); // expected to give 3323 = 0xOCFB
Debug.Write("Eis62Percent 0xCC = " + (KNXConversion.Eis62Percent(204).ToString()) + "\r\n");
Debug.Write("Eis62Percent 0xFF = " + (KNXConversion.Eis62Percent(255).ToString()) + "\r\n");
Debug.Write("Eis62Percent 0x00 = " + (KNXConversion.Eis62Percent(0).ToString()) + "\r\n");
Debug.Write("Percent2Eis6 0.4% = " + (KNXConversion.Percent2Eis6(0.4).ToString()) + "\r\n");
Debug.Write("Percent2Eis6 100% = " + (KNXConversion.Percent2Eis6(100).ToString()) + "\r\n");
Debug.Write("Percent2Eis6 0% = " + (KNXConversion.Percent2Eis6(0).ToString()) + "\r\n");
Debug.Write("Eis62Angle 0x01 = " + (KNXConversion.Eis62Angle(1).ToString()) + "\r\n");
Debug.Write("Eis62Angle 0xFF = " + (KNXConversion.Eis62Angle(255).ToString()) + "\r\n");
Debug.Write("Eis62Angle 0x00 = " + (KNXConversion.Eis62Angle(0).ToString()) + "\r\n");
Debug.Write("Angle2Eis6 1.4 degree = " + (KNXConversion.Angle2Eis6(1.4).ToString()) + "\r\n");
Debug.Write("Angle2Eis6 360 degree = " + (KNXConversion.Angle2Eis6(360).ToString()) + "\r\n");
Debug.Write("Angle2Eis6 0 degree = " + (KNXConversion.Angle2Eis6(0).ToString()) + "\r\n");
}
}
}