串行端口C#,WPF中的数据接收循环 [英] Data Receie Loop in Serial Port C#,WPF

查看:98
本文介绍了串行端口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 .

推荐答案

您已经获得了SerialPort类的MSDN链接,作为对上一页的答案问题 [ ^ ].

对于数据接收,请查看 DataReceived [ ^ ]事件.收到数据时将触发.然后,您可以检查有多少数据可用并读取.链接中提供了一个示例.
You''ve been given the MSDN link for the SerialPort class as an answer to your previous question[^].

For data reception, have a look at the DataReceived[^] event. It fires when data is received. You then can check how much data is available and read it. An example for that is given in the link.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆