首页 > 解决方案 > 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");
            }

标签: wpfwinformsserial-portusb

解决方案


推荐阅读