運行界面:
0.png (11.9 KB, 下載次數: 52)
下載附件
2018-6-14 22:27 上傳
0.png (40.51 KB, 下載次數: 59)
下載附件
2018-6-14 22:21 上傳
全部資料51hei下載地址:
2、COM_HC05_BCM417_1.zip
(2.18 MB, 下載次數: 59)
2018-6-14 20:54 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
源碼預覽:
- 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");
- }
- }
- }
- }
復制代碼
|