// Menu Item // Add a new device not discovery using the broadcast technic : outside the local net private void addRemoteDeviceToolStripMenuItem_Click(object sender, EventArgs e) { if (client == null) { return; } var Input = new GenericInputBoxExtended <TextBox>("Remote device", "IP address", (o) => { o.Text = Properties.Settings.Default.DefaultRemoteDevice; }); DialogResult res = Input.ShowDialog(); if (res != DialogResult.OK) { return; } try { EnIPRemoteDevice remotedevice = new EnIPRemoteDevice(new System.Net.IPEndPoint(IPAddress.Parse(Input.genericInput.Text), 0xAF12), Properties.Settings.Default.TCP_WAN_TimeOut); remotedevice.ProductName = "EnIPExplorer temporary ProductName"; AddRemoteDevice(remotedevice); remotedevice.DeviceArrival += new DeviceArrivalHandler(On_DeviceArrival); remotedevice.DiscoverServer(); Properties.Settings.Default.DefaultRemoteDevice = Input.genericInput.Text; } catch { Trace.WriteLine("Error with IP : " + Input.genericInput.Text); } }
private void devicesTreeView_AfterSelect(object sender, TreeViewEventArgs e) { if (client == null) { return; } EnIPNetworkStatus ReadRet = EnIPNetworkStatus.OnLine; Cursor Memcurs = this.Cursor; this.Cursor = Cursors.WaitCursor; // It's a Device : top level if (e.Node.Tag is EnIPRemoteDevice) { EnIPRemoteDevice device = (EnIPRemoteDevice)e.Node.Tag; propertyGrid.SelectedObject = device; popupAddCToolStripMenuItem.Visible = true; popupAddIToolStripMenuItem.Visible = false; popupAddAToolStripMenuItem.Visible = false; decodeAttributAsToolStripMenuItem.Visible = false; popupDeleteToolStripMenuItem.Text = deleteToolStripMenuItem.Text = "Delete current Device"; if (device.SupportedClassLists.Count == 0) // certainly never discovers { if (device.IsConnected() == false) { device.Connect(); if (device.IsConnected() == false) { propertyGrid.Enabled = false; CurrentRemoteDeviceIcon(EnIPNetworkStatus.OffLine); this.Cursor = Memcurs; return; } } // never discovers if (device.DataLength == 0) { device.DiscoverServer(); } device.GetObjectList(); propertyGrid.Enabled = true; } // change the Text maybe String txt = device.IPAdd().ToString() + " - " + device.ProductName; if (e.Node.Text != txt) { e.Node.Text = txt; } foreach (EnIPClass clId in device.SupportedClassLists) { bool alreadyexist = false; foreach (TreeNode tn in e.Node.Nodes) { if ((tn.Tag as EnIPClass).Id == clId.Id) { alreadyexist = true; break; } } if (!alreadyexist) { e.Node.Nodes.Add(ClassToTreeNode(clId)); } } e.Node.Expand(); } // It's a Class else if (e.Node.Tag is EnIPClass) { // Read it from the remote devie EnIPClass EnClass = (EnIPClass)e.Node.Tag; ReadRet = EnClass.ReadDataFromNetwork(); LastReadNetworkStatus = EnIPNetworkStatus.OffLine; // to avoid periodic reading // In the Grid propertyGrid.SelectedObject = EnClass; propertyGrid.ExpandAllGridItems(); // Popup menu adaptation popupAddCToolStripMenuItem.Visible = true; popupAddIToolStripMenuItem.Visible = true; popupAddAToolStripMenuItem.Visible = false; decodeAttributAsToolStripMenuItem.Visible = false; popupDeleteToolStripMenuItem.Text = deleteToolStripMenuItem.Text = "Delete current Class"; } // It's an Instance else if (e.Node.Tag is EnIPInstance) { // Read it from the remote devie EnIPInstance Instance = (EnIPInstance)e.Node.Tag; LastReadNetworkStatus = ReadRet = Instance.ReadDataFromNetwork(); // remove properties litse filter based on CIPAttribut // in order to show all atrbiuts in the property grid if (Instance.DecodedMembers != null) { Instance.DecodedMembers.FilterAttribut(-1); } LastReadNetworkStatus = ReadRet = Instance.ReadDataFromNetwork(); // In the Grid propertyGrid.SelectedObject = Instance; propertyGrid.ExpandAllGridItems(); // Popup menu adaptation popupAddCToolStripMenuItem.Visible = true; popupAddIToolStripMenuItem.Visible = true; popupAddAToolStripMenuItem.Visible = true; decodeAttributAsToolStripMenuItem.Visible = false; popupDeleteToolStripMenuItem.Text = deleteToolStripMenuItem.Text = "Delete current Instance"; } // It's an Attribut else if (e.Node.Tag is EnIPAttribut) { // Read it from the remote devie EnIPAttribut Att = (EnIPAttribut)e.Node.Tag; LastReadNetworkStatus = ReadRet = Att.ReadDataFromNetwork(); // filter properties list for only the given attribut // remove instance undecoded bytes if exist if (Att.DecodedMembers != null) { Att.DecodedMembers.FilterAttribut(Att.Id); Att.DecodedMembers.Remain_Undecoded_Bytes = null; } // In the Grid propertyGrid.SelectedObject = Att; propertyGrid.ExpandAllGridItems(); // Popup menu adaptation popupAddCToolStripMenuItem.Visible = true; popupAddIToolStripMenuItem.Visible = true; popupAddAToolStripMenuItem.Visible = true; decodeAttributAsToolStripMenuItem.Visible = true; popupDeleteToolStripMenuItem.Text = deleteToolStripMenuItem.Text = "Delete current Attribute"; } propertyGrid.Enabled = (ReadRet == EnIPNetworkStatus.OnLine); CurrentRemoteDeviceIcon(ReadRet); this.Cursor = Memcurs; }