wpf - WPF 和 WinForm SerialPort 控制通过 RS232 的区别?
问题描述
我用 WPF 和 WinForm 测试了 2 个几乎相同的代码,分别通过 USB 和 RS232 进行串行端口通信。WPF 和 Winform 都可以通过 USB 端口正常工作。但我无法使用 WPF 接收来自 RS232 端口的响应,而 WinForm 没有问题。附上代码。WPF和Winform通过Seiralport通过RS232进行通信有什么区别吗?
pmSerialPort 至 USB lsSerialPort 至 RS232
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
DispatcherTimer dtTimer = new DispatcherTimer();
SerialPort lsSerialPort = new SerialPort();
SerialPort pmSerialPort = new SerialPort();
private void Window_Initialized(object sender, EventArgs e)
{
cbPMPort.SelectedIndex = 0;
cbModel.SelectedIndex = 0;
pmSerialPort.PortName = "COM14";
lsSerialPort.PortName = "COM2";
}
//Status check
private void BtnStatus_Click(object sender, RoutedEventArgs e)
{
pmSerialPort.Close();
laserSerialPort.Close();
try
{
ComboBoxItem cbItem = (ComboBoxItem)cbPMPort.SelectedItem;
pmSerialPort.PortName = cbItem.Content.ToString();
pmSerialPort.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message");
}
try
{
lsSerialPort.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message");
}
if (lsSerialPort.IsOpen)
{
stsLsPort.Background = new SolidColorBrush(Colors.Green);
txtBlkLsPort.Text = "Laser COMPort is Open!";
}
else
{
stsLaserPort.Background = new SolidColorBrush(Colors.Red);
txtBlkLsPort.Text = "Laser COMPort is NOT Open!";
}
if (pmSerialPort.IsOpen)
{
stsPMPort.Background = new SolidColorBrush(Colors.Green);
txtBlkPM.Text = "PM COMPort is Open!";
}
else
{
stsPMPort.Background = new SolidColorBrush(Colors.Red);
txtBlkPM.Text = "PM COMPort is NOT Open!";
}
string strStsLs;
double stsLs;
lsSerialPort.WriteLine("RD" + Environment.NewLine);
lsSerialPort.WriteLine("{Enter}");
txtLsStatus.Text = lsSerialPort.ReadExisting().Trim();
if (lsPort.IsOpen)
{
stsLsPort.Background = new SolidColorBrush(Colors.Green);
txtBlkLsPort.Text = "Ls COMPort is Open!";
}
else
{
stsLsPort.Background = new SolidColorBrush(Colors.Red);
txtBlkLsPort.Text = "Ls COMPort is NOT Open!";
}
if (pmSerialPort.IsOpen)
{
stsPMPort.Background = new SolidColorBrush(Colors.Green);
txtBlkPM.Text = "PM COMPort is Open!";
}
else
{
stsPMPort.Background = new SolidColorBrush(Colors.Red);
txtBlkPM.Text = "PM COMPort is NOT Open!";
}
}
private async void BtnStart_Click(object sender, RoutedEventArgs e)
{
if (pmSerialPort.IsOpen && lsSerialPort.IsOpen)
{
pmSerialPort.BaudRate = 115200;
pmSerialPort.WriteLine("*CU");
await System.Threading.Tasks.Task.Delay(3000);
lsSerialPort.WriteLine("WG0" + Environment.NewLine);
txt1WG0.Text = lsSerialPort.ReadExisting();
await System.Threading.Tasks.Task.Delay(3000);
MessageBox.Show("Please check connection", "Action Needed");
await System.Threading.Tasks.Task.Delay(3000);
txt2BNCDC.Text = "P";
await System.Threading.Tasks.Task.Delay(1000);
lsSerialPort.WriteLine("WU1);
await System.Threading.Tasks.Task.Delay(3000);
txt3WU1.Text = lsSerialPort.ReadExisting();
await System.Threading.Tasks.Task.Delay(3000);
pb.Value = 2;
await System.Threading.Tasks.Task.Delay(3000);
lsSerialPort.WriteLine("WU2");
txt4WU2.Text = lsSerialPort.ReadExisting();
await System.Threading.Tasks.Task.Delay(3000);
lsSerialPort.WriteLine("WU3");
txt5WU3.Text = lsSerialPort.ReadExisting().Trim();
await System.Threading.Tasks.Task.Delay(3000);
lsSerialPort.WriteLine("WU4");
txt6WU4.Text = lsSerialPort.ReadExisting().Trim();
await System.Threading.Tasks.Task.Delay(3000);
pb.Value = 4;
await System.Threading.Tasks.Task.Delay(3000);
pmSerialPort.WriteLine("*UV");
txt9LPPower.Text = pmSerialPort.ReadExisting().Trim();
pb.Value = 10;
}
else
{
MessageBox.Show("Please click Check Status for details");
}
解决方案
推荐阅读
- xcode - 如何在命令行 xcode 构建中排除特定架构
- flexbox - Quasar Grid 左对齐最后一行的项目
- amazon-web-services - 从 AWS RDS 实例访问从 AWS RDS 快照导出到 S3 的数据库数据
- google-analytics - 为什么带有包含“gclid”的查询字符串的传入服务器请求格式错误?
- vue.js - Vuetify - 使用 URL 参数同步分页
- java - Kafka No Acknowledgement 可用作参数
- python - PyDrake:从机器人中提取 AutoDiff 梯度(通过微分逆运动学控制)
- php - Laravel PHP以特定方式返回数组
- c# - MVC SPA 应用程序
- .net - 在 Azure 中发布:不匹配依赖项错误。是什么导致它以及在通过管道使用发布时如何解决它?