public void WaitForRxCharEventOn2Bytes() { using (ManualResetEvent rxChar = new ManualResetEvent(false)) using (SerialPortStream src = new SerialPortStream(SourcePort, 115200, 8, Parity.None, StopBits.One)) using (SerialPortStream dst = new SerialPortStream(DestPort, 115200, 8, Parity.None, StopBits.One)) { src.Open(); src.WriteTimeout = TimeOut; src.ReadTimeout = TimeOut; dst.Open(); dst.WriteTimeout = TimeOut; dst.ReadTimeout = TimeOut; dst.ReceivedBytesThreshold = 2; dst.DataReceived += (s, a) => { rxChar.Set(); }; src.WriteByte(0x00); Assert.That(rxChar.WaitOne(1000), Is.False); src.WriteByte(0x01); Assert.That(rxChar.WaitOne(1000), Is.True); } }
public void WaitForRxCharEventOnEofChar() { using (ManualResetEvent rxChar = new ManualResetEvent(false)) using (ManualResetEvent eofChar = new ManualResetEvent(false)) using (SerialPortStream src = new SerialPortStream(SourcePort, 115200, 8, Parity.None, StopBits.One)) using (SerialPortStream dst = new SerialPortStream(DestPort, 115200, 8, Parity.None, StopBits.One)) { src.Open(); src.WriteTimeout = TimeOut; src.ReadTimeout = TimeOut; dst.Open(); dst.WriteTimeout = TimeOut; dst.ReadTimeout = TimeOut; dst.ReceivedBytesThreshold = 5; dst.DataReceived += (s, a) => { if (a.EventType == SerialData.Chars) { rxChar.Set(); } if (a.EventType == SerialData.Eof) { eofChar.Set(); } }; src.WriteByte(0x00); Assert.That(rxChar.WaitOne(500), Is.False); Assert.That(eofChar.WaitOne(0), Is.False); src.WriteByte(0x01); Assert.That(rxChar.WaitOne(500), Is.False); Assert.That(eofChar.WaitOne(0), Is.False); src.WriteByte(0x1A); Assert.That(rxChar.WaitOne(500), Is.False); Assert.That(eofChar.WaitOne(0), Is.True); eofChar.Reset(); src.WriteByte(0x02); Assert.That(rxChar.WaitOne(500), Is.False); Assert.That(eofChar.WaitOne(0), Is.False); src.WriteByte(0x03); Assert.That(rxChar.WaitOne(500), Is.True); Assert.That(eofChar.WaitOne(0), Is.False); } }
public void WriteByteUntilDisconnected() { using (SerialPortStream serialSource = new SerialPortStream(SourcePort, 115200, 8, Parity.None, StopBits.One)) using (SerialPortStream serialDest = new SerialPortStream(DestPort, 115200, 8, Parity.None, StopBits.One)) { serialSource.Open(); serialDest.Open(); while (serialSource.IsOpen) { try { serialSource.WriteByte(0xAA); } catch (Exception ex) { Console.WriteLine("\n** Exception: {0}\n{1}\n\n", ex.Message, ex.ToString()); } Thread.Sleep(100); } } }