wpf - 是否有更有效的方法从串行端口读取值并实时更新图表 - WPF
问题描述
使用实时图表,我正在创建一个实时图表,该图表使用从串行端口读取的值进行更新。现在我可以让它工作,但我认为我没有像我没有使用 C# 和 WPF 那样有效地做到这一点。
我将从串行端口读取的数据存储在 SerialCommunication 类中。然后我使用一个按钮启动一个新任务,该任务打开串行端口并更新我的图表。
我的问题是,我希望能够在每次 Serial 类收到新值时更新图表,但是,我的图表在 Read() 函数中更新,该函数从启动新任务时调用,我觉得这可能会导致线程问题. 任何意见,将不胜感激。
系列类
public class SerialCommunication
{
private string _value;
SerialPort serialPort = null;
public SerialCommunication()
{
InitializeComms();
}
private void InitializeComms()
{
try
{
serialPort = new SerialPort("COM6", 115200, Parity.None, 8, StopBits.One); // Update this to avoid hard coding COM port and BAUD rate
serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
~SerialCommunication()
{
if(serialPort.IsOpen)
serialPort.Close();
}
public void ReceiveData()
{
try
{
if (!serialPort.IsOpen)
serialPort.Open();
}
catch(Exception ex) { MessageBox.Show(ex.Message); }
}
public void StopReceivingData()
{
try
{
if (serialPort.IsOpen)
serialPort.Close();
}
catch(Exception ex) { MessageBox.Show(ex.Message); }
}
public event EventHandler DataReceived;
private void OnDataReceived(EventArgs e)
{
DataReceived?.Invoke(this, e);
}
// read the data in the DataReceivedHandler
// Event Handler
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
try
{
SerialPort sp = (SerialPort)sender;
_value = sp.ReadLine();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
OnDataReceived(EventArgs.Empty);
}
}
Time of Flight 类,它使用从串行端口读取的传感器值更新图表,使用来自 Beto-rodriguez 的 LiveCharts 的代码
public TimeOfFlight()
{
InitializeComponent();
// attach an event handler to update graph
serial.DataReceived += new EventHandler(UpdateChart);
// Use PlotData class for graph data which will use this config every time
var mapper = Mappers.Xy<PlotData>()
.X(model => model.DateTime.Ticks)
.Y(model => model.Value);
// Save mapper globally
Charting.For<PlotData>(mapper);
chartValues = new ChartValues<PlotData>();
//lets set how to display the X Labels
XFormatter = value => new DateTime((long)value).ToString("hh:mm:ss");
YFormatter = x => x.ToString("N0");
//AxisStep forces the distance between each separator in the X axis
AxisStep = TimeSpan.FromSeconds(1).Ticks;
//AxisUnit forces lets the axis know that we are plotting seconds
//this is not always necessary, but it can prevent wrong labeling
AxisUnit = TimeSpan.TicksPerSecond;
SetAxisLimits(DateTime.Now);
//ZoomingMode = ZoomingOptions.X;
IsReading = false;
DataContext = this;
}
public ChartValues<PlotData> chartValues { get; set; }
public Func<double, string> XFormatter { get; set; }
public Func<double, string> YFormatter { get; set; }
public double AxisStep { get; set; }
public double AxisUnit { get; set; }
public double AxisMax
{
set
{
_axisXMax = value;
OnPropertyChanged("AxisMax");
}
get { return _axisXMax; }
}
public double AxisMin
{
set
{
_axisXMin = value;
OnPropertyChanged("AxisMin");
}
get { return _axisXMin; }
}
public bool IsReading { get; set; }
private void StartStopGraph(object sender, RoutedEventArgs e)
{
IsReading = !IsReading;
if (IsReading)
{
serial.ReceiveData();
}
else
serial.StopReceivingData();
}
public void UpdateChart(object sender, EventArgs e) // new task
{
try
{
var now = DateTime.Now;
// can chartValues.Add be called from INotifyPropertyChanged in
// SerialCommunication.cs and would this cause an issue with the
chartValues.Add(new PlotData
{
DateTime = now,
Value = 0 // update this
});
SetAxisLimits(now);
//lets only use the last 150 values
if (chartValues.Count > 1000) chartValues.RemoveAt(0);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void SetAxisLimits(DateTime now)
{
AxisMax = now.Ticks + TimeSpan.FromSeconds(1).Ticks; // lets force the axis to be 1 second ahead
AxisMin = now.Ticks - TimeSpan.FromSeconds(8).Ticks; // and 8 seconds behind
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
绘图数据类
public class PlotData
{
public DateTime DateTime { get; set; }
public int Value { get; set; }
}
解决方案
是的,不是IMO的最佳方式。
首先,我不会对 SerialCommunication 类中的 INotifyPropertyChanged 或业务/视图逻辑做任何事情,从架构的角度来看,它应该做的就是管理串行设备的打开和关闭,并且应该传递任何到达的数据通过事件在您的应用程序的其他部分。
其次,为什么要使用循环?您已经订阅了 DataReceived,因此只需读取 DataReceivedHandler 中的数据并将其传递给所述事件。该处理程序将在不同的线程上调用,但订阅您的事件的处理程序可以在需要时分派到主 GUI 线程。
这让我想到了第三点:如果您正确地进行数据绑定(而不是直接更新您的控件),那么您不需要,只需更新您的数据值并将其留给 WPF 数据绑定引擎根据需要进行更新.
在实际应用程序中,您可能希望以特定间隔进行更新,为此您需要将数据值推送到队列中,然后一次性处理它们。正确的方法是在 C# 中使用异步任务。不要使用线程,绝对不要使用 Thread.Sleep....线程是一种过时的技术,除了一些非常特殊的情况外,它在 C# 中没有位置。
推荐阅读
- php - 在 yii2 activeform 中上传图片
- azure - 将证书导入 Azure Key Vault:密钥在指定状态下无效
- scala - Scala:需要有关从文件中获取属性的更好方法的建议
- kubernetes - Airflow/k8s:如何正确设置存储在持久卷中的 DAG 的权限?
- pdf - 读取并输出pdf文件为文件格式
- powershell - 将用户添加到 WMI 控件并通过 SDDL / powershell 编辑配置
- google-cloud-functions - 有没有办法配置 Google Cloud Function 和 Pub/Sub 并行度?
- python - 如何在一个函数中返回两个数据帧
- java - add 方法只是打印 True。将 Web 元素转换为 List 后如何打印其值
- laravel - Laravel 地图仅适用于第一个循环