Search in sources :

Example 1 with ModbusResponse

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

the class ModbusBridge method queryMultipleRegisters.

/**
 * Executes a query on the Modbus client
 *
 * @param modbusUnitId
 * @param address
 * @param count
 * @return
 * @throws OpenemsModbusException
 */
private Register[] queryMultipleRegisters(int modbusUnitId, int address, int count) throws OpenemsModbusException {
    ModbusTransaction trans = getTransaction();
    ReadMultipleRegistersRequest req = new ReadMultipleRegistersRequest(address, count);
    req.setUnitID(modbusUnitId);
    trans.setRequest(req);
    try {
        trans.execute();
    } catch (ModbusException e) {
        // try again with new connection
        closeModbusConnection();
        trans = getTransaction();
        req = new ReadMultipleRegistersRequest(address, count);
        req.setUnitID(modbusUnitId);
        trans.setRequest(req);
        try {
            trans.execute();
        } catch (ModbusException e1) {
            throw new OpenemsModbusException(// 
            "Error on modbus query. " + "UnitId [" + modbusUnitId + "], Address [" + address + "/0x" + Integer.toHexString(address) + "], Count [" + count + "]: " + e1.getMessage());
        }
    }
    ModbusResponse res = trans.getResponse();
    if (res instanceof ReadMultipleRegistersResponse) {
        ReadMultipleRegistersResponse mres = (ReadMultipleRegistersResponse) res;
        return mres.getRegisters();
    } else {
        throw new OpenemsModbusException(// 
        "Unable to read modbus response. " + "UnitId [" + modbusUnitId + "], Address [" + address + "], Count [" + count + "]: " + res.toString());
    }
}
Also used : ReadMultipleRegistersRequest(com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersRequest) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException) ModbusResponse(com.ghgande.j2mod.modbus.msg.ModbusResponse) ModbusTransaction(com.ghgande.j2mod.modbus.io.ModbusTransaction) ReadMultipleRegistersResponse(com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersResponse) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Example 2 with ModbusResponse

use of com.ghgande.j2mod.modbus.msg.ModbusResponse 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 3 with ModbusResponse

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

the class ModbusBridge method queryInputRegisters.

/**
 * Executes a query on the Modbus client
 *
 * @param modbusUnitId
 * @param address
 * @param count
 * @return
 * @throws OpenemsModbusException
 */
private InputRegister[] queryInputRegisters(int modbusUnitId, int address, int count) throws OpenemsModbusException {
    ModbusTransaction trans = getTransaction();
    ReadInputRegistersRequest req = new ReadInputRegistersRequest(address, count);
    req.setUnitID(modbusUnitId);
    trans.setRequest(req);
    try {
        trans.execute();
    } catch (ModbusException e) {
        // try again with new connection
        closeModbusConnection();
        trans = getTransaction();
        req = new ReadInputRegistersRequest(address, count);
        req.setUnitID(modbusUnitId);
        trans.setRequest(req);
        try {
            trans.execute();
        } catch (ModbusException e1) {
            throw new OpenemsModbusException(// 
            "Error on modbus query. " + "UnitId [" + modbusUnitId + "], Address [" + address + "/0x" + Integer.toHexString(address) + "], Count [" + count + "]: " + e1.getMessage());
        }
    }
    ModbusResponse res = trans.getResponse();
    if (res instanceof ReadInputRegistersResponse) {
        ReadInputRegistersResponse mres = (ReadInputRegistersResponse) res;
        return mres.getRegisters();
    } else {
        throw new OpenemsModbusException(// 
        "Unable to read modbus response. " + "UnitId [" + modbusUnitId + "], Address [" + address + "], Count [" + count + "]: " + res.toString());
    }
}
Also used : OpenemsModbusException(io.openems.api.exception.OpenemsModbusException) ModbusResponse(com.ghgande.j2mod.modbus.msg.ModbusResponse) ReadInputRegistersRequest(com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest) ModbusTransaction(com.ghgande.j2mod.modbus.io.ModbusTransaction) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException) ReadInputRegistersResponse(com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse)

Example 4 with ModbusResponse

use of com.ghgande.j2mod.modbus.msg.ModbusResponse 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)

Example 5 with ModbusResponse

use of com.ghgande.j2mod.modbus.msg.ModbusResponse 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)

Aggregations

ModbusException (com.ghgande.j2mod.modbus.ModbusException)5 ModbusResponse (com.ghgande.j2mod.modbus.msg.ModbusResponse)5 OpenemsModbusException (io.openems.api.exception.OpenemsModbusException)5 ModbusTransaction (com.ghgande.j2mod.modbus.io.ModbusTransaction)3 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