Cup O Noodles
Cup O Noodles

Reputation: 45

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

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?

Upvotes: 3

Views: 5583

Answers (1)

user747487
user747487

Reputation: 36

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.

Upvotes: 2

Related Questions