0

I have a Siemens s1200 plc TCP/IP client demo made with python. I it works and found it from Youtube: https://www.youtube.com/watch?v=5KLLeQeB2EY

My question is, how to translate this code to a java program. I'm currently working on a project to read data from the plc to a java client(and later from java to the plc) and I am currently bit stuck with this project.

This python demo writes "testi1" string on console when run and i'm looking for bringing more data from the "output1" datablock. Picture of the datablock attached.

Kindly asking for help.

Cheers


import socket

HOST = '192.168.0.1' #plc ip
PORT = 2000 # plc port

if __name__ == "__main__":
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as conn:
        conn.connect((HOST, PORT))
        print(conn.recv(1024).decode('UTF-8', errors='ignore')) #.decode('UTF-8', errors='ignore') erases some nonsense output


data block "output1"

Znile
  • 3
  • 5

1 Answers1

0

A simple Ping model

Client class

import java.io.OutputStream;
import java.io.InputStream
import java.net.InetSocketAddress;
import java.net.Socket;


class Client 
{
 public static void main(String args[])
 {
  try(Socket client=new Socket())
  {
   client.connect(new InetSocketAddress("localhost",8000));

   Scanner scanner=new Scanner(System.in);
   String input;

 
   try(OutputStream out=client.getOutputStream();
        InputStream in=client.getInputStream())
   {
    while(!(input=scanner.nextLine()).equals("Bye"))
    { 
     out.write(input.getBytes());

     System.out.println("Server said "+new String(in.readAllBytes()));
    }
   }
   catch(IOException ex){ex.printStackTrace();}     
  }
  catch(IOException ex){ex.printStackTrace();}
 }
}

Server Class

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;


class Server 
{
 public static void main(String args[])
 {
  try(ServerSocket server=new ServerSocket())
  {
   server.bind(new InetSocketAddress("localhost",8000));
   System.out.println("Server Online");
   
   try(Socket client=server.accept())
   {
     try(InputStream in=client.getInputStream();
         OutputStream out=client.getOutputStream())
     {         
      while(true)
      { 
       String response=new String(in.readAllBytes());
       if(response.equals("Bye")){break;}
       else{out.write(("Echo "+response).getBytes());}
      }
     }
   catch(IOException ex){ex.printStackTrace();}
  }
  catch(IOException ex){ex.printStackTrace();}
 }
}

You can model it to your purpose

Sync it
  • 1,180
  • 2
  • 11
  • 29