【物联网原理与应用】实验二:红外传感实验

目录

一、实验目的

二、实验原理

三、实验内容及步骤

四、实验结果

五、核心代码


一、实验目的

  1. 学习试验模块上线路的连接操作
  2. 理解掌握红外传感器的工作原理
  3. 实现对红外传感器数据的接收和处理

二、实验原理

1、将红外辐射能转换成电能的光敏元件称为红外传感器,也常称为红外探测器。红外辐射是由于物体(固体、液体和气体)内部分子的转动及振动而产生的。这类振动过程是物体受热而引起的,只有在绝对零度(-273.16℃)时,一切物体的分子才会停止运动。换言之,在一般的常温下,所有的物体都是红外辐射的发射源。例如火焰、轴承、汽车、飞机、动植物甚至人体等都是红外辐射源。

红外线和所有的电磁波一样,具有反射、折射、散射、等性质。红外传感器测量时不与被测物体直接接触,因而不存在摩擦,并且有灵敏度高,响应快等优点。红外传感器常用于无接触温度测量、气体成分分析和无损探伤,在医学、军事、空间技术和环境工程等领域得到广泛应用。例如采用红外线传感器远距离测量人体表面温度的热像图,可以发现温度异常的部位,及时对疾病进行诊断治疗(见热像仪);利用人造卫星上的红外线传感器对地球云层进行监视,可实现大范围的天气预报;采用红外线传感器可检测飞机上正在运行的发动机 的过热情况等。

三、实验内容及步骤

1、我们先在UI页面上利用Storyboard元素进行动画定义。需要定义:车辆进出停车场和A、B车位的进库动画,一共4个动画。

2、每个动画分别通过设置时间线的显示时间,同时在页面上添加一个Border元素,利用其背景作为显示屏幕,通过功能代码中调用Storyboard元素. Begin()方法和Stop()方法来控制动画的播放和停止。

3、在功能代码中我们开启和检测串口,通过串口帮助类获取红外传感器的感应数据,根据返回的数据启动或停止对应的动画。

4、在结束时关闭串口,回收资源。

四、实验结果

入口动画演示

出口动画演示

​​​​​​​

A点停车动画图片演示

B点停车动画图片演示

五、核心代码

复制代码
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.Windows.Media.Animation;
using System.Timers;

using InfraredElecLibrary;


namespace InfraredElec
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 串口辅助类
        /// </summary>
        static ComHelper com = null;
        /// <summary>
        /// 是否第一次
        /// </summary>
        bool _isfirst = true ;
        /// <summary>
        /// 计时器
        /// </summary>
        static Timer timer;
        /// <summary>
        /// 动画容器A点,B点,驶出动画,驶入动画
        /// </summary>
        Storyboard sbA, sbB, sbOut, sbIn;

        /// <summary>
        /// 是否停止 :A点,B点,驶出动画,驶入动画
        /// </summary>
        bool isstopA = true, isstopB = true, isstopOut = true, isstopIn = true;
        /// <summary>
        /// 是否暂停
        /// </summary>
        static bool isprush = false;
        
        public MainWindow()
        {
            InitializeComponent();

        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e) {
            if (com != null)
                return;
            //注意串口要与具体环境对应
            string strCom = System.Configuration.ConfigurationManager.AppSettings["SName"];
            int intBaud = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SBaud"]);
            com = new ComHelper("COM4",9600);
            if (com != null)
            {
                com.Open();
                com.Init(0x33);
            }
            sbA = (Storyboard)FindResource("sb_A");
            sbB = (Storyboard)FindResource("sb_B");
            sbOut = (Storyboard)FindResource("sb_Out");
            sbIn = (Storyboard)FindResource("sb_In");
            sbA.Completed += sb_Completed;
            sbB.Completed += sb_Completed;
            sbOut.Completed += sb_Completed;
            sbIn.Completed += sb_Completed;
            timer = new Timer(200);
            timer.Elapsed += timer_Elapsed;
            timer.Start();

            
        }
        /// <summary>
        /// 窗体卸载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            if (com != null)
                com.Close();
            timer.Stop();
        }
        /// <summary>
        /// 动画完成时执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void sb_Completed(object sender, EventArgs e)
        {
            var sbname = ((sender as ClockGroup).Timeline as Storyboard).Name;
           
            switch (sbname)
            {
                case "sb_A":
                    isstopA = true;
                    break;
                case "sb_B":
                    isstopB = true;
                    break;
                case "sb_Out":
                    isstopOut = true;
                    break;
                case "sb_In":
                    isstopIn = true;
                    break;
            }
        }
        /// <summary>
        /// 计时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //防止未执行以下代码重新计时
            var tt = sender as Timer;
            tt.Stop();
            if (_isfirst)
            {
                _isfirst = false ;
                com.Init(0x33);
            }

            var bts14 = com.GetD14Data(0x33);
            if (bts14 != null)
                StardControl(bts14[0]);
            //0x40 读取ADC数据
            var ad0 = com.GetRetData(0x33, 0x40, 0x00);
            if (ad0 != null)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    var i0 = (int)ad0[0];
                    pro_ad1.Value = i0 / 2.55;
                    lbad1.Content = i0.ToString();
                }));
            }
            //0x40 读取ADC数据
            var ad1 = com.GetRetData(0x33, 0x40, 0x01);
            if (ad1 != null)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    var i1 = (int)ad1[0];
                    pro_ad2.Value = i1 / 2.55;
                    lbad2.Content = (i1).ToString();
                }));
            }

            if (!isprush)
                tt.Start();//启动监听

        }

        /// <summary>
        /// 根据数值执行UI动画效果
        /// </summary>
        /// <param name="bt">数值</param>
        void StardControl(byte bt)
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                img_B.Source = new BitmapImage(new Uri("Images/switch_0.png", UriKind.Relative));
                img_A.Source = new BitmapImage(new Uri("Images/switch_0.png", UriKind.Relative));
                img_Out.Source = new BitmapImage(new Uri("Images/switch_0.png", UriKind.Relative));
                img_In.Source = new BitmapImage(new Uri("Images/switch_0.png", UriKind.Relative));
            }));
            if ((bt & 0x01) == 0)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    Storyboard sb = (Storyboard)FindResource("sb_B");

                    StardSb(sb);
                }));
            }
            else if ((bt & 0x02) == 0x00)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    Storyboard sb = (Storyboard)FindResource("sb_A");
                    StardSb(sb);
                }));
            }
            else if ((bt & 0x04) == 0x04)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    Storyboard sb = (Storyboard)FindResource("sb_Out");
                    StardSb(sb);
                }));
            }
            else if ((bt & 0x08) == 0x08)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    Storyboard sb = (Storyboard)FindResource("sb_In");
                    StardSb(sb);
                }));
            }
        }
        /// <summary>
        /// 动画执行方法
        /// </summary>
        /// <param name="sb"></param>
        void StardSb(Storyboard sb)
        {

            switch (sb.Name)
            {
                case "sb_B":
                    sbB.Stop();
                    sbOut.Stop();
                    sbIn.Stop();
                    if (isstopB)
                    {
                        sb.Begin(brd);
                        
                    }

                    img_B.Source = new BitmapImage(new Uri("Images/switch_1.png", UriKind.Relative));
                    break;

                case "sb_A":
                     
                     sbA.Stop();
                    sbOut.Stop();
                    sbIn.Stop();
                    if (isstopA)
                    {
                        sb.Begin(brd);
                        
                    }
                    img_A.Source = new BitmapImage(new Uri("Images/switch_1.png", UriKind.Relative));
   
                    break;
                case "sb_In":
                     sbA.Stop();
                    sbB.Stop();
                    sbOut.Stop();
                    if (isstopIn)
                    {
                        sb.Begin(brd);
                        
                    }
                    img_In.Source = new BitmapImage(new Uri("Images/switch_1.png", UriKind.Relative));
                    break;
                case "sb_Out":
                    sbA.Stop();
                    sbB.Stop();
                    sbIn.Stop();
                    if (isstopOut)
                    {
                        sb.Begin(brd);
                        
                    }
                    img_Out.Source = new BitmapImage(new Uri("Images/switch_1.png", UriKind.Relative));
                    break;
            }
        }
    }
}
相关推荐
华奥系科技8 小时前
智慧经济新格局:解码社区、园区与城市一体化建设逻辑
大数据·人工智能·科技·物联网·安全
TDengine (老段)9 小时前
TDengine IDMP 组态面板 —— 画布
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
蓝奥声科技15 小时前
扩展式智能插座,破解多国标准与定制需求的新思路
物联网·智能用电计量插座·lpiot 低功耗物联网·外贸插座
Zevalin爱灰灰16 小时前
零基础入门学用物联网(ESP8266) 第一部分 基础知识篇(三)
单片机·物联网·嵌入式·esp8266
我爱我家88217 小时前
亚洲艺术电影节携澳门文化亮相深圳
人工智能·物联网·算法·区块链·爬山算法
物联通信量讯说17 小时前
从5G迈向未来通信时代,量讯物联深耕连接基础能力
物联网·5g·信息与通信·iot·通信·6g·量讯物联
搜佛说17 小时前
RocksDB, SQLite, TDengine Edge, LiteDB与sfsDb选型
物联网·edge·sqlite·边缘计算·时序数据库·iot·tdengine
沐欣工作室_lvyiyi17 小时前
基于物联网的体温心率监测系统(论文+源码)
stm32·单片机·嵌入式硬件·物联网·体温心率
QYR_111 天前
香叶醇行业深度解析:香精香料领域核心原料的发展潜力与挑战
大数据·人工智能·物联网
taxunjishu1 天前
塔讯总线协议转换信捷 PLC 对接 TCP/IP 设备实战方案
网络·物联网·自动化