温湿度系统(花葵、库房检测)

测试命令

启动加热器
fa 10 01 01 b0 55 fb FA 10 01 02 00 00 20 FB
停止加热器
fa 10 01 03 31 94 fb FA 10 01 04 00 00 0A FB

启动除湿器
fa 10 01 05 b1 96 fb FA 10 01 06 00 00 1C FB
停止除湿器
fa 10 01 07 30 57 fb FA 10 01 08 00 00 25 FB

启动排风
fa 10 01 09 b1 93 fb FA 10 01 0A 00 00 06 FB
停止排风
fa 10 01 0b 30 52 fb FA 10 01 0C 00 00 0F FB

启动报警
fa 10 00 0d b1 c0 fb
停止报警
fa 10 00 0f 30 01 fb

修改设备地址
fa 10 00 59 10 11 f8 1c fb
修改设备地址应答
FA 10 11 69 01 BB 71 FB

fa 10 11 59 10 00 3d 2c fb


温湿度:1005
控制:1001
10 00 59 10 01 f9 d0
监控
fa 10 01 54 70 6a fb
fa 10 02 54 70 9a fb
fa 10 03 54 71 0a fb
控制
fa 20 03 01 b1 3a fb


1 1 监控点1 1001 2001

闲话不多说,直接看代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Drawing;
  5 using System.Drawing.Imaging;
  6 
  7 namespace CKJKRJ.common
  8 {
  9 
 10     /// <summary>
 11     /// 绘制曲线图
 12     /// </summary>
 13     public class Curve
 14     {
 15         public Curve()
 16         { }
 17         private Graphics objGraphics; //Graphics 类提供将对象绘制到显示设备的方法
 18         private Bitmap objBitmap; //位图对象
 19 
 20         private int m_Width = 640; //图像宽度
 21         private int m_Height = 480; //图像高度
 22         private float m_XSlice = 50; //X轴刻度宽度
 23         private float m_YSlice = 50; //Y轴刻度宽度
 24         private float m_YSliceValue = 20; //Y轴刻度的数值宽度
 25         private float m_YSliceBegin = 0; //Y轴刻度开始值
 26         private float m_Tension = 0.5f;//设置张力
 27         private string m_Title = "节目访问量曲线图"; //Title
 28         private string m_Unit = "万元"; //单位
 29         private string m_XAxisText = "日期"; //X轴说明文字
 30         private string m_YAxisText = ""; //Y轴说明文字
 31         private string[] m_Keys = new string[] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }; //
 32         private int[] m_Values = new int[] { 32, 12, 32, 43, 54, 35, 35, 42, 63, 35, 35, 67 };//
 33         private Color m_BgColor = Color.Snow; //背景
 34         private Color m_TextColor = Color.Black; //文字颜色
 35         private Color m_BorderColor = Color.Black; //整体边框颜色
 36         private Color m_AxisColor = Color.Black; //轴线颜色
 37         private Color m_AxisTextColor = Color.Black; //轴说明文字颜色
 38         private Color m_SliceTextColor = Color.Black; //刻度文字颜色
 39         private Color m_SliceColor = Color.Black; //刻度颜色
 40         private Color m_CurveColor = Color.Red; //曲线颜色
 41 
 42         public int Width
 43         {
 44             set
 45             {
 46                 if (value < 300)
 47                 {
 48                     m_Width = 300;
 49                 }
 50                 else
 51                 {
 52                     m_Width = value;
 53                 }
 54             }
 55             get
 56             {
 57                 return m_Width;
 58             }
 59         }
 60 
 61         public int Height
 62         {
 63             set
 64             {
 65                 if (value < 300)
 66                 {
 67                     m_Height = 300;
 68                 }
 69                 else
 70                 {
 71                     m_Height = value;
 72                 }
 73             }
 74             get
 75             {
 76                 return m_Height;
 77             }
 78         }
 79 
 80         public float XSlice
 81         {
 82             set
 83             {
 84                 m_XSlice = value;
 85             }
 86             get
 87             {
 88                 return m_XSlice;
 89             }
 90         }
 91 
 92         public float YSlice
 93         {
 94             set
 95             {
 96                 m_YSlice = value;
 97             }
 98             get
 99             {
100                 return m_YSlice;
101             }
102         }
103 
104         public float YSliceValue
105         {
106             set
107             {
108                 m_YSliceValue = value;
109             }
110             get
111             {
112                 return m_YSliceValue;
113             }
114         }
115 
116         public float YSliceBegin
117         {
118             set
119             {
120                 m_YSliceBegin = value;
121             }
122             get
123             {
124                 return m_YSliceBegin;
125             }
126         }
127 
128         public float Tension
129         {
130             set
131             {
132                 if (value < 0.0f && value > 1.0f)
133                 {
134                     m_Tension = 0.5f;
135                 }
136                 else
137                 {
138                     m_Tension = value;
139                 }
140             }
141             get
142             {
143                 return m_Tension;
144             }
145         }
146 
147         public string Title
148         {
149             set
150             {
151                 m_Title = value;
152             }
153             get
154             {
155                 return m_Title;
156             }
157         }
158 
159         public string Unit
160         {
161             set
162             {
163                 m_Unit = value;
164             }
165             get
166             {
167                 return m_Unit;
168             }
169         }
170 
171         public string[] Keys
172         {
173             set
174             {
175                 m_Keys = value;
176             }
177             get
178             {
179                 return m_Keys;
180             }
181         }
182 
183         public int[] Values
184         {
185             set
186             {
187                 m_Values = value;
188             }
189             get
190             {
191                 return m_Values;
192             }
193         }
194 
195         public Color BgColor
196         {
197             set
198             {
199                 m_BgColor = value;
200             }
201             get
202             {
203                 return m_BgColor;
204             }
205         }
206 
207         public Color TextColor
208         {
209             set
210             {
211                 m_TextColor = value;
212             }
213             get
214             {
215                 return m_TextColor;
216             }
217         }
218 
219         public Color BorderColor
220         {
221             set
222             {
223                 m_BorderColor = value;
224             }
225             get
226             {
227                 return m_BorderColor;
228             }
229         }
230 
231         public Color AxisColor
232         {
233             set
234             {
235                 m_AxisColor = value;
236             }
237             get
238             {
239                 return m_AxisColor;
240             }
241         }
242 
243         public string XAxisText
244         {
245             set
246             {
247                 m_XAxisText = value;
248             }
249             get
250             {
251                 return m_XAxisText;
252             }
253         }
254 
255         public string YAxisText
256         {
257             set
258             {
259                 m_YAxisText = value;
260             }
261             get
262             {
263                 return m_YAxisText;
264             }
265         }
266 
267         public Color AxisTextColor
268         {
269             set
270             {
271                 m_AxisTextColor = value;
272             }
273             get
274             {
275                 return m_AxisTextColor;
276             }
277         }
278 
279         public Color SliceTextColor
280         {
281             set
282             {
283                 m_SliceTextColor = value;
284             }
285             get
286             {
287                 return m_SliceTextColor;
288             }
289         }
290 
291         public Color SliceColor
292         {
293             set
294             {
295                 m_SliceColor = value;
296             }
297             get
298             {
299                 return m_SliceColor;
300             }
301         }
302 
303         public Color CurveColor
304         {
305             set
306             {
307                 m_CurveColor = value;
308             }
309             get
310             {
311                 return m_CurveColor;
312             }
313         }
314 
315 
316         //生成图像并返回bmp图像对象
317         public Bitmap CreateImage()
318         {
319             InitializeGraph();
320 
321             DrawContent(ref objGraphics);
322 
323             return objBitmap;
324         }
325 
326         //初始化和填充图像区域,画出边框,初始标题
327         private void InitializeGraph()
328         {
329 
330             //根据给定的高度和宽度创建一个位图图像
331             objBitmap = new Bitmap(Width, Height);
332 
333             //从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画图)
334             objGraphics = Graphics.FromImage(objBitmap);
335 
336             //根据给定颜色(LightGray)填充图像的矩形区域 (背景)
337             objGraphics.DrawRectangle(new Pen(BorderColor, 1), 0, 0, Width, Height);
338             objGraphics.FillRectangle(new SolidBrush(BgColor), 1, 1, Width - 2, Height - 2);
339 
340             //画X轴,pen,x1,y1,x2,y2 注意图像的原始X轴和Y轴计算是以左上角为原点,向右和向下计算的
341             objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), 100, Height - 100, Width - 75, Height - 100);
342 
343             //画Y轴,pen,x1,y1,x2,y2
344             objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), 100, Height - 100, 100, 75);
345 
346             //初始化轴线说明文字
347             SetAxisText(ref objGraphics);
348 
349             //初始化X轴上的刻度和文字
350             SetXAxis(ref objGraphics);
351 
352             //初始化Y轴上的刻度和文字
353             SetYAxis(ref objGraphics);
354 
355             //初始化标题
356             CreateTitle(ref objGraphics);
357         }
358 
359         private void SetAxisText(ref Graphics objGraphics)
360         {
361             objGraphics.DrawString(XAxisText, new Font("宋体", 10), new SolidBrush(AxisTextColor), Width / 2 - 50, Height - 50);
362 
363             int X = 30;
364             int Y = (Height / 2) - 50;
365             for (int i = 0; i < YAxisText.Length; i++)
366             {
367                 objGraphics.DrawString(YAxisText[i].ToString(), new Font("宋体", 10), new SolidBrush(AxisTextColor), X, Y);
368                 Y += 15;
369             }
370         }
371 
372         private void SetXAxis(ref Graphics objGraphics)
373         {
374             int x1 = 100;
375             int y1 = Height - 110;
376             int x2 = 100;
377             int y2 = Height - 90;
378             int iCount = 0;
379             int iSliceCount = 1;
380             float Scale = 0;
381             int iWidth = (int)((Width - 200) * (50 / XSlice));
382 
383             objGraphics.DrawString(Keys[0].ToString(), new Font("宋体", 10), new SolidBrush(SliceTextColor), 85, Height - 90);
384 
385             for (int i = 0; i <= iWidth; i += 10)
386             {
387                 Scale = i * (XSlice / 50);
388 
389                 if (iCount == 5)
390                 {
391                     objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), x1 + Scale, y1, x2 + Scale, y2);
392                     //The Point!这里显示X轴刻度
393                     if (iSliceCount <= Keys.Length - 1)
394                     {
395                         objGraphics.DrawString(Keys[iSliceCount].ToString(), new Font("宋体", 10), new SolidBrush(SliceTextColor), x1 + Scale - 15, y2);
396                     }
397                     else
398                     {
399                         //超过范围,不画任何刻度文字
400                     }
401                     iCount = 0;
402                     iSliceCount++;
403                     if (x1 + Scale > Width - 100)
404                     {
405                         break;
406                     }
407                 }
408                 else
409                 {
410                     objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), x1 + Scale, y1 + 5, x2 + Scale, y2 - 5);
411                 }
412                 iCount++;
413             }
414         }
415 
416         private void SetYAxis(ref Graphics objGraphics)
417         {
418             int x1 = 95;
419             int y1 = (int)(Height - 100 - 10 * (YSlice / 50));
420             int x2 = 105;
421             int y2 = (int)(Height - 100 - 10 * (YSlice / 50));
422             int iCount = 1;
423             float Scale = 0;
424             int iSliceCount = 1;
425 
426             int iHeight = (int)((Height - 200) * (50 / YSlice));
427 
428             objGraphics.DrawString(YSliceBegin.ToString(), new Font("宋体", 10), new SolidBrush(SliceTextColor), 60, Height - 110);
429 
430             for (int i = 0; i < iHeight; i += 10)
431             {
432                 Scale = i * (YSlice / 50);
433 
434                 if (iCount == 5)
435                 {
436                     objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), x1 - 5, y1 - Scale, x2 + 5, y2 - Scale);
437                     //The Point!这里显示Y轴刻度
438                     objGraphics.DrawString(Convert.ToString(YSliceValue * iSliceCount + YSliceBegin), new Font("宋体", 10), new SolidBrush(SliceTextColor), 60, y1 - Scale);
439 
440                     iCount = 0;
441                     iSliceCount++;
442                 }
443                 else
444                 {
445                     objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), x1, y1 - Scale, x2, y2 - Scale);
446                 }
447                 iCount++;
448             }
449         }
450 
451         private void DrawContent(ref Graphics objGraphics)
452         {
453             if (Keys.Length == Values.Length)
454             {
455                 Pen CurvePen = new Pen(CurveColor, 1);
456                 PointF[] CurvePointF = new PointF[Keys.Length];
457                 float keys = 0;
458                 float values = 0;
459                 float Offset1 = (Height - 100) + YSliceBegin;
460                 float Offset2 = (YSlice / 50) * (50 / YSliceValue);
461 
462                 for (int i = 0; i < Keys.Length; i++)
463                 {
464                     keys = XSlice * i + 100;
465                     values = Offset1 - Values[i] * Offset2;
466                     CurvePointF[i] = new PointF(keys, values);
467                 }
468                 objGraphics.DrawCurve(CurvePen, CurvePointF, Tension);
469             }
470             else
471             {
472                 objGraphics.DrawString("Error!The length of Keys and Values must be same!", new Font("宋体", 16), new SolidBrush(TextColor), new Point(150, Height / 2));
473             }
474         }
475 
476         //初始化标题
477         private void CreateTitle(ref Graphics objGraphics)
478         {
479             objGraphics.DrawString(Title, new Font("宋体", 16), new SolidBrush(TextColor), new Point(5, 5));
480         }
481 
482     }
483 }
Curve
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using CKJKRJ.common;
 10 using System.Collections;
 11 using System.Runtime.InteropServices;
 12 using CKJKRJ.House;
 13 using System.IO.Ports;
 14 using System.Threading;
 15 using Microsoft.Win32;
 16 
 17 namespace CKJKRJ
 18 {
 19     public partial class FormMain : Form
 20     {
 21 
 22        // bool b = true;
 23         public System.Timers.Timer timer = null;
 24         public SerialPort port;
 25 
 26         public static string temperature = "0";           //温度
 27         public static string humidity = "0";              //湿度
 28         public static string smoke = "0";                 //烟感
 29         public static string DewPoint = "0";              //露点
 30 
 31         public static int Pl = 0;           //频率
 32 
 33         public FormMain()
 34         {
 35             InitializeComponent();
 36         }
 37 
 38         /// <summary>
 39         /// 打开登陆窗体的方法
 40         /// </summary>
 41         /// <returns></returns>
 42         private bool UserLogin()
 43         {
 44             FormLogin frmLogin = new FormLogin();
 45             try
 46             {
 47                 DialogResult result = frmLogin.ShowDialog();
 48                 if (result == DialogResult.OK)
 49                 {
 50                     return true;
 51                 }
 52                 else
 53                 {
 54                     return false;
 55                 }
 56             }
 57             finally
 58             {
 59                 frmLogin.Dispose();
 60             }
 61         }
 62         /// <summary>
 63         /// 让窗体居中方法
 64         /// </summary>
 65         private void FormCenter()
 66         {
 67             this.Location = new System.Drawing.Point((SystemInformation.PrimaryMonitorSize.Width - this.Width) / 2,
 68                 (SystemInformation.PrimaryMonitorSize.Height - this.Height) / 2);
 69 
 70         }
 71 
 72         /// <summary>
 73         /// 窗体加载事件
 74         /// </summary>
 75         /// <param name="sender"></param>
 76         /// <param name="e"></param>
 77         private void FormMain_Load(object sender, EventArgs e)
 78         {
 79             IsRegKeIsExt();
 80             StartKiller();
 81 
 82             this.panelContent.Controls.Clear(); //清空panel控件
 83 
 84             DataTable dtArea = Area.QueryArea();//查询所有的监测区域
 85 
 86             FormQueryRecord frm = new FormQueryRecord(dtArea.Rows[0]["E_ID"].ToString());
 87             frm.TopLevel = false;
 88             this.panelContent.Controls.Add(frm);
 89             frm.Show();
 90 
 91             try
 92             {
 93                 Pl = int.Parse(this.comboBox1.SelectedItem.ToString());
 94 
 95                 ZtQx.Operation.upEquipmentInfo();
 96 
 97                 FoldMenu();             //折叠菜单
 98                 CreatList(1);           //按仓库查询区域
 99                 FormCenter();           //窗体居中
100 
101                 timer = new System.Timers.Timer();
102                 timer.Interval = (double)20 * 1000;        //五分钟监控一次
103                 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
104                 timer.Enabled = true;
105                 timer.Start();
106             }
107             catch (Exception ex)
108             {
109                 MessageBox.Show(" 日志: " + ex.Message.ToString());
110             }
111         }
112 
113         //添加监控 代码 _ 周涛
114         public static ZtQx.Com oc = new ZtQx.Com();
115 
116         InspectDal inspectDal = new InspectDal();
117 
118         private string Values = "";
119         public static Hashtable hsTb = new Hashtable();
120 
121         #region 功能:获取接收值
122 
123         //private delegate void ProccessData(byte[] temp);
124 
125         //private void Received(byte[] temp)
126         //{
127         //    Invoke(new ProccessData(this.SetValue), temp);
128         //}
129 
130 
131         /// <summary>
132         /// 功能:接收值
133         /// </summary>
134         /// <param name="temp"></param>
135         public void SetDataValue(byte[] temp)
136         {
137             //try
138             //{
139                 if (temp != null) //如果不为空则合并数据
140                 {
141                     //CheckForIllegalCrossThreadCalls = false;
142                     Values += ZtQx.Func.ByteArrayToHexString(temp);
143                 }
144 
145                 int count = Values.Length;
146 
147                 if (count > 39)
148                 {
149                     string[] OldStr = Values.Split(new string[] { "FB" }, StringSplitOptions.RemoveEmptyEntries);
150 
151                     ArrayList aryOld = new ArrayList(OldStr);
152                     ArrayList aryNew = new ArrayList();
153                     for (int i = 0; i < aryOld.Count; i++)
154                     {
155                         int Num = (aryOld[i].ToString().Trim() + " FB").Length;
156                         if (Num == 38)
157                         {
158                             aryNew.Add(aryOld[i].ToString().Trim() + " FB");
159                         }
160                     }
161                     for (int i = 0; i < aryNew.Count; i++)
162                     {
163                         Values = aryNew[i].ToString();
164                         //循环数据
165                         NewFaTFb(ref Values);
166                     }
167                 }
168                 else
169                 {
170                     NewFaTFb(ref Values);
171                 }
172             //}
173             //catch (Exception)
174             //{
175             //    MessageBox.Show("读取操作数据出错!", "系统提示");
176             //}
177         }
178         #endregion
179 
180 
181         private void NewFaTFb(ref string Values)
182         {
183             //try
184             //{
185                 //判断数据是否以 FB结束 , 没结束不执行赋值方法
186                 int start = Values.IndexOf("FA");
187                 int end = Values.IndexOf("FB");
188 
189                 if (start >= 0 && start < end)
190                 {
191 
192                     if (Values.Length == 24)            //控制
193                     {
194                         Values = string.Empty;
195                         return;
196                     }
197 
198                     if (Values.Trim().Length == 38)            //监控
199                     {
200                         //把16进制字符 转换 为字符
201                         string[] strBy = Values.Split(new char[] { ' ' });
202 
203                         //判断命令 号 是否是属于查询
204                         if (strBy[3].ToString().Equals("69"))           //修改 设备 , 返回69
205                         {
206                             Values = string.Empty;
207                             return;
208                         }
209 
210 
211                         //添加进入到数据库
212                         ArrayList aryList = new ArrayList();
213                         string sbbh = strBy[1] + "" + strBy[2];
214 
215                         DataTable dt = inspectDal.GetHouseByCount(sbbh);
216                         //查询数据库 , 判断该设备是否存在
217                         if (dt.Rows.Count <= 0)
218                         {
219                             //MessageBox.Show("该设备不存在!" + sbbh);
220                             return;
221                         }
222                         if (dt.Rows.Count > 1)
223                         {
224                             MessageBox.Show("该设备重复!");
225                         }
226 
227                         temperature = ZtQx.Func.Hex2Ten(strBy[5].ToString()).ToString();
228                         humidity = ZtQx.Func.Hex2Ten(strBy[6].ToString()).ToString();
229                         smoke = ZtQx.Func.Hex2Ten(strBy[9].ToString()).ToString();
230                         DewPoint = ZtQx.Func.Hex2Ten(strBy[8].ToString()).ToString();
231 
232                         ArrayList aryHs = new ArrayList();
233                         aryHs.Add(temperature);
234                         aryHs.Add(humidity);
235                         aryHs.Add(smoke);
236                         aryHs.Add(DewPoint);
237 
238                         if (!hsTb.Contains(sbbh))
239                         {
240                             hsTb.Add(sbbh, aryHs);
241                         }
242                         else
243                         {
244                             hsTb.Remove(sbbh);
245                             hsTb.Add(sbbh, aryHs);
246                         }
247 
248                         //根据查询数据 , 操作设备
249                         string eId = dt.Rows[0]["E_ID"].ToString();
250                         ZtQx.Operation.Operating(eId, "4", temperature, humidity, smoke, DewPoint);
251 
252                         //获取系统 时间 , 判断分钟 在 10,5
253                         DateTime dtNewTime = DateTime.Now;
254                         int Minute = dtNewTime.Minute;
255                         int tt = Pl;//int.Parse(this.comboBox1.SelectedItem.ToString() == "" ? "5" : this.comboBox1.SelectedItem.ToString());
256                         if (Minute % tt == 0)
257                         {
258                             int yearN = dtNewTime.Year;
259                             int monthN = dtNewTime.Month;
260                             int dayN = dtNewTime.Day; 
261                             int hourN = dtNewTime.Hour;
262 
263                             //查询 要添加的数据 是否 已经存在于数据库
264                             string condition = "select * from Inspect where I_EQUIPMNETID=" + dt.Rows[0]["E_ID"].ToString();
265 
266                             DataTable dtN = new DataTable();
267                             condition += " and datepart('yyyy',I_TIME) ='" + yearN + "' and datepart('m',I_TIME) ='" + monthN + "' and datepart('d',I_TIME) ='" + dayN + "' and datepart('h',I_TIME) ='" + hourN + "' and datepart('n',I_TIME) ='" + Minute + "'";// group by cstr(datepart('yyyy',I_TIME)&'-'&cstr(datepart('m',I_TIME))&'-'&cstr(datepart('d',I_TIME))&' '&cstr(datepart('h',I_TIME))) ";
268                             dtN = inspectDal.GetAllBySql(condition);
269 
270                             //if (dt.Rows[0]["E_ID"].ToString().Equals("2"))
271                             //{
272                             //    MessageBox.Show("有2");
273                             //}
274 
275                             if (dtN.Rows.Count > 0)
276                                 return;
277                             AddIn(dt.Rows[0]["E_ID"].ToString());
278                         }
279                         Values = string.Empty;
280                     }
281                 }
282             //}
283             //catch (Exception)
284             //{
285             //    Values = string.Empty;
286             //    throw;
287             //}
288         }
289 
290         #region 功能:警报
291         /// <summary>
292         /// 功能:警报
293         /// </summary>
294         private void Alarm(string area)
295         {
296             //根据设备编号 查询出该设备检测到的温度、湿度、露点是否正常
297             DataTable dtsp = inspectDal.GetSetupParameterByContion(area);
298             if (dtsp.Rows.Count > 0)
299             {
300                 //露点报警
301                 double highestLd = double.Parse(dtsp.Rows[0]["S_HIGHDEWPOINT"].ToString());
302                 double minimumLd = double.Parse(dtsp.Rows[0]["S_LOWDEWPOINT"].ToString());
303                 //当前露点
304                 double newLd = double.Parse(DewPoint);
305                 if (newLd < minimumLd && newLd > highestLd)
306                 {
307                     AddIn(area);
308                     //添加警报信息 到数据库 , 并弹出警报窗体
309                     ArrayList aryAnnunciator = new ArrayList();
310                     //设备编号
311                     aryAnnunciator.Add(area);
312                     //报警类型
313                     aryAnnunciator.Add("露点报警:现在露点" + DewPoint + "");
314                     //报警日期
315                     aryAnnunciator.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
316                     //添加到数据库
317                     inspectDal.AddAnnunciatorEntity(aryAnnunciator);
318                     //弹出窗体
319                     FormPolice frmPolice = new FormPolice();
320                     frmPolice.ShowDialog();
321                 }
322 
323                 //湿度报警
324                 double highestSd = double.Parse(dtsp.Rows[0]["S_HIGHHUMIDITY"].ToString());
325                 double minimumSd = double.Parse(dtsp.Rows[0]["S_LOWHUMIDITY"].ToString());
326                 //当前湿度
327                 double newSd = double.Parse(humidity);
328                 if (newSd > highestSd)
329                 {
330                     AddIn(area);
331                     //添加警报信息 到数据库 , 并弹出警报窗体
332                     ArrayList aryAnnunciator = new ArrayList();
333                     //设备编号
334                     aryAnnunciator.Add(area);
335                     //报警类型
336                     aryAnnunciator.Add("湿度报警:现在湿度" + humidity + "%");
337                     //报警日期
338                     aryAnnunciator.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
339                     //添加到数据库
340                     inspectDal.AddAnnunciatorEntity(aryAnnunciator);
341                     //弹出窗体
342                     FormPolice frmPolice = new FormPolice();
343                     frmPolice.ShowDialog();
344                 }
345                 //温度报警
346                 double highestWd = double.Parse(dtsp.Rows[0]["S_HIGHTEMPERATURE"].ToString());
347                 double minimumWd = double.Parse(dtsp.Rows[0]["S_LOWTEMPERATURE"].ToString());
348                 //当前温度
349                 double newWd = double.Parse(temperature);
350                 if (newWd < minimumWd || newWd > highestWd)
351                 {
352                     AddIn(area);
353                     //添加警报信息 到数据库 , 并弹出警报窗体
354                     ArrayList aryAnnunciator = new ArrayList();
355                     //设备编号
356                     aryAnnunciator.Add(area);
357                     //报警类型
358                     aryAnnunciator.Add("温度报警:现在温度" + temperature + "");
359                     //报警日期
360                     aryAnnunciator.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
361                     //添加到数据库
362                     inspectDal.AddAnnunciatorEntity(aryAnnunciator);
363                     //弹出窗体
364                     FormPolice frmPolice = new FormPolice();
365                     frmPolice.ShowDialog();
366                 }
367             }
368         }
369         #endregion
370 
371         #region 功能:报警时 添加数据到数据库
372         /// <summary>
373         /// 功能:报警时 添加数据到数据库
374         /// </summary>
375         public void AddIn(string area)
376         {
377             ArrayList aryList = new ArrayList();
378 
379             //设备编号
380             aryList.Add(area);
381             //温度
382             aryList.Add(temperature);
383             //湿度
384             aryList.Add(humidity);
385             //烟感
386             aryList.Add(smoke);
387             //露点
388             aryList.Add(DewPoint);
389             //记录日期
390             aryList.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
391 
392             inspectDal.InsertInspect(aryList);
393         }
394         #endregion
395 
396         //添加委托
397         //private delegate void JkSj();
398 
399         //#region 功能:Timer的委托事件
400         ///// <summary>
401         ///// Timer调用的委托事件
402         ///// </summary>
403         //private void invokeThread()
404         //{
405         //    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
406         //    sw.Reset();
407         //    sw.Start();
408 
409         //    if (this.InvokeRequired)
410         //    {
411         //        //this.BeginInvoke(new JkSj(NewJkMl));
412         //    }
413         //    else
414         //    {
415         //        NewJkMl();
416         //    }
417         //    sw.Stop();
418         //}
419         //#endregion
420 
421         #region 功能:Timer 事件
422         /// <summary>
423         /// 功能:Timer 事件
424         /// </summary>
425         /// <param name="sender"></param>
426         /// <param name="e"></param>
427         protected void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
428         {
429             NewJkMl();
430             //if (!ZtQx.Com.port.IsOpen)
431             //    ZtQx.Com.port.Open();
432             //invokeThread();
433             //JkSj d1 = invokeThread;
434             //IAsyncResult ar = d1.BeginInvoke(null, null);
435             //new System.Threading.Thread(new System.Threading.ThreadStart(invokeThread)).Start();
436 
437             //while (!ar.IsCompleted)
438             //{
439             //    System.Threading.Thread.Sleep(50);
440             //}
441             //System.Threading.Thread th = new System.Threading.Thread(invokeThread);
442             //th.Name = "myNewThread";
443             //th.Start();
444         }
445 
446         private void StartKiller()
447         {
448             System.Windows.Forms.Timer timerMsg = new System.Windows.Forms.Timer();
449             timerMsg.Interval = 2000;//10秒启动
450             timerMsg.Tick += new EventHandler(timerMsg_Tick);
451             timerMsg.Start();
452         }
453         private void timerMsg_Tick(object sender, EventArgs e)
454         {
455             KillMessageBox();
456             //停止计时器
457             //((System.Windows.Forms.Timer)sender).Stop();
458         }
459 
460         private void KillMessageBox()
461         {
462             //查找MessageBox的弹出窗口,注意对应标题
463             IntPtr ptr = FindWindow(null, "系统提示");
464             if (ptr != IntPtr.Zero)
465             {
466                 //查找到窗口则关闭
467                 PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
468             }
469         }
470         [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
471         private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
472         [DllImport("user32.dll", CharSet = CharSet.Auto)]
473         public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
474         public const int WM_CLOSE = 0x10;
475 
476         //private delegate void fszd(string r1,string r2,string r3);
477 
478         private void ztfszd(string r1, string r2, string r3)
479         {
480             byte[] rs2 = ZtQx.Func.CommandAddFAFBCrc16(r1 + " " + r2 + " " + r3);
481             oc.Send(rs2);
482             oc.DataReceivedMonth = SetDataValue;
483         }
484 
485         /// <summary>
486         /// 功能:发送指令
487         /// </summary>
488         private void NewJkMl()
489         {
490             oc.OpenPort();
491 
492             //从数据库中读取出数据 , 现阶段只有1个仓库
493             string areaId = "1";
494             DataTable dtArea = inspectDal.GetEquipmentByArea(areaId);
495             string areaSw = "3";
496             DataTable dtAreaSw = inspectDal.GetEquipmentByArea(areaSw);
497 
498             string r1 = string.Empty;
499             string r2 = string.Empty;
500             string r3 = "0x54";
501 
502             if (dtArea.Rows.Count > 0)
503             {
504                 //循环读取温度信息,循环数据库的设备
505                 for (int i = 0; i < dtArea.Rows.Count; i++)
506                 {
507                     r1 = "0x" + dtArea.Rows[i]["E_ADDRESS"].ToString().Substring(0, 2);
508                     r2 = "0x" + dtArea.Rows[i]["E_ADDRESS"].ToString().Substring(2, 2);
509                     ztfszd(r1, r2, r3);
510                     Thread.Sleep(300);
511                     //Application.DoEvents();  
512                 }
513             }
514 
515             if (dtAreaSw.Rows.Count > 0)
516             {
517                 r1 = "0x" + dtAreaSw.Rows[0]["E_ADDRESS"].ToString().Substring(0, 2);
518                 r2 = "0x" + dtAreaSw.Rows[0]["E_ADDRESS"].ToString().Substring(2, 2);
519 
520                 ztfszd(r1, r2, r3);
521             }
522         }
523         #endregion
524 
525         #region 按钮获取焦点改变图片事件
526         private void pbHistroyData_MouseHover(object sender, EventArgs e)
527         {
528             pbHistroyData.Image = Properties.Resources._1024_768改2_27_r3_c51;
529         }
530 
531         private void pbHistroyData_MouseLeave(object sender, EventArgs e)
532         {
533             pbHistroyData.Image = Properties.Resources._1024_768改2_27_r3_c5;
534         }
535 
536         private void pbHistroycurev_MouseHover(object sender, EventArgs e)
537         {
538             pbHistroycurev.Image = Properties.Resources._1024_768改2_27_r3_c121;
539         }
540 
541         private void pbHistroycurev_MouseLeave(object sender, EventArgs e)
542         {
543             pbHistroycurev.Image = Properties.Resources._1024_768改2_27_r3_c12;
544         }
545 
546         private void pbPlice_MouseHover(object sender, EventArgs e)
547         {
548             pbPlice.Image = Properties.Resources._1024_768改2_27_r3_c17;
549         }
550 
551         private void pbPlice_MouseLeave(object sender, EventArgs e)
552         {
553             pbPlice.Image = Properties.Resources._1024_768改2_27_r3_c171;
554         }
555 
556         private void pbParam_MouseMove(object sender, MouseEventArgs e)
557         {
558             pbParam.Image = Properties.Resources.canshu_sheding_;
559         }
560 
561         private void pbParam_MouseLeave(object sender, EventArgs e)
562         {
563             pbParam.Image = Properties.Resources.canshusheding;
564         }
565         #endregion
566 
567         #region 打开子窗体
568         private void pbHistroyData_Click(object sender, EventArgs e)
569         {
570             //打开历史数据记录窗体
571             FormInspect inspect = new FormInspect();
572             inspect.ShowDialog();
573         }
574 
575         private void pbHistroycurev_Click(object sender, EventArgs e)
576         {
577             //打开历史曲线数据窗体
578             FormHistoryCurve curve = new FormHistoryCurve();
579             curve.ShowDialog();
580         }
581 
582         private void pbPlice_Click(object sender, EventArgs e)
583         {
584             //打开报警历史记录窗体
585             FormPliceHistory plice = new FormPliceHistory();
586             plice.ShowDialog();
587         }
588 
589         private void pbParam_Click(object sender, EventArgs e)
590         {
591             //打开设定最高、最低温湿度参数窗体
592             FormPreferences param = new FormPreferences();
593             param.ShowDialog();
594         }    
595 
596         #endregion
597 
598         #region  动态生成折叠菜单
599         /// <summary>
600         /// 功能:动态生成折叠菜单
601         /// </summary>
602         private void FoldMenu()
603         {
604             //查询库房信息
605             DataTable dtHouse = Area.QueryHouse("");
606 
607             for (int i = 0; i < dtHouse.Rows.Count; i++)
608             {
609                 Button btn = new Button();
610                 btn.Location = new System.Drawing.Point(0, 0);
611                 btn.Size = new System.Drawing.Size(225, 52);
612                 btn.Text = dtHouse.Rows[0]["HOUSE_NAME"].ToString();
613                 btn.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
614                 btn.BackgroundImage = Properties.Resources.shouyecuangku;
615 
616                 this.panel1.Controls.Add(btn);
617             }
618         }
619 
620         void ButtonClick(object sender, System.EventArgs e)
621         {
622             Button clickedButton = (Button)sender;
623             int clickedButtonTabIndex = clickedButton.TabIndex;
624             foreach (Control ctl in panel1.Controls)
625             {
626                 if (ctl is Button)
627                 {
628                     Button btn = (Button)ctl;
629                     if (btn.TabIndex > clickedButtonTabIndex)
630                     {
631                         if (btn.Dock != DockStyle.Bottom)
632                         {
633                             btn.Dock = DockStyle.Bottom;
634 
635                             btn.BringToFront();
636                         }
637                     }
638                     else
639                     {
640                         if (btn.Dock != DockStyle.Top)
641                         {
642                             btn.Dock = DockStyle.Top;
643 
644                             btn.BringToFront();
645                         }
646                     }
647                 }
648             }
649 
650             string menuName = clickedButton.Text;//获取库房名称
651 
652             //按库房名称查询库房ID
653 
654             DataTable house = Area.QueryHouse(" and HOUSE_NAME = '" + menuName + "'");
655 
656             CreatList(int.Parse(house.Rows[0]["HOUSE_ID"].ToString()));
657 
658         }
659 
660 
661         /// <summary>
662         /// 按仓库查询区域
663         /// </summary>
664         /// <param name="id"></param>
665         private void CreatList(int id)
666         {
667             //查询区域信息
668             DataTable dtArea = Area.QueryAreaByCondition(" and E_HOUSEID = " + id + "");
669 
670             for (int i = 0; i < dtArea.Rows.Count; i++)
671             {
672                 Label lable = new Label();
673                 lable.Size = new Size(225, 52);
674                 lable.Location = new Point(0, (i + 1) * 52);
675                 lable.BackColor = System.Drawing.Color.FromArgb(0, 31, 62);
676                 lable.ForeColor = Color.White;
677                 lable.BorderStyle = BorderStyle.None;
678                 lable.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
679                 lable.MouseMove += new MouseEventHandler(lable_MouseMove);
680                 lable.MouseLeave += new EventHandler(lable_MouseLeave);
681                 lable.Image = Properties.Resources.shouyer10c2;
682 
683                 lable.Cursor = System.Windows.Forms.Cursors.Hand;
684                 lable.Click += new EventHandler(lable_Click);//鼠标点击事件
685 
686                 lable.Text = dtArea.Rows[i]["E_NAME"].ToString();
687                 lable.Tag = dtArea.Rows[i]["E_ID"].ToString();
688 
689                 this.panel1.Controls.Add(lable);
690             }
691         }
692 
693         public delegate void Def(Label lbl);//委托事件
694 
695 
696 
697         //区域点击事件
698         private void lable_Click(object sender, EventArgs e)
699         {
700             Label lbl = (Label)sender;
701 
702             //MessageBox.Show("数量:" + this.panelContent.Controls.Count);
703             try
704             {
705                 foreach (Form children in this.panelContent.Controls)
706                 {
707                     children.Close();
708                 }
709                 this.panelContent.Controls.Clear();
710                 FormQueryRecord frm = new FormQueryRecord(lbl.Tag.ToString());
711                 //Testst frm = new Testst(lbl.Tag.ToString());
712                 frm.TopLevel = false;
713                 this.panelContent.Controls.Add(frm);
714                 frm.Show();
715             }
716             catch (Exception)
717             {
718                 //FormQueryRecord frm = new FormQueryRecord(lbl.Tag.ToString());
719                 ////Testst frm = new Testst(lbl.Tag.ToString());
720                 //frm.TopLevel = false;
721                 //this.panelContent.Controls.Add(frm);
722                 //frm.Show();
723                 MessageBox.Show("打开窗体 出错!");
724             }
725        }
726 
727         private void lable_MouseLeave(object sender, EventArgs e)
728         {
729             Label lbl = (Label)sender;
730             lbl.Image = Properties.Resources.shouyer10c2;
731         }
732 
733         private void lable_MouseMove(object sender, EventArgs e)
734         {
735             Label lbl = (Label)sender;
736             lbl.Image = Properties.Resources.shouyer14c21;
737         }
738 
739         #endregion
740 
741         private void pbOut_MouseMove(object sender, MouseEventArgs e)
742         {
743             //pbOut.Image = Properties.Resources.tuichu11;
744             pbOut.Image = Properties.Resources.small;
745         }
746 
747         private void pbOut_MouseLeave(object sender, EventArgs e)
748         {
749             //pbOut.Image = Properties.Resources.tuichu;
750             
751             pbOut.Image = Properties.Resources.bigShow;
752         }
753 
754 
755         //退出事件
756         private void pbOut_Click(object sender, EventArgs e)
757         {
758             //this.Close();
759                 this.Hide();
760                 this.notifyIcon1.Visible = true;
761         }
762 
763         /// <summary>
764         /// 退出
765         /// </summary>
766         /// <param name="sender"></param>
767         /// <param name="e"></param>
768         private void tsmiExit_Click(object sender, EventArgs e)
769         {
770             Application.Exit();
771         }
772 
773         /// <summary>
774         /// 双击打开
775         /// </summary>
776         /// <param name="sender"></param>
777         /// <param name="e"></param>
778         private void notifyIcon1_DoubleClick(object sender, EventArgs e)
779         {
780             this.Show();
781             this.notifyIcon1.Visible = false;
782         }
783 
784         private void tsmiQd_Click(object sender, EventArgs e)
785         {
786             //启动当前应用程序可执行文件exe的路径
787             string R_startPath = Application.ExecutablePath;
788             if (this.tsmiQd.Text == "开机启动")
789             {
790                 #region 添加开机启动
791                 try
792                 {
793                     RegistryKey R_local = Registry.LocalMachine;
794 
795                     //查找到要添加到的注册表项
796                     RegistryKey R_run = R_local.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
797                     //添加注册表项
798                     R_run.SetValue("StartupAppyatou1019", R_startPath);
799 
800                     R_run.Close();
801                     R_local.Close();
802                 }
803                 catch (Exception ex)
804                 {
805                     MessageBox.Show(ex.ToString());
806                 }
807                 #endregion
808             }
809             else
810             {
811                 #region 取消开机启动
812                 try
813                 {
814                     RegistryKey R_local = Registry.LocalMachine; RegistryKey R_run = R_local.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
815                     //删除相应的注册表项
816                     R_run.DeleteValue("StartupAppyatou1019", false);
817                     R_run.Close();
818                     R_local.Close();
819                 }
820                 catch (Exception ex)
821                 {
822                     MessageBox.Show(ex.ToString());
823                 }
824                 #endregion
825             }
826             IsRegKeIsExt();
827         }
828 
829         // <summary>
830         /// 判断开机启动项是否已存在 
831         /// </summary>
832         /// <returns></returns>
833         private bool IsRegKeIsExt()
834         {
835             bool IsExt = false;
836 
837             RegistryKey R_local = Registry.LocalMachine;
838             //查找到要添加到的注册表项
839             RegistryKey R_run = R_local.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
840 
841             //获得注册表项的值
842             object _obj = R_run.GetValue("StartupAppyatou1019");
843             R_run.Close();
844             R_local.Close();
845 
846             if (_obj != null)
847             {
848                 IsExt = true;
849                 this.tsmiQd.Text = "禁止开机启动";
850             }
851             else
852             {
853                 this.tsmiQd.Text = "开机启动";
854             }
855             return IsExt;
856         }
857 
858        
859     }
860    
861 }
FormMain
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using Microsoft.Win32;
 10 using CKJKRJ.House;
 11 
 12 namespace CKJKRJ
 13 {
 14     public partial class frmZtTs : Form
 15     {
 16 
 17         private static Dictionary<string, frmZtTs> frmDict = new Dictionary<string, frmZtTs>();
 18 
 19         public frmZtTs()
 20         {
 21             InitializeComponent();
 22         }
 23 
 24         public frmZtTs(string message)
 25         {
 26             InitializeComponent();
 27             lblMessage.Text = message;
 28         }
 29 
 30         private void button1_Click(object sender, EventArgs e)
 31         {
 32             this.Close();
 33         }
 34 
 35         private void btnOpen_CheckedChanged(object sender, EventArgs e)
 36         {
 37             //启动当前应用程序可执行文件exe的路径
 38             string R_startPath = Application.ExecutablePath;
 39             if (this.btnOpen.Checked==true)
 40             {
 41                 #region 添加开机启动
 42                 try
 43                 {
 44                     RegistryKey R_local = Registry.LocalMachine;
 45 
 46                     //查找到要添加到的注册表项
 47                     RegistryKey R_run = R_local.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
 48                     //添加注册表项
 49                     R_run.SetValue("StartupAppyatou1019", R_startPath);
 50 
 51                     R_run.Close();
 52                     R_local.Close();
 53                 }
 54                 catch (Exception ex)
 55                 {
 56                     MessageBox.Show(ex.ToString());
 57                 }
 58                 #endregion
 59             }
 60             else
 61             {
 62                 #region 取消开机启动
 63                 try
 64                 {
 65                     RegistryKey R_local = Registry.LocalMachine; RegistryKey R_run = R_local.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
 66                     //删除相应的注册表项
 67                     R_run.DeleteValue("StartupAppyatou1019", false);
 68                     R_run.Close();
 69                     R_local.Close();
 70                 }
 71                 catch (Exception ex)
 72                 {
 73                     MessageBox.Show(ex.ToString());
 74                 }
 75                 #endregion
 76             }
 77         }
 78 
 79         /// <summary>
 80         /// 判断开机启动项是否已存在 
 81         /// </summary>
 82         /// <returns></returns>
 83         private bool IsRegKeIsExt()
 84         {
 85             bool IsExt = false;
 86 
 87             RegistryKey R_local = Registry.LocalMachine;
 88             //查找到要添加到的注册表项
 89             RegistryKey R_run = R_local.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
 90 
 91             //获得注册表项的值
 92             object _obj = R_run.GetValue("StartupAppyatou1019");
 93             R_run.Close();
 94             R_local.Close();
 95 
 96             if (_obj != null)
 97             {
 98                 IsExt = true;
 99             }
100             return IsExt;
101         }
102 
103         private void button2_Click(object sender, EventArgs e)
104         {
105             InspectDal ind = new InspectDal();
106             ind.UpZt();
107         }
108     }
109 }
frmZtTs
原文地址:https://www.cnblogs.com/cykj/p/Temperature-and-humidity-system.html