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);
}
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);
});
}
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);
}
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));
}
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);
}
Aggregations