• 大小: 0.61M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-06-20
  • 语言: C#
  • 标签: 获取  湿度  实时  C#  

资源简介

c#通过串口通信实时获取温湿度计的温湿度

资源截图

代码片段和文件信息

using System;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
using TaAndRH;
using WsCommModel;
using WsFileLog;

namespace GetTaRH
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        /// 
        /// 传感器实例
        /// 

        TaAndRHSensor Sensor;
        /// 
        /// 获取数据
        /// 

        Thread GetData;
        private void FrmMain_Load(object sender EventArgs e)
        {

            Control.CheckForIllegalCrossThreadCalls = false;
            Start();
        }

        private void Start()
        {
            initPort();
            string path = Application.StartupPath;
            string strFilePath = path + “\\“ + “AsTaAndRh.ini“;
            Sensor = new TaAndRHSensor(new WsLog() strFilePathnull);
            GetData = new Thread(DoData);
            GetData.Start();

            txtAddress.Text = Sensor.AnSensor.address.ToString();
            txtWucha.Text = Sensor.AnSensor.iWendubuchang.ToString();
            txtRate.Text = Sensor.AnSensor.iUpdateInterval.ToString();
            cmbType.Text = Sensor.AnSensor.SType.ToString();
            cmbPort.Text = Sensor.AnSensor.PortName;
            this.lblDeviceNo.Text = “设备号:“ + Sensor.AnSensor.sDeviceNo;
        }

        private void initPort()
        {
            //获取当前计算机的串行端口名的数组
            string[] strCom = SerialPort.GetPortNames();
            if (strCom == null)
            {
                MessageBox.Show(“本机没有串口!“ “Error“);
                return;
            }
            cmbPort.Items.Clear();
            foreach (string com in strCom)
            {
                cmbPort.Items.Add(com);
            }
            //串口设置默认选项
            cmbPort.SelectedIndex = 0;
        }
        /// 
        /// 获取数据
        /// 

        private void DoData()
        {
            while (true)
            {
                this.lblsd.Text = Sensor.AnSensor.sRH;
                this.lblWd.Text = Sensor.AnSensor.sTa;
                Thread.Sleep(100);
            }
        }

        private void FrmMain_FormClosed(object sender FormClosedEventArgs e)
        {           
            System.Environment.Exit(0);
        }

       
        private void button1_Click_1(object sender EventArgs e)
        {
            Sensor.AnSensor.PortName = cmbPort.Text;
            Sensor.AnSensor.iWendubuchang = int.Parse(txtWucha.Text);
            Sensor.AnSensor.iUpdateInterval = int.Parse(txtRate.Text);
            Sensor.AnSensor.address = (byte)(int.Parse(txtAddress.Text));
            Sensor.AnSensor.SType = (SensorType)Enum.Parse(typeof(SensorType) cmbType.Text);
            Sensor.AnSensor.SetConfig();
            CommFunc.ColoseThread(GetData);
            Sensor.AnSensor.Close();
            Start();
        }
   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1601  2021-10-15 11:14  WsAsTaAndRH.sln

     文件        189  2021-07-06 16:24  GetTaRH\App.config

     文件         82  2021-10-15 14:18  GetTaRH\bin\Debug\AsTaAndRh.ini

     文件       7168  2021-06-19 11:17  GetTaRH\bin\Debug\FileLog.dll

     文件      12800  2021-10-15 14:17  GetTaRH\bin\Debug\GetTaRH.exe

     文件        189  2021-07-06 16:24  GetTaRH\bin\Debug\GetTaRH.exe.config

     文件      34304  2021-10-15 14:17  GetTaRH\bin\Debug\GetTaRH.pdb

     文件     701992  2021-03-17 20:03  GetTaRH\bin\Debug\Newtonsoft.Json.dll

     文件      15872  2021-06-19 17:01  GetTaRH\bin\Debug\SerialPort.dll

     文件      13312  2021-10-15 14:17  GetTaRH\bin\Debug\TaAndRH.dll

     文件      44544  2021-10-15 14:17  GetTaRH\bin\Debug\TaAndRH.pdb

     文件      18432  2021-08-17 15:38  GetTaRH\bin\Debug\WsCommModel.dll

     文件       3106  2021-10-15 11:36  GetTaRH\FrmMain.cs

     文件      10155  2021-07-07 16:15  GetTaRH\FrmMain.Designer.cs

     文件       5817  2021-07-07 16:15  GetTaRH\FrmMain.resx

     文件       4249  2021-10-15 11:36  GetTaRH\GetTaRH.csproj

     文件        214  2021-07-07 16:52  GetTaRH\obj\Debug\.NETframeworkVersion=v4.7.2.AssemblyAttributes.cs

     文件       1435  2021-10-15 14:18  GetTaRH\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7256  2021-10-15 11:36  GetTaRH\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2021-10-15 14:17  GetTaRH\obj\Debug\GetTaRH.csproj.CopyComplete

     文件         42  2021-10-15 14:17  GetTaRH\obj\Debug\GetTaRH.csproj.CoreCompileInputs.cache

     文件       1317  2021-10-15 14:17  GetTaRH\obj\Debug\GetTaRH.csproj.FileListAbsolute.txt

     文件       1014  2021-10-15 14:17  GetTaRH\obj\Debug\GetTaRH.csproj.GenerateResource.cache

     文件      21988  2021-10-15 14:17  GetTaRH\obj\Debug\GetTaRH.csprojAssemblyReference.cache

     文件      12800  2021-10-15 14:17  GetTaRH\obj\Debug\GetTaRH.exe

     文件        180  2021-10-15 14:17  GetTaRH\obj\Debug\GetTaRH.FrmMain.resources

     文件      34304  2021-10-15 14:17  GetTaRH\obj\Debug\GetTaRH.pdb

     文件        180  2021-10-15 14:17  GetTaRH\obj\Debug\GetTaRH.Properties.Resources.resources

     文件        521  2021-07-06 16:25  GetTaRH\Program.cs

     文件       1306  2021-07-06 16:24  GetTaRH\Properties\AssemblyInfo.cs

............此处省略49个文件信息

评论

共有 条评论