Search in sources :

Example 1 with ExceptionResponse

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

the class ModbusBridge method write.

protected void write(int modbusUnitId, int address, Register... register) throws OpenemsModbusException {
    ModbusResponse res;
    try {
        if (register.length == 0) {
            return;
        } else if (register.length == 1) {
            res = writeSingleRegister(modbusUnitId, address, register[0]);
        } else {
            res = writeMultipleRegisters(modbusUnitId, address, register);
        }
    } catch (ModbusException | OpenemsModbusException e) {
        throw new OpenemsModbusException(// 
        "Error on modbus write. " + "UnitId [" + modbusUnitId + "], Address [" + address + "/0x" + Integer.toHexString(address) + "], Register [" + registersAsString(register) + "]: " + e.getMessage());
    }
    if (res instanceof ExceptionResponse) {
        throw new OpenemsModbusException(// 
        "Error on modbus write response. " + "UnitId [" + modbusUnitId + "], Address [" + address + "/0x" + Integer.toHexString(address) + "], Register [" + registersAsString(register) + "]: " + res.toString());
    }
    log.debug(// 
    "Successful write. " + "UnitId [" + modbusUnitId + "], Address [" + address + "/0x" + Integer.toHexString(address) + "], Register [" + registersAsString(register) + "]");
}
Also used : ExceptionResponse(com.ghgande.j2mod.modbus.msg.ExceptionResponse) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException) ModbusResponse(com.ghgande.j2mod.modbus.msg.ModbusResponse) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Example 2 with ExceptionResponse

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

the class ModbusBridge method writeCoil.

protected void writeCoil(int modbusUnitId, int address, Boolean... values) throws OpenemsModbusException {
    boolean[] coils = new boolean[values.length];
    for (int i = 0; i < values.length; i++) {
        coils[i] = values[i];
    }
    ModbusResponse res;
    try {
        if (coils.length == 0) {
            return;
        } else if (coils.length == 1) {
            res = writeSingleCoil(modbusUnitId, address, coils[0]);
        } else {
            res = writeMultipleCoils(modbusUnitId, address, coils);
        }
    } catch (ModbusException | OpenemsModbusException e) {
        throw new OpenemsModbusException(// 
        "Error on modbus write. " + "UnitId [" + modbusUnitId + "], Address [" + address + "/0x" + Integer.toHexString(address) + "], Coil [" + coilsAsString(coils) + "]: " + e.getMessage());
    }
    if (res instanceof ExceptionResponse) {
        throw new OpenemsModbusException(// 
        "Error on modbus write response. " + "UnitId [" + modbusUnitId + "], Address [" + address + "/0x" + Integer.toHexString(address) + "], Coil [" + coilsAsString(coils) + "]: " + res.toString());
    }
    log.debug(// 
    "Successful write. " + "UnitId [" + modbusUnitId + "], Address [" + address + "/0x" + Integer.toHexString(address) + "], Coil [" + coilsAsString(coils) + "]");
}
Also used : ExceptionResponse(com.ghgande.j2mod.modbus.msg.ExceptionResponse) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException) ModbusResponse(com.ghgande.j2mod.modbus.msg.ModbusResponse) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Aggregations

ModbusException (com.ghgande.j2mod.modbus.ModbusException)2 ExceptionResponse (com.ghgande.j2mod.modbus.msg.ExceptionResponse)2 ModbusResponse (com.ghgande.j2mod.modbus.msg.ModbusResponse)2 OpenemsModbusException (io.openems.api.exception.OpenemsModbusException)2