// Each time we received a response to udp broadcast or udp/tcp unicast ListIdentity void On_DeviceArrival(EnIPRemoteDevice device) { if (InvokeRequired) { Invoke(new DeviceArrivalHandler(On_DeviceArrival), new object[] { device }); return; } // Device already present ? foreach (TreeNode tn in devicesTreeView.Nodes) { EnIPRemoteDevice oldRemoteDevice = (EnIPRemoteDevice)tn.Tag; if (oldRemoteDevice.Equals(device)) { oldRemoteDevice.CopyData(device); // change the icon if needed if (tn.SelectedImageIndex != 0) { tn.ImageIndex = tn.SelectedImageIndex = 0; } // change the Text maybe tn.Text = device.IPAdd().ToString() + " - " + device.ProductName; if (devicesTreeView.SelectedNode == tn) { devicesTreeView.SelectedNode = null; } return; } } TreeNode tn2 = new TreeNode(device.IPAdd().ToString() + " - " + device.ProductName, 0, 0); tn2.Tag = device; devicesTreeView.Nodes.Add(tn2); // Queue connection ThreadPool.QueueUserWorkItem((o) => device.Connect()); }
static void client_DeviceArrival(EnIPRemoteDevice device) { Console.WriteLine("Arrvial of : " + device.IPAdd().ToString() + " - " + device.ProductName); // Query the object list : Assembly object, instance 1, atribut 1 Console.WriteLine("Classes inside :"); device.GetObjectList(); foreach (EnIPClass cl in device.SupportedClassLists) { Console.WriteLine("\t" + ((CIPObjectLibrary)cl.Id).ToString()); } // Close pending connection device.Dispose(); }
// Menu Item private TreeNode AddRemoteDevice(EnIPRemoteDevice remotedevice) { foreach (TreeNode tn in devicesTreeView.Nodes) { if ((tn.Tag as EnIPRemoteDevice).Equals(remotedevice)) { return(tn); } } TreeNode tn2 = new TreeNode(remotedevice.IPAdd().ToString() + " - " + remotedevice.ProductName, 1, 1); tn2.Tag = remotedevice; devicesTreeView.Nodes.Add(tn2); return(tn2); }
// Menu Item private void saveConfigurationAsToolStripMenuItem_Click(object sender, EventArgs e) { try { SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = Properties.Settings.Default.DefaultTreeFile; dlg.Filter = "csv|*.csv"; if (dlg.ShowDialog(this) != System.Windows.Forms.DialogResult.OK) { return; } string filename = dlg.FileName; Properties.Settings.Default.DefaultTreeFile = filename; StreamWriter sw = new StreamWriter(filename); char s = Properties.Settings.Default.CSVSeparator; sw.WriteLine("Device IP" + s + "Name" + s + "Class" + s + "ClassName" + s + "Instance" + s + "InstanceName" + s + "Attribute" + s + "AttributeName"); sw.WriteLine("// EnIPExplorer Device Tree, can be modified with a spreadsheet"); foreach (TreeNode tn in devicesTreeView.Nodes) { EnIPRemoteDevice remote = (EnIPRemoteDevice)tn.Tag; sw.WriteLine(remote.IPAdd().ToString() + s + remote.ProductName); SaveFileEnIPOject(sw, s.ToString() + s.ToString(), tn.Nodes); } sw.Close(); MessageBox.Show("Done", "File Save", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { MessageBox.Show("File Error", "EnIPExplorer", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
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; }
public ComboboxItem(EnIPRemoteDevice rd) { RemoteDevice = rd; Text = rd.IPAdd().ToString() + " - " + rd.ProductName; }