C# WPF 串口收发
private void LoadCellRighhtSerialPort_receivedStringEvent(object sender, SerialPortData e)
{
//错误出现在这里,因为 BeginInvoke会阻塞线程,造成写串口的时候,串口一直在等待接收数据
//解决方案是:Dispatcher.Invoke(()=>this.xtLog.Text += e.Data));
Dispatcher.BeginInvoke(new Action(() => { this.xtbLog.Text += e.Data; }));
g_queueValue.Enqueue(RegExForIntData(e.Data));
if(g_queueValue.Count>=5) //发送十次数据接收,但不保证每次都有反馈,所以有效五次后就要固定数值
{
this.g_KnobForceCheckItem.InTimeVal = g_queueValue.Max();
this.g_KnobForceCheckItem.KnobResult = this.g_KnobForceCheckItem.InTimeVal > this.g_KnobForceCheckItem.MinVal
&& this.g_KnobForceCheckItem.InTimeVal < this.g_KnobForceCheckItem.MaxVal;
this.FinalResult = this.g_KnobForceCheckItem.KnobResult;
if (!this.FinalResult)
this.DefectInfo = $"当前值{this.g_KnobForceCheckItem.InTimeVal}---检测范围: {this.g_KnobForceCheckItem.MinVal}~{this.g_KnobForceCheckItem.MaxVal}";
canUpdateData = false;
}
}
在C# WPF应用中进行串口通信时,可能会遇到'由于线程退出或应用程序请求,已放弃IO操作'的错误。本文将探讨该问题的成因及提供解决方案,帮助开发者顺利实现串口的稳定收发。
4436

被折叠的 条评论
为什么被折叠?



