277 lines
8.6 KiB
Plaintext
277 lines
8.6 KiB
Plaintext
|
|
using DTS.Common.Interface.Connection;
|
||
|
|
using System;
|
||
|
|
using System.IO.Ports;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
|
||
|
|
namespace DTS.Common.SerialConnection
|
||
|
|
{
|
||
|
|
public class SerialConnection : IConnection
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// returns true if the unit is soft disconnected
|
||
|
|
/// </summary>
|
||
|
|
public bool IsSoftDisconnected { get; private set; } = false;
|
||
|
|
/// <summary>
|
||
|
|
/// connects the unit
|
||
|
|
/// :note does nothing for serial as we don't have a soft disconnect option yet:
|
||
|
|
/// </summary>
|
||
|
|
public void SoftConnect()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// disconnects the unit
|
||
|
|
/// :note does nothing for serial as we don't have a soft disconnect option yet:
|
||
|
|
/// </summary>
|
||
|
|
public void SoftDisconnect()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
void IConnection.KeepAliveErrorReceived()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Create(string connectString, string hostIPAddress)
|
||
|
|
{
|
||
|
|
//ConnectString = connectString;
|
||
|
|
}
|
||
|
|
public string GetConnectionData() { return ""; }
|
||
|
|
public double GetCurrentDownloadRate() { return 0D; }
|
||
|
|
public double GetCurrentUploadRate() { return 0D; }
|
||
|
|
|
||
|
|
protected SerialPort Port;
|
||
|
|
protected string _PortName;
|
||
|
|
private bool disposed;
|
||
|
|
|
||
|
|
public event EventHandler OnDisconnected;
|
||
|
|
|
||
|
|
public bool Connected { get; } = false;
|
||
|
|
|
||
|
|
//this was only being set, and not used, in addition it's name
|
||
|
|
//is confusing next to _Connected and Connected{}
|
||
|
|
//but since it's value is never referenced it's probably safe to remove it
|
||
|
|
//public bool connected = false;
|
||
|
|
|
||
|
|
public string ConnectString => _PortName;
|
||
|
|
|
||
|
|
public System.Net.Sockets.SocketFlags Flags { get; set; }
|
||
|
|
|
||
|
|
public SerialConnection()
|
||
|
|
{
|
||
|
|
disposed = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
~SerialConnection()
|
||
|
|
{
|
||
|
|
Dispose(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Dispose()
|
||
|
|
{
|
||
|
|
Dispose(true);
|
||
|
|
GC.SuppressFinalize(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
protected virtual void Dispose(bool disposing)
|
||
|
|
{
|
||
|
|
if (disposed)
|
||
|
|
return;
|
||
|
|
|
||
|
|
if (disposing)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (Port != null)
|
||
|
|
Port.Close();
|
||
|
|
Port = null;
|
||
|
|
}
|
||
|
|
catch { }
|
||
|
|
}
|
||
|
|
disposed = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Create(string PortName)
|
||
|
|
{
|
||
|
|
Port = new SerialPort(_PortName);
|
||
|
|
_PortName = PortName;
|
||
|
|
}
|
||
|
|
|
||
|
|
#region Connect
|
||
|
|
|
||
|
|
public IAsyncResult BeginConnect(AsyncCallback cb, object state)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.BeginConnect: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.BeginConnect: socket is not created");
|
||
|
|
}
|
||
|
|
if (string.IsNullOrEmpty(_PortName))
|
||
|
|
{
|
||
|
|
// "SerialConnection.BeginConnect: Connect_String is empty"
|
||
|
|
throw new System.Exception("SerialConnection.BeginConnect: Connect_String is empty");
|
||
|
|
}
|
||
|
|
Port.Open();
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void EndConnect(IAsyncResult ar)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.EndConnect: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.EndConnect: socket is not created");
|
||
|
|
}
|
||
|
|
//connected = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Disconnect
|
||
|
|
|
||
|
|
public IAsyncResult BeginDisconnect(bool reuseSocket,
|
||
|
|
AsyncCallback cb,
|
||
|
|
Object state)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.BeginDisconnect: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.BeginDisconnect: socket is not created");
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void EndDisconnect(IAsyncResult asyncResult)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.EndDisconnect: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.EndDisconnect: socket is not created");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Accept
|
||
|
|
|
||
|
|
public IAsyncResult BeginAccept(AsyncCallback callback,
|
||
|
|
Object state)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.BeginAccept: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.BeginAccept: socket is not created");
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public IConnection EndAccept(IAsyncResult asyncResult)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.EndAccept: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.EndAccept: socket is not created");
|
||
|
|
}
|
||
|
|
|
||
|
|
var newConnection = new SerialConnection();
|
||
|
|
return newConnection;
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Bind
|
||
|
|
|
||
|
|
public void Bind(int port)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.Bind: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.Bind: socket is not created");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Listen
|
||
|
|
|
||
|
|
public void Listen(int backlog)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.Listen: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.Listen: socket is not created");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Send
|
||
|
|
|
||
|
|
public IAsyncResult BeginSend(byte[] buffer, int offset, int size,
|
||
|
|
AsyncCallback cb, object state)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.BeginSend: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.BeginSend: socket is not created");
|
||
|
|
}
|
||
|
|
if (cb == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.BeginSend: callback can't be null"
|
||
|
|
throw new System.Exception("SerialConnection.BeginSend: callback can't be null");
|
||
|
|
}
|
||
|
|
return Port.BaseStream.BeginWrite(buffer, offset, size, new AsyncCallback(cb), state);
|
||
|
|
}
|
||
|
|
|
||
|
|
public int EndSend(IAsyncResult ar)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.EndSend: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.EndSend: socket is not created");
|
||
|
|
}
|
||
|
|
Port.BaseStream.EndWrite(ar);
|
||
|
|
return Port.BytesToWrite;
|
||
|
|
}
|
||
|
|
|
||
|
|
public Task<int> SendAsync(byte[] sendBuffer, int bufferStartOffset, int bufferSizeToSend)
|
||
|
|
{
|
||
|
|
return Task<int>.Factory.FromAsync(
|
||
|
|
(callback, state) => BeginSend(sendBuffer, bufferStartOffset, bufferSizeToSend, callback, state),
|
||
|
|
EndSend, state: null
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Receive
|
||
|
|
|
||
|
|
public IAsyncResult BeginReceive(byte[] buffer, int offset, int size,
|
||
|
|
AsyncCallback cb, object state)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.BeginReceive: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.BeginReceive: socket is not created");
|
||
|
|
}
|
||
|
|
if (cb == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.BeginReceive: callback can't be null"
|
||
|
|
throw new System.Exception("SerialConnection.BeginReceive: callback can't be null");
|
||
|
|
}
|
||
|
|
return Port.BaseStream.BeginRead(buffer, offset, size, new AsyncCallback(cb), state);
|
||
|
|
}
|
||
|
|
|
||
|
|
public int EndReceive(IAsyncResult ar)
|
||
|
|
{
|
||
|
|
if (Port == null)
|
||
|
|
{
|
||
|
|
// "SerialConnection.EndReceive: socket is not created"
|
||
|
|
throw new System.Exception("SerialConnection.EndReceive: socket is not created");
|
||
|
|
}
|
||
|
|
return Port.BaseStream.EndRead(ar);
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
}
|
||
|
|
}
|