欢迎光临散文网 会员登陆 & 注册

模拟环境数据采集的代码

2023-05-27 14:19 作者:被栓住的Q  | 我要投稿


简介

        本人使用的操作系统是:Windows11。本次项目使用到的软件有:编写代码——Visual Studio 2022、发送数据——UartAssist.exe(串口调试助手)、创建虚拟串口——vspdconfig.exe(虚拟串口驱动)。 

界面代码

<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="环境数据采集" Height="274" Width="369" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
    <Grid>
        <GroupBox Header="串口设置" 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="环境状况" 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>

运行代码

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("请选择一个串口!", "系统提示", 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("打开串口异常!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                serialPort.DataReceived += SerialPort_DataReceived;
            }
            ButtonOpenClose.Content = serialPort.IsOpen ? "关闭" : "打开";
            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;
        }
    }
}

运行效果


模拟环境数据采集的代码的评论 (共 条)

分享到微博请遵守国家法律