概述
using System;
using System.IO.Ports;//串口
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows.Forms;
using System.Media;
namespace Monitoring_System
{
public partial class Form1 : Form
{
private SerialPort COMM = new SerialPort();
private StringBuilder builder_temp = new StringBuilder();//处理字符串
private string send_data;
private byte flag = 0;
int minutes = 0;
int state_canshu = 0;
int state_1 = 0, state_2 = 0, state_3 = 0;
byte[] SendBuf = new byte[2];//定义串口发送字符串变量
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ports.Items.Add("COM1"); //ports为串口下拉组合框
ports.Items.Add("COM2");
ports.Items.Add("COM3");
ports.Items.Add("COM4");
ports.Items.Add("COM5");
botelu.Items.Add("4800"); //botelu为波特率下拉组合框
botelu.Items.Add("9600");
botelu.Items.Add("19200");
botelu.Items.Add("38400");
botelu.Items.Add("57600");
botelu.Items.Add("115200");
comboBox1.Items.Add("5");
comboBox1.Items.Add("6");
comboBox1.Items.Add("7");
comboBox1.Items.Add("8");
comboBox2.Items.Add("1");
comboBox2.Items.Add("1.5");
comboBox2.Items.Add("2");
comboBox3.Items.Add("无");
comboBox3.Items.Add("奇校验");
comboBox3.Items.Add("偶校验");
ports.SelectedIndex = ports.Items.IndexOf("COM1"); //comboBox1.SelectedIndex = 0;
botelu.SelectedIndex = botelu.Items.IndexOf("9600"); // comboBox2.SelectedIndex =1;
comboBox1.SelectedIndex = 3;
comboBox2.SelectedIndex = 0;
comboBox3.SelectedIndex = 0;
// COMM.NewLine = "/r/n";
this.COMM.RtsEnable = true;//根据实际情况
//添加事件注册
COMM.DataReceived += COMM_DataReceived;
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000;//1s
aTimer.Enabled = true;
GC.KeepAlive(aTimer);
this.textBox7.Enabled = false;
this.textBox4.Enabled = false;
this.textBox8.Enabled = false;
this.textBox7.Text = "3000";
this.textBox4.Text = "30";
this.textBox8.Text = "3000";
this.textBox11.Text = "关闭";
this.textBox10.Text = "正常";
this.textBox9.Text = "打开";
this.textBox9.Enabled = false;
this.textBox10.Enabled = false;
this.textBox11.Enabled = false;
}
/// <summary>
/// 串口接收数据处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void COMM_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int n = this.COMM.BytesToRead;//数据先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
this.COMM.Read(buf, 0, n);//读取缓冲数据
//因为要访问ui资源,所以需要使用invoke方式同步ui。
this.Invoke((EventHandler) (delegate
{
byte flag_judge = 0;
//直接按ASCII规则转换成字符串
string rece_temp = Encoding.ASCII.GetString(buf);
builder_temp.Append(rece_temp);
while (flag_judge < n)
{
if (buf[flag_judge] == '#')
{
flag++;
int length = builder_temp.Length;
string temp_data = builder_temp.ToString();
send_data = temp_data.Remove(length - 1);
builder_temp.Clear();
break;
}
flag_judge++;
}
for (int i = 0; i < n; i++)
{
buf[i] = 0;
}
if (flag >= 2)
{
if (flag >= 100)
{
flag = 0;
}
deal_value(send_data);
}
}));
}
/// <summary>
/// 处理接收的数据
/// </summary>
/// <param name="value"></param>
private void deal_value(string value)
{
byte[] RxdBuf = System.Text.Encoding.Default.GetBytes(value);
if (RxdBuf[0] == 'A')//终端1开始标志
{
if (RxdBuf[1] == 0x31)
{
this.textBox6.Text = "有人";
Playmusic();
}
else
{
this.textBox6.Text = "无人";
}
this.textBox5.Text = RxdBuf[2].ToString() + RxdBuf[3].ToString() + RxdBuf[4].ToString() + RxdBuf[5].ToString();//烟雾浓度
}
if (RxdBuf[0] == 'B')//终端2开始标志
{
this.textBox1.Text = RxdBuf[1].ToString() + RxdBuf[2].ToString();//温度
this.textBox3.Text = RxdBuf[3].ToString() + RxdBuf[4].ToString();//湿度
this.textBox2.Text = RxdBuf[5].ToString() + RxdBuf[6].ToString() + RxdBuf[7].ToString() + RxdBuf[8].ToString();//光照强度
}
}
//开按钮
private void open_Click(object sender, EventArgs e)
{
this.COMM.PortName = ports.Text;
this.COMM.BaudRate = int.Parse(botelu.Text);
this.COMM.Parity = Parity.None;
this.COMM.StopBits = StopBits.One;
this.COMM.DataBits = 8;
try
{
this.COMM.Open();
}
catch (Exception ex)
{
//捕获到异常信息,创建一个新的comm对象,之前的不能用了。
this.COMM = new SerialPort();
//现实异常信息给客户。
MessageBox.Show(ex.Message);
}
close.Enabled = this.COMM.IsOpen;
open.Enabled = !this.COMM.IsOpen;
}
//关按钮
private void close_Click(object sender, EventArgs e)
{
if (this.COMM.IsOpen)
{
this.COMM.Close();
}
open.Enabled = !this.COMM.IsOpen;
close.Enabled = this.COMM.IsOpen;
}
System.DateTime currentTime = new System.DateTime();
//周期事件
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
minutes++;
int hours = DateTime.Now.Hour;
//label3.Text = DateTime.Now.Hour.ToString();
if (minutes == 1&&state_canshu == 1)
{
int yanwu_1 = Convert.ToInt32(textBox5.Text);//正常值
int yanwu_2 = Convert.ToInt32(textBox7.Text);//设定值
if (yanwu_1 > yanwu_2)//烟雾异常
{
if (state_1 == 1)//烟雾异常
{
}
else
{
SendBuf[0] = 0x31;
SendBuf[1] = 0x31;
COMM.Write(SendBuf, 0, 2);
this.textBox10.Text = "切断";
state_1 = 1;
}
}
else
{
if (state_1 == 1)//正在工作
{
SendBuf[0] = 0x31;
SendBuf[1] = 0x30;
COMM.Write(SendBuf, 0, 2);
this.textBox10.Text = "正常";
state_1 = 0;
}
else
{
}
}
}
if (minutes == 2&&state_canshu == 1)
{
int wendu_1 = Convert.ToInt32(textBox1.Text);//正常值
int wendu_2 = Convert.ToInt32(textBox4.Text);//设定值
if ((wendu_1 > wendu_2) &&(hours>=9&&hours<=17))
//if (wendu_1 > wendu_2)//温度异常
{
if (state_2 == 1)//
{
}
else
{
SendBuf[0] = 0x32;
SendBuf[1] = 0x31;
COMM.Write(SendBuf, 0, 2);
this.textBox11.Text = "打开";
state_2 = 1;
}
}
else
{
if (state_2 == 1)//正在工作
{
SendBuf[0] = 0x32;
SendBuf[1] = 0x30;
COMM.Write(SendBuf, 0, 2);
this.textBox11.Text = "关闭";
state_2 = 0;
}
else
{
}
}
}
if (minutes >= 3 && state_canshu == 1)
{
int guangzhao_1 = Convert.ToInt32(textBox2.Text);//正常值
int guangzhao_2 = Convert.ToInt32(textBox8.Text);//设定值
minutes = 0;
if (guangzhao_1 < guangzhao_2)//光照
{
if (state_3 == 1)//
{
}
else
{
SendBuf[0] = 0x32;
SendBuf[1] = 0x33;
COMM.Write(SendBuf, 0, 2);
this.textBox9.Text = "打开";
state_3 = 1;
}
}
else
{
if (state_3 == 1)//正在工作
{
SendBuf[0] = 0x32;
SendBuf[1] = 0x32;
COMM.Write(SendBuf, 0, 2);
this.textBox9.Text = "关闭";
state_3 = 0;
}
else
{
}
}
}
}
//确定按钮
private void button1_Click(object sender, EventArgs e)
{
state_canshu = 1;
this.textBox7.Enabled = false;
this.textBox4.Enabled = false;
this.textBox8.Enabled = false;
}
//取消按钮
private void button2_Click(object sender, EventArgs e)
{
state_canshu = 0;
this.textBox7.Enabled = true;
this.textBox4.Enabled = true;
this.textBox8.Enabled = true;
}
//音乐函数
public static void Playmusic()
{
try
{
SoundPlayer filew = new SoundPlayer();
filew.SoundLocation = @"D:air.wav";
filew.Load();
filew.Play();//播放一次
}
catch (Exception)
{ MessageBox.Show("音效初始化失败!"); }
}
private void time_Tick(object sender, EventArgs e)
{
}
}
最后
以上就是重要世界为你收集整理的基于ZigBee的智能监控系统-上位机代码的全部内容,希望文章能够帮你解决基于ZigBee的智能监控系统-上位机代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复