Exemplo n.º 1
0
        //  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);
            }
        }
Exemplo n.º 2
0
        //  Write %IW1530
        public static void TestWriteLoop()
        {
            IPEndPoint       ep      = new IPEndPoint(IPAddress.Parse("172.20.54.58"), 0xAF12);
            EnIPRemoteDevice WagoPlc = new EnIPRemoteDevice(ep);

            // class 166, instance 1, attribut 1
            EnIPClass    Class166        = new EnIPClass(WagoPlc, 166);
            EnIPInstance Instance1       = new EnIPInstance(Class166, 1);
            EnIPAttribut FirstMemoryByte = new EnIPAttribut(Instance1, 1);

            // Connect made & retry automatically
            WagoPlc.autoConnect         = true;
            WagoPlc.autoRegisterSession = true;

            ushort i = 0;

            for (; ;)
            {
                FirstMemoryByte.RawData = BitConverter.GetBytes(i++);
                if (FirstMemoryByte.WriteDataToNetwork() == EnIPNetworkStatus.OnLine)
                {
                    Console.WriteLine("OK");
                }

                Thread.Sleep(200);
            }
        }
Exemplo n.º 3
0
        // A new Class inside the Treeview : name & icon
        private TreeNode ClassToTreeNode(EnIPClass Class)
        {
            TreeNode tn;

            // Known classes
            if (Enum.IsDefined(typeof(CIPObjectLibrary), Class.Id))
            {
                CIPObjectLibrary cipobj = (CIPObjectLibrary)Class.Id;
                int img = Classe2Ico(cipobj);
                tn = new TreeNode(cipobj.ToString() + " #" + IdStr(Class.Id), img, img);

                // Special classes with the known instance(s)
                if ((Class.Id == 1) || (Class.Id == 2) || (Class.Id == 0xF4) || (Class.Id == 0xF5) || (Class.Id == 0xF6))
                {
                    EnIPInstance instance = new EnIPInstance(Class, 1); // Instance 1
                    TreeNode     tnI      = new TreeNode("Instance #" + IdStr(1), 9, 9);
                    tnI.Tag         = instance;
                    tnI.ToolTipText = "Node " + IdStr(Class.Id) + "." + IdStr(1);
                    tn.Nodes.Add(tnI);
                }
            }
            else
            {
                tn = new TreeNode("Proprietary #" + IdStr(Class.Id), 2, 2);
            }

            tn.ToolTipText = "Node " + IdStr(Class.Id);
            tn.Tag         = Class;
            return(tn);
        }
        void GetMultiCastAdress()
        {
            try
            {
                EnIPClass    Class245  = new EnIPClass(device, 245);
                EnIPInstance Instance1 = new EnIPInstance(Class245, 1, typeof(CIP_TCPIPInterface_instance));
                Instance1.ReadDataFromNetwork();

                MCastAddress = (Instance1.DecodedMembers as CIP_TCPIPInterface_instance).Mcast_Config.Mcast_Start_Addr;
            }
            catch {}
        }
Exemplo n.º 5
0
        //  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.
            }
        }
Exemplo n.º 6
0
        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();
        }
Exemplo n.º 7
0
        // Menu Item
        private void addClassInstanceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tn = devicesTreeView.SelectedNode;

            // look the node or upper
            for (; ;)
            {
                if (tn == null)
                {
                    return;
                }
                if (tn.Tag is EnIPClass)
                {
                    break;
                }
                tn = tn.Parent;
            }

            int Numbase = 1;

            foreach (TreeNode t in tn.Nodes)
            {
                int num = (t.Tag as EnIPInstance).Id;
                Numbase = Math.Max(num + 1, Numbase);
            }

            var Input =
                new GenericInputBoxExtended <NumericUpDown>("Add Instance", "Instance Id :",
                                                            (o) =>
            {
                o.Minimum     = 1; o.Maximum = 65535; o.Value = Numbase;
                o.Hexadecimal = Properties.Settings.Default.IdHexDisplay;
            },
                                                            (o) =>
            {
                ushort Id             = (ushort)o.Value;
                EnIPClass cl          = (EnIPClass)tn.Tag;
                EnIPInstance instance = new EnIPInstance(cl, Id);
                TreeNode tnI          = new TreeNode("Instance #" + IdStr(Id), 9, 9);
                tnI.Tag         = instance;
                tnI.ToolTipText = "Node " + IdStr(cl.Id) + "." + IdStr(Id);

                // speed me ... automatic add Attribut 3 (Data - Array of Bytes) to all Assembly instances
                if (cl.Id == (ushort)CIPObjectLibrary.Assembly)
                {
                    EnIPAttribut att = new EnIPAttribut(instance, 3);
                    TreeNode tnI2    = new TreeNode("Attribute #" + IdStr(3), 10, 10);
                    tnI2.Tag         = att;
                    tnI2.ToolTipText = "Node " + IdStr(cl.Id) + "." + IdStr(instance.Id) + ".3";
                    tnI.Nodes.Add(tnI2);
                }

                tn.Nodes.Add(tnI);
                tn.Expand();
                tnI.Expand();

                if (o.Value != o.Maximum)
                {
                    o.Value++;
                }
            });

            DialogResult res = Input.ShowDialog();
        }
Exemplo n.º 8
0
        // Menu Item
        private void addClassToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tn = devicesTreeView.SelectedNode;

            // look the node or upper
            for (; ;)
            {
                if (tn == null)
                {
                    return;
                }
                if (tn.Tag is EnIPRemoteDevice)
                {
                    break;
                }
                tn = tn.Parent;
            }

            int Numbase = 1;

            foreach (TreeNode t in tn.Nodes)
            {
                int num = (t.Tag as EnIPClass).Id;
                Numbase = Math.Max(num + 1, Numbase);
            }

            var Input =
                new GenericInputBoxExtended <NumericUpDown>("Add Class", "Class Id :",
                                                            (o) =>
            {
                o.Minimum       = 1; o.Maximum = 65535; o.Value = Numbase;
                o.Hexadecimal   = Properties.Settings.Default.IdHexDisplay;
                ToolTip tt      = new ToolTip();
                tt.AutoPopDelay = 32767;
                // Helper two columns tooltip with the object Id list
                StringBuilder sb = new StringBuilder();
                int i            = 0;
                foreach (CIPObjectLibrary en in Enum.GetValues(typeof(CIPObjectLibrary)))
                {
                    String s = IdStr((int)en) + " : " + en.ToString();
                    if (i == 0)
                    {
                        // 1, 2,3 or 4 tab
                        sb.Append(s + "\t");
                        if (s.Length < 10)
                        {
                            sb.Append('\t');
                        }
                        if (s.Length < 17)
                        {
                            sb.Append('\t');
                        }
                        if (s.Length < 29)
                        {
                            sb.Append('\t');
                        }
                    }
                    else
                    {
                        sb.Append(s + Environment.NewLine);
                    }
                    i = ~i;
                }

                tt.SetToolTip(o, sb.ToString());
            },
                                                            (o) =>
            {
                ushort Id       = (ushort)o.Value;
                EnIPClass Class = new EnIPClass(tn.Tag as EnIPRemoteDevice, Id);
                tn.Nodes.Add(ClassToTreeNode(Class));
                tn.Expand();
                if (o.Value != o.Maximum)
                {
                    o.Value++;
                }
            });

            DialogResult res = Input.ShowDialog();
        }
Exemplo n.º 9
0
        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;
        }
Exemplo n.º 10
0
        // Usage by Load file menu
        private void AddNodesFromFile(StreamReader sr, ref int line)
        {
            TreeNode         ParentDeviceTreeNode = null, ParentClassTreeNode = null, ParentInstanceTreeNode = null;
            EnIPRemoteDevice remotedevice = null; EnIPClass Class = null; EnIPInstance Instance = null;

            while (!sr.EndOfStream)
            {
                try
                {
                    String str = sr.ReadLine(); line++;
                    if ((str != null) && (str[0] != '/'))
                    {
                        String[] Strs   = str.Split(Properties.Settings.Default.CSVSeparator);
                        int      Length = Strs.Length;
                        while (Length != 1)
                        {
                            if (Strs[Length - 1] == "")
                            {
                                Length--;
                            }
                            else
                            {
                                break;
                            }
                        }

                        switch (Length)
                        {
                        case 2:     // A device, assume LAN timeout and not WAN
                            remotedevice             = new EnIPRemoteDevice(new System.Net.IPEndPoint(IPAddress.Parse(Strs[0]), 0xAF12), Properties.Settings.Default.TCP_LAN_Timeout);
                            remotedevice.ProductName = Strs[1];
                            ParentDeviceTreeNode     = AddRemoteDevice(remotedevice);
                            break;

                        case 4:     // A class
                            Class = new EnIPClass(remotedevice, Convert.ToUInt16(Strs[2]));
                            int ico;
                            if (Enum.IsDefined(typeof(CIPObjectLibrary), Class.Id))
                            {
                                ico = Classe2Ico((CIPObjectLibrary)Class.Id);
                            }
                            else
                            {
                                ico = 2;
                            }
                            ParentClassTreeNode             = AddeNode(ParentDeviceTreeNode, Class, Strs[3], ico);
                            ParentClassTreeNode.ToolTipText = "Node " + IdStr(Class.Id);
                            break;

                        case 6:     // An instance
                            Instance = new EnIPInstance(Class, Convert.ToByte(Strs[4]));
                            ParentInstanceTreeNode             = AddeNode(ParentClassTreeNode, Instance, Strs[5], 9);
                            ParentInstanceTreeNode.ToolTipText = ParentClassTreeNode.ToolTipText + "." + IdStr(Instance.Id);
                            break;

                        case 8:     // An attribut
                            EnIPAttribut Attribut = new EnIPAttribut(Instance, Convert.ToByte(Strs[6]));
                            TreeNode     tnAtt    = AddeNode(ParentInstanceTreeNode, Attribut, Strs[7], 10);
                            tnAtt.ToolTipText = ParentInstanceTreeNode.ToolTipText + "." + IdStr(Attribut.Id);
                            break;

                        default:
                            throw new Exception("Not the good number of colums");
                        }
                    }
                }
                catch { throw new Exception("Line content error"); }
            }
        }
Exemplo n.º 11
0
        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");
            }
        }