模擬環(huán)境數(shù)據(jù)采集的代碼



簡介
? ? ? ? 本人使用的操作系統(tǒng)是:Windows11。本次項目使用到的軟件有:編寫代碼——Visual Studio 2022、發(fā)送數(shù)據(jù)——UartAssist.exe(串口調(diào)試助手)、創(chuàng)建虛擬串口——vspdconfig.exe(虛擬串口驅(qū)動)。?

界面代碼
<Window x:Class="HuanJing.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:HuanJing"
? ? ? ?mc:Ignorable="d"
? ? ? ?Title="環(huán)境數(shù)據(jù)采集" Height="274" Width="369" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
? ?<Grid>
? ? ? ?<GroupBox Header="串口設(shè)置" FontFamily="MiSans" FontSize="16" Margin="10,10,0,0" HorizontalAlignment="Left" Width="325" Height="82" VerticalAlignment="Top">
? ? ? ? ? ?<Grid>
? ? ? ? ? ? ? ?<Label Content="選擇串口:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
? ? ? ? ? ? ? ?<ComboBox x:Name="ComboBoxInputPort" HorizontalAlignment="Left" Margin="105,10,0,0" VerticalAlignment="Top" Width="120"/>
? ? ? ? ? ? ? ?<Button x:Name="ButtonOpenClose" Content="打開" HorizontalAlignment="Left" Margin="230,13,0,0" VerticalAlignment="Top" Width="75" Click="ButtonOpenClose_Click"/>
? ? ? ? ? ?</Grid>
? ? ? ?</GroupBox>
? ? ? ?<GroupBox Header="環(huán)境狀況" FontFamily="MiSans" FontSize="16" Margin="10,97,0,0" HorizontalAlignment="Left" Width="325" Height="117" VerticalAlignment="Top">
? ? ? ? ? ?<Grid>
? ? ? ? ? ? ? ?<Grid.ColumnDefinitions>
? ? ? ? ? ? ? ? ? ?<ColumnDefinition Width="41*"/>
? ? ? ? ? ? ? ? ? ?<ColumnDefinition Width="272*"/>
? ? ? ? ? ? ? ?</Grid.ColumnDefinitions>
? ? ? ? ? ? ? ?<Label Content="光照度:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
? ? ? ? ? ? ? ?<Label Content="PM2.5:" HorizontalAlignment="Left" Margin="10,46,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
? ? ? ? ? ? ? ?<TextBox x:Name="TextBoxLight" HorizontalAlignment="Left" Margin="48,14,0,0" TextWrapping="Wrap" Text="—lx—" VerticalAlignment="Top" Width="120" TextAlignment="Center" IsReadOnly="True" Grid.Column="1"/>
? ? ? ? ? ? ? ?<TextBox x:Name="TextBoxPM2_5" HorizontalAlignment="Left" Margin="48,50,0,0" TextWrapping="Wrap" Text="—μg/m3—" VerticalAlignment="Top" Width="120" TextAlignment="Center" IsReadOnly="True" Grid.Column="1"/>
? ? ? ? ? ?</Grid>
? ? ? ?</GroupBox>
? ?</Grid>
</Window>

運(yùn)行代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
namespace HuanJing
{
? ?/// <summary>
? ?/// MainWindow.xaml 的交互邏輯
? ?/// </summary>
? ?public partial class MainWindow : Window
? ?{
? ? ? ?private SerialPort serialPort = new SerialPort();
? ? ? ?public MainWindow()
? ? ? ?{
? ? ? ? ? ?InitializeComponent();
? ? ? ? ? ?ComboBoxInputPort.ItemsSource=SerialPort.GetPortNames();
? ? ? ?}
? ? ? ?private void ButtonOpenClose_Click(object sender, RoutedEventArgs e)
? ? ? ?{
? ? ? ? ? ?if (ComboBoxInputPort.Text == "")
? ? ? ? ? ?{
? ? ? ? ? ? ? ?MessageBox.Show("請選擇一個串口!", "系統(tǒng)提示", MessageBoxButton.OK, MessageBoxImage.Information);
? ? ? ? ? ? ? ?return;
? ? ? ? ? ?}
? ? ? ? ? ?if (serialPort.IsOpen)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?serialPort.Close();
? ? ? ? ? ? ? ?TextBoxLight.Text = "—lx—";
? ? ? ? ? ? ? ?TextBoxPM2_5.Text = "—μg/m3—";
? ? ? ? ? ?}
? ? ? ? ? ?else
? ? ? ? ? ?{
? ? ? ? ? ? ? ?serialPort.PortName = ComboBoxInputPort.Text; ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?try
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?serialPort.Open();
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?catch
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?serialPort =new SerialPort();
? ? ? ? ? ? ? ? ? ?MessageBox.Show("打開串口異常!", "系統(tǒng)提示", MessageBoxButton.OK, MessageBoxImage.Error);
? ? ? ? ? ? ? ? ? ?return;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?serialPort.DataReceived += SerialPort_DataReceived;
? ? ? ? ? ?}
? ? ? ? ? ?ButtonOpenClose.Content = serialPort.IsOpen ? "關(guān)閉" : "打開";
? ? ? ? ? ?ComboBoxInputPort.IsEnabled=serialPort.IsOpen ? false : true;
? ? ? ?}
? ? ? ?private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
? ? ? ?{
? ? ? ? ? ?int intRead = 0;
? ? ? ? ? ?try
? ? ? ? ? ?{
? ? ? ? ? ? ? ?intRead = serialPort.BytesToRead;
? ? ? ? ? ?}
? ? ? ? ? ?catch
? ? ? ? ? ?{
? ? ? ? ? ? ? ?
? ? ? ? ? ?}
? ? ? ? ? ?Console.WriteLine("輸出" + intRead);
? ? ? ? ? ?if(intRead>0)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?byte[] data = new byte[intRead];
? ? ? ? ? ? ? ?serialPort.Read(data, 0, intRead);
? ? ? ? ? ? ? ?this.Dispatcher.Invoke(
? ? ? ? ? ? ? ? ? ?new Action(
? ? ? ? ? ? ? ? ? ? ? ?delegate
? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ?TextBoxLight.Text = AnalogConvert(data[18], data[19], 5000.0, 0).ToString("f2")+"lx";
? ? ? ? ? ? ? ? ? ? ? ? ? ?TextBoxPM2_5.Text = AnalogConvert(data[20], data[21], 300.0, 0).ToString("f2") + "μg/m3";
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?)
? ? ? ? ? ? ? ?);
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?public double AnalogConvert(byte value1, byte value2, double max, double min)
? ? ? ?{
? ? ? ? ? ?int intValue = value1 | value2 << 8;
? ? ? ? ? ?double doubleValue = (intValue * 3300.0 / 1023.0 / 150.0 - 4.0) * (max - min) / 16.0 + min;
? ? ? ? ? ?return doubleValue;
? ? ? ?}
? ?}
}

運(yùn)行效果

標(biāo)簽: