实例介绍
【实例简介】C#温湿度上位机软件,可显示温度曲线
【实例截图】
【核心代码】
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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | using ControlPoxy; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using ZedGraph; namespace OpenSoftware { public partial class mainWindows : Form { float sht2x_temp, sht2x_hum; int receiveIndex = -1; bool b_UploadEnable = false ; bool b_buttonCtrl = false ; Thread dataHandler; private String ComPort = "COM1" ; string [] StringBaud = { "115200" , "57600" , "38400" , "19200" , "9600" , "4800" , "2400" }; //串口波特率设置 Util_Proxy st_unit = new Util_Proxy(); private ControlPoxy.MCS_Protocol st_MCS_Protocol = new ControlPoxy.MCS_Protocol(); ControlPoxy.Serial_Proxy st_Serial_Proxy = new ControlPoxy.Serial_Proxy(); ControlPoxy.CRC_Proxy st_CRC_Proxy = new ControlPoxy.CRC_Proxy(); PointPairList list1; PointPairList list2; public mainWindows() { InitializeComponent(); } private void mainWindows_Load( object sender, EventArgs e) { int baudIndex = 0; string [] port = System.IO.Ports.SerialPort.GetPortNames(); //搜索PC可用的串口号 if (port != null ) { comboBox_ComPort.Items.Clear(); foreach ( string cName in port) { comboBox_ComPort.Items.Add(cName); if (cName == ComPort) { comboBox_ComPort.SelectedIndex = baudIndex; } baudIndex ; } } Control.CheckForIllegalCrossThreadCalls = false ; //波特率 comboBox_Baud.Items.Clear(); foreach ( string cName in StringBaud) { comboBox_Baud.Items.Add(cName); } comboBox_Baud.SelectedIndex = 0; dataHandler = new Thread(DataCommandHander); dataHandler.Start(); tools_cypyRight.Text = st_unit.CopyRight; tools_version.Text = st_unit.SoftVersion; toolsTime_build.Text = "Building: " System.IO.File.GetLastWriteTime( this .GetType().Assembly.Location).ToString( "F" ); textBox_DataLen.Text = "10" ; button_CtrlSht2x.Text = "Start" ; list1 = new PointPairList(); list2 = new PointPairList(); CreateGraph(zedGraphControl); //SetSize(); } private void DataCommandHander() { Boolean b_res = false ; while ( true ) { if (st_Serial_Proxy.b_SerialReceiveReady) { st_Serial_Proxy.b_SerialReceiveReady = false ; if (st_Serial_Proxy.DataLength > 0) { if (st_Serial_Proxy.RtBuff[0] == 0x5b) { b_res = st_MCS_Protocol.MCS_ProtocolPaeser(st_Serial_Proxy.RtBuff); if (b_res) { Mcs_ReceiveAndSendMonitor(st_MCS_Protocol, true ); st_MCS_Protocol.b_result = true ; Parser_Sht2xValue(); } } st_Serial_Proxy.DataLength = 0; } } } } private void Mcs_ReceiveAndSendMonitor(MCS_Protocol mcs, bool mode) { byte [] buff = new byte [512]; int length = 0; if (mode == true ) richTextBox_Monitor.AppendText( "Recv:" "\r\n" ); else richTextBox_Monitor.AppendText( "Send:" "\r\n" ); length = mcs.MCS_Building(mcs.srcAddr, mcs.tarAddr, mcs.Frum, mcs.CMD, mcs.DataBuf, ( byte )mcs.DataLen, buff); Monitor_Display(buff, length); } private void mainWindows_FormClosed( object sender, FormClosedEventArgs e) { st_Serial_Proxy.SerialPort_Stop(); dataHandler.Abort(); Application.Exit(); } private void button_CommCtrl_Click( object sender, EventArgs e) { st_Serial_Proxy.Baud = int .Parse(comboBox_Baud.Text); //设置Baud st_Serial_Proxy.Comport = comboBox_ComPort.Text; //获取端口号 st_Serial_Proxy.Parity = System.IO.Ports.Parity.None; st_Serial_Proxy.TimeOut = 300; if (b_buttonCtrl == false ) { button_CommCtrl.Text = "关闭串口" ; b_buttonCtrl = true ; st_Serial_Proxy.SerialPort_Start(); comboBox_Baud.Enabled = false ; comboBox_ComPort.Enabled = false ; } else { button_CommCtrl.Text = "打开串口" ; b_buttonCtrl = false ; st_Serial_Proxy.SerialPort_Stop(); comboBox_Baud.Enabled = true ; comboBox_ComPort.Enabled = true ; } } private void button_ClearBuff_Click( object sender, EventArgs e) { richTextBox_Monitor.Clear(); } private void ShowMessage( string msg) { richTextBox_Monitor.AppendText(msg); } private void Monitor_Display( byte [] data, int length) { if (length > 0) { string s = "" ; for ( int i = 0; i < length; i ) { s = data[i].ToString( "X2" ); s = " " ; } richTextBox_Monitor.AppendText(s "\r\n" ); } } private void CtrCmd_McsBuiding(MCS_Protocol mcs) { byte [] buff = new byte [256]; int length = 0; length = mcs.MCS_Building(mcs.srcAddr, mcs.tarAddr, mcs.Frum, mcs.CMD, mcs.DataBuf, ( byte )mcs.DataLen, buff); st_Serial_Proxy.SerialSend(buff, length); richTextBox_Monitor.AppendText( "Send" "\r\n" ); Monitor_Display(buff, length); } private byte DataIDConvert( string sID) { byte byteID = 0; try { if (sID == "" ) { MessageBox.Show( "请输入数据ID!" ); } byteID = Convert.ToByte(sID); } catch { } return byteID; } private void button_ProtocolSend_Click( object sender, EventArgs e) { byte DataLen = DataIDConvert(textBox_DataLen.Text); if (DataLen > 45) { DataLen = 45; } ShowMessage( "通信协议包测试 \r\n" ); st_MCS_Protocol.srcAddr = 0; st_MCS_Protocol.tarAddr = 0; st_MCS_Protocol.Frum = 0; st_MCS_Protocol.CMD = 0x0010; st_MCS_Protocol.DataLen = DataLen; if (DataLen > 0) { for ( int i = 0; i < DataLen; i ) { st_MCS_Protocol.DataBuf[i] = ( byte )(0x30 i); } } CtrCmd_McsBuiding(st_MCS_Protocol); } #region FileDownload private void DownlaodFile_CheckCmd() { if (st_MCS_Protocol.CMD == 0x8012) { receiveIndex = st_MCS_Protocol.Frum; } } private void MakePacket_ByMcs( ushort DataLen, byte [] data, ushort frum) { st_MCS_Protocol.srcAddr = 0; st_MCS_Protocol.tarAddr = 0; st_MCS_Protocol.Frum = frum; st_MCS_Protocol.CMD = 0x0012; st_MCS_Protocol.DataLen = DataLen; if (DataLen > 0) { for ( int i = 0; i < DataLen; i ) { st_MCS_Protocol.DataBuf[i] = data[i]; } } CtrCmd_McsBuiding(st_MCS_Protocol); } byte [] AppBt; int totalPacket = 0; const int fwSendLen = 128; private void AddFile_Click( object sender, EventArgs e) { try { OpenFileDialog openbinFile = new OpenFileDialog(); openbinFile.Filter = "下载文件|*.bin;*.hex;*.ZFP" ; if (openbinFile.ShowDialog() == DialogResult.OK) { FileStream IniRead = new FileStream(openbinFile.FileName, FileMode.OpenOrCreate, FileAccess.Read); filePath.Text = openbinFile.FileName; if (IniRead.Length > 0) { AppBt = new byte [IniRead.Length]; IniRead.Read(AppBt, 0, AppBt.Length); //读取app应用代码数据 totalPacket = AppBt.Length / fwSendLen; //计算数据包总数 if (AppBt.Length % fwSendLen != 0) { totalPacket = 1; } } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } bool b_fwStart = false ; Thread sub = null ; ushort sendFrum = 0; private void button_Start_Click( object sender, EventArgs e) { progressBar_download.Maximum = totalPacket; progressBar_download.Value = 0; FwProcessBar.Text = "0%" ; b_fwStart = true ; sendFrum = 0; sub = new Thread(sendUpDataThreading); sub.IsBackground = true ; sub.Start(); button_Start.Enabled = false ; AddFile.Enabled = false ; button1_Stop.Enabled = true ; } private void button1_Stop_Click( object sender, EventArgs e) { button_Start.Enabled = true ; AddFile.Enabled = true ; button1_Stop.Enabled = false ; FwProcessBar.Text = "0%" ; receiveIndex = -1; sendFrum = 0; b_fwStart = false ; sub.Abort(); } private void sendUpDataThreading() { byte [] data = new byte [fwSendLen]; byte readLen; while ( true ) { if (!b_fwStart) return ; if ((AppBt.Length - sendFrum * fwSendLen) >= fwSendLen) { readLen = fwSendLen; } else { readLen = ( byte )(AppBt.Length - sendFrum * fwSendLen); } if (readLen > 0) { Buffer.BlockCopy(AppBt, sendFrum * fwSendLen, data, 0, readLen); try { MakePacket_ByMcs(readLen, data, sendFrum); System.Threading.Thread.Sleep(350); DownlaodFile_CheckCmd(); } catch { } FwProcessBar.Text = (Convert.ToDouble(sendFrum) / Convert.ToDouble(progressBar_download.Maximum) * 100).ToString( "0.0" ) "%" ; if (sendFrum == receiveIndex) { sendFrum ; } progressBar_download.Value = sendFrum; } //判断数据包是否发送完毕 if (sendFrum == totalPacket) { receiveIndex = -1; FwProcessBar.Text = "100%" ; sendFrum = 0; b_fwStart = false ; button_Start.Enabled = true ; AddFile.Enabled = true ; button1_Stop.Enabled = false ; sub.Abort(); return ; } } } #endregion private void button_RTC_Click( object sender, EventArgs e) { byte [] rtc = new byte [6]; ShowMessage( "时间同步 \r\n" ); rtc[0] = ( byte )(DateTime.Now.Year - 2000); rtc[1] = ( byte )(DateTime.Now.Month); rtc[2] = ( byte )(DateTime.Now.Day); rtc[3] = ( byte )(DateTime.Now.Hour); rtc[4] = ( byte )(DateTime.Now.Minute); rtc[5] = ( byte )(DateTime.Now.Second); st_MCS_Protocol.srcAddr = 0; st_MCS_Protocol.tarAddr = 0; st_MCS_Protocol.Frum = 0; st_MCS_Protocol.CMD = 0x0011; st_MCS_Protocol.DataLen = 6; for ( int i = 0; i < rtc.Length; i ) { st_MCS_Protocol.DataBuf[i] = rtc[i]; } CtrCmd_McsBuiding(st_MCS_Protocol); } private void button1_Click( object sender, EventArgs e) { int intervalVal = 0; byte [] interval = new byte [2]; if (b_UploadEnable) { b_UploadEnable = false ; button_CtrlSht2x.Text = "Start" ; } else { b_UploadEnable = true ; button_CtrlSht2x.Text = "Stop" ; intervalVal = 1; } interval[0] = ( byte )(intervalVal >> 8); interval[1] = ( byte )(intervalVal); st_MCS_Protocol.srcAddr = 0; st_MCS_Protocol.tarAddr = 0; st_MCS_Protocol.Frum = 0; st_MCS_Protocol.CMD = 0x0013; st_MCS_Protocol.DataLen = 2; for ( int i = 0; i < interval.Length; i ) { st_MCS_Protocol.DataBuf[i] = interval[i]; } CtrCmd_McsBuiding(st_MCS_Protocol); } private void Parser_Sht2xValue() { if (st_MCS_Protocol.CMD == 0x8013 && b_UploadEnable ) { if (st_MCS_Protocol.DataLen == 4) { sht2x_temp = st_MCS_Protocol.DataBuf[0] << 8 | st_MCS_Protocol.DataBuf[1]; sht2x_hum = st_MCS_Protocol.DataBuf[2] << 8 | st_MCS_Protocol.DataBuf[3]; textBox_temp.Text = (sht2x_temp*0.01).ToString(); textBox_hum.Text = (sht2x_hum*0.01).ToString(); DateTime now = DateTime.Now; double x = ( double ) new XDate(now); list1.Add(x, sht2x_temp * 0.01); list2.Add(x, sht2x_hum * 0.01); if (list1.Count >= 120) { list1.RemoveAt(0); } if (list2.Count >= 120) { list2.RemoveAt(0); } this .zedGraphControl.GraphPane.XAxis.Title.Text = "Time (" zedGraphControl.GraphPane.XAxis.Scale.Format ")" ; this .zedGraphControl.AxisChange(); this .zedGraphControl.Refresh(); } } } private void timer1_Tick( object sender, EventArgs e) { } private void SetSize() { zedGraphControl.Location = new Point(10, 50); // Leave a small margin around the outside of the control zedGraphControl.Size = new Size(ClientRectangle.Width - 20, ClientRectangle.Height - 30); } private void CreateGraph(ZedGraphControl zgc) { zgc.IsShowPointValues = true ; // get a reference to the GraphPane GraphPane myPane = zgc.GraphPane; // Set the Titles myPane.Title.Text = "SHT 20 温湿度监控" ; myPane.YAxis.Title.Text = "温度" ; myPane.Y2Axis.Title.Text = "湿度" ; myPane.YAxis.Title.FontSpec.Size = 12; myPane.Y2Axis.Title.FontSpec.Size = 12; myPane.YAxis.Scale.FontSpec.Size = 10; //设置y轴的文字大小. myPane.Y2Axis.Scale.FontSpec.Size = 10; //设置y轴的文字大小. myPane.XAxis.MajorGrid.IsVisible = true ; //设置x轴虚线. myPane.YAxis.MajorGrid.IsVisible = true ; //设置Y轴虚线. myPane.Y2Axis.MajorGrid.IsVisible = true ; //设置Y轴虚线. myPane.XAxis.Title.Text = "时间" ; myPane.XAxis.Type = AxisType.DateAsOrdinal; myPane.XAxis.Scale.FormatAuto = true ; myPane.XAxis.Scale.Format = "hh:mm:ss" ; myPane.XAxis.Scale.MaxAuto = true ; myPane.XAxis.Scale.FontSpec.Size = 10; //设置x轴的文字大小. myPane.Chart.Border.IsVisible = false ; //图表区域的边框设置. myPane.Legend.IsVisible = false ; //图表的注释标签显示设置项目. // Generate a red curve with diamond // symbols, and "Porsche" in the legend LineItem myCurve = myPane.AddCurve( "温度" , list1, Color.Red, SymbolType.None); myCurve.Symbol.Fill = new Fill(Color.White); myCurve.Line.Width = 2; //设置线宽度 // Generate a blue curve with circle // symbols, and "Piper" in the legend LineItem myCurve2 = myPane.AddCurve( "湿度" , list2, Color.Blue, SymbolType.None); myCurve2.Symbol.Fill = new Fill(Color.White); myCurve2.Line.Width = 2; // Associate this curve with the Y2 axis myCurve2.IsY2Axis = true ; // Associate this curve with the second Y2 axis myCurve2.YAxisIndex = 2; // Show the x axis grid myPane.XAxis.MajorGrid.IsVisible = true ; // Make the Y axis scale red myPane.YAxis.Scale.FontSpec.FontColor = Color.Red; myPane.YAxis.Title.FontSpec.FontColor = Color.Red; // turn off the opposite tics so the Y tics don't show up on the Y2 axis myPane.YAxis.MajorTic.IsOpposite = false ; myPane.YAxis.MinorTic.IsOpposite = false ; // Don't display the Y zero line myPane.YAxis.MajorGrid.IsZeroLine = false ; // Align the Y axis labels so they are flush to the axis myPane.YAxis.Scale.Align = AlignP.Inside; myPane.YAxis.Scale.Min = 0; myPane.YAxis.Scale.Max = 100; // Enable the Y2 axis display myPane.Y2Axis.IsVisible = true ; // Make the Y2 axis scale blue myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue; myPane.Y2Axis.Title.FontSpec.FontColor = Color.Blue; // turn off the opposite tics so the Y2 tics don't show up on the Y axis myPane.Y2Axis.MajorTic.IsOpposite = false ; myPane.Y2Axis.MinorTic.IsOpposite = false ; // Display the Y2 axis grid lines myPane.Y2Axis.MajorGrid.IsVisible = true ; // Align the Y2 axis labels so they are flush to the axis myPane.Y2Axis.Scale.Align = AlignP.Inside; myPane.Y2Axis.Scale.Min = 0; myPane.Y2Axis.Scale.Max = 100; // Fill the axis background with a gradient myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f); // Tell ZedGraph to reconfigure the // axes since the data have changed zgc.AxisChange(); } } } |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论