Forum > Networking and Web Programming

Network communication for plc device

(1/1)

ahmetnurideniz:
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: ---   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);

        }

--- End code ---

this is device class

--- Code: ---    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;
            }
        }
    }


--- End code ---

The other classes for connect


--- Code: ---
        /*
             * 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);
            }
         



--- End code ---

Leledumbo:

--- Quote ---how can i  bind to IPv4 addresses.
--- End quote ---
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?
--- End quote ---
Have no idea. Somebody else got experience on this?

--- Quote ---how can i  create the  socket for 3802 port.
--- End quote ---
Same as the IPv4 question.

bobc:

--- Quote --- how can i take mac adres on remote device?
--- End quote ---
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:
thanks for answers. now i can connect my plc Lnet comp. I am useing telnet protocol and it is working.

Navigation

[0] Message Index

Go to full version