Search in sources :

Example 1 with ModbusTransaction

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

the class ModbusBridge method writeMultipleCoils.

private ModbusResponse writeMultipleCoils(int modbusUnitId, int address, boolean[] coils) throws OpenemsModbusException, ModbusException {
    ModbusTransaction trans = getTransaction();
    BitVector vec = new BitVector(coils.length);
    for (int i = 0; i < coils.length; i++) {
        vec.setBit(i, coils[i]);
    }
    WriteMultipleCoilsRequest req = new WriteMultipleCoilsRequest(address, vec);
    req.setUnitID(modbusUnitId);
    trans.setRequest(req);
    try {
        trans.execute();
    } catch (ModbusException e) {
        // try again with new connection
        closeModbusConnection();
        trans = getTransaction();
        req = new WriteMultipleCoilsRequest(address, vec);
        req.setUnitID(modbusUnitId);
        trans.setRequest(req);
        trans.execute();
    }
    return trans.getResponse();
}
Also used : BitVector(com.ghgande.j2mod.modbus.util.BitVector) WriteMultipleCoilsRequest(com.ghgande.j2mod.modbus.msg.WriteMultipleCoilsRequest) ModbusTransaction(com.ghgande.j2mod.modbus.io.ModbusTransaction) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Example 2 with ModbusTransaction

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

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

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

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

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