【问题标题】:Python TCP/IP PLC Client Demo to Java LanguagePython TCP/IP PLC 客户端演示到 Java 语言
【发布时间】:2020-10-19 22:43:05
【问题描述】:

我有一个使用 python 制作的 Siemens s1200 plc TCP/IP 客户端演示。我它工作并从 Youtube 找到它:https://www.youtube.com/watch?v=5KLLeQeB2EY

我的问题是,如何将此代码转换为 java 程序。我目前正在做一个项目,将数据从 plc 读取到 java 客户端(然后从 java 到 plc),我目前有点卡在这个项目上。

这个 python 演示在运行时在控制台上写入“testi1”字符串,我正在寻找从“output1”数据块中获取更多数据。附上数据块的图片。

请寻求帮助。

干杯


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"

【问题讨论】:

    标签: java python tcp plc siemens


    【解决方案1】:

    一个简单的 Ping 模型

    客户端类

    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();}
     }
    }
    

    服务器类

    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();}
     }
    }
    

    您可以根据自己的目的对其进行建模

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 2018-09-19
      • 2012-10-05
      • 2021-08-23
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多