// Read the first analog value on a Wago PLC 750-8xx public static void TestReadLoop() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("172.20.54.58"), 0xAF12); EnIPRemoteDevice WagoPlc = new EnIPRemoteDevice(ep); // class 103, instance 1, attribut 1 // could be class 4, Instance 104 (with status) or 107 (wihout), attribut 3 for all Input data EnIPClass Class103 = new EnIPClass(WagoPlc, 103); EnIPInstance Instance1 = new EnIPInstance(Class103, 1); EnIPAttribut FirstAnalogInput = new EnIPAttribut(Instance1, 1); WagoPlc.autoConnect = false; WagoPlc.autoRegisterSession = true; for (; ;) { // Connect or try re-connect, could be made with a long delay if (!WagoPlc.IsConnected()) { WagoPlc.Connect(); } if (!WagoPlc.IsConnected()) { return; } // all data will be put in the byte[] RawData member of EnIPInstanceAttribut if (FirstAnalogInput.ReadDataFromNetwork() == EnIPNetworkStatus.OnLine) { Console.WriteLine((FirstAnalogInput.RawData[1] << 8) | FirstAnalogInput.RawData[0]); } Thread.Sleep(200); } }
// Read 4.104.3 on an Eurotherm Epack public static void TestReadLoop() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("172.20.54.119"), 0xAF12); EnIPRemoteDevice EPack = new EnIPRemoteDevice(ep); // class 4, Instance 100, attribut 3 for all Input data EnIPClass Class4 = new EnIPClass(EPack, 4); // here we gives the decoder class (subclass of CIPObject) in the 3rd parameter EnIPInstance Instance100 = new EnIPInstance(Class4, 100, typeof(InstanceDecoder)); EnIPAttribut AllInputs = new EnIPAttribut(Instance100, 3); EPack.autoConnect = false; EPack.autoRegisterSession = true; for (; ;) { // Connect or try re-connect, could be made with a long delay if (!EPack.IsConnected()) { EPack.Connect(); } if (!EPack.IsConnected()) { return; } // all data will be put in the byte[] RawData member of EnIPInstanceAttribut // ... but decoded in the DecoderMemeber if (AllInputs.ReadDataFromNetwork() == EnIPNetworkStatus.OnLine) { // rawdata basic technic // Console.WriteLine((AllInputs.RawData[1] << 8) | AllInputs.RawData[0]); InstanceDecoder decoded = (InstanceDecoder)AllInputs.DecodedMembers; Console.WriteLine(decoded.AnalogInput); Console.WriteLine(decoded.Frequency / 10.0); // and so on } Thread.Sleep(200); // If a Forward Open is done such as in Class1Sample application sample, // the decoder is always called : a first ReadDataFromNetwork is mandatory // before that. } }
// 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()); }
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; }