概述
往期1-3主题请参考:https://blog.csdn.net/weixin_41598714/article/details/107253548
温湿度上报管理系统
1.开发平台与背景
2.硬件准备
3.WPF登录界面开发
4.数据库开发
4.WPF上位机开发
5.ASP.net网页开发
往期进行了登录界面的设置,这篇将介绍对温湿度获取上位机以及上报数据库的主界面。
4.WPF上位机开发
界面展示
贴上界面代码
<Window x:Class="IntegratedPJ75.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:IntegratedPJ75" xmlns:Demo_3_4_WpfControlLibrary="clr-namespace:Demo_3_4_WpfControlLibrary;assembly=Demo_3_4_WpfControlLibrary"
mc:Ignorable="d"
Title="物联网综合项目" Height="536" Width="1338">
<Grid x:Name="LayoutRoot" Margin="4,4,4,4">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<GroupBox Header="监控信息、" Height="195" Width="206" Margin="10,0,0,25" VerticalAlignment="Bottom" RenderTransformOrigin="-0.141,0.559"
HorizontalAlignment="Left">
<Grid Margin="10,0,0,0">
<TextBox x:Name="txtStaus" Height="105" Margin="0,10,3,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
<Button x:Name="BtnReceive" Click="BtnReceive_Click" Content="实时温湿度数据" Margin="0,120,0,14" />
<Button x:Name="BtnSave" Click="BtnSave_Click" Content="保存数据" Margin="0,155,0,0" />
</Grid>
</GroupBox>
<GroupBox Header="串口设置" HorizontalAlignment="Left" VerticalAlignment="Top" Height="262" Width="206" Margin="10,10,0,0">
<Grid HorizontalAlignment="Left" Width="196" Margin="0,0,0,0">
<Label Content="接收端口:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name="cmbPort" HorizontalAlignment="Left" Margin="76,13,0,0" VerticalAlignment="Top" Width="77"/>
<Label Content="数据位:" HorizontalAlignment="Left" Margin="10,120,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name="cmbDataBits" HorizontalAlignment="Left" Margin="76,123,0,0" VerticalAlignment="Top" Width="77">
<ListBoxItem Content="8"/>
<ListBoxItem Content="7"/>
<ListBoxItem Content="6"/>
<ListBoxItem Content="5"/>
</ComboBox>
<Label Content="奇偶校验:" HorizontalAlignment="Left" Margin="8,85,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name="cmbParity" HorizontalAlignment="Left" Margin="76,88,0,0" VerticalAlignment="Top" Width="77">
<ListBoxItem Content="None"/>
<ListBoxItem Content="Odd"/>
<ListBoxItem Content="Even"/>
</ComboBox>
<Label Content="波特率:" HorizontalAlignment="Left" Margin="11,45,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.328,2.2"/>
<ComboBox x:Name="cmbBaudRate" HorizontalAlignment="Left" Margin="76,49,0,0" VerticalAlignment="Top" Width="77"
RenderTransformOrigin="-0.351,3.905">
<ListBoxItem Content="9600"/>
<ListBoxItem Content="19200"/>
<ListBoxItem Content="38400"/>
<ListBoxItem Content="57600"/>
<ListBoxItem Content="115200"/>
</ComboBox>
<Label Content="停止位" HorizontalAlignment="Left" Margin="11,155,0,0" VerticalAlignment="Top"
RenderTransformOrigin="-0.351,3.905"/>
<ComboBox x:Name="cmbStopBits" HorizontalAlignment="Left" Margin="76,159,0,0" VerticalAlignment="Top" Width="77">
<ListBoxItem Content="One"/>
<ListBoxItem Content="Two"/>
<ListBoxItem Content="OnePointFive"/>
<ListBoxItem Content="None"/>
</ComboBox>
<Button x:Name="btnOpen" Click="btnOpen_Click_1" Content="打开串口" HorizontalAlignment="Left" Margin="12,200,0,0"
VerticalAlignment="Top" Width="141"/>
</Grid>
</GroupBox>
<Demo_3_4_WpfControlLibrary:CurveControl x:Name="CurveCtr" Margin="221,5,0,0" Height="462" VerticalAlignment="Top"
HorizontalAlignment="left" Width="545"/>
<Demo_3_4_WpfControlLibrary:BarControl x:Name="BarCtr" Margin="776,16,-19,35" HorizontalAlignment="left" Width="565"/>
</Grid>
</Window>
逻辑部分代码
using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO.Ports;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;
using Visifire.Commons;
namespace IntegratedPJ75
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
//存储温湿度数据
double temp = 0;
double hum = 0;
double num;
private static IModbusMaster master;
//写寄存器数组
private ushort[] registerBuffer;
//串口对象实例化
SerialPort com = new SerialPort();
public MainWindow()
{
InitializeComponent();
//UserLogin userlogin = new UserLogin();
//userlogin.Show();
this.Loaded += MainWindow_Loaded;//添加窗体加载事件
}
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
Thread timer;//声明线程变量
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//获取本机所以串口名字
string[] strPortName = SerialPort.GetPortNames();
//将本机所有串口名称赋值给cmbPort控件
cmbPort.ItemsSource = strPortName;
//下拉框初始化
//如果本串口数量不为0,则将cmbPort的Item第一个索引
if (strPortName.Length > 0) cmbPort.SelectedIndex = 0;
//将波特率下拉框cmbBaudRate的Item第一个索引
cmbBaudRate.SelectedIndex = 0;
cmbDataBits.SelectedIndex = 0;
cmbStopBits.SelectedIndex = 0;
cmbParity.SelectedIndex = 0;
CurveCtr.DrawBackground();//绘制背景
BarCtr.DrawBackground();
timer = new Thread(new ThreadStart(() => //线程实例化
{
while (true)
{
try
{
Dispatcher.Invoke(new Action(() =>
{
CurveCtr.DrawLine(temp);//绘制温度折线
BarCtr.DrawLine(hum);//绘制温度直方图
}));
}
catch
{
}
Thread.Sleep(700);
}
}));
timer.Start();
}
private void Windows_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
{
timer.Abort();//关闭线程
timer = null;
}
/// <summary>
/// 打开按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpen_Click_1(object sender, RoutedEventArgs e)
{
//如果按钮内容是“打开串口”则进行打开串口否则关闭串口操作
if (btnOpen.Content.ToString() == "打开串口")
//尝试打开,出错则在界面进行提示
try
{
//判断串口是否打开
if (!com.IsOpen)
{
//设置串口参数
com.PortName = cmbPort.Text;//串口号
com.BaudRate = int.Parse(cmbBaudRate.Text);//波特率
com.DataBits = int.Parse(cmbDataBits.Text);//数据位
switch (cmbStopBits.SelectedIndex)//停止位
{
case 0:
com.StopBits = StopBits.One; break;
case 1:
com.StopBits = StopBits.Two; break;
case 2:
com.StopBits = StopBits.OnePointFive; break;
case 3:
com.StopBits = StopBits.None; break;
}
switch (cmbParity.SelectedIndex)//奇偶检验
{
case 0: com.Parity = Parity.None; break;
case 1: com.Parity = Parity.Odd; break;
case 2: com.Parity = Parity.Even; break;
}
//设置串口参数
master = ModbusSerialMaster.CreateRtu(com);
com.Open();//打开串口
}
//设置按钮内容为关闭串口
btnOpen.Content = "关闭串口";
//界面显示信息“串口打开”
txtStaus.Text = "串口已打开!n";
}
catch
{
txtStaus.Text = "串口打开错误或串口不存在!";
}
else //关闭串口
{
try
{
if (com.IsOpen)
{
com.Close();//关闭串口
}
btnOpen.Content = "打开串口";
txtStaus.Text = "串口已关闭!";
}
catch
{
txtStaus.Text = "串口关闭错误或串口不存在!";
}
}
}
private void BtnReceive_Click(Object sender, RoutedEventArgs e)
{
txtStaus.Clear();
if (com.IsOpen == false)
{
txtStaus.AppendText("请打开串口!!!rn");
}
else
{
//读取数据
registerBuffer = master.ReadHoldingRegisters(1, 0, 2);
temp = registerBuffer[0] / 10.0;
txtStaus.AppendText("温度:" + temp + " ");
hum = registerBuffer[1] / 10.0;
txtStaus.AppendText("湿度:" + hum + "tn");
}
}
Chart chart; // Chart object
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
string connString = @"Data Source=(localdb)ProjectsV13;Initial Catalog=MySchool;Integrated Security=True;Pooling=False;";
try
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();//打开数据库连接
string sqlSel = String.Format("SELECT MAX(Id) FROM TempHumData");
SqlCommand commSel = new SqlCommand(sqlSel, conn); //创建command对象
//num全局变量,新增数据的Id编号
object i = commSel.ExecuteScalar();//add
if (!(commSel.ExecuteScalar() == System.DBNull.Value))
{
num = (int)commSel.ExecuteScalar();
num++;
}
else
{
num = 1;
}
//Sql语句,插入数据
string sqlIns = String.Format("INSERT INTO TempHumData(Id,time,temp,hum)" +
"VALUES('{0}','{1}','{2}','{3}')", num, DateTime.Now, temp, hum);
SqlCommand commIns = new SqlCommand(sqlIns, conn);//创建command对象
int n = commIns.ExecuteNonQuery();//执行添加命令,返回值为更新的行数
if (n > 0)
{
txtStaus.AppendText("数据保存成功!!!" + "rn");
}
else
{
txtStaus.AppendText("数据保存失败!!!" + "rn");
}
}
}
catch (Exception ex)
{
txtStaus.AppendText(ex.Message + "rn");
}
}
}
}
注意:需要引用第三方的图形库Demo_3_4_WpfControlLibrary
下载地址:链接:https://pan.baidu.com/s/1sw3-MazR4hSXsJGjayJ1QA
提取码:yb09
至此WPF主界面已经完成,可以使用串口虚拟软件模拟数据运行。
下一节将开发APS.net网页数据管理端口。感兴趣可以查看第三章。
最后
以上就是懦弱雪糕为你收集整理的【.net开发实战】物联网温湿度上报管理系统(二) 温湿度上报管理系统的全部内容,希望文章能够帮你解决【.net开发实战】物联网温湿度上报管理系统(二) 温湿度上报管理系统所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复