示例#1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        static void Main(string[] args)
        {
            // Interfaces list
            List <Tuple <String, String, String> > Interfaces = GetAvailableInterfaces();

            // Start network activity on the first interface (indice 0)
            String PcapInterfaceName      = "\\Device\\NPF_" + Interfaces[0].Item3;
            int    NumberofSlavesDetected = SoemInterrop.StartActivity(PcapInterfaceName, 1);

            // Until now if the parameter is missing it's Operational, otherwise slaves are
            // in there default mode (SafeOperational is not used).
            SoemInterrop.Run(SlaveState.SafeOperational);

            byte[] buf  = new byte[50]; // must be large enough
            int    size = buf.Length;

            // some Acyclic Read/Write
            // Could be also realize in operational, init, .... state
            SoemInterrop.ReadPDO(1, 0x6000, -1, ref size, buf);
            SoemInterrop.WritePDO(1, 0x7000, 0, 4, buf);

            DeviceInfo();

            // Change to Operational
            SoemInterrop.WriteState(1, SlaveState.Operational);

            for (; ;)
            {
                SoemInterrop.RefreshSlavesState();
                DeviceInfo();
                Thread.Sleep(2000);
            }
        }
示例#2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        static bool Open()
        {
            try
            {
                // Interfaces list
                List <Tuple <String, String, String> > Interfaces = GetAvailableInterfaces();

                // a wonderfull menu
                int i = 0;
                Console.WriteLine("Choose an Interface : ");
                foreach (Tuple <String, String, String> it in Interfaces)
                {
                    Console.WriteLine("\t" + (i++).ToString() + " : " + it.Item1);
                }

                ConsoleKeyInfo key = Console.ReadKey();

                i = Convert.ToInt32(key.KeyChar - 48);

                // Get the pcap name of this interface
                String PcapInterfaceName = "\\Device\\NPF_" + Interfaces[i].Item3;
                Console.WriteLine("Interface technical name is : " + PcapInterfaceName);

                // Try start with a tiemout delay of 1s
                int NumberofSlavesDetected = SoemInterrop.StartActivity(PcapInterfaceName, 1);
                // Put up (Operational) all slaves
                if (NumberofSlavesDetected > 0)
                {
                    SoemInterrop.Run();
                    return(true);
                }
            }
            catch
            {
            }
            return(false);
        }
示例#3
0
        // Menu
        private void openInterfaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List <Tuple <String, String, String> > interfaces = GetAvailableInterfaces();

            var Input =
                new GenericInputBox <ComboBox>("Local Interface", "Interfaces",
                                               (o) =>
            {
                foreach (Tuple <String, String, String> it in interfaces)
                {
                    o.Items.Add(it.Item1);
                }

                o.Text = o.Items[0].ToString();
            }, 1.7);

            DialogResult res = Input.ShowDialog();

            if (res != DialogResult.OK)
            {
                return;
            }
            String userinput = Input.genericInput.Text;

            Cursor Memcurs = this.Cursor;

            this.Cursor = Cursors.WaitCursor;

            Trace.WriteLine("Openning interface " + userinput);
            Application.DoEvents();

            foreach (Tuple <String, String, String> it in interfaces)
            {
                if (it.Item1 == userinput)
                {
                    String PcapInterfaceName = "\\Device\\NPF_" + it.Item3;
                    int    NbSlaves          = SoemInterrop.StartActivity(PcapInterfaceName, Properties.Settings.Default.DelayUpMs);

                    if (NbSlaves > 0)
                    {
                        for (uint i = 0; i < NbSlaves; i++)
                        {
                            EthCATDevice slave = new EthCATDevice(i + 1);

                            int img = SlaveStatetoIco(slave.State);

                            TreeNode tn = new TreeNode(slave.ToString(), img, img);
                            tn.Tag = slave;
                            devicesTreeView.Nodes.Add(tn);
                        }
                        openInterfaceToolStripMenuItem.Enabled = false;
                        if (tmrRefreshState.Interval >= 1000)
                        {
                            tmrRefreshState.Enabled = true;
                        }

                        SoemInterrop.Run();

                        tmrStart.Enabled     = true;
                        tmrInputFlow.Enabled = true;
                    }
                    else
                    {
                        Trace.WriteLine("No slave behind this Interface");
                    }
                }
            }

            this.Cursor = Memcurs;
        }