• 【CC2530入门教程-增强版】基础技能综合实训案例(基础版)-上位机源码


    【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();
            } 
        }
    }
  • 相关阅读:
    SSL证书指令
    重启机器解决SSL都要输入密码问题
    Nginx + Apache 反向代理
    Ubuntu Nginx安装
    Nginx配置文件详解
    Linux SSL 双向认证 浅解
    SSL 双向认证
    linux ssl 双向认证
    ubuntu apache2 ssl配置
    vi编辑器命令
  • 原文地址:https://www.cnblogs.com/ALittleBee/p/7260451.html
Copyright © 2020-2023  润新知