Skip to content

Commit

Permalink
Merge pull request #94 from Lipotam/cs-api_wifi
Browse files Browse the repository at this point in the history
C# api: tcp implementation
  • Loading branch information
AJIOB authored Apr 17, 2018
2 parents 208ea38 + 506c27c commit fa8cfeb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 66 deletions.
2 changes: 2 additions & 0 deletions code/cs_api/cs_api/Basic/BasicConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace TrackPlatform.Basic
{
public abstract class BasicConnector : IDisposable
{
protected const int ReadWriteTimeoutInMs = 1500;

private bool _isConnectedToArduino = false;
private readonly Timer _autoConnector;

Expand Down
55 changes: 7 additions & 48 deletions code/cs_api/cs_api/Connectors/SerialConnector.cs
Original file line number Diff line number Diff line change
@@ -1,70 +1,29 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using TrackPlatform.Basic;
using TrackPlatform.Tools;

namespace TrackPlatform.Connectors
{
public class SerialConnector : BasicConnector
public class SerialConnector : StreamingConnector
{
private const int MessageMaxSize = 65535;
private const int TimeoutInMs = 1500;

private readonly SerialPort _readPort;
private readonly SerialPort _writePort;

private readonly List<byte> _buffer = new List<byte>();
private readonly byte[] _readBuffer = new byte[MessageMaxSize];
protected override Stream ReadStream => _readPort.BaseStream;
protected override Stream WriteStream => _writePort.BaseStream;

protected override void Write(byte[] s)
{
_writePort.Write(s, 0, s.Length);
}

protected override byte[] Read()
{
if (_buffer.Count == 0)
{
int readByte = _readPort.ReadByte();
if (readByte < 0)
{
throw new TimeoutException();
}
_buffer.Add((byte) readByte);
}

byte len = _buffer[0];
int substringLen = (int) (sizeof(byte) + len + CrcLength);
if ((substringLen) > _buffer.Count)
{
int bytesNeedToRead = Math.Max(substringLen - sizeof(byte), _readPort.BytesToRead);

//TODO: check for max buffer length

int bytesWereRead = _readPort.Read(_readBuffer, 0, bytesNeedToRead);

if (bytesNeedToRead > bytesWereRead)
{
throw new TimeoutException();
}

_buffer.AddRange(_readBuffer.SubArray(0, bytesWereRead));
}

byte[] answer = _buffer.GetRange(0, substringLen).ToArray();
_buffer.RemoveRange(0, substringLen);

return answer;
}
protected override int ReadAvailable => !IsConnected() ? 0 : _readPort.BytesToRead;

public SerialConnector(string rx, string tx, int baudRate)
{
_readPort = new SerialPort(rx, baudRate);
_writePort = (rx == tx) ? _readPort : new SerialPort(tx, baudRate);

_readPort.ReadTimeout = TimeoutInMs;
_writePort.WriteTimeout = TimeoutInMs;
_readPort.ReadTimeout = ReadWriteTimeoutInMs;
_writePort.WriteTimeout = ReadWriteTimeoutInMs;

//board reloading when connect block
_writePort.DtrEnable = false;
Expand Down
65 changes: 65 additions & 0 deletions code/cs_api/cs_api/Connectors/StreamingConnector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using TrackPlatform.Basic;
using TrackPlatform.Tools;

namespace TrackPlatform.Connectors
{
public abstract class StreamingConnector : BasicConnector
{
private const int MessageMaxSize = 65535;

private readonly List<byte> _buffer = new List<byte>();
private readonly byte[] _readBuffer = new byte[MessageMaxSize];

/// <summary>
/// Need to manually manage from derived class
/// </summary>
protected abstract Stream ReadStream { get; }
protected abstract Stream WriteStream { get; }

protected abstract int ReadAvailable { get; }

protected sealed override void Write(byte[] s)
{
WriteStream.Write(s, 0, s.Length);
}

protected sealed override byte[] Read()
{
if (_buffer.Count == 0)
{
int readByte = ReadStream.ReadByte();
if (readByte < 0)
{
throw new TimeoutException();
}
_buffer.Add((byte) readByte);
}

byte len = _buffer[0];
int substringLen = (int) (sizeof(byte) + len + CrcLength);
if ((substringLen) > _buffer.Count)
{
int bytesNeedToRead = Math.Max(substringLen - sizeof(byte), ReadAvailable);

//TODO: check for max buffer length

int bytesWereRead = ReadStream.Read(_readBuffer, 0, bytesNeedToRead);

if (bytesNeedToRead > bytesWereRead)
{
throw new TimeoutException();
}

_buffer.AddRange(_readBuffer.SubArray(0, bytesWereRead));
}

byte[] answer = _buffer.GetRange(0, substringLen).ToArray();
_buffer.RemoveRange(0, substringLen);

return answer;
}
}
}
48 changes: 30 additions & 18 deletions code/cs_api/cs_api/Connectors/TcpConnector.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using System;
using TrackPlatform.Basic;
using System.IO;
using System.Net.Sockets;

namespace TrackPlatform.Connectors
{
public class TcpConnector : BasicConnector
public class TcpConnector : StreamingConnector
{
private const int AnswerWaitInMs = 400;
private readonly string _ip;
private readonly int _port;
private readonly TcpClient _tcpConnect;

private byte[] _receivedBuffer = new byte[1];

protected override void Write(byte[] s)
{
throw new NotImplementedException();
}
protected override byte[] Read()
{
throw new NotImplementedException();
}
protected override Stream ReadStream => _tcpConnect.GetStream();
protected override Stream WriteStream => _tcpConnect.GetStream();

protected override int ReadAvailable => _tcpConnect.Available;

/// <summary>
/// Create TCP/IP connector to trackPlatform
Expand All @@ -28,25 +25,40 @@ protected override byte[] Read()
/// <param name="port">Port of TCP/IP server on trackPlatform</param>
public TcpConnector(string ip, int port)
{
throw new NotImplementedException();
_ip = ip;
_port = port;

_tcpConnect = new TcpClient
{
ReceiveTimeout = ReadWriteTimeoutInMs,
SendTimeout = ReadWriteTimeoutInMs
};

SendStartCommand();
}

public override void Dispose()
{
base.Dispose();
throw new NotImplementedException();

//Fixed in newer framework
//_tcpConnect?.Dispose();
}

public override bool IsConnected()
{
throw new NotImplementedException();
return (base.IsConnected() && _tcpConnect.Connected);
}
public override void Connect()
{
throw new NotImplementedException();
_tcpConnect.Connect(_ip, _port);
}
public override void Disconnect()
{
throw new NotImplementedException();
if (_tcpConnect != null && _tcpConnect.Connected)
{
_tcpConnect.Close();
}
}
}
}
1 change: 1 addition & 0 deletions code/cs_api/cs_api/cs_api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Connectors\StreamingConnector.cs" />
<Compile Include="Exceptions\CannotConnectToArduinoException.cs" />
<Compile Include="Exceptions\CorruptedAnswerException.cs" />
<Compile Include="Exceptions\NoConnectionException.cs" />
Expand Down

0 comments on commit fa8cfeb

Please sign in to comment.