Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a Java program using sockets to connect to a PLC (Programmable Logic Controller)

Tags:

java

sockets

plc

I am writing a program that grabs analog and discrete data points from a PLC (Allen Bradley 1756 L63) via Sockets. So far I am having trouble just creating a socket. My code is as follows:

import java.io.*;
import java.net.*;

class PLCServer
{
    public static void main(String argv[]) throws IOException
    {
        // IP address of the ethernet card
        String ENBTIP = "192.168.10.14";
        DataInputStream socketReader = null;
        PrintStream socketWriter = null;

        try 
        {
                Socket client = new Socket(ENBTIP, 9100);
                socketReader = new DataInputStream(client.getInputStream());
                socketWriter = new PrintStream(client.getOutputStream());
            } catch (UnknownHostException e) {
                System.out.println("Error setting up socket connection");
                System.out.println("host: 192.168.10.14 port: 9100");
            } catch (IOException e) {
                System.out.println("Error setting up socket connection: " + e);
                System.out.println("host: 192.168.10.14 port: 9100");
            }
        // Debugging code
        // System.out.println(InetAddress.getByName(ENBTIP).isReachable(10000));
    }
}

When I run the program I get a connection refused exception.

Output:

nick@ubuntu:~/Java Programs/PLC Program$ java PLCServer 
Error setting up socket connection: java.net.ConnectException: Connection refused
host: 192.168.10.14 port: 9100

Can anyone give me some guidance?

like image 696
Cup O Noodles Avatar asked Feb 28 '26 12:02

Cup O Noodles


1 Answers

You can try 'ping 192.168.10.14' first, if it has response (and it should be), then try 'telnet 192.168.10.14 9100'. If it has some response like: Trying 192.168.10.14... Connected to 192.168.10.14. Then your java code is somehow wrong. Otherwise it will be the network problem.

like image 141
user747487 Avatar answered Mar 02 '26 01:03

user747487



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!