using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Model; using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service; using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service.Impl; using Casic.Birmm.RbFreqStandMeasure.R_DevService.Dto; using Casic.Birmm.RbFreqStandMeasure.R_DevService.Service; using Casic.Birmm.RbFreqStandMeasure.R_DevService.Service.Impl; using Casic.Birmm.RbFreqStandMeasure.tools; using Casic.Birmm.RbFreqStandMeasure.Tools; using System; using System.Collections.Generic; using System.IO.Ports; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace Casic.Birmm.RbFreqStandMeasure.home { public partial class SetDevTestDlg : Form { DetectionItemService detectionItemService = new DetectionItemServiceImpl(); public int deviceId = -1; public static long inDetecCounterId = -1;// 正在检测灵敏度的计数器id DateTime endTimeBySystem = new DateTime(); DictService dictService = new DictServiceImpl(); DeviceService deviceService = new DeviceServiceImpl(); #region 无边框拖动效果/最小化窗口/关闭窗口 [DllImport("user32.dll")]//拖动无窗体的控件 public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; private void panel1_MouseDown(object sender, MouseEventArgs e) { //拖动窗体 ReleaseCapture(); SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } #endregion public Action HideMaskAction { get; set; } public SetDevTestDlg() { InitializeComponent(); timePicker_startTime.Value = DateTime.Now; timePicker_startTime.MinDate = DateTime.Now; getEndTime(); // 设置双缓冲 SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 跨线程访问控件 CheckForIllegalCrossThreadCalls = false; } // 开始检测 private void btnSave_Click(object sender, EventArgs e) { if(text_channelNo.Text.Equals("")) { MessageBox.Show("请选择通道!"); return; } if (checkBox_stability.Checked && !checkBox1s.Checked && !checkBox10s.Checked && !checkBox20s.Checked && !checkBox_100s.Checked) { MessageBox.Show("请选择频率稳定度采样时间!"); return; } if (!checkBox_accuracy.Checked && !checkBox_stability.Checked && !checkBox_ageRate.Checked && !checkBox_bootFeature.Checked ) { MessageBox.Show("请选择检测项目!"); return; } timePicker_endTime.Value = endTimeBySystem; string startTime = timePicker_startTime.Value.ToString("yyyy-MM-dd HH:mm:ss"); string endTime = timePicker_endTime.Value.ToString("yyyy-MM-dd HH:mm:ss"); // 打开串口 DetectionHelper detectionHelper = new DetectionHelper(); SerialPort port = RbFreqStdMeas.portList[Convert.ToInt32(text_channelNo.Text) - 1]; string portName = ConfigHelper.GetAppConfig("channel" + text_channelNo.Text).Split(' ')[0]; int band = Convert.ToInt32(ConfigHelper.GetAppConfig("channel" + text_channelNo.Text).Split(' ')[1]); if (port.IsOpen) port.Close(); port.PortName = portName; port.BaudRate = band; port.Parity = Parity.None; port.DataBits = 8; port.StopBits = StopBits.One; port.Handshake = Handshake.None; port.ReadTimeout = -1; port.WriteTimeout = 3000; try { port.Open(); if (!port.IsOpen) { MessageBox.Show("无法打开串口" + portName + ",添加检测失败!"); return; } else { RbFreqStdMeas.portList[Convert.ToInt32(text_channelNo.Text) - 1] = port; } } catch (Exception exc) { MessageBox.Show("无法打开串口" + portName + ",添加检测失败!"); return; } label_tip.Text = "正在从通道[" + text_channelNo.Text + "]获取数据,请稍侯......"; if (DetectionHelper.detectionHelper.getFrequencyData(port, true).Equals("")) { label_tip.Text = ""; MessageBox.Show("请连接仪器到通道[" + text_channelNo.Text + "]!"); return; } label_tip.Text = ""; //开始检测 string stability = "-3"; string accuracy = "-3"; string bootFeature = "-3"; string ageRate = "-3"; string interval = ""; string stability1 = "-3"; string stability10 = "-3"; string stability20 = "-3"; string stability100 = "-3"; //开始检测 if (checkBox_stability.Checked) { if (checkBox1s.Checked) interval = "1,"; if (checkBox10s.Checked) interval = interval + "10,"; if (checkBox20s.Checked) interval = interval + "20,"; if (checkBox_100s.Checked) interval = interval + "100"; stability = "-2"; if (checkBox_now.Checked || timePicker_startTime.Value <= DateTime.Now) { stability = "-1"; if (checkBox1s.Checked) stability1 = "-1"; if (checkBox10s.Checked) stability10 = "-1"; if (checkBox20s.Checked) stability20 = "-1"; if (checkBox_100s.Checked) stability100 = "-1"; detectionHelper.detecStability(deviceId, startTime, interval, port, true, false); } else { if (checkBox1s.Checked) stability1 = "-2"; if (checkBox10s.Checked) stability10 = "-2"; if (checkBox20s.Checked) stability20 = "-2"; if (checkBox_100s.Checked) stability100 = "-2"; detectionHelper.detecStability(deviceId, startTime, interval, port, false, false); } } if (checkBox_accuracy.Checked) { accuracy = "-2"; if (checkBox_now.Checked || timePicker_startTime.Value <= DateTime.Now) { accuracy = "-1"; detectionHelper.detecAccuracy(deviceId, port, endTime, true, false); } else { detectionHelper.detecAccuracy(deviceId, port, endTime, false, false); } } if (checkBox_bootFeature.Checked) { bootFeature = "-2"; if (checkBox_now.Checked || timePicker_startTime.Value <= DateTime.Now) { bootFeature = "-1"; detectionHelper.detecBootFeature(deviceId, startTime, port, true); } else { detectionHelper.detecBootFeature(deviceId, startTime, port, false); } } if (checkBox_ageRate.Checked) { ageRate = "-2"; if (checkBox_now.Checked || timePicker_startTime.Value <= DateTime.Now) { ageRate = "-1"; detectionHelper.detecAgeRate(deviceId, startTime, port, true); } else { detectionHelper.detecAgeRate(deviceId, startTime, port, false); } } long detectionItemId = detectionItemService.add(deviceId, startTime, endTime, stability, accuracy, bootFeature, ageRate, interval, text_channelNo.Text, stability1, stability10, stability20, stability100); detectionHelper.detectionItemId = detectionItemId; this.HideMaskAction(); HomeCtrlForm.homeCtrlForm.refreshChannelList(); HomeCtrlForm.homeCtrlForm.LoadChannelStatus(); HomeCtrlForm.homeCtrlForm.currentPage = 1; HomeCtrlForm.homeCtrlForm.LoadDevToBeTested(); } private void btnExitDev_Click(object sender, EventArgs e) { this.HideMaskAction(); RbFreqStdMeas.rbFreqStdMeas.TopMost = true; RbFreqStdMeas.rbFreqStdMeas.TopMost = false; } private void btn_downChannelList_Click(object sender, EventArgs e) { comboBox_channelNo.DroppedDown = true; } private void comboBox_channelNo_SelectedIndexChanged(object sender, EventArgs e) { text_channelNo.Text = comboBox_channelNo.SelectedItem.ToString(); } private void checkBox_stability_CheckedChanged(object sender, EventArgs e) { if (checkBox_stability.Checked) { groupBox3.Enabled = true; } else { groupBox3.Enabled = false; } getEndTime(); } // 选择开始时间后,自动计算结束时间 private void timePicker_startTime_ValueChanged(object sender, EventArgs e) { getEndTime(); } private void checkBox_now_CheckedChanged(object sender, EventArgs e) { if (checkBox_now.Checked) { timePicker_startTime.Value = DateTime.Now; } getEndTime(); } private void btn_downStartTime_Click(object sender, EventArgs e) { timePicker_startTime.ShowUpDown = true; } private void getEndTime() { if (checkBox_ageRate.Checked) endTimeBySystem = timePicker_startTime.Value.AddDays(7).AddMinutes(1).AddSeconds(30); else { if (checkBox_bootFeature.Checked) endTimeBySystem = timePicker_startTime.Value.AddHours(7).AddMinutes(1).AddSeconds(30); else { if (checkBox_stability.Checked) { if (checkBox_100s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(101 * 100).AddMinutes(2).AddSeconds(30); else if (checkBox20s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(101 * 20).AddMinutes(2).AddSeconds(30); else if (checkBox10s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(101 * 10).AddMinutes(2).AddSeconds(30); else if (checkBox1s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(101).AddMinutes(2).AddSeconds(30); } else endTimeBySystem = timePicker_startTime.Value.AddMinutes(1).AddSeconds(30); } } timePicker_endTime.Value = endTimeBySystem; } private void timePicker_endTime_ValueChanged(object sender, EventArgs e) { } private void checkBox_accuracy_CheckedChanged(object sender, EventArgs e) { getEndTime(); } private void checkBox_bootFeature_CheckedChanged(object sender, EventArgs e) { getEndTime(); } private void checkBox_ageRate_CheckedChanged(object sender, EventArgs e) { getEndTime(); } private void radio_1s_CheckedChanged(object sender, EventArgs e) { getEndTime(); } private void timePicker_startTime_Click(object sender, EventArgs e) { } private void checkBox1s_CheckedChanged(object sender, EventArgs e) { getEndTime(); } private void checkBox10s_CheckedChanged(object sender, EventArgs e) { getEndTime(); } private void checkBox20s_CheckedChanged(object sender, EventArgs e) { getEndTime(); } private void checkBox_100s_CheckedChanged(object sender, EventArgs e) { getEndTime(); } } }