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

目录

一、实验目的

二、实验原理

三、实验内容及步骤

四、实验结果

五、核心代码


一、实验目的

  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;
            }
        }
    }
}
相关推荐
F133168929571 天前
5030A 芯片 24V 转 5V 15A 大电流快充选型
网络·单片机·嵌入式硬件·物联网·汽车
凯禾瑞华养老实训室1 天前
产教融合新抓手:智慧健康养老服务与管理实训室报价及人才培育路径
大数据·人工智能·物联网·ar·vr·智慧健康养老服务与管理
Deepoch1 天前
中国具身智能三大路径:极限挑战、柔性操作、普惠赋能,竞合共生
大数据·人工智能·物联网·机器人·具身模型·deepoc
亿道电子Emdoor1 天前
【Arm】MDK查看语句的执行累积时间和次数
stm32·单片机·物联网
Tel199253080041 天前
CCD相机同步外触发拍照抓拍识别高速脉冲计数器信号采集模块
单片机·数码相机·物联网·自动化·工业自动化·仪器仪表
乐迪信息1 天前
乐迪信息:AI摄像机识别煤矿出入井车辆数量异常检测
大数据·运维·人工智能·物联网·安全
Wpa.wk1 天前
硬件环境配置-两台电脑进行局域网构建
物联网·microsoft·电脑·信息与通信·局域网构建
盈创力和20071 天前
工业物联网下的智能安全防线:基于以太网的多参量传感器设计与应用
物联网·气体传感器·以太网多参量传感器·智能环境监测终端·双气体模组·三十四种组合可定制
珠海西格电力1 天前
零碳园区边缘计算节点规划:数字底座的硬件部署与能耗控制方案
运维·人工智能·物联网·能源·边缘计算
DBA小马哥1 天前
Oracle迁移金仓全攻略:工业IOT场景下的易用性与安全保障
数据库·物联网·安全·oracle