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; } }
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"); } }