Search in sources :

Example 1 with ModbusRegisterArray

use of org.openhab.core.io.transport.modbus.ModbusRegisterArray 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 ModbusRegisterArray

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

the class StiebelEltronHandler method writeInt16.

/**
 * @param address address of the value to be written on the modbus
 * @param shortValue value to be written on the modbus
 */
protected void writeInt16(int address, short shortValue) {
    StiebelEltronConfiguration myconfig = StiebelEltronHandler.this.config;
    ModbusCommunicationInterface mycomms = StiebelEltronHandler.this.comms;
    if (myconfig == null || mycomms == null) {
        throw new IllegalStateException("registerPollTask called without proper configuration");
    }
    // big endian byte ordering
    byte hi = (byte) (shortValue >> 8);
    byte lo = (byte) shortValue;
    ModbusRegisterArray data = new ModbusRegisterArray(hi, lo);
    ModbusWriteRegisterRequestBlueprint request = new ModbusWriteRegisterRequestBlueprint(slaveId, address, data, false, myconfig.getMaxTries());
    mycomms.submitOneTimeWrite(request, result -> {
        if (hasConfigurationError()) {
            return;
        }
        logger.debug("Successful write, matching request {}", request);
        StiebelEltronHandler.this.updateStatus(ThingStatus.ONLINE);
    }, failure -> {
        StiebelEltronHandler.this.handleWriteError(failure);
    });
}
Also used : StiebelEltronConfiguration(org.openhab.binding.modbus.stiebeleltron.internal.StiebelEltronConfiguration) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) ModbusWriteRegisterRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint)

Example 3 with ModbusRegisterArray

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

the class HeliosEasyControlsHandler method preparePayload.

/**
 * Prepares the payload for the request
 *
 * @param payload The String representation of the payload
 * @return The Register representation of the payload
 */
private static ModbusRegisterArray preparePayload(String payload) {
    // determine number of registers
    byte[] asciiBytes = payload.getBytes(StandardCharsets.US_ASCII);
    int bufferLength = // ascii characters
    asciiBytes.length + // NUL byte
    1 + // to have even number of bytes
    ((asciiBytes.length % 2 == 0) ? 1 : 0);
    // Invariant, ensured above
    assert bufferLength % 2 == 0;
    byte[] buffer = new byte[bufferLength];
    System.arraycopy(asciiBytes, 0, buffer, 0, asciiBytes.length);
    // Fill in rest of bytes with NUL bytes
    for (int i = asciiBytes.length; i < buffer.length; i++) {
        buffer[i] = '\0';
    }
    return new ModbusRegisterArray(buffer);
}
Also used : ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusWriteRegisterRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)

Example 4 with ModbusRegisterArray

use of org.openhab.core.io.transport.modbus.ModbusRegisterArray 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 5 with ModbusRegisterArray

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

the class ModbusDataHandlerTest method testOnRegistersNaNFloatInRegisters.

@Test
public void testOnRegistersNaNFloatInRegisters() throws InvalidSyntaxException {
    ModbusDataThingHandler dataHandler = testReadHandlingGeneric(ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, "0", "default", ModbusConstants.ValueType.FLOAT32, null, new ModbusRegisterArray(// equivalent of floating point NaN
    new byte[] { (byte) 0x7f, (byte) 0xc0, (byte) 0x00, (byte) 0x00 }), null, bundleContext);
    assertSingleStateUpdate(dataHandler, CHANNEL_LAST_READ_SUCCESS, is(notNullValue(State.class)));
    assertSingleStateUpdate(dataHandler, CHANNEL_LAST_READ_ERROR, is(nullValue(State.class)));
    // UNDEF is treated as "boolean true" (OPEN/ON) since it is != 0.
    assertSingleStateUpdate(dataHandler, CHANNEL_CONTACT, OpenClosedType.OPEN);
    assertSingleStateUpdate(dataHandler, CHANNEL_SWITCH, OnOffType.ON);
    assertSingleStateUpdate(dataHandler, CHANNEL_DIMMER, OnOffType.ON);
    assertSingleStateUpdate(dataHandler, CHANNEL_NUMBER, UnDefType.UNDEF);
    assertSingleStateUpdate(dataHandler, CHANNEL_ROLLERSHUTTER, UnDefType.UNDEF);
    assertSingleStateUpdate(dataHandler, CHANNEL_STRING, UnDefType.UNDEF);
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ModbusRegisterArray (org.openhab.core.io.transport.modbus.ModbusRegisterArray)33 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)22 Test (org.junit.jupiter.api.Test)15 ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)13 AsyncModbusReadResult (org.openhab.core.io.transport.modbus.AsyncModbusReadResult)11 ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)10 ModbusWriteRegisterRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint)10 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 Thing (org.openhab.core.thing.Thing)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ModbusPollerThingHandler (org.openhab.binding.modbus.handler.ModbusPollerThingHandler)6 Configuration (org.openhab.core.config.core.Configuration)6 ModbusWriteRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint)6 ModbusReadCallback (org.openhab.core.io.transport.modbus.ModbusReadCallback)5 ModbusWriteCoilRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)5 PollTask (org.openhab.core.io.transport.modbus.PollTask)4 DecimalType (org.openhab.core.library.types.DecimalType)4 Optional (java.util.Optional)3