标题: C#实现串口通讯源码 [打印本页]

作者: 13566004773    时间: 2020-10-16 13:49
标题: C#实现串口通讯源码

主源码:

  private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 20; i++)
            {
                comboBox1.Items.Add("COM" + i.ToString());
            }
            comboBox1.Text = "COM4"; //串口號默認值
            comboBox2.Text = "9600";  //波特率默認值

            /************非常重要***************/
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); //必須手動添加事件
        }

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)   //串口數據接收事件
        {
            if (!radioButton3.Checked) //如果接收模式為字符模式
            {
                string str = serialPort1.ReadExisting(); //字符串方式讀
                textBox1.AppendText(str);  //添加內容
            }
/*           
              else //如果接收模式为数值接收
                        {
                            //易出现异常:由于线程退出或应用程序请求,已中止 I/O 操作
                            //加入异常处理
                            try
                            {
                                int data;
                                data = serialPort1.ReadByte();
                                string str = Convert.ToString(data, 16).ToUpper();//转换为大写十六进制字符串
                                textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");  //空位補“0”               
                            }
                            catch
                            {
                                this.Close();//关闭当前窗体
                            }

                        }   
    */        

           else
            {   //如果接收模式為數值接收
                byte data;
                data = (byte)serialPort1.ReadByte();  //此處需要強制類型轉換,將(int)類型轉換為(byte類型數據,不
                string str = Convert.ToString(data, 16).ToUpper();  //轉換為大寫十六進制字符串
                textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");  //空位補“0”
                //上一句等同於:
                //          if (str.Length == 1)
                //        {
                //            str = "0" + str;
                //        }
                //          else
                //        {
                //            str = str;
                //            textBox1.AppendText("0x" + str);
                //}
            }   
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = comboBox1.Text;   //設置串口號
                serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10); //十進制數據轉換,設置波特率
                serialPort1.Open();  //打卡串口
                button1.Enabled = false; //打卡串口不可用
                button2.Enabled = true;  //關閉串口

            }
            catch
            {
                MessageBox.Show("端口錯誤,請檢查串口", "錯誤");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Close();  //關閉串口
                button1.Enabled = true;   //打卡串口按鈕可用
                button2.Enabled = false;  //關閉串口按鈕不可用

            }
            catch   //一般情況下關閉串口不會出現錯誤,所有不需要加處理程序
            {
                MessageBox.Show("串口关闭错误", "错误");
            }
        }

51hei.png (4.55 KB, 下载次数: 109)

51hei.png

BBT6.zip

60.17 KB, 下载次数: 88, 下载积分: 黑币 -5


作者: kssxdz    时间: 2020-10-25 13:39
程序运行正常,刚好用到串口学习一下,非常感谢楼主分享这么好的资料。
作者: marchmaqi    时间: 2020-10-27 09:27
用的vc还是vs




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1