1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.IO.Ports; 9 using System.Collections; 10 using System.Threading; 11 12 13 namespace 串口通信 14 { 15 public partial class Form1 : Form 16 { 17 public bool isFirstTimeToStart = true; 18 private int timeStamp = 50; 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 /// <summary> 24 /// 初始化串口属性 25 /// </summary> 26 public void InitializeSerialPort() 27 { 28 if (isFirstTimeToStart == true) 29 { 30 port.DataBits = 8; 31 port.PortName = this.comboBox1.SelectedItem.ToString(); 32 port.BaudRate = 19200; 33 port.DiscardNull = false; 34 port.DtrEnable = false; 35 port.Handshake = Handshake.None; 36 port.Parity = Parity.None; 37 port.ParityReplace = Convert.ToByte("63"); 38 port.RtsEnable = false; 39 port.StopBits = StopBits.One; 40 isFirstTimeToStart = false; 41 } 42 } 43 44 private void button2_Click(object sender, EventArgs e) 45 { 46 this.Close(); 47 } 48 49 private void comboBox1_DropDown(object sender, EventArgs e) 50 { 51 ///加载计算机上所有的COM串口 52 comboBox1.Items.Clear(); 53 string[] ports = SerialPort.GetPortNames(); 54 foreach (string p in ports) 55 { 56 comboBox1.Items.Add(p); 57 } 58 } 59 60 private void button1_Click(object sender, EventArgs e) 61 { 62 InitializeSerialPort(); 63 port.Open(); 64 port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); 65 } 66 67 void port_DataReceived(object sender, SerialDataReceivedEventArgs e) 68 { 69 ArrayList array = new ArrayList(); 70 READAGAIN: 71 while (port.BytesToRead > 0) 72 { 73 array.Add((byte)port.ReadByte()); 74 } 75 Thread.Sleep(timeStamp); 76 if (port.BytesToRead > 0) 77 { 78 goto READAGAIN; 79 } 80 InitializedListView2(array); 81 } 82 delegate void SetInfo(ArrayList infos); 83 84 public void InitializedListView2(ArrayList arrayList) 85 { 86 try 87 { 88 if (this.label2.InvokeRequired) 89 { 90 SetInfo ss = new SetInfo(InitializedListView2); 91 this.Invoke(ss, new object[] { arrayList }); 92 } 93 else 94 { 95 byte[] data = new byte[arrayList.Count + 1]; 96 string str = ""; 97 for (int i = 0; i < arrayList.Count; i++) 98 { 99 data[i] = (byte)(arrayList[i]); 100 string aa = data[i].ToString("X"); 101 str += (data[i].ToString("X").Length == 2 ? data[i].ToString("X") : "0" + data[i].ToString("X")) + " "; 102 } 103 if (str.Trim().Equals("00 1E 98")) 104 105 { 106 label2.Text = "离开红外感应!"; 107 } 108 else if (str.Trim().Equals("00 1E 9E")) 109 { 110 label2.Text = "进入红外感应!"; 111 MessageBox.Show("警告"); 112 } 113 else 114 { 115 label2.Text = "错误!"; 116 } 117 textBox1.Text += str + "\r\n"; 118 } 119 } 120 catch (Exception) 121 { 122 throw; 123 } 124 } 125 126 protected void DataReceived(string info) 127 { 128 //这里是要实现的功能,我只测试读出,并做简单的判断,如果你有想法,按自己的想法实现。 129 // rtbSerialInfo.Text += (SignalToHexCode(info).ToUpper()) + "\r\n"; 130 switch (info) 131 { 132 case "00 1E 98": 133 label2.Text = "离开红外感应!"; 134 break; 135 case "00 1E 9E": 136 label2.Text = "进入红外感应!"; 137 break; 138 default: 139 label2.Text = "错误!"; 140 break; 141 } 142 } 143 144 private void button3_Click(object sender, EventArgs e) 145 { 146 this.textBox1.Text = ""; 147 } 148 149 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) 150 { 151 if (e.Button == MouseButtons.Left) 152 { 153 this.Show(); 154 this.WindowState = FormWindowState.Normal; 155 this.Activate(); 156 } 157 } 158 159 private void Form1_Load(object sender, EventArgs e) 160 { 161 if (this.WindowState == FormWindowState.Minimized) 162 { 163 this.Visible = false; 164 } 165 } 166 167 private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) 168 { 169 this.Visible = true; 170 this.TopMost = true; 171 this.WindowState = FormWindowState.Normal; 172 this.Activate(); 173 } 174 175 private void exitMenuItem_Click(object sender, EventArgs e) 176 { 177 if (MessageBox.Show("你确定要退出终端服务程序吗?", "确认", MessageBoxButtons.OKCancel, 178 MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) 179 { 180 notifyIcon1.Visible = false; 181 this.Close(); 182 this.Dispose(); 183 Application.Exit(); 184 } 185 } 186 private void Form1_FormClosing(object sender, FormClosingEventArgs e) //关闭按钮事件 187 { 188 e.Cancel = true; 189 this.Hide(); 190 } 191 192 private void Form1_SizeChanged(object sender, EventArgs e) //最小化事件按钮 193 { 194 this.Hide(); 195 } 196 197 private void hideMenuItem_Click(object sender, EventArgs e) 198 { 199 this.Show(); 200 this.WindowState = FormWindowState.Normal; 201 this.Activate(); 202 } 203 204 private void showMenuItem_Click(object sender, EventArgs e) 205 { 206 this.Hide(); 207 } 208 209 220 221 private void timer1_Tick(object sender, EventArgs e) 222 { 223
if (new TimeSpan(DateTime.Now.Ticks - LastDateTime.Ticks).TotalMinutes > 1)
{
//报警
LastDateTime = DateTime.Now;
}
225 } 226 227 } 228 }
程序没执行一次,下次在执行只能等一分钟后,要详细代码。
Thread.Sleep();等待,如果你没有其他事要做的话
不行啊。。还是在报警。我意思是想让程序每分钟只能报警一次,再次报警只能等一分钟后。等待还是在报警
@董毛毛: 你意思是等一分钟后再报警吗》
@幕三少: 对对对
@董毛毛:兄弟还没解决啊?
@幕三少: 解决啦、就一句代码就搞定了。。。哈哈哈哈
@幕三少: Thread.Sleep(60000);,然后就解决了。不过又有新需求了
@董毛毛: Thread.Sleep(60000);会阻塞当前线程所有消息,如果你是单线程且是窗体线程,那么窗体会一直不可用,用Application.Dovents可以避免这种情况
没看你代码,一般延时用var start=datetime. now; while(start. add(600000)>datetime. now)application.doevents();手机发的,意思是等待600秒,但不会阻塞程序的UI界面哦。