News:

SMF - Just Installed!

Main Menu

How to use TCPIP_SERVER,TCPIP_CLIENT as I/O Device ?

Started by jinyistudio, August 22, 2019, 10:38:32 PM

Previous topic - Next topic

jinyistudio

Hi,

How do they work with external program if  i use TCPIP_Server,TCPIP_CLient as I/O Device ?  ;D

EasyPLC_Master

Hello,

Very easy, if you want to operate the TCPIP protocol with EasyPLC, then you must do the following:

-> Set EasyPLC works as TCPIP Server with VirtualPLC, this is configure this driver in EasyPLC in order VirtualPLC starts using the TCPIP Server

-> Create your own code using a TCP Protocol class to send the Digital Inputs Bytes from your program to Virtual PLC and read the Digital Outputs Bytes from VirtualPLC to your program.

Here is an example Class about how VirtualPLC do it with C#:


private TcpClient client;
private string ip;
private int port;
private string msg = "";
private byte[] resp;
public bool conected;
private Thread listenThread;

public Client(string ip, int port)
{
            client = new TcpClient();
            this.ip = ip;
            this.port = port;
            conected = false;
            resp = new byte[128];
}

public bool Connect()
{
            try
            {
                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
                client.Connect(serverEndPoint);           
                conected = true;
                stat = "Connected with Server";
                this.listenThread = new Thread(new ThreadStart(ListenServerResp));
                this.listenThread.Start();
                return conected;
            }
            catch
            {
                conected = false;
                stat = "Disconnected";
                return conected;               
            }
}

public string Status
{
            get { return stat; }
}

public void Close()
{
            client.Close();
}

private void ListenServerResp()
{
            NetworkStream clientStream = client.GetStream();
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(resp, 0, 128);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }
            }
}

public byte[] Message
{
            get { return resp; } // Here are returned the Digital inputs from byte num 0 to x (where x is the Dig.Input number /8 configured in the driver) managed by your code and will be readed by VirtualPLC
}

// This method is used by VirtualPLC to send the Digitals Outputs to your code:
public void SendMessage(byte[] data)
{
            if (!conected)
                  return;
            try
            {
                NetworkStream clientStream = client.GetStream();
                clientStream.Write(data, 0, data.Length);
                clientStream.Flush();
            }
            catch { }
}