Search in sources :

Example 1 with ModbusReadRequestBlueprint

use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.

the class StuderHandler method registerPollTask.

/**
 * Register poll task
 * This is where we set up our regular poller
 */
private synchronized void registerPollTask(int registerNumber) {
    if (pollTasks.size() >= registers.length) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
        throw new IllegalStateException("New pollTask invalid");
    }
    ModbusCommunicationInterface mycomms = comms;
    StuderConfiguration studerConfig = config;
    if (studerConfig == null || mycomms == null) {
        throw new IllegalStateException("registerPollTask called without proper configuration");
    }
    logger.debug("Setting up regular polling");
    ModbusReadRequestBlueprint request = new ModbusReadRequestBlueprint(studerConfig.slaveAddress, ModbusReadFunctionCode.READ_INPUT_REGISTERS, registerNumber, 2, studerConfig.maxTries);
    long refreshMillis = studerConfig.refresh * 1000;
    PollTask pollTask = mycomms.registerRegularPoll(request, refreshMillis, 1000, result -> {
        if (result.getRegisters().isPresent()) {
            ModbusRegisterArray reg = result.getRegisters().get();
            handlePolledData(registerNumber, reg);
        } else {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
            return;
        }
        if (getThing().getStatus() != ThingStatus.ONLINE) {
            updateStatus(ThingStatus.ONLINE);
        }
    }, this::handleError);
    pollTasks.add(pollTask);
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)

Example 2 with ModbusReadRequestBlueprint

use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.

the class E3DCHandlerStateTest method getDataResult.

private AsyncModbusReadResult getDataResult() {
    byte[] dataBlockBytes = new byte[] { 0, -14, 0, 0, -2, -47, -1, -1, 2, 47, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 125, 2, 21, 0, 0, 0, 27, 0, 26, 0, 0, 0, 103, 0, -117, 0, 0 };
    ModbusReadRequestBlueprint readRequest = mock(ModbusReadRequestBlueprint.class);
    return new AsyncModbusReadResult(readRequest, new ModbusRegisterArray(dataBlockBytes));
}
Also used : ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult)

Example 3 with ModbusReadRequestBlueprint

use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.

the class SunspecDiscoveryProcess method readCommonBlock.

/**
 * Start reading common block
 *
 * @param block
 */
private void readCommonBlock(ModelBlock block) {
    ModbusReadRequestBlueprint request = new ModbusReadRequestBlueprint(slaveId, // Start address
    ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, // Start address
    block.address, // number or words to return
    block.length, maxTries);
    comms.submitOneTimePoll(request, result -> result.getRegisters().ifPresent(this::parseCommonBlock), this::handleError);
}
Also used : ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)

Example 4 with ModbusReadRequestBlueprint

use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.

the class SunspecDiscoveryProcess method detectModel.

/**
 * Start model detection
 *
 * @param uid the thing type to look for
 * @throws EndpointNotInitializedException
 */
public void detectModel() {
    if (possibleAddresses.isEmpty()) {
        parsingFinished();
        return;
    }
    // Try the next address from the possibles
    baseAddress = possibleAddresses.poll();
    logger.trace("Beginning scan for SunSpec device at address {}", baseAddress);
    ModbusReadRequestBlueprint request = new ModbusReadRequestBlueprint(slaveId, // Start address
    ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, // Start address
    baseAddress, // number or words to return
    SUNSPEC_ID_SIZE, maxTries);
    comms.submitOneTimePoll(request, result -> result.getRegisters().ifPresent(this::headerReceived), this::handleError);
}
Also used : ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)

Example 5 with ModbusReadRequestBlueprint

use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.

the class SunspecDiscoveryProcess method handleError.

/**
 * Handle errors received during communication
 */
private void handleError(AsyncModbusFailure<ModbusReadRequestBlueprint> failure) {
    if (blocksFound > 1 && failure.getCause() instanceof ModbusSlaveErrorResponseException) {
        int code = ((ModbusSlaveErrorResponseException) failure.getCause()).getExceptionCode();
        if (code == ModbusSlaveErrorResponseException.ILLEGAL_DATA_ACCESS || code == ModbusSlaveErrorResponseException.ILLEGAL_DATA_VALUE) {
            // It is very likely that the slave does not report an end block (0xffff) after the main blocks
            // so we treat this situation as normal.
            logger.debug("Seems like slave device does not report an end block. Continuing with the dectected blocks");
            parsingFinished();
            return;
        }
    }
    String cls = failure.getCause().getClass().getName();
    String msg = failure.getCause().getMessage();
    logger.warn("Error with read at address {}: {} {}", baseAddress, cls, msg);
    detectModel();
}
Also used : ModbusSlaveErrorResponseException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)

Aggregations

ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)47 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)22 Test (org.junit.jupiter.api.Test)21 ModbusRegisterArray (org.openhab.core.io.transport.modbus.ModbusRegisterArray)21 ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)19 PollTask (org.openhab.core.io.transport.modbus.PollTask)17 ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)14 Configuration (org.openhab.core.config.core.Configuration)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 AsyncModbusReadResult (org.openhab.core.io.transport.modbus.AsyncModbusReadResult)12 ModbusTCPSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ModbusWriteCoilRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)10 Bridge (org.openhab.core.thing.Bridge)9 Thing (org.openhab.core.thing.Thing)9 ModbusPollerThingHandler (org.openhab.binding.modbus.handler.ModbusPollerThingHandler)8 ModbusReadCallback (org.openhab.core.io.transport.modbus.ModbusReadCallback)8 ModbusWriteRegisterRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint)8 ModbusWriteRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint)7