测试命令
启动加热器
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 }