线程“main”中的异常java.net.ConnectException:连接被拒绝:在JAMOD中连接

时间:2015-10-07 10:04:46

标签: java master-slave modbus

用于MODBUS的JAMOD API用于创建Master和Slave.Slave启动正常但Master在连接被拒绝时抛出错误。我遵循了给出的代码 在网址下方 http://jamod.sourceforge.net/kb/tcp_master_howto.html http://jamod.sourceforge.net/kb/tcp_slave_howto.html

请帮我查一下代码

的错误

例外:

Hello ...........ModbusMaster1
localhost:5555:0:4:3
Exception in thread "main" java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:241)
    at net.wimpi.modbus.net.TCPMasterConnection.connect(TCPMasterConnection.java:65)
    at jamod.test.master.ModbusMaster1.main(ModbusMaster1.java:54)
Picked up _JAVA_OPTIONS: -Xmx512M

硕士课程:

package jamod.test.master;

import java.net.InetAddress;

import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadInputDiscretesRequest;
import net.wimpi.modbus.msg.ReadInputDiscretesResponse;
import net.wimpi.modbus.net.TCPMasterConnection;

public class ModbusMaster1 {
    public static void main(String args[]) throws Exception {
        System.out.println("Hello ...........ModbusMaster1");
        /* The important instances of the classes mentioned before */
        TCPMasterConnection con = null; //the connection
        ModbusTCPTransaction trans = null; //the transaction
        ReadInputDiscretesRequest req = null; //the request
        ReadInputDiscretesResponse res = null; //the response

        /* Variables for storing the parameters */
        InetAddress addr = null; //the slave's address
        int port = Modbus.DEFAULT_PORT;
        int ref = 0; //the reference; offset where to start reading from
        int count = 0; //the number of DI's to read
        int repeat = 1; //a loop for repeating the transaction

        String inputs[] = {"localhost:5555","0","4","3"};

        if (inputs.length < 3) {
              System.exit(1);
            } else {
              try {
                String astr = inputs[0];
                int idx = astr.indexOf(':');
                if(idx > 0) {
                  port = Integer.parseInt(astr.substring(idx+1));
                  astr = astr.substring(0,idx);
                }
                addr = InetAddress.getByName(astr);
                ref = Integer.decode(inputs[1]).intValue();
                count = Integer.decode(inputs[2]).intValue();
                if (inputs.length == 4) {
                  repeat = Integer.parseInt(inputs[3]);
                }
              } catch (Exception ex) {
                ex.printStackTrace();
                System.exit(1);
              }
            }
        System.out.println(inputs[0]+":"+inputs[1]+":"+inputs[2]+":"+inputs[3]);
        //2. Open the connection
        con = new TCPMasterConnection(addr);
        con.setPort(port);
        con.connect();

        //3. Prepare the request
        req = new ReadInputDiscretesRequest(ref, count);

        //4. Prepare the transaction
        trans = new ModbusTCPTransaction(con);
        trans.setRequest(req);

        //5. Execute the transaction repeat times
        int k = 0;
        do {
          trans.execute();
          res = (ReadInputDiscretesResponse) trans.getResponse();
          System.out.println("Digital Inputs Status=" + res.getDiscretes().toString());
          k++;
        } while (k < repeat);

         //6. Close the connection
         con.close();
    }
}

奴隶计划:

package jamod.test.slave;

import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.ModbusCoupler;
import net.wimpi.modbus.net.ModbusTCPListener;
import net.wimpi.modbus.procimg.SimpleDigitalIn;
import net.wimpi.modbus.procimg.SimpleDigitalOut;
import net.wimpi.modbus.procimg.SimpleInputRegister;
import net.wimpi.modbus.procimg.SimpleProcessImage;
import net.wimpi.modbus.procimg.SimpleRegister;

public class ModbusSlave1 {
    public static void main(String args[]) throws Exception {
        System.out.println("Hello ...........ModbusSlave1");
        ModbusTCPListener listener = null;
        SimpleProcessImage spi = null;
        int port = Modbus.DEFAULT_PORT;

        // 1. Set port number from parameter

            port = 5555;



        // 2. Prepare a process image
        spi = new SimpleProcessImage();
        spi.addDigitalOut(new SimpleDigitalOut(true));
        spi.addDigitalOut(new SimpleDigitalOut(false));
        spi.addDigitalIn(new SimpleDigitalIn(false));
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addDigitalIn(new SimpleDigitalIn(false));
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addRegister(new SimpleRegister(251));
        spi.addInputRegister(new SimpleInputRegister(45));

        // 3. Set the image on the coupler
        ModbusCoupler.getReference().setProcessImage(spi);
        ModbusCoupler.getReference().setMaster(false);
        ModbusCoupler.getReference().setUnitID(15);

        // 4. Create a listener with 3 threads in pool
        listener = new ModbusTCPListener(3);
        listener.setPort(port);
        listener.start();
    }
}

0 个答案:

没有答案