【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/O端口输入和输出控制

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

  5. 【CC2530入门教程-02】CC2530的通用I/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的定时/计数器原理与应用

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

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

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

随机推荐

  1. touchstart和touchend事件

    touchstart和touchend事件 移动互联网是未来的发展趋势,现在国内很多互联网大佬都在争取移动这一块大饼,如微信及支付宝是目前比较成功的例子,当然还有各种APP和web运用. 由于公司的需 ...

  2. Clonezilla SE---克隆linux------转载

    引入: 本博文将会是<学生机房中的虚拟化>专题中的核心内容.因为,通过本篇博文的讲述,大家可以看到用于网络化批量部署Linux系统的Clonezilla SE搭建的全过程.注意,几乎所有命 ...

  3. Create 命令详解

    mkdir:创建一个目录 /mkdir a b c :创建同级目录 /mkdir -p aa/bb/cc: 递归创建目录touch:修改文件时间戳,或者新建一个不存在的文件 /-a 更改存取时间 /m ...

  4. 云计算---openstack镜像制作

    一:本地部署KVM 1.安装KVM 1.1安装须知 查看CPU是否支持kvm完全虚拟机. [root@LINUX ~]# grep "flags" /proc/cpuinfofla ...

  5. java.util.HashSet

    Operations Time Complexity Notes add, remove, contains, size O(1) assuming the hash functions has di ...

  6. JQuery和JS操作LocalStorage/SessionStorage的方法

    LocalStorage 是对Cookie的优化 没有时间限制的数据存储 在隐私模式下不可读取 大小限制在500万字符左右,各个浏览器不一致 在所有同源窗口中都是共享的 本质是在读写文件,数据多的话会 ...

  7. 进程管理工具htop/glances/dstat的使用

    进程管理工具htop/glances/dstat的使用 Linux中进程的相关知识 1.什么是进程呢? 通俗的来说进程是运行起来的程序.唯一标示进程的是进程描述符(PID). 2.进程的分类 1)根据 ...

  8. 【bird-front】全自动数据表格组件bird-grid

    bird-grid是bird-front前端框架中实现的全自动数据表格组件.组件内部处理数据加载.分页.排序.查询.新增.编辑等一系列操作.让业务表格的开发从繁复的增删查改中脱离出来,编码简洁却又功能 ...

  9. angular4.0项目main.ts详解

    main.ts负责引导整个angular应用的起点 // 导入enableProdMode用来关闭angular开发者模式 import { enableProdMode } from '@angul ...

  10. CSS制作波浪线

    建议先去了解清楚了径向渐变,线性渐变的用法先 这个作者的css制作波浪线讲解很不错额:https://www.jianshu.com/p/8570433e3669不理解的可以看看这个链接的额 可以去菜 ...