Search in sources :

Example 6 with ModbusException

use of com.ghgande.j2mod.modbus.ModbusException in project openems by OpenEMS.

the class ModbusBridge method writeSingleRegister.

private ModbusResponse writeSingleRegister(int modbusUnitId, int address, Register register) throws ModbusException, OpenemsModbusException {
    ModbusTransaction trans = getTransaction();
    WriteSingleRegisterRequest req = new WriteSingleRegisterRequest(address, register);
    req.setUnitID(modbusUnitId);
    trans.setRequest(req);
    try {
        trans.execute();
    } catch (ModbusException e) {
        // try again with new connection
        closeModbusConnection();
        trans = getTransaction();
        req = new WriteSingleRegisterRequest(address, register);
        req.setUnitID(modbusUnitId);
        trans.setRequest(req);
        trans.execute();
    }
    return trans.getResponse();
}
Also used : WriteSingleRegisterRequest(com.ghgande.j2mod.modbus.msg.WriteSingleRegisterRequest) ModbusTransaction(com.ghgande.j2mod.modbus.io.ModbusTransaction) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Example 7 with ModbusException

use of com.ghgande.j2mod.modbus.ModbusException in project openems by OpenEMS.

the class ModbusBridge method writeSingleCoil.

private ModbusResponse writeSingleCoil(int modbusUnitId, int address, boolean b) throws OpenemsModbusException, ModbusException {
    ModbusTransaction trans = getTransaction();
    WriteCoilRequest req = new WriteCoilRequest(address, b);
    req.setUnitID(modbusUnitId);
    trans.setRequest(req);
    try {
        trans.execute();
    } catch (ModbusException e) {
        // try again with new connection
        closeModbusConnection();
        trans = getTransaction();
        req = new WriteCoilRequest(address, b);
        req.setUnitID(modbusUnitId);
        trans.setRequest(req);
        trans.execute();
    }
    return trans.getResponse();
}
Also used : ModbusTransaction(com.ghgande.j2mod.modbus.io.ModbusTransaction) WriteCoilRequest(com.ghgande.j2mod.modbus.msg.WriteCoilRequest) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Example 8 with ModbusException

use of com.ghgande.j2mod.modbus.ModbusException in project openems by OpenEMS.

the class ModbusBridge method queryCoils.

private boolean[] queryCoils(int modbusUnitId, int startAddress, int length) throws OpenemsModbusException {
    ModbusTransaction trans = getTransaction();
    ReadCoilsRequest req = new ReadCoilsRequest(startAddress, length);
    req.setUnitID(modbusUnitId);
    trans.setRequest(req);
    try {
        trans.execute();
    } catch (ModbusException e) {
        // try again with new connection
        closeModbusConnection();
        trans = getTransaction();
        req = new ReadCoilsRequest(startAddress, length);
        req.setUnitID(modbusUnitId);
        trans.setRequest(req);
        try {
            trans.execute();
        } catch (ModbusException e1) {
            throw new OpenemsModbusException(// 
            "Error on modbus query. " + "UnitId [" + modbusUnitId + "], Address [" + startAddress + "/0x" + Integer.toHexString(startAddress) + "], Count [" + length + "]: " + e1.getMessage());
        }
    }
    ModbusResponse res = trans.getResponse();
    if (res instanceof ReadCoilsResponse) {
        ReadCoilsResponse mres = (ReadCoilsResponse) res;
        return toBooleanArray(mres.getCoils().getBytes());
    } else {
        throw new OpenemsModbusException(// 
        "Unable to read modbus response. " + "UnitId [" + modbusUnitId + "], Address [" + startAddress + "], Count [" + length + "]: " + res.toString());
    }
}
Also used : OpenemsModbusException(io.openems.api.exception.OpenemsModbusException) ReadCoilsRequest(com.ghgande.j2mod.modbus.msg.ReadCoilsRequest) ModbusResponse(com.ghgande.j2mod.modbus.msg.ModbusResponse) ReadCoilsResponse(com.ghgande.j2mod.modbus.msg.ReadCoilsResponse) ModbusTransaction(com.ghgande.j2mod.modbus.io.ModbusTransaction) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Example 9 with ModbusException

use of com.ghgande.j2mod.modbus.ModbusException in project openems by OpenEMS.

the class ModbusBridge method writeMultipleRegisters.

/**
 * Write Multiple Registers (function code 16)
 *
 * @param modbusUnitId
 * @param address
 * @param register
 * @throws OpenemsModbusException
 * @throws ModbusException
 */
private ModbusResponse writeMultipleRegisters(int modbusUnitId, int address, Register... register) throws OpenemsModbusException, ModbusException {
    ModbusTransaction trans = getTransaction();
    WriteMultipleRegistersRequest req = new WriteMultipleRegistersRequest(address, register);
    req.setUnitID(modbusUnitId);
    trans.setRequest(req);
    try {
        trans.execute();
    } catch (ModbusException e) {
        // try again with new connection
        closeModbusConnection();
        trans = getTransaction();
        req = new WriteMultipleRegistersRequest(address, register);
        req.setUnitID(modbusUnitId);
        trans.setRequest(req);
        trans.execute();
    }
    return trans.getResponse();
}
Also used : ModbusTransaction(com.ghgande.j2mod.modbus.io.ModbusTransaction) WriteMultipleRegistersRequest(com.ghgande.j2mod.modbus.msg.WriteMultipleRegistersRequest) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Example 10 with ModbusException

use of com.ghgande.j2mod.modbus.ModbusException in project openems by OpenEMS.

the class ModbusTcpApiController method restartSlave.

protected void restartSlave(Optional<Integer> portOpt) {
    // remove and close old slave if existing
    if (this.slaveOpt.isPresent()) {
        ModbusSlave oldSlave = this.slaveOpt.get();
        oldSlave.close();
        this.slaveOpt = Optional.empty();
    }
    // create new slave, initialize and start
    try {
        if (!portOpt.isPresent()) {
            throw new OpenemsException("Port was not set");
        }
        int port = portOpt.get();
        ModbusSlave newSlave = ModbusSlaveFactory.createTCPSlave(port, MAX_CONCURRENT_CONNECTIONS);
        newSlave.addProcessImage(UNIT_ID, this.processImage);
        newSlave.open();
        // TODO slave should be closed on dispose of Controller
        log.info("Modbus/TCP Api started on port [" + port + "] with UnitId [" + UNIT_ID + "].");
        this.slaveOpt = Optional.of(newSlave);
    } catch (OpenemsException | ModbusException e) {
        log.error("Unable to start Modbus/TCP slave: " + e.getMessage());
    }
}
Also used : ModbusSlave(com.ghgande.j2mod.modbus.slave.ModbusSlave) OpenemsException(io.openems.common.exceptions.OpenemsException) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Aggregations

ModbusException (com.ghgande.j2mod.modbus.ModbusException)10 OpenemsModbusException (io.openems.api.exception.OpenemsModbusException)9 ModbusTransaction (com.ghgande.j2mod.modbus.io.ModbusTransaction)7 ModbusResponse (com.ghgande.j2mod.modbus.msg.ModbusResponse)5 ExceptionResponse (com.ghgande.j2mod.modbus.msg.ExceptionResponse)2 ReadCoilsRequest (com.ghgande.j2mod.modbus.msg.ReadCoilsRequest)1 ReadCoilsResponse (com.ghgande.j2mod.modbus.msg.ReadCoilsResponse)1 ReadInputRegistersRequest (com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest)1 ReadInputRegistersResponse (com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse)1 ReadMultipleRegistersRequest (com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersRequest)1 ReadMultipleRegistersResponse (com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersResponse)1 WriteCoilRequest (com.ghgande.j2mod.modbus.msg.WriteCoilRequest)1 WriteMultipleCoilsRequest (com.ghgande.j2mod.modbus.msg.WriteMultipleCoilsRequest)1 WriteMultipleRegistersRequest (com.ghgande.j2mod.modbus.msg.WriteMultipleRegistersRequest)1 WriteSingleRegisterRequest (com.ghgande.j2mod.modbus.msg.WriteSingleRegisterRequest)1 ModbusSlave (com.ghgande.j2mod.modbus.slave.ModbusSlave)1 BitVector (com.ghgande.j2mod.modbus.util.BitVector)1 OpenemsException (io.openems.common.exceptions.OpenemsException)1