找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3447|回复: 2
打印 上一主题 下一主题
收起左侧

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, 下载次数: 87)

51hei.png

BBT6.zip

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

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:635564 发表于 2020-10-25 13:39 | 只看该作者
程序运行正常,刚好用到串口学习一下,非常感谢楼主分享这么好的资料。
回复

使用道具 举报

板凳
ID:794979 发表于 2020-10-27 09:27 | 只看该作者
用的vc还是vs
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表