⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 单片机ATMEL M8 USB监视m8usbmonitor.rar
💻 CS
📖 第 1 页 / 共 5 页
字号:
			{
				SaveConfig();
			}
			catch (Exception ex)
			{
				string sErrMsg = "无法写入程序配置信息!\n\n" + ex.Message;
				MessageBox.Show(sErrMsg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return;
			}

			MessageBox.Show("配置信息成功存盘!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void OnToolButtonRun()
		{
			tmrDelay.Enabled = false;  // Disable once the monitor routine is triggered

			if (tbbRun.Pushed)
			{
				try
				{
					// Check if any parameters essential for monitoring are correctly specified
					int nTestValue;
					nTestValue = Int32.Parse(cboBaudrates.SelectedItem.ToString());
					nTestValue = Int32.Parse(cboSampleIntervals.SelectedItem.ToString());
					nTestValue = 1000 * Int32.Parse(txtValidateInterval.Text);
				}
				catch (Exception ex)
				{
					tbbRun.Pushed = false;
					EnableTrayIcon(false);  // Prevent overlapped operations
					MessageBox.Show(ex.Message, "参数错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					EnableTrayIcon(true);
					return;
				}

				mnuMonitor.Checked = true;
				StartMonitor();
			}
			else
			{
				mnuMonitor.Checked = false;
				StopMonitor();
			}
		}

		private void OnToolButtonAbout()
		{
			Form3 frmAbout = new Form3();
			frmAbout.ShowDialog();
		}

		private void OnToolButtonHelp()
		{
			// Load help file with ShellExecute API
			int result = ShellExecute(0, "open", sHelpFilePath, "", "", 3);
			if (result <= 32)
			{
				MessageBox.Show("无法打开帮助文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}

		private void OnToolButtonHide()
		{
			this.Hide();
			notifyIcon1.Visible = true;
		}

		private void OnToolButtonExit()
		{
			this.Dispose(true);
			Application.Exit();
		}

		private void StartMonitor()
		{
			this.Cursor = Cursors.WaitCursor;

			// Download parameters to the device
			int nBaudrate = Int32.Parse(cboBaudrates.SelectedItem.ToString());
			DoSetRS232Baud(nBaudrate);

			// Create counter instances
			for (int i = 0; i < displayUnits.Length; i++)
			{
				InstantiateCounter(i);
			}

			// Start the timer
			tmrSample.Interval = Int32.Parse(cboSampleIntervals.SelectedItem.ToString());
			tmrSample.Enabled = true;
			
			if (chkValidateCounters.Checked)
			{
				// For periodically validating the counters
				tmrValidate.Interval = 1000 * Int32.Parse(txtValidateInterval.Text);
				tmrValidate.Enabled = true;
			}

			// Switch to the running status page and
			// prevent user from modifying settings at runtime
			tabMain.SelectedIndex = 2;

			this.Cursor = Cursors.Default;
		}

		private void StopMonitor()
		{
			tmrValidate.Enabled = false;
			tmrSample.Enabled = false;

			for (int i = 0; i < displayUnits.Length; i++)
			{
				DisplayUnit du = displayUnits[i];
				
				du.dblSampleValue = 0.0;
				du.nDisplayValue = 0;
				du.pc = null;  // Free counter instance
			}
		}

		private void BuildPacket()
		{
			// Prepare buffer
			Array.Clear(packet, 0, packet.Length);

			for (int i = 0; i < displayUnits.Length; i++)
			{
				DisplayUnit du = displayUnits[i];

				// Get sample value of counter
				//
				if (du.pc != null)
				{
					try
					{
						// actual sample value
						du.dblSampleValue = du.pc.NextValue();
					}
					catch (Exception ex)
					{
						// Destroy the counter on error
						du.pc = null;
					}
				}

				if (du.pc == null)
				{
					// dummy value
					du.dblSampleValue = 0.0;
				}
				
				// Convert into display value
				//
				if (du.parameters is BarParams)
				{
					// It is a bar unit
					BarParams barParams = (BarParams)du.parameters;
 				
					if (du.pc == null)
					{
						// dummy value
						du.nDisplayValue = 0;
					}
					else
					{
						if (du.dblSampleValue >= barParams.nMaxValue)
						{
							du.nDisplayValue = barParams.nSize;
						}
						else if (du.dblSampleValue <= barParams.nMinValue)
						{
							du.nDisplayValue = 0;
						}
						else
						{
							if (barParams.bLogarithmic)
							{
								du.nDisplayValue = (int)Math.Round(Math.Log(du.dblSampleValue - barParams.nMinValue) / Math.Log(barParams.nMaxValue - barParams.nMinValue) * barParams.nSize);
								if (du.nDisplayValue < 0) du.nDisplayValue = 0;  // Logarithm may generate negative numbers
							}
							else
							{
								du.nDisplayValue = (int)Math.Round((du.dblSampleValue - barParams.nMinValue) / (barParams.nMaxValue - barParams.nMinValue) * barParams.nSize);
							}
						}					
					}

					// Write to byte position of the packet
					packet[1 + barParams.nBytePtr] = (byte)du.nDisplayValue;
				}
				else
				{
					// It is a dot unit
					DotParams dotParams = (DotParams)du.parameters;
					
					if (du.pc == null)
					{
						// dummy value
						du.nDisplayValue = 0;
					}
					else
					{
						switch (dotParams.sOperator)
						{
							case "大于":
								du.nDisplayValue = (du.dblSampleValue > dotParams.dblThreshold) ? 1 : 0;
								break;
							case "不小于":
								du.nDisplayValue = (du.dblSampleValue >= dotParams.dblThreshold) ? 1 : 0;
								break;
							case "等于":
								du.nDisplayValue = (du.dblSampleValue == dotParams.dblThreshold) ? 1 : 0;
								break;
							case "不大于":
								du.nDisplayValue = (du.dblSampleValue <= dotParams.dblThreshold) ? 1 : 0;
								break;
							case "小于":
								du.nDisplayValue = (du.dblSampleValue < dotParams.dblThreshold) ? 1 : 0;
								break;
							default:  // error
								du.nDisplayValue = 0;
								break;
						}					
					}

					// Write to bit position of the packet
					if (du.nDisplayValue == 0)
					{
						// Clear bit
						packet[1 + dotParams.nBytePtr] &= (byte)~(byte)(1 << dotParams.nBitPtr);
					}
					else
					{
						// Set bit
						packet[1 + dotParams.nBytePtr] |= (byte)(1 << dotParams.nBitPtr);
					}
				}
			}
		}

		private void SendPacket()
		{
			packet[0] = 0xBB;
			packet[packet.Length - 1] = 0xEE;

			int len = packet.Length;
			DoRS232BufferSend(packet, ref len);
		}

		private void tmrSample_Tick(object sender, System.EventArgs e)
		{
			BuildPacket();
			SendPacket();
		}

		private void chkTraceData_CheckedChanged(object sender, System.EventArgs e)
		{
			lblRefreshInterval.Enabled = chkTraceData.Checked;
			cboRefreshIntervals.Enabled = chkTraceData.Checked;
			lblMillisecond2.Enabled = chkTraceData.Checked;
			lvTraceData.Enabled = chkTraceData.Checked;
			
			if (chkTraceData.Checked)
			{
				// Start tracing
				BuildTraceList();
				tmrTrace.Interval = Int32.Parse(cboRefreshIntervals.SelectedItem.ToString());
				tmrTrace.Enabled = true;
			}
			else
			{
				tmrTrace.Enabled = false;
			}
		}

		private void tmrTrace_Tick(object sender, System.EventArgs e)
		{
			for (int i = 0; i < displayUnits.Length; i++)
			{
				DisplayUnit du = displayUnits[i];
				ListViewItem lvi = lvTraceData.Items[i];
				
				// Update text
				lvi.SubItems[1].Text = du.dblSampleValue.ToString("#,0.0");
				lvi.SubItems[2].Text = du.nDisplayValue.ToString();

				// Update icon
				if (!du.bEnabled)
				{
					if (lvi.ImageIndex != 0)
					{
						lvi.ImageIndex = 0;
					}
				}
				else
				{
					if (du.pc == null)
					{
						if (lvi.ImageIndex != 2)
						{
							lvi.ImageIndex = 2;
						}
					}
					else
					{
						if (lvi.ImageIndex != 1)
						{
							lvi.ImageIndex = 1;
						}
					}
				}
			}
		}

		private void BuildTraceList()
		{
			lvTraceData.Items.Clear();

			for (int i = 0; i < displayUnits.Length; i++)
			{
				DisplayUnit du = displayUnits[i];
				int nImageIndex;
				
				// initial text
				string[] saItems =
				{
					du.sName,
					du.dblSampleValue.ToString("#,0.0"),
					du.nDisplayValue.ToString(),
					BuildCounterString(du.sCatName, du.sInstName, du.sCntName)
				};				

				// initial icon
				if (!du.bEnabled)
				{
					nImageIndex = 0;
				}
				else
				{
					if (du.pc == null)
					{
						nImageIndex = 2;
					}
					else
					{
						nImageIndex = 1;
					}
				}

				lvTraceData.Items.Add(new ListViewItem(saItems, nImageIndex));
			}			
		}

		public static string BuildCounterString(string sCatName, string sInstName, string sCntName)
		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -