目录
一、实验目的
二、实验原理
三、实验内容及步骤
四、实验结果
五、核心代码
一、实验目的
- 学习试验模块上线路的连接操作
- 理解掌握红外传感器的工作原理
- 实现对红外传感器数据的接收和处理
二、实验原理
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;
}
}
}
}