在 WPF 中从串行端口接收数据时执行某些操作



我想创建程序,该程序将根据"实时"接收的数据以任何方式为椭圆着色。数据如下:x1-1x2-1等。

我有一个奇怪的问题,我在调试时可以看到,填充椭圆的颜色应该改变,但它没有发生。

我接收数据的部分代码:

private void ReceiveData()
{
SerialPort serialPort = new SerialPort("COM1", 9600);
serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
serialPort.Open();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
if (indata.Contains(" "))
{
testString = indata.Substring(0, 4);
}
Console.Write(indata);
}

我可以在控制台中看到接收的数据,即使在单击按钮后,也开始接收数据。testString是从indata中提取单个x1-1,在接收数据期间仍在增长。我知道它不能像它应该的那样工作,但现在没关系了。

按钮上的操作代码:

private void button1_Click(object sender, RoutedEventArgs e)
{
ReceiveData();
while (true)
{
if (testString == "x1-1") x1.Fill = Brushes.Blue;
else x1.Fill = Brushes.Red;
}
}

x1是我想做红色或蓝色的椭圆。我可以在此函数中设置断点,我可以看到它应该更改。我认为问题是,该程序正在等待关闭端口并完成传输,然后可能会更改,但我需要"实时"拥有它。有人知道该怎么做吗?

我相信你这样做的方式是错误的。与其不断检查teststring的值,您只需启动 comm 端口侦听器并让它在后台运行 - 因为这将在它自己的线程上。然后,您需要创建颜色的数据绑定。每当通信端口侦听器解析字符串时,它都可以自动更新颜色。

首先,在通信端口类中:

class MyCommPort : INotifyPropertyChanged
{
SerialPort serialPort = null;
public MyCommPort()
{
serialPort = new SerialPort("COM3", 9600);
serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
serialPort.Open();
}
~MyCommPort()
{
serialPort.Close();
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string testString = null;
string indata = sp.ReadLine();
if (indata.Length >= 4)
{
testString = indata.Substring(0, 4);
// Update the value
if (testString == "x1-1") EllipseBrush = Brushes.Blue;
else EllipseBrush = Brushes.Red;
}
Console.Write(testString);
}
// Create a property that will be bound
private SolidColorBrush ellipseBrush = Brushes.Red;
public SolidColorBrush EllipseBrush
{
get { return ellipseBrush; }
set
{
ellipseBrush = value;
OnPropertyChanged("EllipseBrush");
}
}
// Extend the INotifyPropertyChanged interface
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
// Alert anyone bound to this that the value has changed
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

然后,将 Ellipse 的 DataContext 设置为 CommPort 类,并将 Ellipse 的 Fill 属性绑定到 EllipseBrush。 现在您所要做的就是启动Commport侦听器(ReceiveData();),颜色更新应该会自动发生。

例如:MainWindow.xaml

<Window x:Class="delete_me.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Ellipse x:Name="ellipse" Fill="{Binding Path=EllipseBrush}" />
</Grid>
</Window>

后面的代码:MainWindow.xaml.cs

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ellipse.DataContext = new MyCommPort();
}
}

不要通过在按钮单击事件中具有while(true)来挂起 UI 线程。使用调度计时器每 500 秒执行一次工作。

System.Windows.Threading.DispatcherTimer MyTimer = null;
private void ReceiveData()
{
MyTimer = new System.Windows.Threading.DispatcherTimer();
MyTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
MyTimer.Tick += MyTimer_Tick;
MyTimer.Start();
SerialPort serialPort = new SerialPort("COM1", 9600);
serialPort.DataReceived += new 
SerialDataReceivedEventHandler(DataReceivedHandler);
serialPort.Open();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
if (indata.Contains(" "))
{
testString = indata.Substring(0, 4);
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ReceiveData();
}
private void MyTimer_Tick(object sender, EventArgs e)
{
if (testString == "x1-1")
{
x1.Fill = Brushes.Blue;
}
else 
{
x1.Fill = Brushes.Red;
}
}

相关内容

  • 没有找到相关文章

最新更新