Files
Nefarius.Peripherals.Serial…/Nefarius.Peripherals.SerialPort/QueueStatus.cs
T

151 lines
4.7 KiB
C#
Raw Normal View History

2012-03-03 13:20:54 +03:30
using System.Text;
2022-09-26 21:00:43 +02:00
using Nefarius.Peripherals.SerialPort.Win32PInvoke;
2012-03-03 13:20:54 +03:30
2022-09-26 21:00:43 +02:00
namespace Nefarius.Peripherals.SerialPort
2012-03-03 13:20:54 +03:30
{
/// <summary>
2018-11-22 21:33:25 +01:00
/// Represents the current condition of the port queues.
2012-03-03 13:20:54 +03:30
/// </summary>
public struct QueueStatus
{
2018-11-22 21:33:25 +01:00
private readonly uint _status;
private readonly uint _inQueue;
private readonly uint _outQueue;
private readonly uint _inQueueSize;
private readonly uint _outQueueSize;
2012-03-03 13:20:54 +03:30
internal QueueStatus(uint stat, uint inQ, uint outQ, uint inQs, uint outQs)
2018-11-22 21:33:25 +01:00
{
_status = stat;
_inQueue = inQ;
_outQueue = outQ;
_inQueueSize = inQs;
_outQueueSize = outQs;
}
2022-10-03 19:16:35 +02:00
internal const uint fCtsHold = 0x1;
internal const uint fDsrHold = 0x2;
internal const uint fRlsdHold = 0x4;
internal const uint fXoffHold = 0x8;
internal const uint fXoffSent = 0x10;
internal const uint fEof = 0x20;
internal const uint fTxim = 0x40;
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// Output is blocked by CTS handshaking.
2012-03-03 13:20:54 +03:30
/// </summary>
2022-10-03 19:16:35 +02:00
public bool CtsHold => (_status & fCtsHold) != 0;
2018-11-22 21:33:25 +01:00
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// Output is blocked by DRS handshaking.
2012-03-03 13:20:54 +03:30
/// </summary>
2022-10-03 19:16:35 +02:00
public bool DsrHold => (_status & fDsrHold) != 0;
2018-11-22 21:33:25 +01:00
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// Output is blocked by RLSD handshaking.
2012-03-03 13:20:54 +03:30
/// </summary>
2022-10-03 19:16:35 +02:00
public bool RlsdHold => (_status & fRlsdHold) != 0;
2018-11-22 21:33:25 +01:00
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// Output is blocked because software handshaking is enabled and XOFF was received.
2012-03-03 13:20:54 +03:30
/// </summary>
2022-10-03 19:16:35 +02:00
public bool XoffHold => (_status & fXoffHold) != 0;
2018-11-22 21:33:25 +01:00
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// Output was blocked because XOFF was sent and this station is not yet ready to receive.
2012-03-03 13:20:54 +03:30
/// </summary>
2022-10-03 19:16:35 +02:00
public bool XoffSent => (_status & fXoffSent) != 0;
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// There is a character waiting for transmission in the immediate buffer.
2012-03-03 13:20:54 +03:30
/// </summary>
2022-10-03 19:16:35 +02:00
public bool ImmediateWaiting => (_status & fTxim) != 0;
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// Number of bytes waiting in the input queue.
2012-03-03 13:20:54 +03:30
/// </summary>
2018-11-22 21:33:25 +01:00
public long InQueue => _inQueue;
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// Number of bytes waiting for transmission.
2012-03-03 13:20:54 +03:30
/// </summary>
2018-11-22 21:33:25 +01:00
public long OutQueue => _outQueue;
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// Total size of input queue (0 means information unavailable)
2012-03-03 13:20:54 +03:30
/// </summary>
2018-11-22 21:33:25 +01:00
public long InQueueSize => _inQueueSize;
2012-03-03 13:20:54 +03:30
/// <summary>
2018-11-22 21:33:25 +01:00
/// Total size of output queue (0 means information unavailable)
2012-03-03 13:20:54 +03:30
/// </summary>
2018-11-22 21:33:25 +01:00
public long OutQueueSize => _outQueueSize;
2012-03-03 13:20:54 +03:30
public override string ToString()
{
var m = new StringBuilder("The reception queue is ", 60);
2018-11-22 21:33:25 +01:00
if (_inQueueSize == 0)
2012-03-03 13:20:54 +03:30
m.Append("of unknown size and ");
else
2018-11-22 21:33:25 +01:00
m.Append(_inQueueSize + " bytes long and ");
if (_inQueue == 0)
2012-03-03 13:20:54 +03:30
{
m.Append("is empty.");
}
2018-11-22 21:33:25 +01:00
else if (_inQueue == 1)
2012-03-03 13:20:54 +03:30
{
m.Append("contains 1 byte.");
}
else
{
m.Append("contains ");
2018-11-22 21:33:25 +01:00
m.Append(_inQueue.ToString());
2012-03-03 13:20:54 +03:30
m.Append(" bytes.");
}
2018-11-22 21:33:25 +01:00
2012-03-03 13:20:54 +03:30
m.Append(" The transmission queue is ");
2018-11-22 21:33:25 +01:00
if (_outQueueSize == 0)
2012-03-03 13:20:54 +03:30
m.Append("of unknown size and ");
else
2018-11-22 21:33:25 +01:00
m.Append(_outQueueSize + " bytes long and ");
if (_outQueue == 0)
2012-03-03 13:20:54 +03:30
{
m.Append("is empty");
}
2018-11-22 21:33:25 +01:00
else if (_outQueue == 1)
2012-03-03 13:20:54 +03:30
{
m.Append("contains 1 byte. It is ");
}
else
{
m.Append("contains ");
2018-11-22 21:33:25 +01:00
m.Append(_outQueue.ToString());
2012-03-03 13:20:54 +03:30
m.Append(" bytes. It is ");
}
2018-11-22 21:33:25 +01:00
if (_outQueue > 0)
2012-03-03 13:20:54 +03:30
{
2018-11-22 21:33:25 +01:00
if (CtsHold || DsrHold || RlsdHold || XoffHold || XoffSent)
2012-03-03 13:20:54 +03:30
{
m.Append("holding on");
2018-11-22 21:33:25 +01:00
if (CtsHold) m.Append(" CTS");
if (DsrHold) m.Append(" DSR");
if (RlsdHold) m.Append(" RLSD");
if (XoffHold) m.Append(" Rx XOff");
if (XoffSent) m.Append(" Tx XOff");
2012-03-03 13:20:54 +03:30
}
else
{
m.Append("pumping data");
}
}
2018-11-22 21:33:25 +01:00
2012-03-03 13:20:54 +03:30
m.Append(". The immediate buffer is ");
2018-11-22 21:33:25 +01:00
if (ImmediateWaiting)
2012-03-03 13:20:54 +03:30
m.Append("full.");
else
m.Append("empty.");
return m.ToString();
}
}
}