【CC2530入門教程-增強版】基礎技能綜合實訓案例(基礎版)-上位機源碼
廣東職業技術學院 歐浩源
一、需求分析
按照指定參數打開串口,與測控終端建立數據傳輸通道,並根據應用要求實現程序邏輯,具體需求詳見《【CC2530入門教程-增強版】基礎技能綜合實訓案例(基礎版)-題目需求》。
二、界面設計
三、程序源碼分析
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.Threading; using System.IO.Ports; namespace 基礎技能綜合實訓_基礎版_ { public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort com = new SerialPort(); //實例化一個串口對象 byte[] SendData = new byte[8]; //定義一個8字節的發送數據緩存 byte[] readBuffer = new byte[8]; //實例化接收串口數據的數組 private void Form1_Load(object sender, EventArgs e) { string[] ports = { "COM1", "COM2", "COM3", "COM4", "COM5" }; foreach (string str in ports) { comboBox1.Items.Add(str); } comboBox1.SelectedIndex = 2; string[] baudrate = { "2400", "4800", "9600", "19200", "57600", "115200" }; foreach (string str in baudrate) { comboBox2.Items.Add(str); } comboBox2.SelectedIndex = 2; comboBox3.Items.Add("6"); comboBox3.Items.Add("7"); comboBox3.Items.Add("8"); comboBox3.SelectedIndex = 2; comboBox4.Items.Add("1"); comboBox4.Items.Add("1.5"); comboBox4.Items.Add("2"); comboBox4.SelectedIndex = 0; comboBox5.Items.Add("None"); comboBox5.SelectedIndex = 0; button2.Enabled = false; button3.Enabled = false; button4.Enabled = false; textBox1.ReadOnly = true; label14.Text = "終端未連接"; label14.ForeColor = Color.Red; label9.Text = "0.00" + " V"; label9.ForeColor = Color.Blue; label12.Text = ""; label13.Text = ""; label7.Text = "串口未連接!"; label7.ForeColor = Color.Red; com.ReceivedBytesThreshold = 8; //設置串口接收到8個字節數據才觸發DataReceived事件 //為串口DataReceived事件添加處理方法 com.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); } //串口數據接收DataReceived事件觸發處理方法 private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { string strRcv = ""; int count = com.BytesToRead; //獲取串口緩沖器的字節數 if (count != 8) { return; } com.Read(readBuffer, 0, 8); //從串口緩沖區讀出數據到數組 com.DiscardInBuffer(); for (int i = 0; i < readBuffer.Length; i++) { strRcv += readBuffer[i].ToString("X2") + " "; //16進制顯示 } this.BeginInvoke(new Action(() => { textBox1.Text = strRcv; })); if (readBuffer[0] == 0xAF && readBuffer[7] == 0xFA) //判斷數據的幀頭和幀尾 { this.BeginInvoke(new Action(() => { switch (readBuffer[2]) { case 0x10: label14.Text = string.Format("{0}號終端在線", readBuffer[1]); label14.ForeColor = Color.Green; button4.Enabled = false; button2.Enabled = true; button3.Text = "打開照明燈"; button2.Text = "開始采集數據"; label12.Text = "關閉"; label12.ForeColor = Color.Red; label13.Text = "關閉"; label13.ForeColor = Color.Red; break; case 0x11: Int32 ad = readBuffer[3]; double advalue; ad <<= 8; ad |= readBuffer[4]; //從數據幀中將電壓數據取出 advalue = ad; advalue = (advalue * 3.3) / 32768; //將數據換算為實際的電壓值 label9.Text = advalue.ToString("F2") + " V"; if ((readBuffer[5] & 0x01) == 0x01) { label12.Text = "打開"; label12.ForeColor = Color.Blue; button3.Text = "關閉照明燈"; } else { label12.Text = "關閉"; label12.ForeColor = Color.Red; button3.Text = "打開照明燈"; } if ((readBuffer[5] & 0x02) == 0x02) { label13.Text = "打開"; label13.ForeColor = Color.Blue; } else { label13.Text = "關閉"; label13.ForeColor = Color.Red; } break; case 0x1f: label14.Text = "現場報警!!!"; label14.ForeColor = Color.Red; button4.Enabled = true; button2.Enabled = false; button3.Enabled = false; break; } // com.DiscardInBuffer(); })); } } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "打開串口") { com.PortName = comboBox1.Text; //選擇串口號 com.BaudRate = int.Parse(comboBox2.Text); //選擇波特率 com.DataBits = int.Parse(comboBox3.Text); //選擇數據位數 com.StopBits = (StopBits)int.Parse(comboBox4.Text); //選擇停止位數 com.Parity = Parity.None; //選擇是否奇偶校驗 try { if (com.IsOpen) //判斷該串口是否已打開 { com.Close(); com.Open(); } else { com.Open(); } label7.Text = "串口已成功連接!"; label7.ForeColor = Color.Blue; } catch (Exception ex) { MessageBox.ReferenceEquals("錯誤:" + ex.Message, "串口通信"); } button1.Text = "關閉串口"; } else if (button1.Text == "關閉串口") { com.Close(); //關閉串口 label7.Text = "串口未連接!"; label14.Text = "終端未連接"; label14.ForeColor = Color.Red; label7.ForeColor = Color.Red; button1.Text = "打開串口"; button2.Enabled = false; button3.Enabled = false; button4.Enabled = false; textBox1.Clear(); } } private void SendUartData() { SendData[0] = 0xAF; SendData[3] = 0x00; SendData[4] = 0x00; SendData[5] = 0x00; SendData[6] = 0x00; SendData[7] = 0xFA; for (int i = 1; i < 7; i++) { SendData[6] += SendData[i]; } com.Write(SendData, 0, 8); } private void button2_Click(object sender, EventArgs e) { if (button2.Text == "開始采集數據") { button2.Text = "停止采集數據"; button3.Enabled = true; SendData[1] = 0x01; SendData[2] = 0x01; SendUartData(); } else { button2.Text = "開始采集數據"; button3.Enabled = false; SendData[1] = 0x01; SendData[2] = 0x02; SendUartData(); } } private void button3_Click(object sender, EventArgs e) { if (button3.Text == "打開照明燈") { button3.Text = "關閉照明燈"; SendData[1] = 0x01; SendData[2] = 0x03; SendUartData(); } else { button3.Text = "打開照明燈"; SendData[1] = 0x01; SendData[2] = 0x04; SendUartData(); } } private void button4_Click(object sender, EventArgs e) { SendData[1] = 0x01; SendData[2] = 0x0f; SendUartData(); } } }