using Casic.Birmm.RbFreqStandMeasure.home; using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Dto; 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 System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace Casic.Birmm.RbFreqStandMeasure.counter { public partial class CounterCtrlForm : UserControl { DataTable table_counterDetecInit = null; DeviceService deviceService = new DeviceServiceImpl(); DictService dictService = new DictServiceImpl(); CounterDetecInitService counterDetecInitService = new CounterDetecInitServiceImpl(); CounterDataService counterDataService = new CounterDataServiceImpl(); SensitivityService sensitivityService = new SensitivityServiceImpl(); Label colStdValue = new Label(); Label colValue = new Label(); string devType = ""; string devCode = ""; string devPort = "输入A"; long deviceId = 0; bool isAdd = false; GPIBService gPIBService = new GPIBServiceImpl(); GPIBService tcpService = new TCPServiceImpl(); #region 样式常量定影 private readonly Color titleBackColor = Color.FromArgb(63, 132, 215); // #3F64D7 private readonly Font titleFont = new Font("微软雅黑", 14F, FontStyle.Regular, GraphicsUnit.Pixel, 134); #endregion public CounterCtrlForm() { InitializeComponent(); List<DeviceDto> list = deviceService.getAllCounter(); comboBox_counters.DataSource = list; comboBox_counters.DisplayMember = "DevCode"; comboBox_counters.ValueMember = "Id"; InitTableCounter(); LoadCounterDetecInitList(); //string[] ids = gPIBService.getId(); //string[] ips = tcpService.getId(); if (gPIBService.getId() != null && gPIBService.getId().Length > 0) { text_signal.Text = gPIBService.getId()[0]; } if (tcpService.getId() != null && tcpService.getId().Length > 0) { text_counterIp.Text = tcpService.getId()[0]; } } private void InitTableCounter() { dataGridView_CounterResult.DataSource = null; // 添加标题栏 Panel tableHeader = new Panel { BackColor = Color.White, Size = new Size(714, 50), Location = new Point(0, 0) }; tableHeader.BackColor = titleBackColor; dataGridView_CounterResult.Controls.Add(tableHeader); // 序号 Label colIndex = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "序号", Location = new Point(0, 0), Size = new Size(50, 50) }; dataGridView_CounterResult.Controls.Add(colIndex); colIndex.BringToFront(); // 仪器型号 Label colDevType = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "仪器型号", Location = new Point(46, 0), Size = new Size(100, 50) }; dataGridView_CounterResult.Controls.Add(colDevType); colDevType.BringToFront(); // 仪器编号 Label colDevNo = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "仪器编号", Location = new Point(145, 0), Size = new Size(100, 50) }; dataGridView_CounterResult.Controls.Add(colDevNo); colDevNo.BringToFront(); // 描述/端口 Label colDevPort = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "描述/端口", Location = new Point(240, 0), Size = new Size(100, 50) }; dataGridView_CounterResult.Controls.Add(colDevPort); colDevPort.BringToFront(); // 标称值 colStdValue = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "输出频率", Location = new Point(328, 0), Size = new Size(100, 50) }; dataGridView_CounterResult.Controls.Add(colStdValue); colStdValue.BringToFront(); // 指标值 colValue = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "测量频率", Location = new Point(413, 0), Size = new Size(100, 50) }; dataGridView_CounterResult.Controls.Add(colValue); colValue.BringToFront(); // 创建日期 Label colSensitivity = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "灵敏度", Location = new Point(512, 0), Size = new Size(75, 50) }; dataGridView_CounterResult.Controls.Add(colSensitivity); colSensitivity.BringToFront(); Label colTime = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "测量时间", Location = new Point(588, 0), Size = new Size(100, 50) }; dataGridView_CounterResult.Controls.Add(colTime); colTime.BringToFront(); } private void LoadCounterDetecInitList() { //dataGridView_CounterResult.Columns.Add("index", ""); //dataGridView_CounterResult.Columns.Add("devType", ""); //dataGridView_CounterResult.Columns.Add("devNo", ""); //dataGridView_CounterResult.Columns.Add("devPort", ""); //dataGridView_CounterResult.Columns.Add("stdValue", ""); //dataGridView_CounterResult.Columns.Add("value", ""); //dataGridView_CounterResult.Columns.Add("sensitivity", ""); //dataGridView_CounterResult.Columns.Add("logTime", ""); //dataGridView_CounterResult.Columns.Add("id", ""); table_counterDetecInit = new DataTable(); table_counterDetecInit.Columns.Add("index", Type.GetType("System.String")); table_counterDetecInit.Columns.Add("devType", Type.GetType("System.String")); table_counterDetecInit.Columns.Add("devNo", Type.GetType("System.String")); table_counterDetecInit.Columns.Add("devPort", Type.GetType("System.String")); table_counterDetecInit.Columns.Add("stdValue", Type.GetType("System.String")); table_counterDetecInit.Columns.Add("value", Type.GetType("System.String")); table_counterDetecInit.Columns.Add("sensitivity", Type.GetType("System.String")); table_counterDetecInit.Columns.Add("logTime", Type.GetType("System.String")); table_counterDetecInit.Columns.Add("id", Type.GetType("System.Int32")); if (text_counterCode.Text != "") { List<DeviceDto> list =deviceService.search("", text_counterCode.Text,"","","",""); if (null != list && list.Count > 0) { devType = list[0].DevTypeName; devCode = list[0].DevCode; } // 获取数据 List<CounterDetecInit> queryList = counterDetecInitService.getAll(); if (null != queryList && queryList.Count > 0) { int index = 1; foreach (CounterDetecInit counterDetecInit in queryList) { if (textBox_detecModel.Text.Equals("周期测量")) { table_counterDetecInit.Rows.Add(index, devType, devCode, devPort,counterDetecInit.Cycle,"","","", counterDetecInit.Id); } else { table_counterDetecInit.Rows.Add(index, devType, devCode, devPort,counterDetecInit.Frequency,"","","", counterDetecInit.Id); } //dataGridView_CounterResult.Rows.Add(index, devType, devCode, devPort, counterDetecInit.Frequency, "", "", "", counterDetecInit.Id); index++; } dataGridView_CounterResult.DataSource = table_counterDetecInit; dataGridView_CounterResult.Columns[8].Visible = false; // width=714px dataGridView_CounterResult.Columns[0].Width = 50; dataGridView_CounterResult.Columns[1].Width = 95; dataGridView_CounterResult.Columns[2].Width = 95; dataGridView_CounterResult.Columns[3].Width = 95; dataGridView_CounterResult.Columns[4].Width = 85; dataGridView_CounterResult.Columns[5].Width = 85; dataGridView_CounterResult.Columns[6].Width = 85; dataGridView_CounterResult.Columns[7].Width = 104; dataGridView_CounterResult.Columns[0].ReadOnly = true; dataGridView_CounterResult.Columns[1].ReadOnly = true; dataGridView_CounterResult.Columns[2].ReadOnly = true; dataGridView_CounterResult.Columns[3].ReadOnly = true; dataGridView_CounterResult.Columns[4].ReadOnly = true; dataGridView_CounterResult.Columns[5].ReadOnly = true; dataGridView_CounterResult.Columns[6].ReadOnly = true; dataGridView_CounterResult.Columns[7].ReadOnly = true; } else { //tableToBeTested.Visible = false; //labelNodata.Visible = true; } } } private void btn_downCounterList_Click(object sender, EventArgs e) { comboBox_counters.DroppedDown = true; } private void comboBox_counters_SelectedIndexChanged(object sender, EventArgs e) { text_counterCode.Text = ((DeviceDto)comboBox_counters.SelectedItem).DevCode; LoadCounterDetecInitList(); if (gPIBService.getId() != null && gPIBService.getId().Length > 0) { text_signal.Text = gPIBService.getId()[0]; } if (tcpService.getId() != null && tcpService.getId().Length > 0) { text_counterIp.Text = tcpService.getId()[0]; } } private void btn_dropDetecModel_Click(object sender, EventArgs e) { comboBox_detecModel.DroppedDown = true; } private void comboBox_detecModel_SelectedIndexChanged(object sender, EventArgs e) { textBox_detecModel.Text = comboBox_detecModel.SelectedItem.ToString(); if (textBox_detecModel.Text.Equals("频率测量")) { colStdValue.Text = "输出频率"; colValue.Text = "测量频率"; } else if (textBox_detecModel.Text.Equals("周期测量")) { colStdValue.Text = "输出周期"; colValue.Text = "测量周期"; } table_counterDetecInit.Rows.Clear(); List<CounterDetecInit> queryList = counterDetecInitService.getAll(); if (null != queryList && queryList.Count > 0) { int index = 1; foreach (CounterDetecInit counterDetecInit in queryList) { if (textBox_detecModel.Text.Equals("周期测量")) { table_counterDetecInit.Rows.Add(index, devType, devCode, devPort, counterDetecInit.Cycle, "", "", "", counterDetecInit.Id); } else { table_counterDetecInit.Rows.Add(index, devType, devCode, devPort, counterDetecInit.Frequency, "", "", "", counterDetecInit.Id); } //dataGridView_CounterResult.Rows.Add(index, devType, devCode, devPort, counterDetecInit.Frequency, "", "", "", counterDetecInit.Id); index++; } } } private void dataGridView_CounterResult_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 4) { dataGridView_CounterResult.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = false;//将当前单元格设为可读 dataGridView_CounterResult.CurrentCell = dataGridView_CounterResult.Rows[e.RowIndex].Cells[e.ColumnIndex];//获取当前单元格 dataGridView_CounterResult.BeginEdit(true);//将单元格设为编辑状态 } } private void dataGridView_CounterResult_CellValuePushed(object sender, DataGridViewCellValueEventArgs e) { } private void benDelete_Click(object sender, EventArgs e) { if (!(dataGridView_CounterResult.SelectedRows.Count > 0)) { MessageBox.Show("请选择想要删除的记录!"); return; } if (MessageBox.Show("确认删除选中记录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int id = Convert.ToInt32(dataGridView_CounterResult.CurrentRow.Cells[8].Value); counterDetecInitService.delete(id); string outValue = dataGridView_CounterResult.CurrentRow.Cells[4].Value.ToString(); string value = dataGridView_CounterResult.CurrentRow.Cells[5].Value.ToString(); string sensitivity = dataGridView_CounterResult.CurrentRow.Cells[6].Value.ToString(); DateTime logTime = (DateTime)dataGridView_CounterResult.CurrentRow.Cells[7].Value; if (!String.IsNullOrEmpty(outValue)&& !String.IsNullOrEmpty(value) && !String.IsNullOrEmpty(sensitivity)) { counterDataService.delete(deviceId, outValue, value, sensitivity, logTime); } //deviceService.delete(Convert.ToInt32(dataGridView_DevList.Rows[dataGridView_DevList.CurrentRow.Index].Cells[8].Value)); MessageBox.Show("删除成功", "提示"); //LoadCounterDetecInitList(); } } private void btn_add_Click(object sender, EventArgs e) { isAdd = true; int index = table_counterDetecInit.Rows.Count; table_counterDetecInit.Rows.Add(index+1, devType, devCode, devPort, "", "", "", "", -1); //dataGridView_CounterResult.dat dataGridView_CounterResult.Rows[index].Cells[4].ReadOnly = false;//将当前单元格设为可读 dataGridView_CounterResult.CurrentCell = dataGridView_CounterResult.Rows[index].Cells[4];//获取当前单元格 dataGridView_CounterResult.BeginEdit(true);//将单元格设为编辑状态 } private void roundButton1_Click(object sender, EventArgs e) { } private string freAndCycle(string input) { string output = ""; double inputNumberFre = 0.0; double inputMumberCye = 0.0; if (input.Contains("Hz")) { if (input.Contains("kHz")) { inputNumberFre = Convert.ToInt64(Convert.ToInt32(input.Substring(0, input.Length - 3)) * 1000); } else if (input.Contains("MHz")) { inputNumberFre = Convert.ToInt64(Convert.ToInt32(input.Substring(0, input.Length - 3)) * 1000 * 1000); } else if (input.Contains("GHz")) { inputNumberFre = Convert.ToInt64(Convert.ToInt32(input.Substring(0, input.Length - 3)) * 1000 * 1000); } else { inputNumberFre = Convert.ToInt64(Convert.ToInt32(input.Substring(0, input.Length - 2))); if (inputNumberFre == 1) { return "1s"; } } double cyc = 1 / inputNumberFre; int index = 0; foreach (char c in cyc.ToString().Split('.')[1]) { if (c.Equals('0')) index++; else break; } if (index < 2) output = cyc + "s"; else if (index >= 2 && index < 5) output = cyc * 1000 + "ms"; else if (index >= 5 && index < 8) output = cyc * 1000 * 1000 + "μs"; else if (index >= 8) output = cyc * 1000 * 1000 * 1000 + "ns"; } else if (input.Contains("s")) { if (input.Contains("ms")) { inputMumberCye = Convert.ToInt32(input.Substring(0, input.Length - 2)) / 1000; } else if (input.Contains("μs")) { inputMumberCye = Convert.ToInt32(input.Substring(0, input.Length - 2)) / 1000 / 1000; } else if (input.Contains("ns")) { inputMumberCye = Convert.ToInt32(input.Substring(0, input.Length - 2)) / 1000 / 1000 / 1000; } double fre = 1 / inputMumberCye; int len = fre.ToString().Split('.')[0].Length; if (len > 0 && len <= 3) output = fre + "Hz"; if (len > 3 && len <= 6) output = fre / 1000 + "KHz"; if (len > 6 && len <= 9) output = fre / 1000 / 1000+ "MHz"; if (len > 9) output = fre / 1000 / 1000+ "GHz"; } return output; } private void dataGridView_CounterResult_CellValueChanged(object sender, DataGridViewCellEventArgs e) { } private void btnStart_Click(object sender, EventArgs e) { string[] ids = gPIBService.getId(); string[] ips = tcpService.getId(); if (ids != null && ids.Length > 0) { text_signal.Text = ids[0]; } else { MessageBox.Show("请连接信号源!"); return; } if (ips != null && ips.Length > 0) { text_counterIp.Text = ips[0]; } else { MessageBox.Show("请连接频率计数器!"); return; } text_counterCode.ReadOnly = true; btn_downCounterList.Enabled = false; btnStart.Enabled = false; btn_dropDetecModel.Enabled = false; textBox_detecModel.ReadOnly = true; SetDevTestDlg.inDetecCounterId = deviceId; string type = textBox_detecModel.Text.Equals("频率测量") ? "freq" : "period"; string typeId = type.Equals("freq") ? "1" : "2"; new Thread(() => { // 获取数据 List<CounterDetecInit> counterDetecInitList = counterDetecInitService.getAll(); CounterCheckParam counterCheckParam = dictService.getCounterCheckParam(); gPIBService.open(gPIBService.getId()[0]); tcpService.open(tcpService.getId()[0]); foreach (DataGridViewRow row in dataGridView_CounterResult.Rows) { SensitivityContentParam sensitivityContentParam = new SensitivityContentParam(); if (type.Equals("freq")) { sensitivityContentParam.Freq = row.Cells[4].Value.ToString(); sensitivityContentParam.CheckModel = "0"; } else { sensitivityContentParam.Period = row.Cells[4].Value.ToString(); sensitivityContentParam.CheckModel = "1"; } int re = sensitivityService.beginTest(type, gPIBService, tcpService, counterCheckParam, ref sensitivityContentParam);//0:成功;-1:失败 string outValue = ""; outValue = type.Equals("freq") ? sensitivityContentParam.ReFreq : sensitivityContentParam.RePeriod; if (re == 0) { counterDataService.add(deviceId, row.Cells[5].Value.ToString(), outValue, sensitivityContentParam.VolTage, sensitivityContentParam.CheckTime, typeId); row.Cells[5].Value = outValue; row.Cells[6].Value = sensitivityContentParam.VolTage; row.Cells[7].Value = sensitivityContentParam.CheckTime; } } gPIBService.close(); tcpService.close(); Invoke(new MethodInvoker(delegate () { SetDevTestDlg.inDetecCounterId = -1; text_counterCode.ReadOnly = false; btn_downCounterList.Enabled = true; btnStart.Enabled = true; btn_dropDetecModel.Enabled = true; textBox_detecModel.ReadOnly = false; })); }).Start(); } private void dataGridView_CounterResult_CellEndEdit(object sender, DataGridViewCellEventArgs e) { int id = Convert.ToInt32(dataGridView_CounterResult.Rows[e.RowIndex].Cells[8].Value); string value = dataGridView_CounterResult.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); if (e.ColumnIndex == 4) { if (isAdd && String.IsNullOrEmpty(value)) { if (textBox_detecModel.Text.Equals("频率测量")) MessageBox.Show("输出频率不能为空,添加失败!"); else MessageBox.Show("输出周期不能为空,添加失败!"); table_counterDetecInit.Rows.RemoveAt(table_counterDetecInit.Rows.Count - 1); isAdd = false; return; } string freq = ""; string cycle = ""; if (textBox_detecModel.Text.Equals("频率测量")) { freq = value; cycle = freAndCycle(freq); } else { cycle = value; freq = freAndCycle(freq); } if (!isAdd) counterDetecInitService.update(id, freq, cycle); else counterDetecInitService.add(freq, cycle); isAdd = false; } } } }