ENDA.PLCNetLib.Finder.Scan C# (CSharp) Method

Scan() public method

public Scan ( ) : void
return void
        public void Scan()
        {
            // Rebind to ports if they are released by ReleasePorts()
            if (m_udpClients.Count == 0)
            {
                NetworkInterface[] ifs = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface i in ifs)
                {
                    Console.Out.WriteLine(i.Name);
                    if (i.OperationalStatus != OperationalStatus.Up) continue;
                    if (!i.Supports(NetworkInterfaceComponent.IPv4)) continue;
                    IPInterfaceProperties prop = i.GetIPProperties();

                    foreach (UnicastIPAddressInformation ip in prop.UnicastAddresses)
                    {

                        if (ip.Address.GetAddressBytes().Length != 4) continue;
                        UdpClient client = new UdpClient(new IPEndPoint(ip.Address, 3802));
                        client.BeginReceive(new AsyncCallback(ReceiveCallback), client);
                        client.EnableBroadcast = true;
                        m_udpClients.Add(client);
                    }
                }
            }
            foreach (UdpClient client in m_udpClients)
            {
                client.Send(Encoding.ASCII.GetBytes("PING"), 4, m_remoteEP);
            }

            // Release the port after some time, as other programs such as EndaSoft might want to use them.
            m_t = new Timer((x) => { ReleasePorts(); }, null, 10000, System.Threading.Timeout.Infinite);
        }

Usage Example

Esempio n. 1
0
 static void Main(string[] args)
 {
     /*
     args = new string[4];
     args[0] = "00:25:fc:00:02:bd";
     args[1] = "1234";
     args[2] = "fw";
     args[3] = @"C:\users\engin\code\workspace-cpp\plc\app\plc.bin.enda";
      * */
     m_args = args;
     if (args.Length < 2)
     {
         Console.Error.WriteLine("Usage: plcctrl MAC|IP PASS ACTION PARAMS");
         return;
     }
     IPEndPoint ip = null;
     if (args[0].Contains(":"))
     {
         Finder f = new Finder();
         f.DeviceFound += new Finder.DeviceFoundHandler(f_DeviceFound);
         f.Scan();
         m_wait.WaitOne(5000);
     }
     else
     {
         ip = new IPEndPoint(IPAddress.Parse(args[0]), 23);
         doit(ip, args);
     }
 }