connection from bike/simulator to client to server

This commit is contained in:
shinichi
2020-09-23 15:05:09 +02:00
parent d5eadbe529
commit c782301cf2
4 changed files with 18 additions and 3 deletions

View File

@@ -92,12 +92,13 @@ namespace Client
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
} }
private void OnWrite(IAsyncResult ar) private void OnWrite(IAsyncResult ar)
{ {
this.stream.EndWrite(ar); this.stream.EndWrite(ar);
Console.WriteLine("wrote some stuff");
} }
#region interface #region interface

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Hardware; using Hardware;
using Hardware.Simulators;
using ProftaakRH; using ProftaakRH;
namespace Client namespace Client
@@ -24,6 +25,11 @@ namespace Client
BLEHandler bLEHandler = new BLEHandler(client); BLEHandler bLEHandler = new BLEHandler(client);
bLEHandler.Connect(); bLEHandler.Connect();
//BikeSimulator bikeSimulator = new BikeSimulator(client);
//bikeSimulator.StartSimulation();
while (true) while (true)
{ {
} }

View File

@@ -10,7 +10,7 @@ using System.Threading;
namespace Hardware.Simulators namespace Hardware.Simulators
{ {
class BikeSimulator : IHandler public class BikeSimulator : IHandler
{ {
IDataReceiver dataReceiver; IDataReceiver dataReceiver;
private int elapsedTime = 0; private int elapsedTime = 0;
@@ -34,6 +34,7 @@ namespace Hardware.Simulators
{ {
this.dataReceiver = dataReceiver; this.dataReceiver = dataReceiver;
} }
public void StartSimulation() public void StartSimulation()
{ {
//Example BLE Message //Example BLE Message

View File

@@ -48,6 +48,7 @@ namespace Server
{ {
//message hasn't completely arrived yet //message hasn't completely arrived yet
this.bytesReceived += receivedBytes; 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); 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("something has gone completely wrong");
Console.WriteLine($"expected: {expectedMessageLength} bytesReceive: {bytesReceived} receivedBytes: {receivedBytes}"); 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) 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) else if (buffer[4] == 0x01)
{ {
@@ -71,8 +74,12 @@ namespace Server
Console.WriteLine(Encoding.ASCII.GetString(packet)); Console.WriteLine(Encoding.ASCII.GetString(packet));
HandleData(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) private void HandleData(string packet)