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

时间:2022-09-15 10:19:17

【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字节的发送数据缓存
byte[] readBuffer = new byte[]; //实例化接收串口数据的数组
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 = ;
string[] baudrate = { "", "", "", "", "", "" };
foreach (string str in baudrate)
{
comboBox2.Items.Add(str);
}
comboBox2.SelectedIndex = ;
comboBox3.Items.Add("");
comboBox3.Items.Add("");
comboBox3.Items.Add("");
comboBox3.SelectedIndex = ;
comboBox4.Items.Add("");
comboBox4.Items.Add("1.5");
comboBox4.Items.Add("");
comboBox4.SelectedIndex = ;
comboBox5.Items.Add("None");
comboBox5.SelectedIndex = ; 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个字节数据才触发DataReceived事件
//为串口DataReceived事件添加处理方法
com.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
} //串口数据接收DataReceived事件触发处理方法
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{ string strRcv = "";
int count = com.BytesToRead; //获取串口缓冲器的字节数
if (count != )
{
return;
}
com.Read(readBuffer, , ); //从串口缓冲区读出数据到数组
com.DiscardInBuffer(); for (int i = ; i < readBuffer.Length; i++)
{
strRcv += readBuffer[i].ToString("X2") + " "; //16进制显示
}
this.BeginInvoke(new Action(() =>
{
textBox1.Text = strRcv;
})); if (readBuffer[] == 0xAF && readBuffer[] == 0xFA) //判断数据的帧头和帧尾
{
this.BeginInvoke(new Action(() =>
{
switch (readBuffer[])
{
case 0x10:
label14.Text = string.Format("{0}号终端在线", readBuffer[]);
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[];
double advalue;
ad <<= ;
ad |= readBuffer[]; //从数据帧中将电压数据取出
advalue = ad;
advalue = (advalue * 3.3) / ; //将数据换算为实际的电压值
label9.Text = advalue.ToString("F2") + " V";
if ((readBuffer[] & 0x01) == 0x01)
{
label12.Text = "打开";
label12.ForeColor = Color.Blue;
button3.Text = "关闭照明灯";
}
else
{
label12.Text = "关闭";
label12.ForeColor = Color.Red;
button3.Text = "打开照明灯";
}
if ((readBuffer[] & 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[] = 0xAF;
SendData[] = 0x00;
SendData[] = 0x00;
SendData[] = 0x00;
SendData[] = 0x00;
SendData[] = 0xFA;
for (int i = ; i < ; i++)
{
SendData[] += SendData[i];
}
com.Write(SendData, , );
} private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "开始采集数据")
{
button2.Text = "停止采集数据";
button3.Enabled = true;
SendData[] = 0x01;
SendData[] = 0x01;
SendUartData();
}
else
{
button2.Text = "开始采集数据";
button3.Enabled = false;
SendData[] = 0x01;
SendData[] = 0x02;
SendUartData();
}
} private void button3_Click(object sender, EventArgs e)
{
if (button3.Text == "打开照明灯")
{
button3.Text = "关闭照明灯";
SendData[] = 0x01;
SendData[] = 0x03;
SendUartData();
}
else
{
button3.Text = "打开照明灯";
SendData[] = 0x01;
SendData[] = 0x04;
SendUartData();
}
} private void button4_Click(object sender, EventArgs e)
{
SendData[] = 0x01;
SendData[] = 0x0f;
SendUartData();
}
}
}

【CC2530入门教程-增强版】基础技能综合实训案例(基础版)-上位机源码的更多相关文章

  1. 【CC2530入门教程-01】CC2530微控制器开发入门基础

    [引言] 本系列教程就有关CC2530单片机应用入门基础的实训案例进行分析,主要包括以下6部分的内容:[1]CC2530微控制器开发入门基础.[2]通用I/O端口的输入和输出.[3]外部中断初步应用. ...

  2. 【CC2530入门教程-01】IAR集成开发环境的建立与项目开发流程

    [引言] 本系列教程就有关CC2530单片机应用入门基础的实训案例进行分析,主要包括以下6部分的内容:1.CC2530单片机开发入门.2.通用I/O端口的输入和输出.3.外部中断初步应用.4.定时/计 ...

  3. 【CC2530入门教程-03】CC2530的中断系统及外部中断应用

    第3课  CC2530的中断系统及外部中断应用 广东职业技术学院  欧浩源 一.中断相关的基础概念  内核与外设之间的主要交互方式有两种:轮询和中断. 轮询的方式貌似公平,但实际工作效率很低,且不能及 ...

  4. CC2530入门教程-02】CC2530的通用I&sol;O端口输入和输出控制

    第2课  CC2530的通用I/O端口输入和输出控制 广东职业技术学院  欧浩源 一.CC2530的引脚概述 CC2530微控制器采用QFN40封装,有40 个引脚.其中,有21个数字I/O端口,其中 ...

  5. 【CC2530入门教程-02】CC2530的通用I&sol;O端口输入和输出控制

    第2课  CC2530的通用I/O端口输入和输出控制 小蜜蜂科教 / 广东职业技术学院  欧浩源 [通用I/O端口视频教程:https://v.qq.com/x/page/x0793aol7us.ht ...

  6. 【专题教程第8期】基于emWin模拟器的USB BULK上位机开发,仅需C即可,简单易实现

    说明:1.如果你会emWin话的,就可以轻松制作上位机.做些通信和控制类上位机,比使用C#之类的方便程度一点不差,而且你仅会C语言就可以.2.并且成功将emWin人性化,可以做些Windows系统上的 ...

  7. 【CC2530入门教程-06】CC2530的ADC工作原理与应用

    第6课  CC2530的ADC工作原理与应用 广东职业技术学院  欧浩源 一.A/D转换的基本工作原理 将时间上连续变化的模拟量转化为脉冲有无的数字量,这一过程就叫做数字化,实现数字化的关键设备是AD ...

  8. 【CC2530入门教程-04】CC2530的定时&sol;计数器原理与应用

    第4课  CC2530的定时/计数器原理与应用 广东职业技术学院  欧浩源 一.定时/技术器的基本原理 定时/计数器,是一种能够对内部时钟信号或外部输入信号进行计数,当计数值达到设定要求时,向CPU提 ...

  9. 【CC2530入门教程-05】CC2530的串行接口原理与应用

    第5课  CC2530的串行接口原理与应用 广东职业技术学院  欧浩源 一.并行通信与串行通信 微控制器与外设之间的数据通信,根据连线结构和传送方式的不同,可以分为两种:并行通信和串行通信. 并行通信 ...

随机推荐

  1. RGB888-&gt&semi;RGB565-&gt&semi;RGB888

     转自CB的博客:http://blog.chinaaet.com/detail/28298 在我们的计算机中,图像是以RGB888显示的,24位图每个像素保存了32bit的数据,即RGB888+Al ...

  2. 解决Deprecated&colon; preg&lowbar;replace&lpar;&rpar;&colon; The &sol;e modifier is deprecated&comma; use

    使用php5.5运行ecshop的时候出现如下错误Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace ...

  3. 如何解决svn图标不显示呢?

    svn图标不显示解决 确保设置正确: 右键->TortoiseSVN->setting->Icon Overlays->Status cache->default/She ...

  4. codevs 1455 路径 计算m&Hat;n&percnt;p

    题目链接 题目描述 Description 小明从A1到An+1,他知道从A1到A2,从A2到A3,......,从An到An+1都有m条路,且从A1到An+1都只有这些路.小明想知道,从A1地到An ...

  5. 【Revit API】梁构件支座检查算法

    一.前言         应该是第二次写关于Revit API的博文了.虽然在BIM企业中工作,从事桌面BIM软件开发,但是我是不怎么喜欢写Revit API相关的代码.平时更多的是在写界面展示,架构 ...

  6. centos安装软件依赖问题

    yum install gcc gcc-c++ ncurses-devel perl 基础包安装

  7. java中,字符串和集合判断是否为空

    字符串: 集合: 不为空

  8. EBS WEBADI导入日记账 客户化账户组合规则校验

    近期项目需求对EBS中WEBADI导入日记账时,在加载数据时需要对账户组合额外进行客户化的校验,需要能够做到将校验结果体现在WEBADI模板的数据上,并且对每条错误数据都单独报错. 项目上的方案是调整 ...

  9. CSS表单2 组件排版

    <!DOCTYPE html> <html>     <head>         <title>单选按钮对齐</title>        ...

  10. 基于Prometheus的Pushgateway实战

    一.Pushgateway 简介 Pushgateway 是 Prometheus 生态中一个重要工具,使用它的原因主要是: Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙 ...