我想讀取我的串行端口,但只有當數據來(我不想輪詢)。C#只讀串行端口數據來
這就是我的做法。
Schnittstelle = new SerialPort("COM3");
Schnittstelle.BaudRate = 115200;
Schnittstelle.DataBits = 8;
Schnittstelle.StopBits = StopBits.Two;
....
然後我啓動一個線程
beginn = new Thread(readCom);
beginn.Start();
,並在我的readCom裏我讀連續(輪詢:()
private void readCom()
{
try
{
while (Schnittstelle.IsOpen)
{
Dispatcher.BeginInvoke(new Action(() =>
{
ComWindow.txtbCom.Text = ComWindow.txtbCom.Text + Environment.NewLine + Schnittstelle.ReadExisting();
ComWindow.txtbCom.ScrollToEnd();
}));
beginn.Join(10);
}
}
catch (ThreadAbortException)
{
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我想YOUT讀時中斷來臨。但我該怎麼做呢?
此示例代碼在.NET 4.5中不再安全。 Console.ReadKey()獲取阻止Console.Write()寫入任何內容的鎖。 Debug.Print()沒問題。 – 2013-04-25 13:22:14
我會編輯這個!謝謝! – 2013-04-25 13:23:38
@HansPassant謝謝你。這肯定已經打破了MSDN上的許多線程示例:) – kenny 2013-04-25 13:23:47