资讯详情

用C#编写一个串口助手接收温湿度、烟雾浓度传感器数据(cc2530)

C#做串口调试助手

新建工程

选择visual c#---->窗体应用程序

界面设计:拖一个textbox(文本框)控件,两个label三个控件(标签)button(按钮)控件,两个combobox(组合框)控件。combobox(组合框)控件在右下角的属性中找到items点击集合输入您的波特率值。

修改label、button属性中的Text,输入您想要的内容,调整Textbox。

需要使用计算机上的串口号来获取电脑上的串口号serialPort控件,直接从工具箱中拖拽过来。

.界面完成后,代码开始敲击。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports;//头文件  namespace WindowsFormsApp1 {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();             CheckForIllegalCrossThreadCalls = false;             serialPort1.DataReceived  = new SerialDataReceivedEventHandler(port_DataReceived);             serialPort1.Encoding = Encoding.GetEncoding("GB2312");             System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;         }           /// /数据接收部分         private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//接收函数  SerialDataReceivedEventArgs写错,导致错误,谨记         {             try             {                 string recive_data;                 recive_data = serialPort1.ReadExisting();                 textBox1.AppendText(recive_data);                 textBox1.AppendText("\r\n");             }             catch { }         }          private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)         {          }          private void Form1_Load(object sender, EventArgs e)         {          }          /// ///搜索串口部分         private void button1_Click(object sender, EventArgs e)         {             SearchAnAddSerialToComboBox(serialPort1, comboBox1);         }         private void SearchAnAddSerialToComboBox(SerialPort MyPort, ComboBox MyBox)///搜索串口函数         { ///添加可用的串口号ComboBox             string[] NmberOfport = new string[20];//最多容纳20张,会卡太多,影响效率             string MidString1;//中间数组,用于缓存             MyBox.Items.Clear();//清空combobox的内容             for (int i = 1; i < 20; i  )             {                 try //核心靠try和catch 完成遍历                 {                     MidString1 = "COM"   i.ToString();  ///给串口名字MidString1                     MyPort.PortName = MidString1;       //把MidString1赋给 MyPort.PortName                      MyPort.Open();                      //如果失败,后代码不执行??                     NmberOfport[i - 1] = MidString1;    //依次把MidString1的字符赋给NmberOfport                     MyBox.Items.Add(MidString1);        ///成功打开,添加到下列表中                     MyPort.Close();                     //关闭                     MyBox.Text = NmberOfport[i - 1];    //显示最终扫描成功的串口                 }                 catch { };             }         }            /// 打开串口部分         private void button2_Click(object sender, EventArgs e)         {             if (button2.Text == "打开串口")//0时,表示关闭,此时可以打开操作             {                 try                 {                     serialPort1.PortName = comboBox1.Text;//获取端口号                     serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);//设置波特率                     serialPort1.Open();//打开串口                     button2.Text = " 关闭串口";                 }                 catch                 {                     MessageBox.Show("打开串口错误");                 }             }             else  //为1时,表示开启,此时可进行关闭操作             {                 try                 {                     serialPort1.Close();//关闭串口                     button2.Text = "打开串口";//位置为0,表示状态为关闭                 }                 catch { }             }         }         /// <summary>         /// /清空显示部分         /// </summary>         /// <param name="sender"></param>         /// <param name="e"></param>         private void button3_Click(object sender, EventArgs e)         {             textBox1.Clear();         }     } } 

以下是程序源码自取

http://链接:https://pan.baidu.com/s/13H5UBFEz_AW8k8Dpzu4BNA?pwd=hxy6 提取码:hxy6

标签: 传感器pwd12

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台