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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

hxc-mkp opened this issue · comments

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);
}