C#控制红外报警,每报警一次后,数分钟内禁止报警

dyz7910980 2013-12-06 03:13:26
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Collections;
using System.Threading;


namespace 串口通信
{
public partial class Form1 : Form
{
public bool isFirstTimeToStart = true;
private int timeStamp = 50;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 初始化串口属性
/// </summary>
public void InitializeSerialPort()
{
if (isFirstTimeToStart == true)
{
port.DataBits = 8;
port.PortName = this.comboBox1.SelectedItem.ToString();
port.BaudRate = 19200;
port.DiscardNull = false;
port.DtrEnable = false;
port.Handshake = Handshake.None;
port.Parity = Parity.None;
port.ParityReplace = Convert.ToByte("63");
port.RtsEnable = false;
port.StopBits = StopBits.One;
isFirstTimeToStart = false;
}
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void comboBox1_DropDown(object sender, EventArgs e)
{
///加载计算机上所有的COM串口
comboBox1.Items.Clear();
string[] ports = SerialPort.GetPortNames();
foreach (string p in ports)
{
comboBox1.Items.Add(p);
}
}

private void button1_Click(object sender, EventArgs e)
{
InitializeSerialPort();
port.Open();
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}

void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
ArrayList array = new ArrayList();
READAGAIN:
while (port.BytesToRead > 0)
{
array.Add((byte)port.ReadByte());
}
Thread.Sleep(timeStamp);
if (port.BytesToRead > 0)
{
goto READAGAIN;
}
InitializedListView2(array);
}
delegate void SetInfo(ArrayList infos);

public void InitializedListView2(ArrayList arrayList)
{
try
{
if (this.label2.InvokeRequired)
{
SetInfo ss = new SetInfo(InitializedListView2);
this.Invoke(ss, new object[] { arrayList });
}
else
{
byte[] data = new byte[arrayList.Count + 1];
string str = "";
for (int i = 0; i < arrayList.Count; i++)
{
data[i] = (byte)(arrayList[i]);
string aa = data[i].ToString("X");
str += (data[i].ToString("X").Length == 2 ? data[i].ToString("X") : "0" + data[i].ToString("X")) + " ";
}
if (str.Trim().Equals("00 1E 98"))

{
label2.Text = "离开红外感应!";
}
else if (str.Trim().Equals("00 1E 9E"))
{
label2.Text = "进入红外感应!";
MessageBox.Show("警告");
}
else
{
label2.Text = "错误!";
}
textBox1.Text += str + "\r\n";
}
}
catch (Exception)
{
throw;
}
}

protected void DataReceived(string info)
{
//这里是要实现的功能,我只测试读出,并做简单的判断,如果你有想法,按自己的想法实现。
// rtbSerialInfo.Text += (SignalToHexCode(info).ToUpper()) + "\r\n";
switch (info)
{
case "00 1E 98":
label2.Text = "离开红外感应!";
break;
case "00 1E 9E":
label2.Text = "进入红外感应!";
break;
default:
label2.Text = "错误!";
break;
}
}

private void button3_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}

private void Form1_Load(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
}
}

private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.Visible = true;
this.TopMost = true;
this.WindowState = FormWindowState.Normal;
this.Activate();
}

private void exitMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定要退出终端服务程序吗?", "确认", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
notifyIcon1.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) //关闭按钮事件
{
e.Cancel = true;
this.Hide();
}

private void Form1_SizeChanged(object sender, EventArgs e) //最小化事件按钮
{
this.Hide();
}

private void hideMenuItem_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}

private void showMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
}

private void exitMenuItem_Click_1(object sender, EventArgs e)
{
if (MessageBox.Show("你确定要退出终端服务程序吗?", "确认", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
notifyIcon1.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}
}

private void timer1_Tick(object sender, EventArgs e)
{
Thread.Sleep(60000);
timer1.Stop();
}

}
}


要代码,让这个程序没报警一次,过一分钟后才能再次报警。
要详细代码,
...全文
322 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞小猪 2013-12-06
  • 打赏
  • 举报
回复
我跪了
dyz7910980 2013-12-06
  • 打赏
  • 举报
回复
闹不了啊。。。
threenewbee 2013-12-06
  • 打赏
  • 举报
回复
引用 7 楼 dyz7910980 的回复:
还是在报警啊!。。。。
自己调试下。
dyz7910980 2013-12-06
  • 打赏
  • 举报
回复
引用 5 楼 wg5945 的回复:
DateTime.Now.Ticks
。。。。
dyz7910980 2013-12-06
  • 打赏
  • 举报
回复


还是在报警啊!。。。。
threenewbee 2013-12-06
  • 打赏
  • 举报
回复
引用 4 楼 dyz7910980 的回复:
[quote=引用 3 楼 caozhy 的回复:] [quote=引用 2 楼 dyz7910980 的回复:] [quote=引用 1 楼 caozhy 的回复:] 添加一个私有变量 DateTime LastDateTime; 在定时器的报警逻辑中写: if (new TimeSpan(DateTime.Now.Tick - LastDateTime.Tick).TotalMinutes > 多少分钟) { 报警 LastDateTime = DateTime.Now; }
不行。。。[/quote] 怎么不行。[/quote][/quote] 首先,给你的是思路 其次,你抄都抄错了
wg5945 2013-12-06
  • 打赏
  • 举报
回复
DateTime.Now.Ticks
dyz7910980 2013-12-06
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
[quote=引用 2 楼 dyz7910980 的回复:]
[quote=引用 1 楼 caozhy 的回复:]
添加一个私有变量
DateTime LastDateTime;
在定时器的报警逻辑中写:
if (new TimeSpan(DateTime.Now.Tick - LastDateTime.Tick).TotalMinutes > 多少分钟)
{
报警
LastDateTime = DateTime.Now;
}
不行。。。[/quote]
怎么不行。[/quote]
threenewbee 2013-12-06
  • 打赏
  • 举报
回复
引用 2 楼 dyz7910980 的回复:
[quote=引用 1 楼 caozhy 的回复:] 添加一个私有变量 DateTime LastDateTime; 在定时器的报警逻辑中写: if (new TimeSpan(DateTime.Now.Tick - LastDateTime.Tick).TotalMinutes > 多少分钟) { 报警 LastDateTime = DateTime.Now; }
不行。。。[/quote] 怎么不行。
dyz7910980 2013-12-06
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
添加一个私有变量 DateTime LastDateTime; 在定时器的报警逻辑中写: if (new TimeSpan(DateTime.Now.Tick - LastDateTime.Tick).TotalMinutes > 多少分钟) { 报警 LastDateTime = DateTime.Now; }
不行。。。
threenewbee 2013-12-06
  • 打赏
  • 举报
回复
添加一个私有变量 DateTime LastDateTime; 在定时器的报警逻辑中写: if (new TimeSpan(DateTime.Now.Tick - LastDateTime.Tick).TotalMinutes > 多少分钟) { 报警 LastDateTime = DateTime.Now; }

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧