标题:C#串口通信触发不了DataReceived事件
只看楼主
m564522634
Rank: 2
等 级:论坛游民
帖 子:34
专家分:16
注 册:2010-9-1
结帖率:60%
已结贴  问题点数:20 回复次数:2 
C#串口通信触发不了DataReceived事件
public void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)  //接收中断
        {
            int receive_length = SerialPort1.BytesToRead;
            byte[] receive_data = new byte[receive_length];
            //receive_data = Convert.FromBase64String(SerialPort1.ReadLine());
            //txt_receive.Text = Encoding.Unicode.GetString(receive_data);
            SerialPort1.Read(receive_data, 0, receive_length);
         
                string str = "";
                for (int i = 0; i < receive_length; i++)
                {
                    str += receive_data[i].ToString("X2") + " ";
                }
                this.Invoke(interfaceUpdateHandle, new string[] { str });
        }
  private void SerierCom_Load(object sender, EventArgs e)
        {
            //默认串口值
            combobox_com.SelectedIndex = 0;
            combobox_baudrate.SelectedIndex = 3;
            combobox_verify.SelectedIndex = 1;
            combobox_databit.SelectedIndex = 0;
            combobox_stopbit.SelectedIndex = 0;

            SerialPort1.PortName = "COM1";
            SerialPort1.BaudRate = 9600;
            SerialPort1.Parity =
            SerialPort1.Open();
            //public event HandleInterfaceUpdateDelegate SerialPort1_DataReceived;
            SerialPort1.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);//注册事件
            //HandleInterfaceUpdateDelegate interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox);
            //Control.CheckForIllegalCrossThreadCalls = false;//屏蔽非法跨线程调用将始终引发异常
            SerialPort1.ReceivedBytesThreshold = 1;
            //thread1 = new Thread(new ThreadStart(UpdateTextBox));
            //thread1.Start();
        }
搜索更多相关主题的帖子: public 通信 
2013-12-12 15:39
m564522634
Rank: 2
等 级:论坛游民
帖 子:34
专家分:16
注 册:2010-9-1
得分:0 
没有人回复,自己顶一个了看来是要
2013-12-12 16:02
zjsxwc
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:252
专家分:601
注 册:2011-1-20
得分:20 
http://msdn.

The tools I recommended:
GUI: CSharp(VS), QT;    Core Code: Plain C (Tiny C Compiler);    Web: Python, JavaScript;    Android: Java;    Embedded System: ASM&C (Linux)
2013-12-16 10:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-425109-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046917 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved