// 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); } }
private void _DragDrop(DragEventArgs e, Label lbl, PropertyGrid Pg, string Txt, ref EnIPAttribut cipObj) { TreeNode t = (TreeNode)e.Data.GetData(typeof(TreeNode)); cipObj = (EnIPAttribut)t.Tag; lbl.Text = Txt + "\r\nNode " + cipObj.GetStrPath(); cipObj.ReadDataFromNetwork(); Pg.SelectedObject = cipObj; Pg.ExpandAllGridItems(); }
// 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. } }
static void Main(string[] args) { Console.WriteLine("Starting"); IPEndPoint ep = new IPEndPoint(IPAddress.Parse("172.20.54.58"), 0xAF12); EnIPRemoteDevice WagoPlc = new EnIPRemoteDevice(ep); WagoPlc.autoConnect = true; WagoPlc.autoRegisterSession = true; // class 4, instance 107, attribut 3 : Input Data EnIPClass Class4 = new EnIPClass(WagoPlc, 4); EnIPInstance Instance107 = new EnIPInstance(Class4, 107); EnIPAttribut Inputs = new EnIPAttribut(Instance107, 3); // Read require, it provides the data size in the RawData field // If not, one have to make a new on it with the good size before // calling ForwardOpen : Inputs.RawData=new byte[xx] Inputs.ReadDataFromNetwork(); // Open an Udp endpoint, server mode is mandatory : default port 0x8AE // Local IP should be given if more than 1 ethernet/wifi interface is present IPEndPoint LocalEp = new IPEndPoint(IPAddress.Parse(""), 0x8AE); // It's not a problem to do this with more than one remote device, // the underlying udp socket is static WagoPlc.Class1Activate(LocalEp); // Not required in P2P mode WagoPlc.Class1AddMulticast("239.192.72.32"); // Attach concerned attributs to the UDP callback handler : here just one Inputs.Class1Enrolment(); // Register me to get notified Inputs.T2OEvent += new T2OEventHandler(Inputs_T2OEvent); // ForwardOpen in Multicast, T2O, cycle 200 ms, duration infinite (-1) Inputs.ForwardOpen(false, true, false, 200, -1); Console.WriteLine("Running, hit a key to stop"); Console.ReadKey(); Inputs.ForwardClose(); }
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; }
static void Main(string[] args) { Console.WriteLine("Starting"); IPEndPoint ep = new IPEndPoint(IPAddress.Parse("100.75.137.27"), 0xAF12); EnIPRemoteDevice OpENer = new EnIPRemoteDevice(ep); OpENer.autoConnect = true; OpENer.autoRegisterSession = true; // class 4, instance 151, attribut 3 : Config Data EnIPClass Class4 = new EnIPClass(OpENer, 4); EnIPInstance Instance151 = new EnIPInstance(Class4, 151); EnIPAttribut Config = new EnIPAttribut(Instance151, 3); // class 4, instance 150, attribut 3 : Output Data EnIPInstance Instance150 = new EnIPInstance(Class4, 150); EnIPAttribut Outputs = new EnIPAttribut(Instance150, 3); // class 4, instance 100, attribut 3 : Input Data EnIPInstance Instance100 = new EnIPInstance(Class4, 100); EnIPAttribut Inputs = new EnIPAttribut(Instance100, 3); // Read require, it provides the data size in the RawData field // If not, one have to make a new on it with the good size before // calling ForwardOpen : Inputs.RawData=new byte[xx] Config.ReadDataFromNetwork(); Inputs.ReadDataFromNetwork(); Outputs.ReadDataFromNetwork(); IPEndPoint LocalEp = new IPEndPoint(IPAddress.Any, 0x8AE); // It's not a problem to do this with more than one remote device, // the underlying udp socket is static OpENer.Class1Activate(LocalEp); // ForwardOpen in P2P, cycle 200 ms ForwardOpen_Config conf = new ForwardOpen_Config(Outputs, Inputs, true, 200 * 1000); // here can change conf for exemple to set Exclusive use, change priority or CycleTime not equal in the both direction // Attributes order cannot be changed, last optional attribute true // will write the config value Config.RawData (modifies it after ReadDataFromNetwork before this call) ForwardClose_Packet CloseData; EnIPNetworkStatus result = OpENer.ForwardOpen(Config, Outputs, Inputs, out CloseData, conf, false); if (result == EnIPNetworkStatus.OnLine) { // Register Inputs events to get notified Inputs.T2OEvent += new T2OEventHandler(Inputs_T2OEvent); Console.WriteLine("Running, hit a key to stop"); while (!Console.KeyAvailable) { Outputs.RawData[0] = (byte)(Outputs.RawData[0] + 1); Outputs.Class1UpdateO2T(); // must be called even if no data changed to maintain the link (Heartbeat) Thread.Sleep(200); } OpENer.ForwardClose(CloseData); } else { Console.WriteLine("Fail"); } }