Пример #1
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);
            }
        }
Пример #2
0
        // change in properties Grid, a Byte in the Raw Data
        // send it to the device if it's an attribut value
        private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if ((e.ChangedItem.Parent != null) && (e.ChangedItem.Parent.Label == "RawData") && (devicesTreeView.SelectedNode.Tag is EnIPAttribut))
            {
                EnIPAttribut v = (EnIPAttribut)devicesTreeView.SelectedNode.Tag;
                if (v.WriteDataToNetwork() == EnIPNetworkStatus.OnLine)
                {
                    Trace.WriteLine("Write OK");
                }
            }
            else
            if ((e.ChangedItem.Parent != null) && (e.ChangedItem.Parent.Label == "DecodedMembers") && (devicesTreeView.SelectedNode.Tag is EnIPAttribut))
            {
                EnIPAttribut v = (EnIPAttribut)devicesTreeView.SelectedNode.Tag;
                if (v.EncodeFromDecodedMembers() == true)     // encoding is done into the previous RawByte (and same size)
                {
                    if (v.WriteDataToNetwork() == EnIPNetworkStatus.OnLine)
                    {
                        Trace.WriteLine("Write OK");
                    }
                }
                else
                {
                    Trace.WriteLine("Encoding not allow here or error during the encoding process, nothing written");
                }
            }
            else
            {
                Trace.WriteLine("Modifications are not taken into account here");
            }

            readAgainToolStripMenuItem_Click(null, null);
        }
Пример #3
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);
            }
        }
Пример #4
0
        private void DecodeMenuItem_Click(object sender, EventArgs e)
        {
            if ((devicesTreeView.SelectedNode == null) ||
                (!(devicesTreeView.SelectedNode.Tag is EnIPAttribut)))
            {
                return;                                                     // hoops !
            }
            EnIPAttribut attribut = (EnIPAttribut)devicesTreeView.SelectedNode.Tag;

            if (sender == defaultToolStripMenuItem)
            {
                // sset to null, next Read will put back the default decoder
                attribut.DecodedMembers = null;
            }
            else
            if (sender == arrayOfUINTToolStripMenuItem)
            {
                attribut.DecodedMembers = new CIPUInt16Array();
            }
            else
            {
                ToolStripMenuItem menustrip = (ToolStripMenuItem)sender;
                attribut.DecodedMembers = (CIPObject)Activator.CreateInstance((Type)menustrip.Tag);
            }

            readAgainToolStripMenuItem_Click(null, null);
        }
 void Input_T2OEvent(EnIPAttribut sender)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new Action <EnIPAttribut>(Input_T2OEvent), new object[] { sender });
         return;
     }
     //propertyGridInput.Refresh();
     MainForm.SoftRefreshPropertyGrid(propertyGridInput);
     ImgInputActivity.Visible = !ImgInputActivity.Visible;
 }
Пример #6
0
        // Menu Item
        private void addInstanceAttributToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tn = devicesTreeView.SelectedNode;

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

            int Numbase = 1;

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

            var Input =
                new GenericInputBoxExtended <NumericUpDown>("Add Attribute", "Attribute Id :",
                                                            (o) =>
            {
                o.Minimum     = 1; o.Maximum = 65535; o.Value = Numbase;
                o.Hexadecimal = Properties.Settings.Default.IdHexDisplay;
            },
                                                            (o) =>
            {
                ushort Id        = (ushort)o.Value;
                EnIPInstance ist = (EnIPInstance)tn.Tag;
                EnIPAttribut att = new EnIPAttribut(ist, Id);
                TreeNode tnI     = new TreeNode("Attribute #" + IdStr(Id), 10, 10);
                tnI.Tag          = att;
                tnI.ToolTipText  = "Node " + IdStr((tn.Parent.Tag as EnIPClass).Id) + "." + IdStr(ist.Id) + "." + IdStr(Id);
                tn.Nodes.Add(tnI);
                tn.Expand();
                if (o.Value != o.Maximum)
                {
                    o.Value++;
                }
            });

            DialogResult res = Input.ShowDialog();
        }
Пример #7
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.
            }
        }
Пример #8
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();
        }
Пример #9
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();
        }
Пример #10
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;
        }
Пример #11
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"); }
            }
        }
Пример #12
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");
            }
        }
Пример #13
0
 static void Inputs_T2OEvent(EnIPAttribut sender)
 {
     Console.Write('.');
 }
        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();
        }