Ejemplo n.º 1
0
        private void devicesTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (e.Node.Tag is EthCATDevice)
                {
                    SoemInterrop.RefreshSlavesState();

                    EthCATDevice slave = (EthCATDevice)e.Node.Tag;
                    slave.Refresh();

                    e.Node.SelectedImageIndex = e.Node.ImageIndex = SlaveStatetoIco(slave.State);

                    propertyGrid.SelectedObject = slave;
                    propertyGrid.ExpandAllGridItems();

                    propertyInput.SelectedObjects = new object[] { slave.InputData };
                    propertyInput.ExpandAllGridItems();

                    propertyOutput.SelectedObjects = new object[] { slave.OutputData };
                    propertyOutput.ExpandAllGridItems();

                    PDOToolStripMenuItem.Enabled = slave.MailboxProtocol.Contains(MailBoxProto.CoE.ToString());
                }
                else
                {
                    propertyGrid.SelectedObject = null;
                }
            }
            catch { }
        }
Ejemplo n.º 2
0
        private void GetDynamicAttribut()
        {
            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.State, sExchange);
            State = (SlaveState)(Convert.ToByte(sExchange.ToString()) & 0x1F);

            SoemInterrop.GetDeviceInfo(Id, (DeviceInfoParam)(-1), sExchange);
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        static void DeviceInfo()
        {
            StringBuilder sb = new StringBuilder(200); // Buffer large enough to get back slave parameters

            SoemInterrop.GetDeviceInfo(1, DeviceInfoParam.State, sb);
            SlaveState s = (SlaveState)Convert.ToUInt16(sb.ToString());

            Console.WriteLine("State : " + s.ToString());
        }
Ejemplo n.º 5
0
        private void GetConstAttribut()
        {
            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.Name, sExchange);
            Name = sExchange.ToString();

            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.OutputSize, sExchange);
            OutputSize = Convert.ToUInt16(sExchange.ToString());
            if (OutputSize > 0)
            {
                OutputData = new byte[(OutputSize / 8) + (OutputSize % 8)];
            }

            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.InputSize, sExchange);
            InputSize = Convert.ToUInt16(sExchange.ToString());
            if (InputSize > 0)
            {
                InputData = new byte[(InputSize / 8) + (InputSize % 8)];
            }

            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.Delay, sExchange);
            Delay = Convert.ToUInt16(sExchange.ToString());

            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.Config_Address, sExchange);
            Config_Address = Convert.ToUInt16(sExchange.ToString());

            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.ManufacturerId, sExchange);
            _ManufacturerId = Convert.ToUInt32(sExchange.ToString());
            ManufacturerId  = "0x" + _ManufacturerId.ToString("X8");

            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.TypeId, sExchange);
            _TypeId = Convert.ToUInt32(sExchange.ToString());
            TypeId  = "0x" + _TypeId.ToString("X8");

            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.Rev, sExchange);
            Revision = Convert.ToUInt32(sExchange.ToString());

            // Maibox Protocol
            SoemInterrop.GetDeviceInfo(Id, DeviceInfoParam.MailboxProtocol, sExchange);
            ushort mbx = Convert.ToUInt16(sExchange.ToString());

            StringBuilder sb = new StringBuilder();

            foreach (MailBoxProto proto in Enum.GetValues(typeof(MailBoxProto)))
            {
                if ((mbx & (ushort)proto) != 0)
                {
                    sb.Append(proto.ToString() + " ");
                }
            }
            MailboxProtocol = sb.ToString();
        }
Ejemplo n.º 6
0
 private void MainDialog_FormClosing(object sender, FormClosingEventArgs e)
 {
     try
     {
         Properties.Settings.Default.GUI_FormSize = this.Size;
         Properties.Settings.Default.GUI_State    = this.WindowState;
         Properties.Settings.Default.Save();
         if (openInterfaceToolStripMenuItem.Enabled == false)
         {
             SoemInterrop.StopActivity();
         }
     }
     catch { }
 }
Ejemplo n.º 7
0
        // SubIndex=-1 for all
        public bool WritePDO(int Idx, int SubIndex, int size, byte[] buff)
        {
            int ret = SoemInterrop.WritePDO(Id, Idx, SubIndex, size, buff);

            if (ret != 0)
            {
                Trace.WriteLine("PDO Write OK");
                return(true);
            }
            else
            {
                Trace.WriteLine("PDO Write Fail");
                return(false);
            }
        }
Ejemplo n.º 8
0
        private void readToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CurrentBuf = new byte[128 * 256]; // get a big buffer
            int size = 0;

            // Read first 128 bytes
            int ret = SoemInterrop.EEprom_Read(1, 0, 128, CurrentBuf);

            if (ret > 0)
            {
                // size is at 0x7C, 0x7D. Here I only get the less-significant byte
                // up to 32Ko of eeprom is certainly never implemented
                // FIXME if needed
                size = (CurrentBuf[0x7c] + 1) * 128; // Adjustment

                progress.Maximum = size;
                progress.Visible = true;

                for (int s = 128; s < size; s += 128)
                {
                    ret = SoemInterrop.EEprom_Read(1, s, 128, CurrentBuf);

                    if (ret <= 0)
                    {
                        size = 0;
                        break;
                    }
                    progress.Value = s + 128;
                    Application.DoEvents();
                }
            }

            progress.Visible = false;

            if (size == 0)
            {
                Trace.WriteLine("EEPROM read error");
                Mem.Text   = "";
                CurrentBuf = null;
            }
            else
            {
                Array.Resize(ref CurrentBuf, size);
                Buf2Display(CurrentBuf, AsciiMode);
            }
        }
Ejemplo n.º 9
0
        // SubIndex=-1 for all
        public byte[] ReadPDO(int Idx, int SubIndex, out int size)
        {
            byte[] buff = new byte[4096];
            size = 4096;

            int ret = SoemInterrop.ReadPDO(Id, Idx, SubIndex, ref size, buff);

            if (ret != 0)
            {
                Trace.WriteLine("PDO Read OK");
                return(buff);
            }
            else
            {
                Trace.WriteLine("PDO Read Fail");
                return(null);
            }
        }
Ejemplo n.º 10
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        static void Main(string[] args)
        {
            if (Open() == true)
            {
                StringBuilder sb = new StringBuilder(200); // Buffer to get back slave parameters

                // Assume two slave devices
                for (uint i = 1; i < 3; i++)
                {
                    // We can get some information ... for nothing, just to show
                    SoemInterrop.GetDeviceInfo(i, DeviceInfoParam.OutputSize, sb);
                    SoemInterrop.GetDeviceInfo(i, DeviceInfoParam.InputSize, sb);

                    SoemInterrop.GetDeviceInfo(i, DeviceInfoParam.Config_Address, sb);
                    Console.WriteLine("Slave @ : ", sb.ToString());
                }

                // Assume two devices, copy two bytes from the input into the other output
                // echange buffers with the SOEM stack
                byte[] bin1  = new byte[50]; // must be equal or more than InputSize in the corresponding slave
                byte[] bout2 = new byte[50]; // must be strikcly equal to OutputSize in the corresponding slave

                for (; ;)
                {
                    // Get Input from the first slave
                    SoemInterrop.GetInput(1, bin1);

                    // Some action !
                    bout2[0] = bin1[0];
                    bout2[1] = bin1[1];

                    // Set Output in the second slave
                    SoemInterrop.SetOutput(2, bout2);

                    // Quite tired with this work
                    Thread.Sleep(200);
                }
            }
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        private void tmrRefreshState_Tick(object sender, EventArgs e)
        {
            tmrStart.Enabled = false;

            SoemInterrop.RefreshSlavesState();
            foreach (TreeNode tn in devicesTreeView.Nodes)
            {
                EthCATDevice slave = tn.Tag as EthCATDevice;
                SlaveState   s     = slave.State;
                slave.Refresh();
                if (s != slave.State)
                {
                    int img = SlaveStatetoIco(slave.State);
                    tn.ImageIndex = tn.SelectedImageIndex = img;
                    devicesTreeView.Refresh();
                }

                if (tn == devicesTreeView.SelectedNode)
                {
                    propertyGrid.Refresh();
                    propertyInput.Refresh();
                }
            }
        }
Ejemplo n.º 13
0
 public void WriteState(SlaveState state)
 {
     SoemInterrop.WriteState(Id, state);
 }
Ejemplo n.º 14
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;
        }
Ejemplo n.º 15
0
 public void Reconfigure()
 {
     SoemInterrop.Reconfigure(Id);
 }
Ejemplo n.º 16
0
 public void WriteOutput()
 {
     SoemInterrop.SetOutput(Id, OutputData);
 }
Ejemplo n.º 17
0
 public void ReadInput()
 {
     SoemInterrop.GetInput(Id, InputData);
 }