运行界面:
全部资料51hei下载地址:
2、COM_HC05_BCM417_1.zip
(2.18 MB, 下载次数: 57)
源码预览:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Diagnostics;
- using System.Diagnostics.PerformanceData;
- using System.Threading;
- namespace COM_A
- {
- public partial class analysis : Form
- {
- Form1 myFrm=new Form1();
- public string myStr1;
- public int myFlag=0;
- public analysis()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- myFrm.Show();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- myFrm.sendData("AT\r\n");
- myFlag = 1;
- }
- private void analysis_Load(object sender, EventArgs e)
- {
- Control.CheckForIllegalCrossThreadCalls = false;
- //this.pictureBox1.Image = Image.FromFile(@"C:\Users\nxzh\Desktop\2.jpg");
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\2.jpg");//bin文件中
- this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\2.jpg");//bin文件中
- //开始执行后台操作
- //backgroundWorker1.RunWorkerAsync();
- //多线程处理
- Thread thread = new Thread(CrossThreadFlush);
- thread.IsBackground = true;
- thread.Start();
- }
- private void CrossThreadFlush()
- {
- while (true)
- {
- //将sleep和无限循环放在等待异步的外面
- Thread.Sleep(10);
- ThreadFunction();
- }
- }
- private delegate void FlushClient();//代理
- private void ThreadFunction()//多线程处理
- {
- //try
- {
- if ((this.txbBAUD.InvokeRequired) || (this.txbNameIn.InvokeRequired) || (this.txbNameOut.InvokeRequired) || (this.textBox4.InvokeRequired) || (this.textBox5.InvokeRequired) || (this.textBox6.InvokeRequired))//等待异步
- {
- FlushClient fc = new FlushClient(ThreadFunction);
- try
- {
- this.Invoke(fc);//通过代理调用刷新方法
- }
- catch
- { }
- }
- else
- {
- //this.txbBAUD.Text = DateTime.Now.ToString();
- analysisData(myFrm.myStr);
- }
- }
- //catch
- { }
- }
- /*private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
- {
- while (true)
- {
- #region
- /*this.Invoke((EventHandler)(delegate
- {
- try
- {
- if (myFrm.myStr == "OK\r\n")
- {
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\green.png");
- }
- else
- {
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- }
- if (myFrm.myStr == "OK:" + comboBox1.Text + "\r\n")
- {
- this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\green.png");
- }
- else
- {
- this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- }
- }
- catch
- {
- }
- }));//
- //多线程使用时调用
- #endregion
- //analysisData(myFrm.myStr);
- }
- }*/
- public void analysisData(string str)//解析函数
- {
- if ((str == "OK\r\n"))
- {
- if (myFlag == 1)
- {
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\green.png");
- this.pictureBox5.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- this.pictureBox6.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- }
- else if (myFlag == 2)
- {
- this.pictureBox5.Image = Image.FromFile(Application.StartupPath + "\\green.png");
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- this.pictureBox6.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- }
- else if (myFlag == 3)
- {
- this.pictureBox6.Image = Image.FromFile(Application.StartupPath + "\\green.png");
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- this.pictureBox5.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- }
- }
- else
- {
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- this.pictureBox5.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- this.pictureBox6.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- }
- if (str == "OK:" + myStr1 + "\r\n")
- {
- this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\green.png");
- txbBAUD.Text = myStr1;
- }
- else
- {
- this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- txbBAUD.Text = "";
- }
- if (str == "OK:" + txbNameIn.Text.ToString() + "\r\n")
- {
- this.pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\green.png");
- txbNameOut.Text = txbNameIn.Text;
- }
- else
- {
- this.pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- txbNameOut.Text = "";
- }
- if (str == "OK:" + textBox4.Text.ToString() + "\r\n")
- {
- this.pictureBox4.Image = Image.FromFile(Application.StartupPath + "\\green.png");
- textBox5.Text = textBox4.Text;
- }
- else
- {
- this.pictureBox4.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- textBox5.Text = "";
- }
- if ((str == "OK:M\r\n") || (myFrm.myStr == "OK:S\r\n"))
- {
- this.pictureBox7.Image = Image.FromFile(Application.StartupPath + "\\green.png");
- textBox6.Text = comboBox2.Text;
- }
- else
- {
- this.pictureBox7.Image = Image.FromFile(Application.StartupPath + "\\red.png");
- textBox6.Text = "";
- }
- }
- private void analysis_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (DialogResult.Yes == MessageBox.Show("程序正在使用中,确认退出?", "确认退出", MessageBoxButtons.YesNo))
- {
- //MessageBox.Show("立即退出!");
- e.Cancel = false;
- }
- else
- {
- e.Cancel = true;
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- string myTempStr;
- switch (cbxBAUD.SelectedIndex)
- {
- case 9:
- myTempStr = "A";
- break;
- case 10:
- myTempStr = "B";
- break;
- case 11:
- myTempStr = "C";
- break;
- default:
- myTempStr = (cbxBAUD.SelectedIndex + 1).ToString();
- break;
- }
- //MessageBox.Show(comboBox1.SelectedIndex.ToString());
- myFrm.sendData("AT+BAUD:" + myTempStr + "\r\n");
- myStr1 = this.cbxBAUD.Text;
- }
- private void button4_Click(object sender, EventArgs e)
- {
- if (txbNameIn.Text.Length < 20)
- {
- myFrm.sendData("AT+NAME:" + txbNameIn.Text.ToString() + "\r\n");
- }
- else
- {
- MessageBox.Show("蓝牙名称超过20个字符","提示");
- }
- }
- private void button5_Click(object sender, EventArgs e)
- {
- if (textBox4.Text.Length ==4)
- {
- myFrm.sendData("AT+PIN:" + textBox4.Text.ToString() + "\r\n");
- }
- else
- {
- MessageBox.Show("蓝牙密码不等于4个字符", "提示");
- }
- }
- private void button6_Click(object sender, EventArgs e)
- {
- myFlag = 2;
- myFrm.sendData("AT+PAIR\r\n");
- }
- private void button7_Click(object sender, EventArgs e)
- {
- myFrm.sendData("AT+DEFAULT\r\n");
- myFlag = 3;
- }
- private void button8_Click(object sender, EventArgs e)
- {
- if (comboBox2.Text == "主模式")
- {
- myFrm.sendData("AT+ROLE=M\r\n");
- }
- else if (comboBox2.Text == "从模式")
- {
- myFrm.sendData("AT+ROLE=S\r\n");
- }
- }
- }
- }
复制代码
|