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.tools; using Casic.Birmm.RbFreqStandMeasure.Tools; using DevComponents.DotNetBar.Controls; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.IO.Ports; using System.Reflection; using System.Text; using System.Threading; using System.Timers; using System.Windows.Forms; namespace Casic.Birmm.RbFreqStandMeasure { public partial class HomeCtrlForm : UserControl { public static HomeCtrlForm homeCtrlForm; int totalCount = 0; int currentPage = 1; int pageCount = 0; int limitCount = 4; DataTable devToBeTestedTable = null; DataTable channelsTable = null; DeviceService deviceService = new DeviceServiceImpl(); List<DeviceView> listChannelStatus = new List<DeviceView>(); DetectionItemService detectionItemService = new DetectionItemServiceImpl(); public static SerialPort frePort_1 = new SerialPort(); public static SerialPort frePort_2 = new SerialPort(); public static SerialPort frePort_3 = new SerialPort(); public static SerialPort frePort_4 = new SerialPort(); public static SerialPort frePort_5 = new SerialPort(); public static SerialPort clockPort = new SerialPort(); public static SerialPort satellitePort = new SerialPort(); private string[] portNames = null; private List<SerialPort> serialPorts; private byte[] portBuffer; public static List<string> channelFreeList = new List<string>(); System.Timers.Timer timerPort; //计时器,每1s中读取一次串口数据 #region 样式常量定影 private readonly Color titleBackColor = Color.FromArgb(63, 132, 215); // #3F84D7 private readonly Font titleFont = new Font("微软雅黑", 14F, FontStyle.Regular, GraphicsUnit.Pixel, 134); #endregion public HomeCtrlForm() { InitializeComponent(); homeCtrlForm = this; } private void HomeCtrlForm_Load(object sender, EventArgs e) { // 初始化表格标题栏 InitTableDevList(); // 加载待检测列表 LoadDevToBeTested(); // 初始化各通道样式 InitTableChannel(); LoadChannelStatus(); } #region 待测仪器列表 private void InitTableDevList() { tableToBeTested.Columns.Clear(); tableToBeTested.Rows.Clear(); // 添加标题栏 Panel tableHeader = new Panel { BackColor = Color.White, Size = new Size(764, 32), Location = new Point(0, 0) }; tableHeader.BackColor = titleBackColor; tableToBeTested.Controls.Add(tableHeader); // 仪器名称 Label colDevName = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "仪器名称", Location = new Point(20, 0), Size = new Size(100, 32) }; tableToBeTested.Controls.Add(colDevName); colDevName.BringToFront(); // 仪器类型 Label colDevType = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "仪器类型", Location = new Point(160, 0), Size = new Size(100, 32) }; tableToBeTested.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(300, 0), Size = new Size(100, 32) }; tableToBeTested.Controls.Add(colDevNo); colDevNo.BringToFront(); // 客户名称 Label colCustomName = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "客户名称", Location = new Point(440, 0), Size = new Size(100, 32) }; tableToBeTested.Controls.Add(colCustomName); colCustomName.BringToFront(); // 客户单位 Label colCustomComp = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "客户单位", Location = new Point(600, 0), Size = new Size(120, 32) }; tableToBeTested.Controls.Add(colCustomComp); colCustomComp.BringToFront(); totalCount = 0; currentPage = 1; pageCount = 0; limitCount = 4; devToBeTestedTable = new DataTable(); devToBeTestedTable.Columns.Add("id", Type.GetType("System.String")); devToBeTestedTable.Columns.Add("devName", Type.GetType("System.String")); devToBeTestedTable.Columns.Add("devTypeName", Type.GetType("System.String")); devToBeTestedTable.Columns.Add("devCode", Type.GetType("System.String")); devToBeTestedTable.Columns.Add("customerName", Type.GetType("System.String")); devToBeTestedTable.Columns.Add("customerDev", Type.GetType("System.String")); } public void LoadDevToBeTested() { devToBeTestedTable.Rows.Clear(); // 获取数据 // 获取待检测设备的总条数 totalCount = deviceService.getTotalCount("", "", "", "", "", "1"); // 获取当前页的数据 List<DeviceDto> queryList = deviceService.searchPage("", "", "", "", "", "1", currentPage, limitCount); if (null != queryList && queryList.Count > 0) { // 重新计算页码数据 pageCount = totalCount / limitCount; int ys = totalCount % limitCount; if (ys != 0) pageCount++; setPageLabel(); foreach (DeviceDto device in queryList) { devToBeTestedTable.Rows.Add(device.Id, device.DevName, device.DevTypeName, device.DevCode, device.CustomerName, device.CustomerDev); } //tableToBeTested.Columns[0].Visible = false; //tableToBeTested.Columns[0].Visible = false; tableToBeTested.Visible = true; labelNodata.Visible = false; tableToBeTested.DataSource = devToBeTestedTable; tableToBeTested.Columns[0].Visible = false; // width=984px tableToBeTested.Columns[1].Width = 140; tableToBeTested.Columns[2].Width = 140; tableToBeTested.Columns[3].Width = 140; tableToBeTested.Columns[4].Width = 140; tableToBeTested.Columns[5].Width = 200; tableToBeTested.Columns[0].ReadOnly = true; tableToBeTested.Columns[1].ReadOnly = true; tableToBeTested.Columns[2].ReadOnly = true; tableToBeTested.Columns[3].ReadOnly = true; tableToBeTested.Columns[4].ReadOnly = true; tableToBeTested.Columns[5].ReadOnly = true; } else { tableToBeTested.Visible = false; labelNodata.Visible = true; } } private void tableToBeTested_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { int rowIndex = tableToBeTested.CurrentCell.RowIndex; int colIndex = tableToBeTested.CurrentCell.ColumnIndex; SetDevTestDlg devTestDlg = new SetDevTestDlg(); devTestDlg.deviceId = Convert.ToInt32(tableToBeTested.Rows[rowIndex].Cells[0].Value); devTestDlg.text_customerDev.Text = tableToBeTested.Rows[rowIndex].Cells[5].Value.ToString(); devTestDlg.text_devName.Text = tableToBeTested.Rows[rowIndex].Cells[1].Value.ToString(); devTestDlg.text_devCode.Text = tableToBeTested.Rows[rowIndex].Cells[3].Value.ToString(); devTestDlg.text_customerName.Text = tableToBeTested.Rows[rowIndex].Cells[4].Value.ToString(); devTestDlg.text_devType.Text = tableToBeTested.Rows[rowIndex].Cells[2].Value.ToString(); ((ComboBox)devTestDlg.Controls.Find("comboBox_channelNo", true)[0]).DataSource = channelFreeList; if (channelFreeList.Count > 0) { devTestDlg.Controls.Find("groupBox2", true)[0].Enabled = true; devTestDlg.Controls.Find("btnSave", true)[0].Enabled = true; devTestDlg.Controls.Find("timePicker_startTime", true)[0].Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } else { ((TextBoxX)devTestDlg.Controls.Find("text_channelNo", true)[0]).WatermarkText = "无空闲通道"; } devTestDlg.HideMaskAction += () => { this.HideMask(); }; this.ShowMask(); devTestDlg.StartPosition = FormStartPosition.CenterParent; // 指定窗口弹出在父窗口的中间位置 devTestDlg.ShowDialog(); } private void setPageLabel() { label_pageInfo.Text = "共" + totalCount + "条,每页" + limitCount + "条,共" + pageCount + "页"; textBoxPage.Text = currentPage.ToString(); } private void btnPre_Click(object sender, EventArgs e) { if (Convert.ToInt16(textBoxPage.Text) > 1) { currentPage = Convert.ToInt16(textBoxPage.Text) - 1; LoadDevToBeTested(); } } private void btnNext_Click(object sender, EventArgs e) { if (Convert.ToInt16(textBoxPage.Text) < pageCount) { currentPage = Convert.ToInt16(textBoxPage.Text) + 1; LoadDevToBeTested(); } } #endregion #region 通道列表 private void InitTableChannel() { dataGridView_Channel.Columns.Clear(); dataGridView_Channel.Rows.Clear(); // 添加标题栏 Panel tableHeader = new Panel { BackColor = Color.White, Size = new Size(984, 30), Location = new Point(0, 0) }; tableHeader.BackColor = titleBackColor; dataGridView_Channel.Controls.Add(tableHeader); // 通道 Label colChannelNo = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "通道", Location = new Point(0, 0), Size = new Size(50, 30) }; dataGridView_Channel.Controls.Add(colChannelNo); colChannelNo.BringToFront(); // 仪器名称 Label colDevName = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "仪器名称", Location = new Point(50, 0), Size = new Size(110, 30) }; dataGridView_Channel.Controls.Add(colDevName); colDevName.BringToFront(); // 仪器编号 Label colDevNo = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "仪器编号", Location = new Point(160, 0), Size = new Size(110, 30) }; dataGridView_Channel.Controls.Add(colDevNo); colDevNo.BringToFront(); // 客户名称 Label colCustomName = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "客户名称", Location = new Point(270, 0), Size = new Size(110, 30) }; dataGridView_Channel.Controls.Add(colCustomName); colCustomName.BringToFront(); // 开始时间 Label colStartTime = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "开始时间", Location = new Point(380, 0), Size = new Size(120, 30) }; dataGridView_Channel.Controls.Add(colStartTime); colStartTime.BringToFront(); // 结束时间 Label colEndTime = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "结束时间", Location = new Point(500, 0), Size = new Size(120, 30) }; dataGridView_Channel.Controls.Add(colEndTime); colEndTime.BringToFront(); // 状态 Label colStatus = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "状态", Location = new Point(620, 0), Size = new Size(64, 30) }; dataGridView_Channel.Controls.Add(colStatus); colStatus.BringToFront(); // 开机特性 Label colStartup = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "开机特性", Location = new Point(684, 0), Size = new Size(75, 30) }; dataGridView_Channel.Controls.Add(colStartup); colStartup.BringToFront(); // 频率稳定度 Label colStablility = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "稳定度", Location = new Point(759, 0), Size = new Size(75, 30) }; dataGridView_Channel.Controls.Add(colStablility); colStablility.BringToFront(); // 日老化率 Label colAgingRate = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "日老化率", Location = new Point(824, 0), Size = new Size(75, 30) }; // 频率准确度 Label colAccuracy = new Label { Font = titleFont, ForeColor = Color.White, TextAlign = ContentAlignment.MiddleCenter, BackColor = titleBackColor, AutoSize = false, Text = "准确度", Location = new Point(899, 0), Size = new Size(75, 30) }; dataGridView_Channel.Controls.Add(colAccuracy); colAccuracy.BringToFront(); dataGridView_Channel.Controls.Add(colAgingRate); colAgingRate.BringToFront(); } public void LoadChannelStatus() { channelsTable = new DataTable(); channelsTable.Columns.Add("channelNo", Type.GetType("System.String")); channelsTable.Columns.Add("devName", Type.GetType("System.String")); channelsTable.Columns.Add("devCode", Type.GetType("System.String")); channelsTable.Columns.Add("customName", Type.GetType("System.String")); channelsTable.Columns.Add("startTime", Type.GetType("System.DateTime")); channelsTable.Columns.Add("endTime", Type.GetType("System.DateTime")); channelsTable.Columns.Add("channelStatus", Type.GetType("System.String")); channelsTable.Columns.Add("startup", Type.GetType("System.String")); channelsTable.Columns.Add("stablility", Type.GetType("System.String")); channelsTable.Columns.Add("accuracy", Type.GetType("System.String")); channelsTable.Columns.Add("agingRate", Type.GetType("System.String")); channelsTable.Columns.Add("deviceId", Type.GetType("System.Int32")); //channelsTable.Columns.Add("devType", Type.GetType("System.String")); //channelsTable.Columns.Add("devModel", Type.GetType("System.String")); //channelsTable.Columns.Add("customCom", Type.GetType("System.String")); listChannelStatus = deviceService.getDeviceByChannel(); channelsTable.Rows.Add(channelsTable.NewRow()); channelsTable.Rows.Add(channelsTable.NewRow()); channelsTable.Rows.Add(channelsTable.NewRow()); channelsTable.Rows.Add(channelsTable.NewRow()); channelsTable.Rows.Add(channelsTable.NewRow()); channelFreeList.Clear(); channelFreeList.Add("1"); channelFreeList.Add("2"); channelFreeList.Add("3"); channelFreeList.Add("4"); channelFreeList.Add("5"); foreach (DeviceView device in listChannelStatus) { channelFreeList.Remove(device.Channel); int index = Convert.ToInt32(device.Channel) - 1; channelsTable.Rows[index]["channelNo"] = device.Channel; channelsTable.Rows[index]["devName"] = device.DevName; channelsTable.Rows[index]["devCode"] = device.DevCode; channelsTable.Rows[index]["customName"] = device.CustomerName; channelsTable.Rows[index]["startTime"] = device.StartTime; channelsTable.Rows[index]["endTime"] = device.EndTime; channelsTable.Rows[index]["startTime"] = device.StartTime; channelsTable.Rows[index]["channelStatus"] = "占用"; channelsTable.Rows[index]["deviceId"] = device.Id; if (device.Stability.Equals("-3")) this.Controls.Find("pic_stability_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.line; else if (device.Stability.Equals("-2")) this.Controls.Find("pic_stability_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_gray; else if (device.Stability.Equals("-1")) this.Controls.Find("pic_stability_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_orange; else this.Controls.Find("pic_stability_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_green; if (device.Accuracy.Equals("-3")) this.Controls.Find("pic_accuracy_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.line; else if (device.Accuracy.Equals("-2")) this.Controls.Find("pic_accuracy_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_gray; else if (device.Accuracy.Equals("-1")) this.Controls.Find("pic_accuracy_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_orange; else this.Controls.Find("pic_accuracy_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_green; if (device.AgeRate.Equals("-3")) this.Controls.Find("pic_ageRate_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.line; else if (device.AgeRate.Equals("-2")) this.Controls.Find("pic_ageRate_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_gray; else if (device.AgeRate.Equals("-1")) this.Controls.Find("pic_ageRate_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_orange; else this.Controls.Find("pic_ageRate_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_green; if (device.BootFeature.Equals("-3")) this.Controls.Find("pic_bootFeature_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.line; else if (device.BootFeature.Equals("-2")) this.Controls.Find("pic_bootFeature_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_gray; else if (device.BootFeature.Equals("-1")) this.Controls.Find("pic_bootFeature_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_orange; else this.Controls.Find("pic_bootFeature_" + device.Channel, true)[0].BackgroundImage = Properties.Resources.dot_green; } foreach (string no in channelFreeList) { channelsTable.Rows[Convert.ToInt32(no) - 1]["channelNo"] = no; channelsTable.Rows[Convert.ToInt32(no) - 1]["channelStatus"] = "空闲"; } dataGridView_Channel.DataSource = channelsTable; // width=764px dataGridView_Channel.Columns[11].Visible = false; //dataGridView_Channel.Columns[12].Visible = false; dataGridView_Channel.Columns[0].Width = 50; dataGridView_Channel.Columns[1].Width = 110; dataGridView_Channel.Columns[2].Width = 110; dataGridView_Channel.Columns[3].Width = 110; dataGridView_Channel.Columns[4].Width = 120; dataGridView_Channel.Columns[5].Width = 120; dataGridView_Channel.Columns[6].Width = 64; dataGridView_Channel.Columns[7].Width = 75; dataGridView_Channel.Columns[8].Width = 75; dataGridView_Channel.Columns[9].Width = 75; dataGridView_Channel.Columns[10].Width = 75; dataGridView_Channel.Columns[0].ReadOnly = true; dataGridView_Channel.Columns[1].ReadOnly = true; dataGridView_Channel.Columns[2].ReadOnly = true; dataGridView_Channel.Columns[3].ReadOnly = true; dataGridView_Channel.Columns[4].ReadOnly = true; dataGridView_Channel.Columns[5].ReadOnly = true; dataGridView_Channel.Columns[6].ReadOnly = true; dataGridView_Channel.Columns[7].ReadOnly = true; dataGridView_Channel.Columns[8].ReadOnly = true; dataGridView_Channel.Columns[9].ReadOnly = true; dataGridView_Channel.Columns[10].ReadOnly = true; } private void dataGridView_Channel_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { dataGridView_Channel.ClearSelection(); int channelNo = e.RowIndex + 1; if (channelFreeList.Contains(channelNo + "")) { dataGridView_Channel.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(232, 234, 238); panel_channelList.Controls.Find("panel" + channelNo, true)[0].Visible = false; } else { dataGridView_Channel.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(201, 203, 208); panel_channelList.Controls.Find("panel" + channelNo, true)[0].Visible = true; } foreach (string no in channelFreeList) { dataGridView_Channel.Rows[Convert.ToInt32(no) - 1].DefaultCellStyle.BackColor = Color.FromArgb(232, 234, 238); panel_channelList.Controls.Find("panel" + no, true)[0].Visible = false; } } private void dataGridView_Channel_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { int rowIndex = dataGridView_Channel.CurrentCell.RowIndex; int colIndex = dataGridView_Channel.CurrentCell.ColumnIndex; if (dataGridView_Channel.Rows[rowIndex].Cells[6].Value.ToString().Equals("占用")) { if (MessageBox.Show("该通道有正在检测的仪器,确定要停止检测?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int deviceId = (int)dataGridView_Channel.Rows[rowIndex].Cells[11].Value; DateTime startTime = (DateTime)dataGridView_Channel.Rows[rowIndex].Cells[4].Value; DateTime endTime = (DateTime)dataGridView_Channel.Rows[rowIndex].Cells[5].Value; if (MessageBox.Show("删除之前是否保存此次检测的数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { detectionItemService.stopDetection(deviceId, startTime, endTime,true); } else { detectionItemService.stopDetection(deviceId, startTime, endTime, false); } LoadChannelStatus(); LoadDevToBeTested(); return; } else { return; } } SetDevChannelDlg channelDlg = new SetDevChannelDlg(rowIndex+1); //channelDlg.deviceId = Convert.ToInt32(dataGridView_Channel.Rows[rowIndex].Cells[11].Value); //channelDlg.text_devCode.Text = dataGridView_Channel.Rows[rowIndex].Cells[2].Value.ToString(); //channelDlg.text_devName.Text = dataGridView_Channel.Rows[rowIndex].Cells[1].Value.ToString(); //channelDlg.text_devType.Text = dataGridView_Channel.Rows[rowIndex].Cells[12].Value.ToString(); //channelDlg.text_devModel.Text = dataGridView_Channel.Rows[rowIndex].Cells[13].Value.ToString(); //channelDlg.text_customerDev.Text = dataGridView_Channel.Rows[rowIndex].Cells[14].Value.ToString(); //channelDlg.text_customerName.Text = dataGridView_Channel.Rows[rowIndex].Cells[3].Value.ToString(); channelDlg.HideMaskAction += () => { this.HideMask(); }; this.ShowMask(); channelDlg.StartPosition = FormStartPosition.CenterParent; // 指定窗口弹出在父窗口的中间位置 channelDlg.ShowDialog(); } #endregion private MaskForm maskForm; private void ShowMask() { maskForm = new MaskForm(this.Parent.Parent.Location, this.Parent.Parent.Size); maskForm.Show(); } private void HideMask() { if (maskForm != null) { maskForm.Close(); } } private void openPortsAndReceive() { // 检测数据串口 string portName = ConfigHelper.GetAppConfig("channel1").ToString().Split(' ')[0]; int bandRate = Convert.ToInt32(ConfigHelper.GetAppConfig("channel1").ToString().Split(' ')[1]); frePort_1 = portOpen(portName, bandRate); if (frePort_1 == null) return; portName = ConfigHelper.GetAppConfig("channel2").ToString().Split(' ')[0]; bandRate = Convert.ToInt32(ConfigHelper.GetAppConfig("channel2").ToString().Split(' ')[1]); frePort_2 = portOpen(portName, bandRate); if (frePort_2 == null) return; portName = ConfigHelper.GetAppConfig("channel3").ToString().Split(' ')[0]; bandRate = Convert.ToInt32(ConfigHelper.GetAppConfig("channel3").ToString().Split(' ')[1]); frePort_3 = portOpen(portName, bandRate); if (frePort_3 == null) return; portName = ConfigHelper.GetAppConfig("channel4").ToString().Split(' ')[0]; bandRate = Convert.ToInt32(ConfigHelper.GetAppConfig("channel4").ToString().Split(' ')[1]); frePort_4 = portOpen(portName, bandRate); if (frePort_4 == null) return; portName = ConfigHelper.GetAppConfig("channel5").ToString().Split(' ')[0]; bandRate = Convert.ToInt32(ConfigHelper.GetAppConfig("channel5").ToString().Split(' ')[1]); frePort_5 = portOpen(portName, bandRate); if (frePort_5 == null) return; // 原子钟串口 portName = ConfigHelper.GetAppConfig("clock").ToString().Split(' ')[0]; bandRate = Convert.ToInt32(ConfigHelper.GetAppConfig("clock").ToString().Split(' ')[1]); clockPort = portOpen(portName, bandRate); if (clockPort == null) return; // 接收机串口 portName = ConfigHelper.GetAppConfig("satellite").ToString().Split(' ')[0]; bandRate = Convert.ToInt32(ConfigHelper.GetAppConfig("satellite").ToString().Split(' ')[1]); satellitePort = portOpen(portName, bandRate); if (satellitePort == null) return; label_clockStatus.Text = getClockData(clockPort); getSatelliteData(satellitePort); } private SerialPort portOpen(string portName, int bandRate) { try { SerialPort port = new SerialPort(); // 搜索串口 string[] names = SerialPort.GetPortNames(); if (names.Length == 0 || Array.IndexOf(names, portName) == -1 ) { MessageBox.Show("没有搜索到串口" + portName + "!"); return null; } // 设置串口参数 port.PortName = names[0]; port.BaudRate = bandRate; port.Parity = Parity.None; port.DataBits = 8; port.StopBits = StopBits.One; port.Handshake = Handshake.None; port.ReadTimeout = -1; port.WriteTimeout = 3000; // 打开串口 if (!port.IsOpen) { port.Open(); } if (port.IsOpen) { return port; } return null; } catch(Exception ex) { MessageBox.Show("错误","打开串口失败!"); LogHelper.WriteInfoLog(MethodBase.GetCurrentMethod().DeclaringType, "打开串口失败!"+ ex.Message); return null; } } // 获取检测数据 public string getFrequencyData(SerialPort port) { string received = ""; if (ConfigHelper.GetAppConfig("deviceType").ToString().Equals("1")) { int count = 0; while (count < 11) { int readLen = port.BytesToRead; if (readLen > 0) { byte[] buffer = new byte[readLen]; port.Read(buffer, 0, readLen);// 接收数据到buffer里面 string data = Encoding.ASCII.GetString(buffer); if (data.StartsWith("$") && data.EndsWith("<LF><CR>")) { received = data.Split(' ')[0].Replace("$", "") + "." + data.Split(' ')[1].Replace(".", "").TrimEnd('0'); break; } } count++; } } else { int count = 0; while (count < 11) { int readLen = port.BytesToRead; if (readLen > 0) { byte[] buffer = new byte[readLen]; port.Read(buffer, 0, readLen);// 接收数据到buffer里面 string data = Encoding.ASCII.GetString(buffer); if ((data.StartsWith("0") || data.StartsWith("1") || data.StartsWith("2") || data.StartsWith("3")) && data.EndsWith("\r\n")) { received = data.Split(' ')[2]; break; } } count++; } } return received; } // 获取铷钟数据 public string getClockData(SerialPort port) { byte[] clockData = new byte[94]; string clockStatus = ""; int offset = 0; int count = 0; int count2 = 0; while (count<11) { // 获取接收缓冲区长度 int Readlen = port.BytesToRead; // 接收到数据 if (Readlen > 0) { byte[] buffer = new byte[Readlen]; port.Read(buffer, 0, Readlen);// 接收数据到buffer里面 // 判断接收到的数据 string data = Encoding.ASCII.GetString(buffer); if (data.StartsWith("$")) { Array.Copy(buffer, 0, clockData, offset, Readlen); offset = offset + Readlen; if (Readlen == 94) { data = Encoding.ASCII.GetString(clockData); string clockStatusCode = data.Split(',')[9]; switch (clockStatusCode) { case "1": clockStatus = "预热"; break; case "2": clockStatus = "自由运动"; break; case "3": clockStatus = "捕获"; break; case "4": clockStatus = "快锁"; break; case "5": clockStatus = "慢锁"; break; case "7": clockStatus = "保持"; break; default: break; } break; } else { while (count2 < 60) { Readlen = port.BytesToRead; if (Readlen > 0) { buffer = new byte[Readlen]; port.Read(buffer, 0, Readlen);// 接收数据到buffer里面 Array.Copy(buffer, 0, clockData, offset, Readlen); data = Encoding.ASCII.GetString(clockData); string clockStatusCode = data.Split(',')[9]; switch (clockStatusCode) { case "1": clockStatus = "预热"; break; case "2": clockStatus = "自由运动"; break; case "3": clockStatus = "捕获"; break; case "4": clockStatus = "快锁"; break; case "5": clockStatus = "慢锁"; break; case "7": clockStatus = "保持"; break; default: break; } offset = offset + Readlen; if (offset == 94) break; } count2++; } } break; } } count++; } return clockStatus; } // 获取卫星数据 public void getSatelliteData(SerialPort port) { // RMC string strRMC = ""; int offset = 0;// GSV卫星数据的总条数 // GSV byte[] dataGSV = new byte[1024]; List<string> strGSVList = new List<string>(); int count = 0; while (count < 10) { // 获取接收缓冲区长度 int Readlen = port.BytesToRead; if (Readlen > 0) { byte[] buffer = new byte[Readlen]; port.Read(buffer, 0, Readlen);// 接收数据到buffer里面 string data = Encoding.ASCII.GetString(buffer); // RMC卫星信号,用于获取卫星状态 if (data.Contains("RMC")) { strRMC = data; } // GSV卫星信号,用于展示时间、日期、画卫星图等 if (data.Contains("GSV")) { strGSVList.Add(data); } } } } } }