From c782301cf270d306ee49c8f95a710c11d68ce69f Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 15:05:09 +0200 Subject: [PATCH] connection from bike/simulator to client to server --- Client/Client.cs | 3 ++- Client/Program.cs | 6 ++++++ ProftaakRH/BikeSimulator.cs | 3 ++- Server/Client.cs | 9 ++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 26f27ed..86fba5e 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -92,12 +92,13 @@ namespace Client throw new NotImplementedException(); } } + this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); + } private void OnWrite(IAsyncResult ar) { this.stream.EndWrite(ar); - Console.WriteLine("wrote some stuff"); } #region interface diff --git a/Client/Program.cs b/Client/Program.cs index 9598930..60cf68e 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Hardware; +using Hardware.Simulators; using ProftaakRH; namespace Client @@ -24,6 +25,11 @@ namespace Client BLEHandler bLEHandler = new BLEHandler(client); bLEHandler.Connect(); + + //BikeSimulator bikeSimulator = new BikeSimulator(client); + + //bikeSimulator.StartSimulation(); + while (true) { } diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 86dddc9..304a00d 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -10,7 +10,7 @@ using System.Threading; namespace Hardware.Simulators { - class BikeSimulator : IHandler + public class BikeSimulator : IHandler { IDataReceiver dataReceiver; private int elapsedTime = 0; @@ -34,6 +34,7 @@ namespace Hardware.Simulators { this.dataReceiver = dataReceiver; } + public void StartSimulation() { //Example BLE Message diff --git a/Server/Client.cs b/Server/Client.cs index fc29226..d1d5599 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -48,6 +48,7 @@ namespace Server { //message hasn't completely arrived yet this.bytesReceived += receivedBytes; + Console.WriteLine("segmented message, {0} arrived", receivedBytes); this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null); } @@ -58,10 +59,12 @@ namespace Server { Console.WriteLine("something has gone completely wrong"); Console.WriteLine($"expected: {expectedMessageLength} bytesReceive: {bytesReceived} receivedBytes: {receivedBytes}"); + Console.WriteLine($"received WEIRD data {BitConverter.ToString(buffer.Take(receivedBytes).ToArray())} string {Encoding.ASCII.GetString(buffer.Take(receivedBytes).ToArray())}"); + } else if (buffer[4] == 0x02) { - Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).ToArray(), 16)}"); + Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).Take(expectedMessageLength).ToArray())}"); } else if (buffer[4] == 0x01) { @@ -71,8 +74,12 @@ namespace Server Console.WriteLine(Encoding.ASCII.GetString(packet)); HandleData(Encoding.ASCII.GetString(packet)); } + this.bytesReceived = 0; } + + this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); + } private void HandleData(string packet)