測試命令 啟動加熱器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 1 ...
測試命令
啟動加熱器
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("宋體"