Skip to content

Commit

Permalink
renamed DPT classes, fixed 9.001 DPT
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsilva committed Jun 6, 2015
1 parent e3fcfc3 commit f95c45b
Show file tree
Hide file tree
Showing 30 changed files with 1,345 additions and 1,146 deletions.
193 changes: 104 additions & 89 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,89 +1,104 @@
KNX.net ![build status](https://travis-ci.org/lifeemotions/knx.net.svg?branch=master) ![nuget status](http://img.shields.io/nuget/v/KNX.net.svg?style=flat)
=======

KNX.net provides a [KNX](http://en.wikipedia.org/wiki/KNX_%28standard%29) API for C#

This API allows to connect in both modes:
* Tunneling
* Routing

After connecting you will be able to send actions to the bus and receive messages from it.

The following datapoints are available in the API:

| DPT | input type | input range | output type | output range | Description |
| ------- |-------------------------------------------- | --------------- | ----------- | --------------- | ----------------------------------- |
| `3.007` | `int`, `float`, `long`, `double`, `decimal` | `[-7,7]` | `int` | `[-7,7]` | Control blinds (steps) [`0` stops] |
| `3.008` | `int`, `float`, `long`, `double`, `decimal` | `[-7,7]` | `int` | `[-7,7]` | Control dimming (steps) [`0` stops] |
| `5.001` | `int`, `float`, `long`, `double`, `decimal` | `[0,100]` | `decimal` | `[0,100]` | Percentage (%) |
| `5.003` | `int`, `float`, `long`, `double`, `decimal` | `[0,360]` | `decimal` | `[0,360]` | Angle (°) |
| `5.004` | `int`, `float`, `long`, `double`, `decimal` | `[0,255]` | `int` | `[0,255]` | Percentage [0,255] (%) |
| `5.010` | `int`, `float`, `long`, `double`, `decimal` | `[0,255]` | `int` | `[0,255]` | Counter Pulses |
| `6.001` | `int`, `float`, `long`, `double`, `decimal` | `[-128,127]` | `int` | `[-128,127]` | Percentage (%) |
| `6.010` | `int`, `float`, `long`, `double`, `decimal` | `[-128,127]` | `int` | `[-128,127]` | Counter Pulses |
| `9.001` | `int`, `float`, `long`, `double`, `decimal` | `[-273,670760]` | `decimal` | `[-273,670760]` | Temperature in Celsius (°C) |

Also working but no implemented as datapoints (see below for better explanation):
* bit (lights, buttons)
* byte (dimmers, temperature difference, RGB)

Examples
--------

### Connecting using Routing (turn off and on a light)

```csharp
static void Main(string[] args)
{
var connection = new KNXConnectionRouting();
connection.Connect();
connection.KNXEventDelegate += new KNXConnection.KNXEvent(Event);
connection.Action("5/0/2", false);
Thread.Sleep(5000);
connection.Action("5/0/2", true);
Thread.Sleep(5000);
}
static void Event(string address, string state)
{
Console.WriteLine("New Event: device " + address + " has status " + state);
}
```

### Working with datapoints

Sending an action

```csharp
connection.Action("1/1/16", connection.toDPT("9.001", 24.0f));
connection.Action("1/1/17", connection.toDPT("5.001", 50));
```

Converting status from event

```csharp
float temp = (float)connection.fromDPT("9.001", state);
```

### Sending actions without using datapoints

```csharp
connection.Action("1/1/19", true);
connection.Action("1/1/20", false);
connection.Action("1/1/21", 60);
```

### Connecting using Tunneling

The only difference is how the connection object is created

```csharp
connection = new KNXConnectionTunneling(remoteIP, remotePort, localIP, localPort);
```

### Notes

If connecting in routing mode:
* make sure the system firewall allows incoming connections to the routing port ()if not specified when connecting, default is `3671`)

If connecting in tunneling mode:
* make sure the system firewall allows incoming connections to the specified `localPort`
KNX.net ![build status](https://travis-ci.org/lifeemotions/knx.net.svg?branch=master) ![nuget status](http://img.shields.io/nuget/v/KNX.net.svg?style=flat)
=======

KNX.net provides a [KNX](http://en.wikipedia.org/wiki/KNX_%28standard%29) API for C#

This API allows to connect in both modes:
* Tunneling
* Routing

After connecting you will be able to send actions to the bus and receive messages from it.

The following datapoints are available in the API:

| DPT | input type | input range | output type | output range | Description |
| ------- |-------------------------------------------- | --------------- | ----------- | --------------- | ----------------------------------- |
| `3.007` | `int`, `float`, `long`, `double`, `decimal` | `[-7,7]` | `int` | `[-7,7]` | Control blinds (steps) [`0` stops] |
| `3.008` | `int`, `float`, `long`, `double`, `decimal` | `[-7,7]` | `int` | `[-7,7]` | Control dimming (steps) [`0` stops] |
| `5.001` | `int`, `float`, `long`, `double`, `decimal` | `[0,100]` | `decimal` | `[0,100]` | Percentage (%) |
| `5.003` | `int`, `float`, `long`, `double`, `decimal` | `[0,360]` | `decimal` | `[0,360]` | Angle (°) |
| `5.004` | `int`, `float`, `long`, `double`, `decimal` | `[0,255]` | `int` | `[0,255]` | Percentage [0,255] (%) |
| `5.010` | `int`, `float`, `long`, `double`, `decimal` | `[0,255]` | `int` | `[0,255]` | Counter Pulses |
| `6.001` | `int`, `float`, `long`, `double`, `decimal` | `[-128,127]` | `int` | `[-128,127]` | Percentage (%) |
| `6.010` | `int`, `float`, `long`, `double`, `decimal` | `[-128,127]` | `int` | `[-128,127]` | Counter Pulses |
| `9.001` | `int`, `float`, `long`, `double`, `decimal` | `[-273,670760]` | `decimal` | `[-273,670760]` | Temperature in Celsius (°C) |

Also working but no implemented as datapoints (see below for better explanation):
* bit (lights, buttons)
* byte (dimmers, temperature difference, RGB)

Examples
--------

### Connecting using Routing (turn off and on a light)

```csharp
static void Main(string[] args)
{
var connection = new KNXConnectionRouting();
connection.Connect();
connection.KNXEventDelegate += new KNXConnection.KNXEvent(Event);
connection.Action("5/0/2", false);
Thread.Sleep(5000);
connection.Action("5/0/2", true);
Thread.Sleep(5000);
}
static void Event(string address, string state)
{
Console.WriteLine("New Event: device " + address + " has status " + state);
}
```

### Working with datapoints

Sending an action

```csharp
connection.Action("1/1/16", connection.toDPT("9.001", 24.0f));
connection.Action("1/1/17", connection.toDPT("5.001", 50));
```

Converting status from event

```csharp
static void Event(string address, string state)
{
if (address == "1/1/16")
{
int temp = (int)connection.fromDPT("9.001", state);
Console.WriteLine("New Event: device " + address + " has status " + temp);
return;
}
if (address == "1/1/17")
{
int perc = (int)connection.fromDPT("5.001", state);
Console.WriteLine("New Event: device " + address + " has status " + perc);
return;
}
}

```

### Sending actions without using datapoints

```csharp
connection.Action("1/1/19", true);
connection.Action("1/1/20", false);
connection.Action("1/1/21", 60);
```

### Connecting using Tunneling

The only difference is how the connection object is created

```csharp
connection = new KNXConnectionTunneling(remoteIP, remotePort, localIP, localPort);
```

### Notes

If connecting in routing mode:
* make sure the system firewall allows incoming connections to the routing port ()if not specified when connecting, default is `3671`)

If connecting in tunneling mode:
* make sure the system firewall allows incoming connections to the specified `localPort`
129 changes: 129 additions & 0 deletions src/KNXLib/DPT/DataPoint2ByteFloatTemperature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;
using System.Globalization;
using KNXLib.Log;

namespace KNXLib.DPT
{
internal sealed class DataPoint2ByteFloatTemperature : DataPoint
{
public override string[] Ids
{
get
{
return new[] { "9.001" };
}
}

public override object FromDataPoint(string data)
{
var dataConverted = new byte[data.Length];
for (var i = 0; i < data.Length; i++)
dataConverted[i] = (byte) data[i];

return FromDataPoint(dataConverted);
}

public override object FromDataPoint(byte[] data)
{
// DPT bits high byte: MEEEEMMM, low byte: MMMMMMMM
// first M is signed state from two's complement notation

int val = 0;
uint m = (uint) ((data[0] & 0x07) << 8) | (data[1]);
bool signed = ((data[0] & 0x80) >> 7) == 1;

if (signed)
{
// change for two's complement notation and use only mantissa bytes
m = m - 1;
m = ~(m);
m = m & (0 | 0x07FF);
val = (int) (m * -1);
}
else
{
val = (int) m;
}

int power = (data[0] & 0x78) >> 3;

double calc = 0.01d * val;

return (decimal) Math.Round(calc * Math.Pow(2, power), 2);
}

public override byte[] ToDataPoint(string value)
{
return ToDataPoint(float.Parse(value, CultureInfo.InvariantCulture));
}

public override byte[] ToDataPoint(object val)
{
var dataPoint = new byte[] { 0x00, 0x00, 0x00 };

decimal value;
if (val is int)
value = (int) val;
else if (val is float)
value = (decimal) ((float) val);
else if (val is long)
value = (long) val;
else if (val is double)
value = (decimal) ((double) val);
else if (val is decimal)
value = (decimal) val;
else
{
Logger.Error("9.001", "input value received is not a valid type");
return dataPoint;
}

if (value < -273 || value > +670760)
{
Logger.Error("9.001", "input value received is not in a valid range");
return dataPoint;
}

// value will be multiplied by 0.01
decimal v = Math.Round(value * 100m);
// mantissa only holds 11 bits for value, so, check if exponet is required
int e = 0;
while (v < -2048m)
{
v = v / 2;
e++;
}
while (v > 2047m)
{
v = v / 2;
e++;
}

int mantissa;
bool signed;
if (v < 0)
{
// negative value > two's complement
signed = true;
mantissa = ((int) v * -1);
mantissa = ~mantissa;
mantissa = mantissa + 1;
}
else
{
signed = false;
mantissa = (int) v;
}

// signed value > enable first bit
if (signed)
dataPoint[1] = 0x80;

dataPoint[1] = ((byte) (dataPoint[1] | ((e & 0x0F) << 3)));
dataPoint[1] = ((byte) (dataPoint[1] | ((mantissa >> 8) & 0x07)));
dataPoint[2] = ((byte) mantissa);

return dataPoint;
}
}
}
Loading

0 comments on commit f95c45b

Please sign in to comment.