modbus tcp通讯 modbus4j java实现 中遇到的问题:Connection reset by peer: socket write error

在写代码时,采用了https://blog.csdn.net/qq_35638499/article/details/81300574#commentsedit的代码作为基石,

即“modbus tcp通讯 modbus4j java实现”

但是在编程过程中一直遇到这个问题:

com.serotonin.modbus4j.exception.ModbusTransportException: java.net.SocketException: Connection reset by peer: socket write error
    at com.serotonin.modbus4j.ip.tcp.TcpMaster.sendImpl(TcpMaster.java:172)
    at com.serotonin.modbus4j.ModbusMaster.send(ModbusMaster.java:105)
    at com.serotonin.modbus4j.ModbusMaster.setValue(ModbusMaster.java:398)
    at com.serotonin.modbus4j.ModbusMaster.setValue(ModbusMaster.java:177)
    at com.jamod.exe.master.Modbus4jWriteUtils.writeHoldingRegister(Modbus4jWriteUtils.java:95)
    at com.jamod.exe.master.Modbus4jWriteUtilsTest.white(Modbus4jWriteUtilsTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.net.SocketException: Connection reset by peer: socket write error

于是经过多次的尝试,找到了原因:

在博主的代码中:

package com.jamod.exe.master;

import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.ip.IpParameters;

public class TcpMaster {

    private static ModbusFactory modbusFactory;

    static {
        if (modbusFactory == null) {
            modbusFactory = new ModbusFactory();
        }
    }

    /**
     * 获取master
     *
     * @return master
     */
    public static ModbusMaster getMaster() {
        IpParameters params = new IpParameters();
        //params.setHost("localhost");
        params.setHost("localhost");
        params.setPort(502);
        params.setEncapsulated(true);
        ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议
        try {
            //设置超时时间
            master.setTimeout(1000);
            //设置重连次数
            master.setRetries(3);
            //初始化
            master.init();
        } catch (ModbusInitException e) {
            e.printStackTrace();
        }
        return master;
    }

}

这里由于用到了

params.setEncapsulated(true);

即将params强制为true的情况,有的时候编辑会出错,所有对代码进行了如下修改:

package com.jamod.exe.master;

import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.ip.IpParameters;

public class TcpMaster {

    private static ModbusFactory modbusFactory;

    static {
        if (modbusFactory == null) {
            modbusFactory = new ModbusFactory();
        }
    }

    /**
     * 获取master
     *
     * @return master
     */
    public static ModbusMaster getMaster() {
        IpParameters params = new IpParameters();
        //params.setHost("localhost");
        params.setHost("127.0.0.1");
        params.setPort(502);
        //params.setEncapsulated(true);
        ModbusMaster master = null;
       // ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议
        master = modbusFactory.createTcpMaster(params, true);
        try {
            //设置超时时间
            master.setTimeout(1000);
            //设置重连次数
            master.setRetries(3);
            //初始化
            master.init();
        } catch (ModbusInitException e) {
            e.printStackTrace();
        }
        return master;
    }

}

最后就能编译通过啦!

你可能感兴趣的:(通讯,modubus4j,java)