MangoAutomation / modbus4j

A high-performance and ease-of-use implementation of the Modbus protocol written in Java. Supports ASCII, RTU, TCP, and UDP transports as slave or master, automatic request partitioning and response data type parsing.
GNU General Public License v3.0
897 stars 370 forks source link

发送读写保持寄存器时会出现 Connection reset by peer: socket write error 。再次发送却成功了 #54

Open hxc-mkp opened 2 years ago

hxc-mkp commented 2 years ago

public static ModbusFactory modbusFactory = new ModbusFactory();

/**
 * 获取tcpMaster
 */
public static ModbusMaster getMaster() throws Exception {
    IpParameters params = new IpParameters();
    params.setHost("192.168.100.66");
    params.setPort(502);
    params.setEncapsulated(false);

    ModbusMaster tcpMaster = modbusFactory.createTcpMaster(params, false);
    tcpMaster.setTimeout(500);
    tcpMaster.setRetries(3);
    tcpMaster.init();
    return tcpMaster;
}

public static void readHoldingRegister(int slaveId, int offset, int dataType) throws Exception { BaseLocator locator = BaseLocator.holdingRegister(slaveId, offset, dataType); Number value = getMaster().getValue(locator); System.out.println("HoldingRegister寄存器第" + offset + "位数据为:" + value); }

jdzhang1221 commented 1 month ago

怎么解决的