WPF中的数据接收循环

WPF中的数据接收循环

本文介绍了串行端口C#,WPF中的数据接收循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SerialPort sp;

SerialPort sp;

 Public string[] portnames=SerialPort.GetPortNames();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           foreach (string s in portnames)
            {
                comport.ItemsSource = OrderedPortNames();
            }
        }

        private void Read_Click(object sender, RoutedEventArgs e)
        {

sp = new System.IO.Ports.SerialPort(comport.Text, Convert.ToInt32(baud.Text), Parity.None, Convert.ToInt32(bitrat.Text), StopBits.One);
 sp.DataReceived += new SerialDataReceivedEventHandler(receive_data);

     private void receive_data (object sender, SerialDataReceivedEventArgs e)
        {
           string data = sp.ReadExisting();
            MyLog(data);
        }

       private void MyLog(string msg)
        {
            this.Dispatcher.Invoke(new DisplayTextDelegate(DisplayText), data);
        }

        private void DisplayText(string text)
        {
            textBox1.AppendText(text);
        }

        private void write_Click(object sender, RoutedEventArgs e)
        {
            sp.Write(textBox2.Text);
            textBox2.Text = "";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (sp.IsOpen)
                sp.Close();
        }




这是我为Serialport数据接收和发送编写的一个小型Chat程序,我的问题是我们是否从一个COM端口向另一个COM端口发送数据,另一个COMport应该连续读取,例如,如果我在另一个COM端口发送"HELLO"阅读"HELLO"连续无限制,给出一个复选框来停止循环,请使用Threading可以提供任何帮助,我使用TextBoxes发送和接收数据.




This is a small Chat program i wrote for Serialport Data Receive and Send,my question is if we send data from one COM Port to other ,other COMport should read continuously ,for example if i send ''HELLO'' ,in other COM Port read ''HELLO'' continuous un limited,giving one checkbox to stop loop, using Threading can any one help please, i used TextBoxes for sending and receiving data .

推荐答案


这篇关于串行端口C#,WPF中的数据接收循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:11