📄 commbase.cs
字号:
throw new CommPortException(reason);
}
else
{
if (online)
{
BeforeClose(true);
InternalClose();
}
if (rxException == null)
{
throw new CommPortException(reason);
}
else
{
throw new CommPortException(rxException);
}
}
}
/// <summary>
/// Queues bytes for transmission.
/// </summary>
/// <param name="tosend">Array of bytes to be sent</param>
protected void Send(byte[] tosend) {
uint sent = 0;
CheckOnline();
CheckResult();
writeCount = tosend.GetLength(0);
if (Win32Com.WriteFile(hPort, tosend, (uint)writeCount, out sent, ptrUWO))
{
writeCount -= (int)sent;
}
else
{
if (Marshal.GetLastWin32Error() != Win32Com.ERROR_IO_PENDING) ThrowException("Unexpected failure");
}
}
/// <summary>
/// Queues a single byte for transmission.
/// </summary>
/// <param name="tosend">Byte to be sent</param>
protected void Send(byte tosend)
{
byte[] b = new byte[1];
b[0] = tosend;
Send(b);
}
private void CheckResult()
{
uint sent = 0;
if (writeCount > 0)
{
if (Win32Com.GetOverlappedResult(hPort, ptrUWO, out sent, checkSends))
{
writeCount -= (int)sent;
if (writeCount != 0) ThrowException("Send Timeout");
}
else
{
if (Marshal.GetLastWin32Error() != Win32Com.ERROR_IO_PENDING) ThrowException("Unexpected failure");
}
}
}
/// <summary>
/// Sends a protocol byte immediately ahead of any queued bytes.
/// </summary>
/// <param name="tosend">Byte to send</param>
/// <returns>False if an immediate byte is already scheduled and not yet sent</returns>
protected void SendImmediate(byte tosend) {
CheckOnline();
if (!Win32Com.TransmitCommChar(hPort, tosend)) ThrowException("Transmission failure");
}
/// <summary>
/// Delay processing.
/// </summary>
/// <param name="milliseconds">Milliseconds to delay by</param>
protected void Sleep(int milliseconds)
{
Thread.Sleep(milliseconds);
}
/// <summary>
/// Represents the status of the modem control input signals.
/// </summary>
public struct ModemStatus
{
private uint status;
internal ModemStatus(uint val) {status = val;}
/// <summary>
/// Condition of the Clear To Send signal.
/// </summary>
public bool cts {get{return ((status & Win32Com.MS_CTS_ON) != 0);}}
/// <summary>
/// Condition of the Data Set Ready signal.
/// </summary>
public bool dsr {get{return ((status & Win32Com.MS_DSR_ON) != 0);}}
/// <summary>
/// Condition of the Receive Line Status Detection signal.
/// </summary>
public bool rlsd {get{return ((status & Win32Com.MS_RLSD_ON) != 0);}}
/// <summary>
/// Condition of the Ring Detection signal.
/// </summary>
public bool ring {get{return ((status & Win32Com.MS_RING_ON) != 0);}}
}
/// <summary>
/// Gets the status of the modem control input signals.
/// </summary>
/// <returns>Modem status object</returns>
protected ModemStatus GetModemStatus() {
uint f;
CheckOnline();
if (!Win32Com.GetCommModemStatus(hPort, out f)) ThrowException("Unexpected failure");
return new ModemStatus(f);
}
/// <summary>
/// Represents the current condition of the port queues.
/// </summary>
public struct QueueStatus
{
private uint status;
private uint inQueue;
private uint outQueue;
private uint inQueueSize;
private uint outQueueSize;
internal QueueStatus(uint stat, uint inQ, uint outQ, uint inQs, uint outQs)
{status = stat; inQueue = inQ; outQueue = outQ; inQueueSize = inQs; outQueueSize = outQs;}
/// <summary>
/// Output is blocked by CTS handshaking.
/// </summary>
public bool ctsHold {get{return ((status & Win32Com.COMSTAT.fCtsHold) != 0);}}
/// <summary>
/// Output is blocked by DRS handshaking.
/// </summary>
public bool dsrHold {get{return ((status & Win32Com.COMSTAT.fDsrHold) != 0);}}
/// <summary>
/// Output is blocked by RLSD handshaking.
/// </summary>
public bool rlsdHold {get{return ((status & Win32Com.COMSTAT.fRlsdHold) != 0);}}
/// <summary>
/// Output is blocked because software handshaking is enabled and XOFF was received.
/// </summary>
public bool xoffHold {get{return ((status & Win32Com.COMSTAT.fXoffHold) != 0);}}
/// <summary>
/// Output was blocked because XOFF was sent and this station is not yet ready to receive.
/// </summary>
public bool xoffSent {get{return ((status & Win32Com.COMSTAT.fXoffSent) != 0);}}
/// <summary>
/// There is a character waiting for transmission in the immediate buffer.
/// </summary>
public bool immediateWaiting {get{return ((status & Win32Com.COMSTAT.fTxim) != 0);}}
/// <summary>
/// Number of bytes waiting in the input queue.
/// </summary>
public long InQueue {get{return (long)inQueue;}}
/// <summary>
/// Number of bytes waiting for transmission.
/// </summary>
public long OutQueue {get{return (long)outQueue;}}
/// <summary>
/// Total size of input queue (0 means information unavailable)
/// </summary>
public long InQueueSize {get{return (long)inQueueSize;}}
/// <summary>
/// Total size of output queue (0 means information unavailable)
/// </summary>
public long OutQueueSize {get{return (long)outQueueSize;}}
public override string ToString()
{
StringBuilder m = new StringBuilder("The reception queue is ", 60);
if (inQueueSize == 0)
{
m.Append("of unknown size and ");
}
else
{
m.Append(inQueueSize.ToString() + " bytes long and ");
}
if (inQueue == 0)
{
m.Append("is empty.");
}
else if (inQueue == 1)
{
m.Append("contains 1 byte.");
}
else
{
m.Append("contains ");
m.Append(inQueue.ToString());
m.Append(" bytes.");
}
m.Append(" The transmission queue is ");
if (outQueueSize == 0)
{
m.Append("of unknown size and ");
}
else
{
m.Append(outQueueSize.ToString() + " bytes long and ");
}
if (outQueue == 0)
{
m.Append("is empty");
}
else if (outQueue == 1)
{
m.Append("contains 1 byte. It is ");
}
else
{
m.Append("contains ");
m.Append(outQueue.ToString());
m.Append(" bytes. It is ");
}
if (outQueue > 0)
{
if (ctsHold || dsrHold || rlsdHold || xoffHold || xoffSent)
{
m.Append("holding on");
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");
}
else
{
m.Append("pumping data");
}
}
m.Append(". The immediate buffer is ");
if (immediateWaiting)
m.Append("full.");
else
m.Append("empty.");
return m.ToString();
}
}
/// <summary>
/// Get the status of the queues
/// </summary>
/// <returns>Queue status object</returns>
protected QueueStatus GetQueueStatus()
{
Win32Com.COMSTAT cs;
Win32Com.COMMPROP cp;
uint er;
CheckOnline();
if (!Win32Com.ClearCommError(hPort, out er, out cs)) ThrowException("Unexpected failure");
if (!Win32Com.GetCommProperties(hPort, out cp)) ThrowException("Unexpected failure");
return new QueueStatus(cs.Flags, cs.cbInQue, cs.cbOutQue, cp.dwCurrentRxQueue, cp.dwCurrentTxQueue);
}
/// <summary>
/// True if the RTS pin is controllable via the RTS property
/// </summary>
protected bool RTSavailable { get { return (stateRTS < 2);}}
/// <summary>
/// Set the state of the RTS modem control output
/// </summary>
protected bool RTS
{
set {
if (stateRTS > 1) return;
CheckOnline();
if (value)
{
if (Win32Com.EscapeCommFunction(hPort, Win32Com.SETRTS))
stateRTS = 1;
else
ThrowException("Unexpected Failure");
}
else
{
if (Win32Com.EscapeCommFunction(hPort, Win32Com.CLRRTS))
stateRTS = 1;
else
ThrowException("Unexpected Failure");
}
}
get {
return (stateRTS == 1);
}
}
/// <summary>
/// True if the DTR pin is controllable via the DTR property
/// </summary>
protected bool DTRavailable { get { return (stateDTR < 2);}}
/// <summary>
/// The state of the DTR modem control output
/// </summary>
protected bool DTR {
set {
if (stateDTR > 1) return;
CheckOnline();
if (value)
{
if (Win32Com.EscapeCommFunction(hPort, Win32Com.SETDTR))
stateDTR = 1;
else
ThrowException("Unexpected Failure");
}
else
{
if (Win32Com.EscapeCommFunction(hPort, Win32Com.CLRDTR))
stateDTR = 0;
else
ThrowException("Unexpected Failure");
}
}
get {
return (stateDTR == 1);
}
}
/// <summary>
/// Assert or remove a break condition from the transmission line
/// </summary>
protected bool Break {
set {
if (stateBRK > 1) return;
CheckOnline();
if (value)
{
if (Win32Com.EscapeCommFunction(hPort, Win32Com.SETBREAK))
stateBRK = 0;
else
ThrowException("Unexpected Failure");
}
else
{
if (Win32Com.EscapeCommFunction(hPort, Win32Com.CLRBREAK))
stateBRK = 0;
else
ThrowException("Unexpected Failure");
}
}
get {
return (stateBRK == 1);
}
}
/// <summary>
/// Override this to provide settings. (NB this is called during Open method)
/// </summary>
/// <returns>CommBaseSettings, or derived object with required settings initialised</returns>
protected virtual CommBaseSettings CommSettings() {return new CommBaseSettings();}
/// <summary>
/// Override this to provide processing after the port is openned (i.e. to configure remote
/// device or just check presence).
/// </summary>
/// <returns>false to close the port again</returns>
protected virtual bool AfterOpen() {return true;}
/// <summary>
/// Override this to provide processing prior to port closure.
/// </summary>
/// <param name="error">True if closing due to an error</param>
protected virtual void BeforeClose(bool error) {}
/// <summary>
/// Override this to process received bytes.
/// </summary>
/// <param name="ch">The byte that was received</param>
protected virtual void OnRxChar(byte ch) {}
/// <summary>
/// Override this to take action when transmission is complete (i.e. all bytes have actually
/// been sent, not just queued).
/// </summary>
protected virtual void OnTxDone() {}
/// <summary>
/// Override this to take action when a break condition is detected on the input line.
/// </summary>
protected virtual void OnBreak() {}
/// <summary>
/// Override this to take action when a ring condition is signalled by an attached modem.
/// </summary>
protected virtual void OnRing() {}
/// <summary>
/// Override this to take action when one or more modem status inputs change state
/// </summary>
/// <param name="mask">The status inputs that have changed state</param>
/// <param name="state">The state of the status inputs</param>
protected virtual void OnStatusChange(ModemStatus mask, ModemStatus state) {}
/// <summary>
/// Override this to take action when the reception thread closes due to an exception being thrown.
/// </summary>
/// <param name="e">The exception which was thrown</param>
protected virtual void OnRxException(Exception e) {}
private void ReceiveThread() {
byte[] buf = new Byte[1];
uint gotbytes;
AutoResetEvent sg = new AutoResetEvent(false);
Win32Com.OVERLAPPED ov = new Win32Com.OVERLAPPED();
IntPtr unmanagedOv = Marshal.AllocHGlobal(Marshal.SizeOf(ov));
ov.Offset = 0; ov.OffsetHigh = 0;
ov.hEvent = sg.Handle;
Marshal.StructureToPtr(ov, unmanagedOv, true);
uint eventMask = 0;
IntPtr uMask = Marshal.AllocHGlobal(Marshal.SizeOf(eventMask));
try
{
while(true)
{
if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR | Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING | Win32Com.EV_ERR))
{
throw new CommPortException("IO Error [001]");
}
Marshal.WriteInt32(uMask, 0);
if (!Win32Com.WaitCommEvent(hPort, uMask, unmanagedOv))
{
if (Marshal.GetLastWin32Error() == Win32Com.ERROR_IO_PENDING)
{
sg.WaitOne();
}
else
{
throw new CommPortException("IO Error [002]");
}
}
eventMask = (uint)Marshal.ReadInt32(uMask);
if ((eventMask & Win32Com.EV_ERR) != 0)
{
UInt32 errs;
if (Win32Com.ClearCommError(hPort, out errs, IntPtr.Zero))
{
StringBuilder s = new StringBuilder("UART Error: ", 40);
if ((errs & Win32Com.CE_FRAME) != 0) s = s.Append("Framing,");
if ((errs & Win32Com.CE_IOE) != 0) s = s.Append("IO,");
if ((errs & Win32Com.CE_OVERRUN) != 0) s = s.Append("Overrun,");
if ((errs & Win32Com.CE_RXOVER) != 0) s = s.Append("Receive Cverflow,");
if ((errs & Win32Com.CE_RXPARITY) != 0) s = s.Append("Parity,");
if ((errs & Win32Com.CE_TXFULL) != 0) s = s.Append("Transmit Overflow,");
s.Length = s.Length - 1;
throw new CommPortException(s.ToString());
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -