private void InitCtrl() { BusController.OnConnectStateChanged += BusController_OnConnectStateChanged; barButtonItemConnect.Enabled = true; barButtonItem_Disconnect.Enabled = false; MenuConnect.Enabled = barButtonItemConnect.Enabled; MenuDisconnect.Enabled = barButtonItem_Disconnect.Enabled; ImageList imgList = new ImageList(); imgList.Images.Add(new Bitmap(@"images/Category.png")); imgList.Images.Add(new Bitmap(@"images/Bus.png")); imgList.Images.Add(new Bitmap(@"images/SlaveMaster.png")); imgList.Images.Add(new Bitmap(@"images/Company.png")); imgList.Images.Add(new Bitmap(@"images/Device.png")); treeViewDevice.ImageList = imgList; var BustypeList = new List <string>(); for (int i = 0; i < DeviceCfg.Length; i++) { BustypeList.Add(DeviceCfg[i].BusType); } var RootNode = new TreeNode("Device"); RootNode.ImageIndex = 0; foreach (var BusType in BustypeList.Distinct()) { var BusRelationshipList = new List <string>(); var BusTypeNode = new TreeNode(BusType); BusTypeNode.ImageIndex = 1; var DevBusType = DeviceCfg.Where(d => d.BusType.Equals(BusType)); for (int i = 0; i < DevBusType.Count(); i++) { BusRelationshipList.Add(DevBusType.ElementAt(i).Category); } foreach (var Relationship in BusRelationshipList.Distinct()) { var DeviceNameList = new List <string>(); var BusRelationshipNode = new TreeNode(Relationship); BusRelationshipNode.ImageIndex = 2; var DevBusRelationship = DevBusType.Where(d => d.Category.Equals(Relationship)); for (int i = 0; i < DevBusRelationship.Count(); i++) { DeviceNameList.Add(DevBusRelationship.ElementAt(i).DeviceName); } foreach (var Dev in DeviceNameList.Distinct()) { var DeviceNameNode = new TreeNode(Dev); DeviceNameNode.ImageIndex = 4; BusRelationshipNode.Nodes.Add(DeviceNameNode); } BusTypeNode.Nodes.Add(BusRelationshipNode); } RootNode.Nodes.Add(BusTypeNode); } treeViewDevice.Nodes.Add(RootNode); treeViewDevice.ExpandAll(); treeViewDevice.ItemDrag += TreeViewDevice_ItemDrag; treeViewDevice.NodeMouseDoubleClick += TreeViewDevice_NodeMouseDoubleClick; this.barSubIteExportFile.Popup += BarSubIteExportFile_Popup; //添加中间控件 MiddleControl = new ProductContrainer(); this.dockPanelMiddle.Controls.Add(MiddleControl); MiddleControl.Dock = DockStyle.Fill; //添加侧面控件 LeftControl = new treeviewContrainer(); LeftControl.OnBusModulChanged += LeftControl_OnBusModulChanged; this.dockPanelLeft.Controls.Add(LeftControl); LeftControl.Dock = DockStyle.Fill; LeftControl.ProductContrainer = MiddleControl; //VarMonitor UC_VarMonitor ucMonitor = new UC_VarMonitor(); elementHost2.Child = ucMonitor; ucMonitor.OnStartMonitorEventHandler += UcMonitor_OnStartMonitorEventHandler; ucMonitor.OnStopMonitorEventHandler += UcMonitor_OnStopMonitorEventHandler; ucMonitor.OnModifyValueEventHandler += UcMonitor_OnModifyValueEventHandler; VarCollect = ucMonitor.VarCollect; //for (int i = 0; i < 3; i++) //{ //VarCollect.Add(new MonitorVarModel() { IoType = Definations.EnumModuleIOType.IN }); //VarCollect.Add(new MonitorVarModel() { IoType = Definations.EnumModuleIOType.IN }); //VarCollect.Add(new MonitorVarModel() { IoType = Definations.EnumModuleIOType.IN }); //VarCollect.Add(new MonitorVarModel() { IoType = Definations.EnumModuleIOType.OUT }); //VarCollect.Add(new MonitorVarModel() { IoType = Definations.EnumModuleIOType.OUT }); // VarCollect.Add(new MonitorVarModel() { IoType = Definations.EnumModuleIOType.IN }); // VarCollect.Add(new MonitorVarModel() { IoType = Definations.EnumModuleIOType.OUT }); // VarCollect.Add(new MonitorVarModel() { IoType = Definations.EnumModuleIOType.OUT }); // VarCollect.Add(new MonitorVarModel() { IoType = Definations.EnumModuleIOType.IN }); //} this.elementHost1.BackColorTransparent = true; this.elementHost2.BackColorTransparent = true; dockManager1.ActivePanel = dockPanelMiddle; dockPanelVarMonitor.VisibilityChanged += DockPanelVarMonitor_VisibilityChanged; //dockManager1.RemovePanel(dockPanelVarMonitor); ProjController.BusFileMgr = new EthercatFileMgr(); //Start Task to monitor Controller Task.Run(() => { var ModifyValueList = new List <UInt32>(); var ModuleInfoList = new List <ModuleInfoBase>(); while (!ctsMonitorController.IsCancellationRequested) { EventMonitorController.WaitOne(); Thread.Sleep(200); if (BusController.IsConnected) { if (OnFirstCircle) { OnFirstCircle = false; //首次不修改输出模块的值,只是在需要修改的时候才修改 UpdateMonitorVarCollect(out ModuleInfoList); for (int i = 0; i < ModuleInfoList.Count; i++) { var L = ModuleInfoList[i].ModuleList.Where(m => m.IOType == EnumModuleIOType.OUT); if (L != null) { foreach (var it in L) { ModifyValueList.Add(0x55); } } } } BusController.GetModuleValue(ModifyValueList, out OutputValueRecv_List, out InputValueRecv_List); var OutputMonitorModule = VarCollect.Where(c => c.IoType == EnumModuleIOType.OUT); var InputMonitorModule = VarCollect.Where(c => c.IoType == EnumModuleIOType.IN); if (OutputMonitorModule != null && OutputMonitorModule.Count() == OutputValueRecv_List.Count) { for (int i = 0; i < OutputValueRecv_List.Count; i++) { OutputMonitorModule.ElementAt(i).CurValue = $"{OutputValueRecv_List[i]}"; } } if (InputMonitorModule != null && InputMonitorModule.Count() == InputValueRecv_List.Count) { for (int i = 0; i < InputValueRecv_List.Count; i++) { InputMonitorModule.ElementAt(i).CurValue = $"{InputValueRecv_List[i]}"; } } } } }, ctsMonitorController.Token); //HeartBeat Task.Run(() => { int i = 0; while (!ctsHeartBeat.IsCancellationRequested) { EventHeartBeat.WaitOne(); Thread.Sleep(500); Console.WriteLine($"---------------Heart beat {i++}------------------"); if (BusController.IsConnected) { try { if (!BusController.Hearbeat()) { EventHeartBeat.Reset(); MessageBox.Show("Connection Timeout"); } } catch (Exception ex) { EventHeartBeat.Reset(); MessageBox.Show($"Connection Timeout:{ex.Message}"); Console.WriteLine($"Connection Timeout:{ex.StackTrace}"); } } } }, ctsHeartBeat.Token); }