シリアルポートを読みたいのですが、データが来るときだけです(ポーリングしたくない)。
これが私のやり方です。
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);
}
}
割り込みが来たら読んでほしい。しかし、どうすればそれができますか?