using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.Windows.Forms; namespace TProduct.Link.Debugger { internal partial class DebugShunLinkForm : DevExpress.XtraEditors.XtraForm { private Dictionary _userGaugeDict = null; private TProduct.Link.Shun.LinkHelper theLinkHelper = null;//发送消息列表 //private System.Timers.Timer tTimer; public DebugShunLinkForm() { InitializeComponent(); btnConnectCom.Enabled = false; btnStopDisp.Enabled = false; } //加载 private void WindowLoad(object sender, EventArgs e) { RichTextBox.CheckForIllegalCrossThreadCalls = false;//运行子线程修改RichTextBox的内容 //初始化COM口 InitialDispContrl(comboBoxCom); //comboBoxCom.Text = TPump.UI.WinFrm.Properties.Settings.Default.ComPortName; //// //comboBoxTestBed.Properties.Items.Clear(); //tableBed = new TPump.DAL.DataSetTestTableAdapters.TestBedsTableAdapter().GetData(); //foreach (var row in tableBed.AsEnumerable()) //{ // if (!row.IsUse) // continue; // comboBoxTestBed.Properties.Items.Add(new Model.BindValueItem1() { ID = row.TestBedID, Name = row.TestBedName }); //} } public static bool InitialDispContrl(System.Windows.Forms.ComboBox ctrl) { ctrl.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames()); if (ctrl.Items.Count == 0) { MessageBox.Show("没有COM端口"); return false; } ctrl.SelectedIndex = 0; return true; } //关闭 private void WindowClose(object sender, FormClosingEventArgs e) { //if (tTimer != null) // tTimer.Close(); if (theLinkHelper == null) return; // theInstrumentManager.Stop(); } // private void comboBoxTestBed_SelectedIndexChanged(object sender, EventArgs e) { // btnConnectCom.Enabled = true; btnStopDisp.Enabled = true; } Model.WorkBenchInstrumentShunBundle _bundles = null;//从数据库中获取 //初始化仪表控件 private bool InitialGuages() { this.flowLayoutPanel1.Controls.Clear(); if (_bundles == null) return false; _userGaugeDict = new Dictionary(); foreach (var moniotor in _bundles.MonitorPointList) { UserGaugeControl sControl = new UserGaugeControl(moniotor, false);//并没有测试,所以没有单位 //sControl.gaugeControl1.Height = 60; //sControl.gaugeControl1.Width = 120; sControl.Height = 90; sControl.Width = 170; sControl.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.flowLayoutPanel1.Controls.Add(sControl); _userGaugeDict[moniotor.ID] = sControl; } return true; } //连接或断开 COM口 private void btnConnectCom_Click(object sender, EventArgs e) { try { if (btnConnectCom.Text == "连接测试仪器") { ////初始化仪表控件 //if (!InitialGuages()) //{ // return; //} //if (theInstrumentManager == null) //{ // theInstrumentManager = new TProduct.Link.Shun.LinkHelper();//发送消息列表 // //使用事件接收 // theInstrumentManager.OnReceiveMsg += OnReceiveMessage; // theInstrumentManager.OnDisMessageEvent += OnDisplayMessage; // theInstrumentManager.IsDispMessage = true; //} //string error_info; //if (!theInstrumentManager.InitialComPort(comboBoxCom.Text, 9600, out error_info)) //{ // if (!string.IsNullOrEmpty(error_info)) // MessageBox.Show(error_info); // return; //} //if (!theInstrumentManager.InitialWorkBenchInfo(_bundles, out error_info)) //{ // if (!string.IsNullOrEmpty(error_info)) // MessageBox.Show(error_info); // return; //} ////开始 //if (!theInstrumentManager.Start( out error_info)) //{ // if (!string.IsNullOrEmpty(error_info)) // MessageBox.Show(error_info); // return; //} ////!保存Com端口号 //TProduct.Link.Debugger.Properties.Settings.Default.PortName = comboBoxCom.Text; //TProduct.Link.Debugger.Properties.Settings.Default.Save(); //btnConnectCom.Text = "测试仪器断开"; //comboBoxCom.Enabled = false; //btnStopDisp.Enabled = true; //btnClearDisp.Enabled = true; //comboBoxTestBed.Enabled = false; ////设置通讯周期 //tTimer = new System.Timers.Timer(); //tTimer.Interval = 300 ;//!通讯间隔时间 //tTimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件 //tTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnSendMessage);//到达时间的时候执行事件; //tTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true) } else if (btnConnectCom.Text == "测试仪器断开") { //if (tTimer != null) //{ // tTimer.Close(); // Thread.Sleep(500); //} //theInstrumentManager.CloseComPort(); //theInstrumentManager.ClearAllEvent(); btnConnectCom.Text = "连接测试仪器"; comboBoxCom.Enabled = true; btnStopDisp.Enabled = false; btnClearDisp.Enabled = false; comboBoxTestBed.Enabled = true; } } catch (Exception ex) { XtraMessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } //自动发消息(事件) private void OnSendMessage(object source, System.Timers.ElapsedEventArgs e) { if (theLinkHelper == null) return; //theInstrumentManager.SendMessage(); } //接收消息(事件) private void OnReceiveMessage(object sender, TProduct.Model.ReceiveMsgEventArgs e) { //具体的值 List receiveMessagesInfos = e.MonitorValues; if (receiveMessagesInfos == null) return; foreach (TProduct.Model.MonitorPointValue value in receiveMessagesInfos) { double rValue = value.Value; _userGaugeDict[value.ID].SetValue(rValue);//!设置测试值 } } //显示消息(事件) public void OnDisplayMessage(string str) { this.rtfTerminal1.AppendText(str); this.rtfTerminal1.AppendText("\n"); //this.rtfTerminal1.Invoke // ( // /*表示一个委托,该委托可执行托管代码中声明为 void 且不接受任何参数的任何方法。 // * 在对控件的 Invoke 方法进行调用时或需要一个简单委托又不想自己定义时可以使用该委托。*/ // new MethodInvoker // ( // delegate // { // /*匿名方法,这是一种允许程序员将一段完整代码区块当成参数传递的程序代码编写技术, // * 通过此种方法可 以直接使用委托来设计事件响应程序以下就是你要在主线程上实现的功能但是有一点要注意, // * 这里不适宜处理过多的方法,因为C#消息机制是消息流水线响应机制, // * 如果这里在主线程上处理语句的时间过长会导致主UI线程阻塞, // * 停止响应或响应不顺畅,这时你的主form界面会延迟或卡死 */ // // string str = "R--" + strMessage + "V0--" + rValue.ToString() ; // this.rtfTerminal1.AppendText(str);//输出到主窗口文本控件 // } // ) // ); } // private void btnClearDisp_Click(object sender, EventArgs e) { rtfTerminal1.Clear(); } // private bool isDispMessage = true; private void btnStopDisp_Click(object sender, EventArgs e) { isDispMessage = !isDispMessage; if (isDispMessage) { btnStopDisp.Text = "停止显示"; //theLinkHelper.IsDispMessage = true; } else { btnStopDisp.Text = "继续显示"; //theLinkHelper.IsDispMessage = false; } } } }