Skip to content

Commit

Permalink
working on kberry driver
Browse files Browse the repository at this point in the history
  • Loading branch information
p3root committed Mar 6, 2019
1 parent ca4154b commit 0d24f60
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 9 deletions.
21 changes: 18 additions & 3 deletions src/automatica.drivers/automatica.driver.knx/P3.Driver.Knx.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28124.53
# Visual Studio Version 16
VisualStudioVersion = 16.0.28701.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P3.Driver.Knx.DriverFactory", "P3.Driver.Knx.DriverFactory\P3.Driver.Knx.DriverFactory.csproj", "{4AC5C115-5709-475D-97CB-2ECFC1F3636D}"
EndProject
Expand All @@ -13,7 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P3.Driver.Knx.Tests", "Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P3.Knx.Core.Console", "Tests\P3.Knx.Core.Console\P3.Knx.Core.Console.csproj", "{459CEF01-B996-439F-96C7-45FBEB3ADE6B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P3.Knx.Core.Baos.Driver", "P3.Knx.Core.Baos.Driver\P3.Knx.Core.Baos.Driver.csproj", "{6161EB8C-4732-49C0-B629-A90635F733A5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P3.Knx.Core.Baos.Driver", "P3.Knx.Core.Baos.Driver\P3.Knx.Core.Baos.Driver.csproj", "{6161EB8C-4732-49C0-B629-A90635F733A5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{BB13B1D0-2882-43BB-9575-EE137C3A48F8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P3.Knx.Core.Baos.Console", "P3.Knx.Core.Baos.Console\P3.Knx.Core.Baos.Console.csproj", "{0DB5429A-F485-44E7-BC34-A6501014499C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -58,10 +62,21 @@ Global
{6161EB8C-4732-49C0-B629-A90635F733A5}.DebugLocal|Any CPU.Build.0 = Debug|Any CPU
{6161EB8C-4732-49C0-B629-A90635F733A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6161EB8C-4732-49C0-B629-A90635F733A5}.Release|Any CPU.Build.0 = Release|Any CPU
{0DB5429A-F485-44E7-BC34-A6501014499C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0DB5429A-F485-44E7-BC34-A6501014499C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DB5429A-F485-44E7-BC34-A6501014499C}.DebugLocal|Any CPU.ActiveCfg = Debug|Any CPU
{0DB5429A-F485-44E7-BC34-A6501014499C}.DebugLocal|Any CPU.Build.0 = Debug|Any CPU
{0DB5429A-F485-44E7-BC34-A6501014499C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DB5429A-F485-44E7-BC34-A6501014499C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F1763E39-4D0D-4B08-84CE-7AD7B242DA57} = {BB13B1D0-2882-43BB-9575-EE137C3A48F8}
{459CEF01-B996-439F-96C7-45FBEB3ADE6B} = {BB13B1D0-2882-43BB-9575-EE137C3A48F8}
{0DB5429A-F485-44E7-BC34-A6501014499C} = {BB13B1D0-2882-43BB-9575-EE137C3A48F8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8AC487B5-995F-43E4-B26E-3DBD97DFAA45}
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\P3.Knx.Core.Baos.Driver\P3.Knx.Core.Baos.Driver.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Extensions.Logging.Console;
using P3.Knx.Core.Baos.Driver;
using System.Threading.Tasks;

namespace P3.Knx.Core.Baos.Console
{
class Program
{
static async Task Main(string[] args)
{
System.Console.WriteLine("Hello World!");

var logger = new ConsoleLoggerProvider((s, level) => true, true).CreateLogger("test");

var driver = new BaosDriver("/dev/ttyAMA0", logger);

await driver.Start();

System.Console.ReadLine();

await driver.Stop();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace P3.Knx.Core.Baos.Driver
{
public class BaosDriver
{
private readonly string _port;
private readonly BaosSerial _serial;

public BaosDriver(string port, ILogger logger)
{
_port = port;
_serial = new BaosSerial(_port, logger);
}


public Task<bool> Start()
{
return Task.FromResult(_serial.Open());
}
public Task<bool> Stop()
{
return Task.FromResult(_serial.Close());
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,26 @@ public virtual ReadOnlyMemory<byte> ToByteFrame()
if (FrameType == BaosFrameType.ShortFrame)
{
byte[] data = new byte[4];
data[0] = BaosFrame.ShortFrameStart;
data[0] = ShortFrameStart;
data[1] = ControlField;
data[2] = CalculateChecksum();
data[3] = BaosFrame.FrameEndByte;
data[3] = FrameEndByte;

return data;
}
else if (FrameType == BaosFrameType.LongFrame)
{
byte[] data = new byte[7 + UserData.Length];

data[0] = BaosFrame.ControlFrameLongFrameStart;
data[0] = ControlFrameLongFrameStart;
data[1] = Convert.ToByte(3 + UserData.Length);
data[2] = Convert.ToByte(3 + UserData.Length);
data[3] = BaosFrame.ControlFrameLongFrameStart;
data[3] = ControlFrameLongFrameStart;
data[4] = ControlField;
Array.Copy(UserData.Span.ToArray(), 0, data, 5, UserData.Length);

data[7 + UserData.Length] = CalculateChecksum();
data[8 + UserData.Length] = BaosFrame.FrameEndByte;
data[8 + UserData.Length] = FrameEndByte;
return data;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class BaosSerial
private readonly string _port;
private SerialPortStream _stream;
private bool _connected;

private readonly SemaphoreSlim _waitSemaphore = new SemaphoreSlim(1);

public ILogger Logger { get; }

Expand All @@ -35,6 +37,7 @@ public bool Open()
_stream = new SerialPortStream(_port, 19200, 8, Parity.None,
StopBits.One);

_stream.DataReceived += _stream_DataReceived;
_stream.Open();
_connected = true;

Expand All @@ -48,9 +51,20 @@ public bool Open()
return true;
}

private async void _stream_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
var frame = await ReadFrameInternal();

if(frame != null)
{
Logger.LogTrace($"New frame received....");
Logger.LogHexIn(frame.ToByteFrame());
}
}

private async Task<BaosFrame> ReadFrameInternal()
{
await _waitSemaphore.WaitAsync();
try
{
int firstChar = _stream.ReadByte();
Expand Down Expand Up @@ -139,6 +153,10 @@ private async Task<BaosFrame> ReadFrameInternal()
{
Logger.LogError($"Could not read frame {e}", e);
}
finally
{
_waitSemaphore.Release();
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override Task<bool> Start()

if (!mbus)
{
return Task.Factory.StartNew(() => false);
return Task.FromResult(false);
}

_timer.Start();
Expand Down

0 comments on commit 0d24f60

Please sign in to comment.