复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375using System; using System.IO.Ports;//串口 using System.Drawing; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Text; using System.Timers; using System.Windows.Forms; using System.Media; namespace Monitoring_System { public partial class Form1 : Form { private SerialPort COMM = new SerialPort(); private StringBuilder builder_temp = new StringBuilder();//处理字符串 private string send_data; private byte flag = 0; int minutes = 0; int state_canshu = 0; int state_1 = 0, state_2 = 0, state_3 = 0; byte[] SendBuf = new byte[2];//定义串口发送字符串变量 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { ports.Items.Add("COM1"); //ports为串口下拉组合框 ports.Items.Add("COM2"); ports.Items.Add("COM3"); ports.Items.Add("COM4"); ports.Items.Add("COM5"); botelu.Items.Add("4800"); //botelu为波特率下拉组合框 botelu.Items.Add("9600"); botelu.Items.Add("19200"); botelu.Items.Add("38400"); botelu.Items.Add("57600"); botelu.Items.Add("115200"); comboBox1.Items.Add("5"); comboBox1.Items.Add("6"); comboBox1.Items.Add("7"); comboBox1.Items.Add("8"); comboBox2.Items.Add("1"); comboBox2.Items.Add("1.5"); comboBox2.Items.Add("2"); comboBox3.Items.Add("无"); comboBox3.Items.Add("奇校验"); comboBox3.Items.Add("偶校验"); ports.SelectedIndex = ports.Items.IndexOf("COM1"); //comboBox1.SelectedIndex = 0; botelu.SelectedIndex = botelu.Items.IndexOf("9600"); // comboBox2.SelectedIndex =1; comboBox1.SelectedIndex = 3; comboBox2.SelectedIndex = 0; comboBox3.SelectedIndex = 0; // COMM.NewLine = "/r/n"; this.COMM.RtsEnable = true;//根据实际情况 //添加事件注册 COMM.DataReceived += COMM_DataReceived; System.Timers.Timer aTimer = new System.Timers.Timer(); aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); aTimer.Interval = 1000;//1s aTimer.Enabled = true; GC.KeepAlive(aTimer); this.textBox7.Enabled = false; this.textBox4.Enabled = false; this.textBox8.Enabled = false; this.textBox7.Text = "3000"; this.textBox4.Text = "30"; this.textBox8.Text = "3000"; this.textBox11.Text = "关闭"; this.textBox10.Text = "正常"; this.textBox9.Text = "打开"; this.textBox9.Enabled = false; this.textBox10.Enabled = false; this.textBox11.Enabled = false; } /// <summary> /// 串口接收数据处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void COMM_DataReceived(object sender, SerialDataReceivedEventArgs e) { int n = this.COMM.BytesToRead;//数据先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致 byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据 this.COMM.Read(buf, 0, n);//读取缓冲数据 //因为要访问ui资源,所以需要使用invoke方式同步ui。 this.Invoke((EventHandler) (delegate { byte flag_judge = 0; //直接按ASCII规则转换成字符串 string rece_temp = Encoding.ASCII.GetString(buf); builder_temp.Append(rece_temp); while (flag_judge < n) { if (buf[flag_judge] == '#') { flag++; int length = builder_temp.Length; string temp_data = builder_temp.ToString(); send_data = temp_data.Remove(length - 1); builder_temp.Clear(); break; } flag_judge++; } for (int i = 0; i < n; i++) { buf[i] = 0; } if (flag >= 2) { if (flag >= 100) { flag = 0; } deal_value(send_data); } })); } /// <summary> /// 处理接收的数据 /// </summary> /// <param name="value"></param> private void deal_value(string value) { byte[] RxdBuf = System.Text.Encoding.Default.GetBytes(value); if (RxdBuf[0] == 'A')//终端1开始标志 { if (RxdBuf[1] == 0x31) { this.textBox6.Text = "有人"; Playmusic(); } else { this.textBox6.Text = "无人"; } this.textBox5.Text = RxdBuf[2].ToString() + RxdBuf[3].ToString() + RxdBuf[4].ToString() + RxdBuf[5].ToString();//烟雾浓度 } if (RxdBuf[0] == 'B')//终端2开始标志 { this.textBox1.Text = RxdBuf[1].ToString() + RxdBuf[2].ToString();//温度 this.textBox3.Text = RxdBuf[3].ToString() + RxdBuf[4].ToString();//湿度 this.textBox2.Text = RxdBuf[5].ToString() + RxdBuf[6].ToString() + RxdBuf[7].ToString() + RxdBuf[8].ToString();//光照强度 } } //开按钮 private void open_Click(object sender, EventArgs e) { this.COMM.PortName = ports.Text; this.COMM.BaudRate = int.Parse(botelu.Text); this.COMM.Parity = Parity.None; this.COMM.StopBits = StopBits.One; this.COMM.DataBits = 8; try { this.COMM.Open(); } catch (Exception ex) { //捕获到异常信息,创建一个新的comm对象,之前的不能用了。 this.COMM = new SerialPort(); //现实异常信息给客户。 MessageBox.Show(ex.Message); } close.Enabled = this.COMM.IsOpen; open.Enabled = !this.COMM.IsOpen; } //关按钮 private void close_Click(object sender, EventArgs e) { if (this.COMM.IsOpen) { this.COMM.Close(); } open.Enabled = !this.COMM.IsOpen; close.Enabled = this.COMM.IsOpen; } System.DateTime currentTime = new System.DateTime(); //周期事件 private void OnTimedEvent(object source, ElapsedEventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; minutes++; int hours = DateTime.Now.Hour; //label3.Text = DateTime.Now.Hour.ToString(); if (minutes == 1&&state_canshu == 1) { int yanwu_1 = Convert.ToInt32(textBox5.Text);//正常值 int yanwu_2 = Convert.ToInt32(textBox7.Text);//设定值 if (yanwu_1 > yanwu_2)//烟雾异常 { if (state_1 == 1)//烟雾异常 { } else { SendBuf[0] = 0x31; SendBuf[1] = 0x31; COMM.Write(SendBuf, 0, 2); this.textBox10.Text = "切断"; state_1 = 1; } } else { if (state_1 == 1)//正在工作 { SendBuf[0] = 0x31; SendBuf[1] = 0x30; COMM.Write(SendBuf, 0, 2); this.textBox10.Text = "正常"; state_1 = 0; } else { } } } if (minutes == 2&&state_canshu == 1) { int wendu_1 = Convert.ToInt32(textBox1.Text);//正常值 int wendu_2 = Convert.ToInt32(textBox4.Text);//设定值 if ((wendu_1 > wendu_2) &&(hours>=9&&hours<=17)) //if (wendu_1 > wendu_2)//温度异常 { if (state_2 == 1)// { } else { SendBuf[0] = 0x32; SendBuf[1] = 0x31; COMM.Write(SendBuf, 0, 2); this.textBox11.Text = "打开"; state_2 = 1; } } else { if (state_2 == 1)//正在工作 { SendBuf[0] = 0x32; SendBuf[1] = 0x30; COMM.Write(SendBuf, 0, 2); this.textBox11.Text = "关闭"; state_2 = 0; } else { } } } if (minutes >= 3 && state_canshu == 1) { int guangzhao_1 = Convert.ToInt32(textBox2.Text);//正常值 int guangzhao_2 = Convert.ToInt32(textBox8.Text);//设定值 minutes = 0; if (guangzhao_1 < guangzhao_2)//光照 { if (state_3 == 1)// { } else { SendBuf[0] = 0x32; SendBuf[1] = 0x33; COMM.Write(SendBuf, 0, 2); this.textBox9.Text = "打开"; state_3 = 1; } } else { if (state_3 == 1)//正在工作 { SendBuf[0] = 0x32; SendBuf[1] = 0x32; COMM.Write(SendBuf, 0, 2); this.textBox9.Text = "关闭"; state_3 = 0; } else { } } } } //确定按钮 private void button1_Click(object sender, EventArgs e) { state_canshu = 1; this.textBox7.Enabled = false; this.textBox4.Enabled = false; this.textBox8.Enabled = false; } //取消按钮 private void button2_Click(object sender, EventArgs e) { state_canshu = 0; this.textBox7.Enabled = true; this.textBox4.Enabled = true; this.textBox8.Enabled = true; } //音乐函数 public static void Playmusic() { try { SoundPlayer filew = new SoundPlayer(); filew.SoundLocation = @"D:air.wav"; filew.Load(); filew.Play();//播放一次 } catch (Exception) { MessageBox.Show("音效初始化失败!"); } } private void time_Tick(object sender, EventArgs e) { } }
最后
以上就是重要世界最近收集整理的关于基于ZigBee的智能监控系统-上位机代码的全部内容,更多相关基于ZigBee内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复