找回密码
 立即注册

QQ登录

只需一步,快速开始

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

C#写的串口设置HC05_BCM417蓝牙上位机源码

[复制链接]
跳转到指定楼层
楼主
运行界面:




全部资料51hei下载地址:
2、COM_HC05_BCM417_1.zip (2.18 MB, 下载次数: 57)


源码预览:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. using System.Diagnostics.PerformanceData;
  11. using System.Threading;

  12. namespace COM_A
  13. {
  14.     public partial class analysis : Form
  15.     {
  16.         Form1 myFrm=new Form1();
  17.         public string myStr1;
  18.         public int myFlag=0;

  19.         public analysis()
  20.         {
  21.             InitializeComponent();
  22.         }

  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             myFrm.Show();
  26.         }

  27.         private void button2_Click(object sender, EventArgs e)
  28.         {
  29.             myFrm.sendData("AT\r\n");
  30.             myFlag = 1;

  31.         }



  32.         private void analysis_Load(object sender, EventArgs e)
  33.         {
  34.             Control.CheckForIllegalCrossThreadCalls = false;
  35.             //this.pictureBox1.Image = Image.FromFile(@"C:\Users\nxzh\Desktop\2.jpg");
  36.             this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\2.jpg");//bin文件中
  37.             this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\2.jpg");//bin文件中
  38.             //开始执行后台操作
  39.             //backgroundWorker1.RunWorkerAsync();
  40.             //多线程处理
  41.             Thread thread = new Thread(CrossThreadFlush);
  42.             thread.IsBackground = true;
  43.             thread.Start();
  44.         }

  45.         private void CrossThreadFlush()
  46.         {
  47.             while (true)
  48.             {
  49.                 //将sleep和无限循环放在等待异步的外面
  50.                 Thread.Sleep(10);
  51.                 ThreadFunction();
  52.             }
  53.         }

  54.         private delegate void FlushClient();//代理

  55.         private void ThreadFunction()//多线程处理
  56.         {
  57.             //try
  58.             {
  59.                 if ((this.txbBAUD.InvokeRequired) || (this.txbNameIn.InvokeRequired) || (this.txbNameOut.InvokeRequired) || (this.textBox4.InvokeRequired) || (this.textBox5.InvokeRequired) || (this.textBox6.InvokeRequired))//等待异步
  60.                 {
  61.                     FlushClient fc = new FlushClient(ThreadFunction);
  62.                     try
  63.                     {
  64.                         this.Invoke(fc);//通过代理调用刷新方法
  65.                     }
  66.                     catch
  67.                     { }

  68.                 }
  69.                 else
  70.                 {
  71.                     //this.txbBAUD.Text = DateTime.Now.ToString();
  72.                     analysisData(myFrm.myStr);
  73.                 }
  74.             }
  75.             //catch
  76.             { }

  77.         }

  78.         /*private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  79.         {
  80.             while (true)
  81.             {
  82.                 #region
  83.                 /*this.Invoke((EventHandler)(delegate
  84.                 {
  85.                     try
  86.                     {
  87.                         if (myFrm.myStr == "OK\r\n")
  88.                         {
  89.                             this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\green.png");

  90.                         }
  91.                         else
  92.                         {
  93.                             this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  94.                         }

  95.                         if (myFrm.myStr == "OK:" + comboBox1.Text + "\r\n")
  96.                         {
  97.                             this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\green.png");
  98.                         }
  99.                         else
  100.                         {
  101.                             this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  102.                         }
  103.                     }
  104.                     catch
  105.                     {

  106.                     }

  107.                 }));//
  108.                 //多线程使用时调用
  109.                 #endregion
  110.                 //analysisData(myFrm.myStr);

  111.            }
  112.         }*/

  113.         public void analysisData(string str)//解析函数
  114.         {
  115.             if ((str == "OK\r\n"))
  116.             {
  117.                 if (myFlag == 1)
  118.                 {
  119.                     this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\green.png");
  120.                     this.pictureBox5.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  121.                     this.pictureBox6.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  122.                 }
  123.                 else if (myFlag == 2)
  124.                 {
  125.                     this.pictureBox5.Image = Image.FromFile(Application.StartupPath + "\\green.png");
  126.                     this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  127.                     this.pictureBox6.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  128.                 }
  129.                 else if (myFlag == 3)
  130.                 {
  131.                     this.pictureBox6.Image = Image.FromFile(Application.StartupPath + "\\green.png");
  132.                     this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  133.                     this.pictureBox5.Image = Image.FromFile(Application.StartupPath + "\\red.png");

  134.                 }

  135.             }
  136.             else
  137.             {
  138.                 this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  139.                 this.pictureBox5.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  140.                 this.pictureBox6.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  141.             }

  142.             if (str == "OK:" + myStr1 + "\r\n")
  143.             {
  144.                 this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\green.png");
  145.                 txbBAUD.Text = myStr1;
  146.             }
  147.             else
  148.             {
  149.                 this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  150.                 txbBAUD.Text = "";
  151.             }
  152.             if (str == "OK:" + txbNameIn.Text.ToString() + "\r\n")
  153.             {
  154.                 this.pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\green.png");
  155.                 txbNameOut.Text = txbNameIn.Text;
  156.             }
  157.             else
  158.             {
  159.                 this.pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  160.                 txbNameOut.Text = "";
  161.             }

  162.             if (str == "OK:" + textBox4.Text.ToString() + "\r\n")
  163.             {
  164.                 this.pictureBox4.Image = Image.FromFile(Application.StartupPath + "\\green.png");
  165.                 textBox5.Text = textBox4.Text;
  166.             }
  167.             else
  168.             {
  169.                 this.pictureBox4.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  170.                 textBox5.Text = "";
  171.             }

  172.             if ((str == "OK:M\r\n") || (myFrm.myStr == "OK:S\r\n"))
  173.             {
  174.                 this.pictureBox7.Image = Image.FromFile(Application.StartupPath + "\\green.png");
  175.                 textBox6.Text = comboBox2.Text;
  176.             }
  177.             else
  178.             {
  179.                 this.pictureBox7.Image = Image.FromFile(Application.StartupPath + "\\red.png");
  180.                 textBox6.Text = "";
  181.             }
  182.         }

  183.         private void analysis_FormClosing(object sender, FormClosingEventArgs e)
  184.         {
  185.             if (DialogResult.Yes == MessageBox.Show("程序正在使用中,确认退出?", "确认退出", MessageBoxButtons.YesNo))
  186.             {
  187.                 //MessageBox.Show("立即退出!");

  188.                 e.Cancel = false;
  189.             }
  190.             else
  191.             {
  192.                 e.Cancel = true;
  193.             }
  194.         }

  195.         private void button3_Click(object sender, EventArgs e)
  196.         {
  197.             string myTempStr;
  198.             switch (cbxBAUD.SelectedIndex)
  199.             {
  200.                 case 9:
  201.                     myTempStr = "A";
  202.                     break;
  203.                 case 10:
  204.                     myTempStr = "B";
  205.                     break;
  206.                 case 11:
  207.                     myTempStr = "C";
  208.                     break;
  209.                 default:
  210.                     myTempStr = (cbxBAUD.SelectedIndex + 1).ToString();
  211.                     break;
  212.             }
  213.             //MessageBox.Show(comboBox1.SelectedIndex.ToString());
  214.             myFrm.sendData("AT+BAUD:" + myTempStr + "\r\n");
  215.             myStr1 = this.cbxBAUD.Text;

  216.         }

  217.         private void button4_Click(object sender, EventArgs e)
  218.         {
  219.             if (txbNameIn.Text.Length < 20)
  220.             {
  221.                 myFrm.sendData("AT+NAME:" + txbNameIn.Text.ToString() + "\r\n");
  222.             }
  223.             else
  224.             {
  225.                 MessageBox.Show("蓝牙名称超过20个字符","提示");
  226.             }

  227.         }

  228.         private void button5_Click(object sender, EventArgs e)
  229.         {
  230.             if (textBox4.Text.Length ==4)
  231.             {
  232.                 myFrm.sendData("AT+PIN:" + textBox4.Text.ToString() + "\r\n");
  233.             }
  234.             else
  235.             {
  236.                 MessageBox.Show("蓝牙密码不等于4个字符", "提示");
  237.             }
  238.         }

  239.         private void button6_Click(object sender, EventArgs e)
  240.         {
  241.             myFlag = 2;
  242.             myFrm.sendData("AT+PAIR\r\n");
  243.         }

  244.         private void button7_Click(object sender, EventArgs e)
  245.         {
  246.             myFrm.sendData("AT+DEFAULT\r\n");
  247.             myFlag = 3;
  248.         }

  249.         private void button8_Click(object sender, EventArgs e)
  250.         {
  251.             if (comboBox2.Text == "主模式")
  252.             {
  253.                 myFrm.sendData("AT+ROLE=M\r\n");
  254.             }
  255.             else if (comboBox2.Text == "从模式")
  256.             {
  257.                 myFrm.sendData("AT+ROLE=S\r\n");
  258.             }
  259.         }
  260.     }
  261. }
复制代码


评分

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

查看全部评分

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

使用道具 举报

沙发
ID:393156 发表于 2018-9-3 20:15 | 只看该作者
很详细啊,学习学习
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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