?? port.cs
字號:
return ptxBuffer;
}
}
public uint RThreshold
{
get
{
return rthreshold;
}
set
{
rthreshold = value;
}
}
public uint SThreshold
{
get
{
return sthreshold;
}
set
{
sthreshold = value;
}
}
public bool Break
{
get
{
if(!isOpen) return false;
return (brk == 1);
}
set
{
if(!isOpen) return;
if(brk < 0) return;
if(hPort == (IntPtr)CommAPI.INVALID_HANDLE_VALUE) return;
if (value)
{
if (CommAPI.EscapeCommFunction(hPort, CommEscapes.SETBREAK))
brk = 1;
else
throw new CommPortException("Failed to set break!");
}
else
{
if (CommAPI.EscapeCommFunction(hPort, CommEscapes.CLRBREAK))
brk = 0;
else
throw new CommPortException("Failed to clear break!");
}
}
}
public bool DTRAvailable
{
get
{
return dtravail;
}
}
public bool DTREnable
{
get
{
return (dtr == 1);
}
set
{
if(dtr < 0) return;
if(hPort == (IntPtr)CommAPI.INVALID_HANDLE_VALUE) return;
if (value)
{
if (CommAPI.EscapeCommFunction(hPort, CommEscapes.SETDTR))
dtr = 1;
else
throw new CommPortException("Failed to set DTR!");
}
else
{
if (CommAPI.EscapeCommFunction(hPort, CommEscapes.CLRDTR))
dtr = 0;
else
throw new CommPortException("Failed to clear DTR!");
}
}
}
public bool RTSAvailable
{
get
{
return rtsavail;
}
}
public bool RTSEnable
{
get
{
return (rts == 1);
}
set
{
if(rts < 0) return;
if(hPort == (IntPtr)CommAPI.INVALID_HANDLE_VALUE) return;
if (value)
{
if (CommAPI.EscapeCommFunction(hPort, CommEscapes.SETRTS))
rts = 1;
else
throw new CommPortException("Failed to set RTS!");
}
else
{
if (CommAPI.EscapeCommFunction(hPort, CommEscapes.CLRRTS))
rts = 0;
else
throw new CommPortException("Failed to clear RTS!");
}
}
}
public DetailedPortSettings DetailedSettings
{
get
{
return portSettings;
}
set
{
portSettings = value;
}
}
public BasicPortSettings Settings
{
get
{
return portSettings.BasicSettings;
}
set
{
portSettings.BasicSettings = value;
}
}
private void CommEventThread()
{
CommEventFlags eventFlags = new CommEventFlags();
byte[] readbuffer = new Byte[1];
int bytesread = 0;
// specify the set of events to be monitored for the port.
CommAPI.SetCommMask(hPort, CommEventFlags.ALL);
try
{
// let Open() know we're started
threadStarted.Set();
while(hPort != (IntPtr)CommAPI.INVALID_HANDLE_VALUE)
{
// wait for a Comm event
if(!CommAPI.WaitCommEvent(hPort, ref eventFlags))
{
int e = Marshal.GetLastWin32Error();
if(e == (int)APIErrors.ERROR_IO_PENDING)
{
// IO pending so just wait and try again
Thread.Sleep(0);
continue;
}
if(e == (int)APIErrors.ERROR_INVALID_HANDLE)
{
// Calling Port.Close() causes hPort to become invalid
// Since Thread.Abort() is unsupported in the CF, we must
// accept that calling Close will throw an error here.
// Close signals the closeEvent, so wait on it
// We wait 1 second, though Close should happen much sooner
int eventResult = CommAPI.WaitForSingleObject(closeEvent, 1000);
if(eventResult == (int)APIConstants.WAIT_OBJECT_0)
{
// the event was set so close was called
hPort = (IntPtr)CommAPI.INVALID_HANDLE_VALUE;
// reset our ResetEvent for the next call to Open
threadStarted.Reset();
return;
}
}
// WaitCommEvent failed!
string error = String.Format("Wait Failed: {0}", e);
throw new CommPortException(error);
}
// Re-specify the set of events to be monitored for the port.
CommAPI.SetCommMask(hPort, CommEventFlags.ALL);
// check the event for errors
if(((uint)eventFlags & (uint)CommEventFlags.ERR) != 0)
{
CommErrorFlags errorFlags = new CommErrorFlags();
CommStat commStat = new CommStat();
// get the error status
if(!CommAPI.ClearCommError(hPort, ref errorFlags, commStat))
{
// ClearCommError failed!
string error = String.Format("ClearCommError Failed: {0}", Marshal.GetLastWin32Error());
throw new CommPortException(error);
}
if(((uint)errorFlags & (uint)CommErrorFlags.BREAK) != 0)
{
// BREAK can set an error, so make sure the BREAK bit is set an continue
eventFlags |= CommEventFlags.BREAK;
}
else
{
// we have an error. Build a meaningful string and throw an exception
StringBuilder s = new StringBuilder("UART Error: ", 80);
if ((errorFlags & CommErrorFlags.FRAME) != 0)
{ s = s.Append("Framing,"); }
if ((errorFlags & CommErrorFlags.IOE) != 0)
{ s = s.Append("IO,"); }
if ((errorFlags & CommErrorFlags.OVERRUN) != 0)
{ s = s.Append("Overrun,"); }
if ((errorFlags & CommErrorFlags.RXOVER) != 0)
{ s = s.Append("Receive Overflow,"); }
if ((errorFlags & CommErrorFlags.RXPARITY) != 0)
{ s = s.Append("Parity,"); }
if ((errorFlags & CommErrorFlags.TXFULL) != 0)
{ s = s.Append("Transmit Overflow,"); }
// no known bits are set
if(s.Length == 12)
{ s = s.Append("Unknown"); }
// raise an error event
if(OnError != null)
OnError(s.ToString());
continue;
}
} // if(((uint)eventFlags & (uint)CommEventFlags.ERR) != 0)
// check for status changes
uint status = 0;
CommAPI.GetCommModemStatus(hPort, ref status);
// check the CTS
if(((uint)eventFlags & (uint)CommEventFlags.CTS) != 0)
{
if(CTSChange != null)
CTSChange((status & (uint)CommModemStatusFlags.MS_CTS_ON) != 0);
}
// check the DSR
if(((uint)eventFlags & (uint)CommEventFlags.DSR) != 0)
{
if(DSRChange != null)
DSRChange((status & (uint)CommModemStatusFlags.MS_DSR_ON) != 0);
}
// check for a RING
if(((uint)eventFlags & (uint)CommEventFlags.RING) != 0)
{
if(RingChange != null)
RingChange((status & (uint)CommModemStatusFlags.MS_RING_ON) != 0);
}
// check for a RLSD
if(((uint)eventFlags & (uint)CommEventFlags.RLSD) != 0)
{
if(RLSDChange != null)
RLSDChange((status & (uint)CommModemStatusFlags.MS_RLSD_ON) != 0);
}
// check for TXEMPTY
if(((uint)eventFlags & (uint)CommEventFlags.TXEMPTY) != 0)
{
if(TxDone != null)
TxDone();
}
// check for RXFLAG
if(((uint)eventFlags & (uint)CommEventFlags.RXFLAG) != 0)
{
if(FlagCharReceived != null)
FlagCharReceived();
}
// check for POWER
if(((uint)eventFlags & (uint)CommEventFlags.POWER) != 0)
{
if(PowerEvent != null)
PowerEvent();
}
// check for high-water state
if((eventFlags & CommEventFlags.RX80FULL) != 0)
{
if(HighWater != null)
HighWater();
}
// check for RXCHAR
if((eventFlags & CommEventFlags.RXCHAR) != 0)
{
if(DataReceived != null)
{
// data came in, put it in the buffer and set the event
CommAPI.ReadFile(hPort, readbuffer, 1, ref bytesread);
if(bytesread == 1)
{
if(prxBuffer > rxBuffer.GetUpperBound(0))
{
// data beyond the buffer is lost!
if(RxOverrun != null)
RxOverrun();
}
else
{
rxBufferBusy.WaitOne();
// store the byte in out buffer and increment the pointer
rxBuffer[prxBuffer] = readbuffer[0];
prxBuffer++;
rxBufferBusy.ReleaseMutex();
// prxBuffer gets reset when the Input value is read. (FIFO)
// fire the DataReceived event every RThreshold bytes
if(prxBuffer % rthreshold == 0)
{
if(DataReceived != null)
DataReceived();
}
}
}
}
}
} // while(true)
} // try
catch(Exception e)
{
throw e;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -