public override EnIPNetworkStatus ReadDataFromNetwork() { byte[] DataPath = EnIPPath.GetPath(myInstance.myClass.Id, myInstance.Id, Id); EnIPNetworkStatus ret = ReadDataFromNetwork(DataPath, CIPServiceCodes.GetAttributeSingle); if (ret == EnIPNetworkStatus.OnLine) { CIPObjectLibrary classid = (CIPObjectLibrary)myInstance.myClass.Id; try { if (DecodedMembers == null) // No decoder { if (myInstance.DecodedMembers == null) { myInstance.AttachDecoderClass(); } DecodedMembers = myInstance.DecodedMembers; // get the same object as the associated Instance } int Idx = 0; DecodedMembers.DecodeAttr(Id, ref Idx, RawData); } catch { } } return(ret); }
// Menu Item private void readAgainToolStripMenuItem_Click(object sender, EventArgs e) { if (propertyGrid.SelectedObject is EnIPCIPObject) { LastReadNetworkStatus = (propertyGrid.SelectedObject as EnIPCIPObject).ReadDataFromNetwork(); SoftRefreshPropertyGrid(propertyGrid); } else { LastReadNetworkStatus = EnIPNetworkStatus.OffLine; } }
private void buttonFw_Click(object sender, EventArgs e) { if (!checkP2P.Checked) // multicast mode, get first @ by device query { if (MCastAddress == null) { GetMultiCastAdress(); if (MCastAddress != null) { device.Class1AddMulticast(MCastAddress); // I will be possible to Join the 32 consecutives @ to be sure } } } if (FwclosePacket == null) { // CycleTime in microseconds EnIPNetworkStatus result = device.ForwardOpen(Config, Output, Input, out FwclosePacket, (uint)(CycleTime.Value * 1000), checkP2P.Checked, checkWriteConfig.Checked); if (result == EnIPNetworkStatus.OnLine) { buttonFw.Text = "Forward Close"; tmrO2T.Interval = (int)CycleTime.Value; // here in ms it's a Windows timer tmrO2T.Enabled = true; if (Input != null) { Input.T2OEvent += new T2OEventHandler(Input_T2OEvent); } } else { FwclosePacket = null; } } else { tmrO2T.Enabled = false; device.ForwardClose(FwclosePacket); buttonFw.Text = "(Large)Forward Open"; FwclosePacket = null; if (Input != null) { Input.T2OEvent -= new T2OEventHandler(Input_T2OEvent); } ImgInputActivity.Visible = false; } }
// Put the Online of Offline icon to the RemoteDevice in the TreeView private void CurrentRemoteDeviceIcon(EnIPNetworkStatus status) { TreeNode tn = devicesTreeView.SelectedNode; while (tn.Parent != null) { tn = tn.Parent; } if ((tn.SelectedImageIndex == 0) && (status == EnIPNetworkStatus.OffLine)) { tn.ImageIndex = tn.SelectedImageIndex = 1; } else if ((tn.SelectedImageIndex != 0) && (status != EnIPNetworkStatus.OffLine)) { tn.ImageIndex = tn.SelectedImageIndex = 0; } }
public override EnIPNetworkStatus ReadDataFromNetwork() { byte[] DataPath = EnIPPath.GetPath(myClass.Id, Id, null); EnIPNetworkStatus ret = ReadDataFromNetwork(DataPath, CIPServiceCodes.GetAttributesAll); if (ret == EnIPNetworkStatus.OnLine) { if (DecodedMembers == null) { AttachDecoderClass(); } try { DecodedMembers.SetRawBytes(RawData); } catch { } } return(ret); }
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"); } }
public override EnIPNetworkStatus ReadDataFromNetwork() { // Read all class static attributes byte[] ClassDataPath = EnIPPath.GetPath(Id, 0, null); EnIPNetworkStatus ret = ReadDataFromNetwork(ClassDataPath, CIPServiceCodes.GetAttributesAll); // If rejected try to read all attributes one by one if (ret == EnIPNetworkStatus.OnLineReadRejected) { MemoryStream rawbuffer = new MemoryStream(); ushort AttId = 1; // first static attribut number do { ClassDataPath = EnIPPath.GetPath(Id, 0, AttId); ret = ReadDataFromNetwork(ClassDataPath, CIPServiceCodes.GetAttributeSingle); // push the buffer into the data stream if (ret == EnIPNetworkStatus.OnLine) { rawbuffer.Write(RawData, 0, RawData.Length); } AttId++; }while (ret == EnIPNetworkStatus.OnLine); // yes OK like this, pull the data out of the stream into the RawData if (rawbuffer.Length != 0) { Status = ret = EnIPNetworkStatus.OnLine; // all is OK even if the last request is (always) rejected RawData = rawbuffer.ToArray(); } } if (ret == EnIPNetworkStatus.OnLine) { CIPObjectLibrary classid = (CIPObjectLibrary)Id; try { if (DecodedMembers == null) { try { if (DecoderClass == null) { // try to create the associated class object var o = Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, "System.Net.EnIPStack.ObjectsLibrary.CIP_" + classid.ToString() + "_class"); DecodedMembers = (CIPObject)o.Unwrap(); } else { var o = Activator.CreateInstance(DecoderClass); DecodedMembers = (CIPObject)o; } } catch { // echec, get the base class as described in Volume 1, §4-4.1 Class Attributes DecodedMembers = new CIPObjectBaseClass(classid.ToString()); } } DecodedMembers.SetRawBytes(RawData); } catch { } } return(ret); }