Recent

Author Topic: Network communication for plc device  (Read 9783 times)

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Network communication for plc device
« on: August 13, 2010, 12:57:56 am »
hi, i am new Lazarus user. And i newer develop program for plc.
i want to ask 4     question  on this topic.

I have example c#  program for communication plc. My plc use Tcp and Udp port for communication.
I need answer this questations
 how can i  bind to IPv4 addresses.
 how can i take mac adres on remote device?
 how can i  create the  socket for 3802 port.

i have c# code you can see it from here

this is connect button click
Code: [Select]
   private void connectB_Click(object sender, EventArgs e)
        {
                  deviceLB.Items.Clear();
            m_remoteEP.Address = IPAddress.Broadcast;
            foreach (UdpClient client in m_udpClients)
                client.Send(Encoding.ASCII.GetBytes("PING"), 4, m_remoteEP);

        }

this is device class
Code: [Select]
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;

    namespace zamanmak
    {
        public class Device
        {
            private string m_mac;
            private IPEndPoint m_ep;

            public Device(string mac, IPEndPoint ep)
            {
                m_mac = mac;
                m_ep = new IPEndPoint(ep.Address, ep.Port);
            }

            public IPEndPoint EP
            {
                get
                {
                    return m_ep;
                }
            }

            public string MAC
            {
                get
                {
                    return m_mac;
                }
            }

            public override string ToString()
            {
                return MAC + "=>" + EP;
            }

            public override bool Equals(object obj)
            {
                if (obj is Device)
                {
                    Device d = (Device)obj;
                    return d.EP.Equals(m_ep) && d.MAC == m_mac;
                }
                return false;
            }
        }
    }


The other classes for connect

Code: [Select]

        /*
             * UDP sockets which will create soon.
             */
            private List<UdpClient> m_udpClients = new List<UdpClient>();

            /*

             * Destination IP address and port.
             */
            private IPEndPoint m_remoteEP = new IPEndPoint(IPAddress.Broadcast, 3802);

            public void ReceiveCallback(IAsyncResult ar)
            {
                /*

                 * To avoid cross-thread problems.
                 */
                if (this.InvokeRequired)
                {
                    this.BeginInvoke(new AsyncCallback(ReceiveCallback), new object[] { ar });
                    return;
                }

                /*

                 * This is the socket which triggered the callback.
                 */
                UdpClient client = (UdpClient)ar.AsyncState;
                byte[] data = client.EndReceive(ar, ref m_remoteEP);

                /*

                 * We're expecting a 6-byte message which contains MAC address of the device.
                 */
                if (data.Length != 6)
                {
                    client.BeginReceive(new AsyncCallback(ReceiveCallback), client);
                    return;
                }

                String str = String.Empty;
                for (int i = 0; i < data.Length; i++)
                    str += Convert.ToString(data[i], 16).PadLeft(2, '0') + ":";

                /*

                 * Create an Device instance for the found device
                 */
                Device dev = new Device(str, m_remoteEP);
                if (!deviceLB.Items.Contains(dev))
                    deviceLB.Items.Add(dev);
                client.BeginReceive(new AsyncCallback(ReceiveCallback), client);
            }
         




Leledumbo

  • Hero Member
  • *****
  • Posts: 8799
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Network communication for plc device
« Reply #1 on: August 13, 2010, 04:12:31 am »
Quote
how can i  bind to IPv4 addresses.
You can use standard sockets calls (prefixed with FP) from sockets unit if you want to go the hard way. Or, you may choose from the best two networking components for lazarus/fpc: synapse and lnet.
Quote
how can i take mac adres on remote device?
Have no idea. Somebody else got experience on this?
Quote
how can i  create the  socket for 3802 port.
Same as the IPv4 question.

bobc

  • New Member
  • *
  • Posts: 41
Re: Network communication for plc device
« Reply #2 on: August 25, 2010, 09:40:03 pm »
Quote
how can i take mac adres on remote device?
If you are using TCP or UDP you wouldn't normally get access the MAC address via the TCP or UDP socket API, as the MAC address would be handled internally by the IP layer. You can sometimes get access at a lower level by using a "raw" socket, and writing your own low level protocol code. For security reasons access to raw sockets is usually limited.

However, in this case, looking at your C# code, there is a much simpler solution. The PC sends a 4 UDP packet containing the ASCII text "PING" which is sent to the broadcast address, and the reply is a UDP packet containing the MAC address. So I guess any PLC listening on that port (3802) will reply when it sees a PING packet.

Effectively this is implementing an address discovery mechanism at a higher level. This should be easy to implement with any socket library that has a UDP socket, but will only work on those PLCs that support it. Maybe you can hold of a spec for the PLC, it should give you a list of supported commands.

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: Network communication for plc device
« Reply #3 on: August 25, 2010, 09:45:17 pm »
thanks for answers. now i can connect my plc Lnet comp. I am useing telnet protocol and it is working.

 

TinyPortal © 2005-2018