Search in sources :

Example 6 with ModbusTransaction

use of com.ghgande.j2mod.modbus.io.ModbusTransaction 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 7 with ModbusTransaction

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

Aggregations

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